UserExtract.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lianghuan
  5. * Date: 2018-03-03
  6. * Time: 16:47
  7. */
  8. namespace app\admin\model\user;
  9. use app\admin\model\many\ManyOrder;
  10. use app\admin\model\order\StoreOrder;
  11. use app\admin\model\wechat\WechatUser;
  12. use app\models\routine\RoutineTemplate;
  13. use think\facade\Route as Url;
  14. use crmeb\traits\ModelTrait;
  15. use crmeb\basic\BaseModel;
  16. use crmeb\services\WechatTemplateService;
  17. /**
  18. * 用户提现管理 model
  19. * Class User
  20. * @package app\admin\model\user
  21. */
  22. class UserExtract extends BaseModel
  23. {
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'user_extract';
  34. use ModelTrait;
  35. /**
  36. * 获得用户提现总金额
  37. * @param $uid
  38. * @return mixed
  39. */
  40. public static function userExtractTotalPrice($uid, $status = 1, $where = [])
  41. {
  42. return self::getModelTime($where, self::where('uid', 'in', $uid)->where('status', $status))->sum('extract_price') ?: 0;
  43. }
  44. public static function extractStatistics()
  45. {
  46. //待提现金额
  47. $data['price'] = floatval(self::where('status', 0)->sum('extract_price'));
  48. //佣金总金额
  49. $data['brokerage_count'] = floatval(\app\models\user\User::sum('business_integral'));
  50. //已提现金额
  51. $data['priced'] = floatval(self::where('status', 1)->sum('extract_price'));
  52. //未提现金额
  53. $data['brokerage_not'] = floatval(\app\models\user\User::sum('integral'));
  54. //商品金额
  55. $data['order_price'] = StoreOrder::where('paid', 1)->sum('pay_price');
  56. //打怪金额
  57. $data['many_price'] = ManyOrder::sum('price');
  58. //阳光积分
  59. $data['user_int'] = floatval(\app\models\user\User::sum('purple_integral'));
  60. //打怪金额
  61. $data['dfh'] = ManyOrder::where('status', 0)->sum('price');
  62. return compact('data');
  63. }
  64. /**
  65. * @param $where
  66. * @return array
  67. */
  68. public static function systemPage($where)
  69. {
  70. $model = new self;
  71. if ($where['date'] != '') {
  72. list($startTime, $endTime) = explode(' - ', $where['date']);
  73. $model = $model->where('a.add_time', '>', strtotime($startTime));
  74. $model = $model->where('a.add_time', '<', (int)bcadd(strtotime($endTime), 86400, 0));
  75. }
  76. if ($where['status'] != '') $model = $model->where('a.status', $where['status']);
  77. if ($where['extract_type'] != '') $model = $model->where('a.extract_type', $where['extract_type']);
  78. if ($where['nireid'] != '') $model = $model->where('a.real_name|a.id|b.nickname|a.bank_code|a.alipay_code', 'like', "%$where[nireid]%");
  79. if ($where['uid'] != '') $model = $model->where('a.uid', $where['uid']);
  80. $model = $model->alias('a');
  81. $model = $model->field('a.*,b.nickname');
  82. $model = $model->join('user b', 'b.uid=a.uid', 'LEFT');
  83. $model = $model->order('a.id desc');
  84. return self::page($model, $where);
  85. }
  86. public static function changeFail($id, $fail_msg)
  87. {
  88. $fail_time = time();
  89. $data = self::get($id);
  90. $extract_number = $data['extract_price'];
  91. $mark = '提现失败,退回阳光积分' . $extract_number;
  92. $uid = $data['uid'];
  93. $status = -1;
  94. $User = User::where('uid', $uid)->find()->toArray();
  95. UserBill::income('提现失败', $uid, 'purple_integral', 'extract', $extract_number, $id, bcadd($User['purple_integral'], $extract_number, 2), $mark);
  96. User::bcInc($uid, 'purple_integral', $extract_number, 'uid');
  97. $extract_type = '未知方式';
  98. switch ($data['extract_type']) {
  99. case 'alipay':
  100. $extract_type = '支付宝';
  101. break;
  102. case 'bank':
  103. $extract_type = '银行卡';
  104. break;
  105. case 'weixin':
  106. $extract_type = '微信';
  107. break;
  108. }
  109. if (strtolower($User['user_type']) == 'wechat') {
  110. WechatTemplateService::sendTemplate(WechatUser::where('uid', $uid)->value('openid'), WechatTemplateService::USER_BALANCE_CHANGE, [
  111. 'first' => $mark,
  112. 'keyword1' => '积分提现',
  113. 'keyword2' => date('Y-m-d H:i:s', time()),
  114. 'keyword3' => $extract_number,
  115. 'remark' => '错误原因:' . $fail_msg
  116. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  117. } else if (strtolower($User['user_type']) == 'routine') {
  118. RoutineTemplate::sendExtractFail($uid, $fail_msg, $extract_number, $User['nickname']);
  119. }
  120. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  121. }
  122. public static function changeSuccess($id)
  123. {
  124. $data = self::get($id);
  125. $extractNumber = $data['extract_price'];
  126. $mark = '成功提现佣金' . $extractNumber . '元';
  127. $wechatUserInfo = WechatUser::where('uid', $data['uid'])->field('openid,user_type,routine_openid,nickname')->find();
  128. $extract_type = '未知方式';
  129. switch ($data['extract_type']) {
  130. case 'alipay':
  131. $extract_type = '支付宝';
  132. break;
  133. case 'bank':
  134. $extract_type = '银行卡';
  135. break;
  136. case 'weixin':
  137. $extract_type = '微信';
  138. break;
  139. }
  140. if ($wechatUserInfo) {
  141. if (strtolower($wechatUserInfo->user_type) == 'routine') {
  142. RoutineTemplate::sendExtractSuccess($data['uid'], $extractNumber, $wechatUserInfo->nickname);
  143. } else if (strtolower($wechatUserInfo->user_type) == 'wechat') {
  144. WechatTemplateService::sendTemplate($wechatUserInfo->openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  145. 'first' => $mark,
  146. 'keyword1' => '佣金提现',
  147. 'keyword2' => date('Y-m-d H:i:s', time()),
  148. 'keyword3' => $extractNumber,
  149. 'remark' => '点击查看我的佣金明细'
  150. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  151. }
  152. }
  153. return self::edit(['status' => 1], $id);
  154. }
  155. //测试数据
  156. public static function test()
  157. {
  158. $uids = User::order('uid desc')->limit(2, 20)->field(['uid', 'nickname'])->select()->toArray();
  159. $type = ['bank', 'alipay', 'weixin'];
  160. foreach ($uids as $item) {
  161. $data = [
  162. 'uid' => $item['uid'],
  163. 'real_name' => $item['nickname'],
  164. 'extract_type' => isset($type[rand(0, 2)]) ? $type[rand(0, 2)] : 'alipay',
  165. 'bank_code' => rand(1000000, 999999999),
  166. 'bank_address' => '中国',
  167. 'alipay_code' => rand(1000, 9999999),
  168. 'extract_price' => rand(100, 9999),
  169. 'mark' => '测试数据',
  170. 'add_time' => time(),
  171. 'status' => 1,
  172. 'wechat' => rand(999, 878788) . $item['uid'],
  173. ];
  174. self::create($data);
  175. }
  176. }
  177. //获取头部提现信息
  178. public static function getExtractHead()
  179. {
  180. //本月提现人数
  181. $month = self::getModelTime(['data' => 'month'], self::where('status', 1))->group('uid')->count();
  182. //本月提现笔数
  183. $new_month = self::getModelTime(['data' => 'month'], self::where('status', 1))->distinct(true)->count();
  184. //上月提现人数
  185. $last_month = self::whereTime('add_time', 'last month')->where('status', 1)->group('uid')->distinct(true)->count();
  186. //上月提现笔数
  187. $last_count = self::whereTime('add_time', 'last month')->where('status', 1)->count();
  188. //本月提现金额
  189. $extract_price = self::getModelTime(['data' => 'month'], self::where('status', 1))->sum('extract_price');
  190. //上月提现金额
  191. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', 1)->sum('extract_price');
  192. return [
  193. [
  194. 'name' => '总提现人数',
  195. 'field' => '个',
  196. 'count' => self::where('status', 1)->group('uid')->count(),
  197. 'content' => '',
  198. 'background_color' => 'layui-bg-blue',
  199. 'sum' => '',
  200. 'class' => 'fa fa-bar-chart',
  201. ],
  202. [
  203. 'name' => '总提现笔数',
  204. 'field' => '笔',
  205. 'count' => self::where('status', 1)->distinct(true)->count(),
  206. 'content' => '',
  207. 'background_color' => 'layui-bg-cyan',
  208. 'sum' => '',
  209. 'class' => 'fa fa-line-chart',
  210. ],
  211. [
  212. 'name' => '本月提现人数',
  213. 'field' => '人',
  214. 'count' => $month,
  215. 'content' => '',
  216. 'background_color' => 'layui-bg-orange',
  217. 'sum' => '',
  218. 'class' => 'fa fa-line-chart',
  219. ],
  220. [
  221. 'name' => '本月提现笔数',
  222. 'field' => '笔',
  223. 'count' => $new_month,
  224. 'content' => '',
  225. 'background_color' => 'layui-bg-green',
  226. 'sum' => '',
  227. 'class' => 'fa fa-line-chart',
  228. ],
  229. [
  230. 'name' => '本月提现金额',
  231. 'field' => '元',
  232. 'count' => $extract_price,
  233. 'content' => '提现总金额',
  234. 'background_color' => 'layui-bg-cyan',
  235. 'sum' => self::where('status', 1)->sum('extract_price'),
  236. 'class' => 'fa fa-line-chart',
  237. ],
  238. [
  239. 'name' => '上月提现人数',
  240. 'field' => '个',
  241. 'count' => $last_month,
  242. 'content' => '环比增幅',
  243. 'background_color' => 'layui-bg-blue',
  244. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  245. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  246. ],
  247. [
  248. 'name' => '上月提现笔数',
  249. 'field' => '笔',
  250. 'count' => $last_count,
  251. 'content' => '环比增幅',
  252. 'background_color' => 'layui-bg-black',
  253. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  254. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  255. ],
  256. [
  257. 'name' => '上月提现金额',
  258. 'field' => '元',
  259. 'count' => $last_extract_price,
  260. 'content' => '环比增幅',
  261. 'background_color' => 'layui-bg-gray',
  262. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  263. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  264. ],
  265. ];
  266. }
  267. //获取提现分布图和提现人数金额曲线图
  268. public static function getExtractList($where, $limit = 15)
  269. {
  270. $legdata = ['提现人数', '提现金额'];
  271. $list = self::getModelTime($where, self::where('status', 1))
  272. ->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(uid) as count,sum(extract_price) as sum_price')->group('un_time')->order('un_time asc')->select();
  273. if (count($list)) $list = $list->toArray();
  274. $xdata = [];
  275. $itemList = [0 => [], 1 => []];
  276. $chatrList = [];
  277. $zoom = '';
  278. foreach ($list as $value) {
  279. $xdata[] = $value['un_time'];
  280. $itemList[0][] = $value['count'];
  281. $itemList[1][] = $value['sum_price'];
  282. }
  283. foreach ($legdata as $key => $name) {
  284. $item['name'] = $name;
  285. $item['type'] = 'line';
  286. $item['data'] = $itemList[$key];
  287. $chatrList[] = $item;
  288. }
  289. unset($item, $name, $key);
  290. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  291. //饼状图
  292. $cake = ['支付宝', '银行卡', '微信'];
  293. $fenbulist = self::getModelTime($where, self::where('status', 1))
  294. ->field('count(uid) as count,extract_type')->group('extract_type')->order('count asc')->select();
  295. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  296. $sum_count = self::getModelTime($where, self::where('status', 1))->count();
  297. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  298. $fenbudata = [];
  299. foreach ($fenbulist as $key => $item) {
  300. if ($item['extract_type'] == 'bank') {
  301. $item_date['name'] = '银行卡';
  302. } else if ($item['extract_type'] == 'alipay') {
  303. $item_date['name'] = '支付宝';
  304. } else if ($item['extract_type'] == 'weixin') {
  305. $item_date['name'] = '微信';
  306. }
  307. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  308. $item_date['itemStyle']['color'] = $color[$key];
  309. $fenbudata[] = $item_date;
  310. }
  311. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  312. }
  313. /**
  314. * 获取用户累计提现金额
  315. * @param int $uid
  316. * @return int|mixed
  317. */
  318. public static function getUserCountPrice($uid = 0)
  319. {
  320. if (!$uid) return 0;
  321. $price = self::where('uid', $uid)->where('status', 1)->sum('extract_price');
  322. return $price ? $price : 0;
  323. }
  324. /**
  325. * 获取用户累计提现次数
  326. * @param int $uid
  327. * @return int|string
  328. */
  329. public static function getUserCountNum($uid = 0)
  330. {
  331. if (!$uid) return 0;
  332. return self::where('uid', $uid)->count();
  333. }
  334. }