UserExtract.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\user;
  12. use app\admin\model\user\User;
  13. use app\admin\model\user\UserBill;
  14. use app\admin\model\wechat\WechatUser;
  15. use think\Url;
  16. use traits\ModelTrait;
  17. use basic\ModelBasic;
  18. use service\WechatTemplateService;
  19. /**
  20. * 用户提现管理 model
  21. * Class User
  22. * @package app\admin\model\user
  23. */
  24. class UserExtract extends ModelBasic
  25. {
  26. use ModelTrait;
  27. /**
  28. * @param $where
  29. * @return array
  30. */
  31. public static function systemPage($where)
  32. {
  33. $model = new self;
  34. $model = $model->alias('a');
  35. if ($where['status'] != '') $model = $model->where('a.status', $where['status']);
  36. if ($where['extract_type'] != '') $model = $model->where('a.extract_type', $where['extract_type']);
  37. if ($where['nireid'] != '') $model = $model->where('a.real_name|a.id|b.nickname|a.bank_code|a.alipay_code|a.wechat', 'like', "%$where[nireid]%");
  38. $model = $model->field('a.*,b.nickname');
  39. $model = $model->join('__USER__ b', 'b.uid=a.uid', 'LEFT');
  40. $model = $model->order('a.id desc');
  41. return self::page($model);
  42. }
  43. public static function changeFail($id, $fail_msg)
  44. {
  45. $fail_time = time();
  46. $data = self::get($id);
  47. $extract_number = $data['extract_price'];
  48. $mark = '提现失败,退回佣金' . $extract_number . '元';
  49. $uid = $data['uid'];
  50. $status = -1;
  51. $User = User::find(['uid' => $uid])->toArray();
  52. UserBill::income('提现失败', $uid, 'now_money', 'extract_fail', $extract_number, $id, $User['now_money'], $mark);
  53. User::bcInc($uid, 'brokerage_price', $extract_number, 'uid');
  54. try {
  55. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($uid), WechatTemplateService::USER_BALANCE_CHANGE, [
  56. 'first' => $mark,
  57. 'keyword1' => '佣金提现',
  58. 'keyword2' => $extract_number,
  59. 'keyword3' => date('Y-m-d H:i:s', time()),
  60. 'keyword4' => $User['brokerage_price'],
  61. 'remark' => '错误原因:' . $fail_msg
  62. ], Url::build('wap/spread/spread', [], true, true));
  63. } catch (\Exception $e) {
  64. }
  65. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  66. }
  67. public static function changeSuccess($id)
  68. {
  69. $status = 1;
  70. $data = self::get($id);
  71. $extract_number = $data['extract_price'];
  72. $mark = '成功提现佣金' . $extract_number . '元';
  73. $uid = $data['uid'];
  74. $User = User::find(['uid' => $uid])->toArray();
  75. $now_money = User::where(['uid' => $uid])->value('brokerage_price');
  76. UserBill::expend('提现成功', $uid, 'now_money', 'extract_success', $extract_number, $id, $now_money, $mark);
  77. try {
  78. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($uid), WechatTemplateService::USER_BALANCE_CHANGE, [
  79. 'first' => $mark,
  80. 'keyword1' => '佣金提现',
  81. 'keyword2' => $extract_number,
  82. 'keyword3' => date('Y-m-d H:i:s', time()),
  83. 'keyword4' => $User['brokerage_price'],
  84. 'remark' => '点击查看我的佣金明细'
  85. ], Url::build('wap/spread/spread', [], true, true));
  86. } catch (\Exception $e) {
  87. }
  88. return self::edit(compact('status'), $id);
  89. }
  90. //测试数据
  91. public static function test()
  92. {
  93. $uids = User::order('uid desc')->limit(2, 20)->field(['uid', 'nickname'])->select()->toArray();
  94. $type = ['bank', 'alipay', 'weixin'];
  95. foreach ($uids as $item) {
  96. $data = [
  97. 'uid' => $item['uid'],
  98. 'real_name' => $item['nickname'],
  99. 'extract_type' => isset($type[rand(0, 2)]) ? $type[rand(0, 2)] : 'alipay',
  100. 'bank_code' => rand(1000000, 999999999),
  101. 'bank_address' => '中国',
  102. 'alipay_code' => rand(1000, 9999999),
  103. 'extract_price' => rand(100, 9999),
  104. 'mark' => '测试数据',
  105. 'add_time' => time(),
  106. 'status' => 1,
  107. 'wechat' => rand(999, 878788) . $item['uid'],
  108. ];
  109. self::set($data);
  110. }
  111. }
  112. //获取头部提现信息
  113. public static function getExtractHead()
  114. {
  115. //本月提现人数
  116. $month = self::getModelTime(['data' => 'month'], self::where(['status' => 1]))->group('uid')->count();
  117. //本月提现笔数
  118. $new_month = self::getModelTime(['data' => 'month'], self::where(['status' => 1]))->distinct(true)->count();
  119. //上月提现人数
  120. $last_month = self::whereTime('add_time', 'last month')->where('status', 1)->group('uid')->distinct(true)->count();
  121. //上月提现笔数
  122. $last_count = self::whereTime('add_time', 'last month')->where('status', 1)->count();
  123. //本月提现金额
  124. $extract_price = self::getModelTime(['data' => 'month'], self::where(['status' => 1]))->sum('extract_price');
  125. //上月提现金额
  126. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', 1)->sum('extract_price');
  127. return [
  128. [
  129. 'name' => '总提现人数',
  130. 'field' => '个',
  131. 'count' => self::where(['status' => 1])->group('uid')->count(),
  132. 'content' => '',
  133. 'background_color' => 'layui-bg-blue',
  134. 'sum' => '',
  135. 'class' => 'fa fa-bar-chart',
  136. ],
  137. [
  138. 'name' => '总提现笔数',
  139. 'field' => '笔',
  140. 'count' => self::where(['status' => 1])->distinct(true)->count(),
  141. 'content' => '',
  142. 'background_color' => 'layui-bg-cyan',
  143. 'sum' => '',
  144. 'class' => 'fa fa-line-chart',
  145. ],
  146. [
  147. 'name' => '本月提现人数',
  148. 'field' => '人',
  149. 'count' => $month,
  150. 'content' => '',
  151. 'background_color' => 'layui-bg-orange',
  152. 'sum' => '',
  153. 'class' => 'fa fa-line-chart',
  154. ],
  155. [
  156. 'name' => '本月提现笔数',
  157. 'field' => '笔',
  158. 'count' => $new_month,
  159. 'content' => '',
  160. 'background_color' => 'layui-bg-green',
  161. 'sum' => '',
  162. 'class' => 'fa fa-line-chart',
  163. ],
  164. [
  165. 'name' => '本月提现金额',
  166. 'field' => '元',
  167. 'count' => $extract_price,
  168. 'content' => '提现总金额',
  169. 'background_color' => 'layui-bg-cyan',
  170. 'sum' => self::where(['status' => 1])->sum('extract_price'),
  171. 'class' => 'fa fa-line-chart',
  172. ],
  173. [
  174. 'name' => '上月提现人数',
  175. 'field' => '个',
  176. 'count' => $last_month,
  177. 'content' => '环比增幅',
  178. 'background_color' => 'layui-bg-blue',
  179. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  180. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  181. ],
  182. [
  183. 'name' => '上月提现笔数',
  184. 'field' => '笔',
  185. 'count' => $last_count,
  186. 'content' => '环比增幅',
  187. 'background_color' => 'layui-bg-black',
  188. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  189. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  190. ],
  191. [
  192. 'name' => '上月提现金额',
  193. 'field' => '元',
  194. 'count' => $last_extract_price,
  195. 'content' => '环比增幅',
  196. 'background_color' => 'layui-bg-gray',
  197. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  198. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  199. ],
  200. ];
  201. }
  202. //获取提现分布图和提现人数金额曲线图
  203. public static function getExtractList($where, $limit = 15)
  204. {
  205. $legdata = ['提现人数', '提现金额'];
  206. $list = self::getModelTime($where, self::where('status', 1))
  207. ->field([
  208. 'FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time',
  209. 'count(uid) as count',
  210. 'sum(extract_price) as sum_price',
  211. ])->group('un_time')->order('un_time asc')->select();
  212. if (count($list)) $list = $list->toArray();
  213. $xdata = [];
  214. $itemList = [0 => [], 1 => []];
  215. $chatrList = [];
  216. $zoom = '';
  217. foreach ($list as $value) {
  218. $xdata[] = $value['un_time'];
  219. $itemList[0][] = $value['count'];
  220. $itemList[1][] = $value['sum_price'];
  221. }
  222. foreach ($legdata as $key => $name) {
  223. $item['name'] = $name;
  224. $item['type'] = 'line';
  225. $item['data'] = $itemList[$key];
  226. $chatrList[] = $item;
  227. }
  228. unset($item, $name, $key);
  229. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  230. //饼状图
  231. $cake = ['支付宝', '银行卡', '微信'];
  232. $fenbulist = self::getModelTime($where, self::where('status', 1))
  233. ->field(['count(uid) as count', 'extract_type'])->group('extract_type')->order('count asc')->select();
  234. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  235. $sum_count = self::getModelTime($where, self::where('status', 1))->count();
  236. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  237. $fenbudata = [];
  238. foreach ($fenbulist as $key => $item) {
  239. if ($item['extract_type'] == 'bank') {
  240. $item_date['name'] = '银行卡';
  241. } else if ($item['extract_type'] == 'alipay') {
  242. $item_date['name'] = '支付宝';
  243. } else if ($item['extract_type'] == 'weixin') {
  244. $item_date['name'] = '微信';
  245. }
  246. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  247. $item_date['itemStyle']['color'] = $color[$key];
  248. $fenbudata[] = $item_date;
  249. }
  250. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  251. }
  252. /**
  253. * 获取用户累计提现金额
  254. * @param int $uid
  255. * @return int|mixed
  256. */
  257. public static function getUserCountPrice($uid = 0)
  258. {
  259. if (!$uid) return 0;
  260. $price = self::where('uid', $uid)->where('status', 1)->field('sum(extract_price) as price')->find()['price'];
  261. return $price ? $price : 0;
  262. }
  263. /**
  264. * 获取用户累计提现次数
  265. * @param int $uid
  266. * @return int|string
  267. */
  268. public static function getUserCountNum($uid = 0)
  269. {
  270. if (!$uid) return 0;
  271. return self::where('uid', $uid)->count();
  272. }
  273. /**
  274. * 获得用户提现总金额
  275. * @param $uid
  276. * @return mixed
  277. */
  278. public static function userExtractTotalPrice($uid, $status = 1, $where = [])
  279. {
  280. return self::getModelTime($where, self::where('uid', 'in', $uid)->where('status', $status))->sum('extract_price') ?: 0;
  281. }
  282. }