UserExtract.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 app\admin\model\merchant\Merchant;
  16. use app\admin\model\merchant\MerchantBill;
  17. use think\Url;
  18. use traits\ModelTrait;
  19. use basic\ModelBasic;
  20. use service\WechatTemplateService;
  21. use app\wap\model\routine\RoutineTemplate;
  22. use service\SystemConfigService;
  23. /**
  24. * 用户提现管理 model
  25. * Class UserExtract
  26. * @package app\admin\model\user
  27. */
  28. class UserExtract extends ModelBasic
  29. {
  30. use ModelTrait;
  31. /**条件处理
  32. * @param $where
  33. * @return DataDownload
  34. */
  35. public static function setWhere($where)
  36. {
  37. $model = new self();
  38. $model = $model->alias('a');
  39. $time['data'] = '';
  40. if (isset($where['start_time']) && isset($where['end_time']) && $where['start_time'] != '' && $where['end_time'] != '') {
  41. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  42. $model = $model->getModelTime($time, $model, 'a.add_time');
  43. }
  44. if (isset($where['nireid']) && $where['nireid']) {
  45. $model = $model->where('a.real_name|a.id|b.nickname|a.bank_code|a.alipay_code|a.wechat', 'like', "%$where[nireid]%");
  46. }
  47. if (isset($where['status']) && $where['status'] != '') $model = $model->where('a.status', $where['status']);
  48. if (isset($where['extract_type']) && $where['extract_type'] != '') $model = $model->where('a.extract_type', $where['extract_type']);
  49. $model = $model->join('__USER__ b', 'b.uid=a.uid', 'LEFT');
  50. return $model->where('a.mer_id', 0);
  51. }
  52. public static function get_user_extract_list($where)
  53. {
  54. $data = self::setWhere($where)->field('a.*,b.nickname')->order('a.id DESC')->page((int)$where['page'], (int)$where['limit'])->select();
  55. foreach ($data as $key => &$datum) {
  56. $datum['name'] = $datum['nickname'] . '/' . $datum['uid'];
  57. $datum['add_time'] = date('Y-m-d H:i:s', $datum['add_time']);
  58. $datum['fail_time'] = date('Y-m-d H:i:s', $datum['fail_time']);
  59. }
  60. $data = count((array)$data) ? $data->toArray() : [];
  61. $count = self::setWhere($where)->count();
  62. return compact('data', 'count');
  63. }
  64. /**条件处理
  65. * @param $where
  66. * @return DataDownload
  67. */
  68. public static function setMerWhere($where)
  69. {
  70. $model = new self();
  71. $model = $model->alias('a');
  72. $time['data'] = '';
  73. if (isset($where['start_time']) && isset($where['end_time']) && $where['start_time'] != '' && $where['end_time'] != '') {
  74. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  75. $model = $model->getModelTime($time, $model, 'a.add_time');
  76. }
  77. if (isset($where['nireid']) && $where['nireid']) {
  78. $model = $model->where('a.real_name|a.id|m.mer_name|a.bank_code|a.alipay_code|a.wechat', 'like', "%$where[nireid]%");
  79. }
  80. if (isset($where['status']) && $where['status'] != '') $model = $model->where('a.status', $where['status']);
  81. if (isset($where['mer_id']) && $where['mer_id'] != '') $model = $model->where('a.mer_id', '>', 0);
  82. if (isset($where['extract_type']) && $where['extract_type'] != '') $model = $model->where('a.extract_type', $where['extract_type']);
  83. $model = $model->join('Merchant m', 'm.id=a.mer_id', 'LEFT');
  84. return $model;
  85. }
  86. public static function get_mer_user_extract_list($where)
  87. {
  88. $data = self::setMerWhere($where)->field('a.*,m.mer_name')->order('a.id DESC')->page((int)$where['page'], (int)$where['limit'])->select();
  89. foreach ($data as $key => &$datum) {
  90. $datum['name'] = $datum['mer_name'] . '/' . $datum['uid'];
  91. $datum['add_time'] = date('Y-m-d H:i:s', $datum['add_time']);
  92. $datum['fail_time'] = date('Y-m-d H:i:s', $datum['fail_time']);
  93. }
  94. $data = count((array)$data) ? $data->toArray() : [];
  95. $count = self::setMerWhere($where)->count();
  96. return compact('data', 'count');
  97. }
  98. public static function changeFail($id, $fail_msg, $data)
  99. {
  100. $fail_time = time();
  101. $extract_number = $data['extract_price'];
  102. $mark = '提现失败,退回佣金' . $extract_number . '元';
  103. $uid = $data['uid'];
  104. $status = -1;
  105. $User = User::where(['uid' => $uid])->find();
  106. if (!$User) return false;
  107. UserBill::income('提现失败', $uid, 'now_money', 'extract_fail', $extract_number, $id, bcadd($User['brokerage_price'], $extract_number, 2), $mark);
  108. User::bcInc($uid, 'brokerage_price', $extract_number, 'uid');
  109. try {
  110. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  111. if ($wechat_notification_message == 1) {
  112. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($uid), WechatTemplateService::USER_BALANCE_CHANGE, [
  113. 'first' => $mark,
  114. 'keyword1' => '佣金提现',
  115. 'keyword2' => $extract_number,
  116. 'keyword3' => date('Y-m-d H:i:s', time()),
  117. 'keyword4' => bcadd($User['brokerage_price'], $extract_number, 2),
  118. 'remark' => '错误原因:' . $fail_msg
  119. ], Url::build('wap/spread/spread', [], true, true));
  120. } else {
  121. $dat['thing8']['value'] = '佣金提现';
  122. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  123. $dat['amount1']['value'] = $extract_number;
  124. $dat['amount2']['value'] = bcadd($User['brokerage_price'], $extract_number, 2);
  125. $dat['thing5']['value'] = '错误原因:' . $fail_msg;
  126. RoutineTemplate::sendAccountChanges($dat, $uid, Url::build('wap/spread/spread', [], true, true));
  127. }
  128. } catch (\Exception $e) {
  129. }
  130. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  131. }
  132. public static function changeSuccess($id, $data)
  133. {
  134. $status = 1;
  135. $extract_number = $data['extract_price'];
  136. $mark = '成功提现佣金' . $extract_number . '元';
  137. $uid = $data['uid'];
  138. $User = User::where(['uid' => $uid])->find();
  139. if (!$User) return false;
  140. UserBill::expend('提现成功', $uid, 'now_money', 'extract_success', $extract_number, $id, $User['brokerage_price'], $mark);
  141. if ($data['extract_type'] == 'yue') {
  142. User::bcInc($uid, 'now_money', $extract_number, 'uid');
  143. UserBill::income('提现到零钱', $uid, 'now_money', 'extract_to_yue', $extract_number, $id, bcadd($User['now_money'], $extract_number, 2), $mark);
  144. }
  145. try {
  146. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  147. if ($wechat_notification_message == 1) {
  148. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($uid), WechatTemplateService::USER_BALANCE_CHANGE, [
  149. 'first' => $mark,
  150. 'keyword1' => '佣金提现',
  151. 'keyword2' => $extract_number,
  152. 'keyword3' => date('Y-m-d H:i:s', time()),
  153. 'keyword4' => $User['brokerage_price'],
  154. 'remark' => '点击查看我的佣金明细!'
  155. ], Url::build('wap/spread/spread', [], true, true));
  156. } else {
  157. $dat['thing8']['value'] = '佣金提现';
  158. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  159. $dat['amount1']['value'] = $extract_number;
  160. $dat['amount2']['value'] = $User['brokerage_price'];
  161. $dat['thing5']['value'] = $mark;
  162. RoutineTemplate::sendAccountChanges($dat, $uid, Url::build('wap/spread/spread', [], true, true));
  163. }
  164. } catch (\Exception $e) {
  165. }
  166. return self::edit(compact('status'), $id);
  167. }
  168. public static function changeMerFail($id, $fail_msg, $data)
  169. {
  170. $fail_time = time();
  171. $extract_number = $data['extract_price'];
  172. $mark = '提现失败,退回' . $extract_number . '元';
  173. $uid = $data['uid'];
  174. $status = -1;
  175. $merchant = Merchant::where(['id' => $data['mer_id']])->find();
  176. if (!$merchant) return false;
  177. MerchantBill::income('提现失败', 0, $data['mer_id'], 'now_money', 'extract_fail', $extract_number, bcadd($merchant['now_money'], $extract_number, 2), $mark);
  178. Merchant::bcInc($data['mer_id'], 'now_money', $extract_number, 'id');
  179. try {
  180. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  181. if ($wechat_notification_message == 1) {
  182. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($uid), WechatTemplateService::USER_BALANCE_CHANGE, [
  183. 'first' => $mark,
  184. 'keyword1' => '余额提现',
  185. 'keyword2' => $extract_number,
  186. 'keyword3' => date('Y-m-d H:i:s', time()),
  187. 'keyword4' => bcadd($merchant['now_money'], $extract_number, 2),
  188. 'remark' => '错误原因:' . $fail_msg
  189. ], '');
  190. } else {
  191. $dat['thing8']['value'] = '余额提现';
  192. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  193. $dat['amount1']['value'] = $extract_number;
  194. $dat['amount2']['value'] = bcadd($merchant['now_money'], $extract_number, 2);
  195. $dat['thing5']['value'] = '错误原因:' . $fail_msg;
  196. RoutineTemplate::sendAccountChanges($dat, $uid, '');
  197. }
  198. } catch (\Exception $e) {
  199. }
  200. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  201. }
  202. public static function changeMerSuccess($id, $data)
  203. {
  204. $status = 1;
  205. $extract_number = $data['extract_price'];
  206. $mark = '成功提现' . $extract_number . '元';
  207. $uid = $data['uid'];
  208. $merchant = Merchant::where(['id' => $data['mer_id']])->find();
  209. if (!$merchant) return false;
  210. try {
  211. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  212. if ($wechat_notification_message == 1) {
  213. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($uid), WechatTemplateService::USER_BALANCE_CHANGE, [
  214. 'first' => $mark,
  215. 'keyword1' => '余额提现',
  216. 'keyword2' => $extract_number,
  217. 'keyword3' => date('Y-m-d H:i:s', time()),
  218. 'keyword4' => $merchant['now_money'],
  219. 'remark' => '点击查看我的佣金明细!'
  220. ], '');
  221. } else {
  222. $dat['thing8']['value'] = '余额提现';
  223. $dat['date4']['value'] = date('Y-m-d H:i:s', time());
  224. $dat['amount1']['value'] = $extract_number;
  225. $dat['amount2']['value'] = $merchant['now_money'];
  226. $dat['thing5']['value'] = $mark;
  227. RoutineTemplate::sendAccountChanges($dat, $uid, '');
  228. }
  229. } catch (\Exception $e) {
  230. }
  231. return self::edit(compact('status'), $id);
  232. }
  233. //测试数据
  234. public static function test()
  235. {
  236. $uids = User::order('uid desc')->limit(2, 20)->field(['uid', 'nickname'])->select()->toArray();
  237. $type = ['bank', 'alipay', 'weixin'];
  238. foreach ($uids as $item) {
  239. $data = [
  240. 'uid' => $item['uid'],
  241. 'real_name' => $item['nickname'],
  242. 'extract_type' => isset($type[rand(0, 2)]) ? $type[rand(0, 2)] : 'alipay',
  243. 'bank_code' => rand(1000000, 999999999),
  244. 'bank_address' => '中国',
  245. 'alipay_code' => rand(1000, 9999999),
  246. 'extract_price' => rand(100, 9999),
  247. 'mark' => '测试数据',
  248. 'add_time' => time(),
  249. 'status' => 1,
  250. 'wechat' => rand(999, 878788) . $item['uid'],
  251. ];
  252. self::set($data);
  253. }
  254. }
  255. //获取头部提现信息
  256. public static function getExtractHead()
  257. {
  258. //本月提现人数
  259. $month = self::getModelTime(['data' => 'month'], self::where(['status' => 1]))->group('uid')->count();
  260. //本月提现笔数
  261. $new_month = self::getModelTime(['data' => 'month'], self::where(['status' => 1]))->distinct(true)->count();
  262. //上月提现人数
  263. $last_month = self::whereTime('add_time', 'last month')->where('status', 1)->group('uid')->distinct(true)->count();
  264. //上月提现笔数
  265. $last_count = self::whereTime('add_time', 'last month')->where('status', 1)->count();
  266. //本月提现金额
  267. $extract_price = self::getModelTime(['data' => 'month'], self::where(['status' => 1]))->sum('extract_price');
  268. //上月提现金额
  269. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', 1)->sum('extract_price');
  270. return [
  271. [
  272. 'name' => '总提现人数',
  273. 'field' => '个',
  274. 'count' => self::where(['status' => 1])->group('uid')->count(),
  275. 'content' => '',
  276. 'background_color' => 'layui-bg-blue',
  277. 'sum' => '',
  278. 'class' => 'fa fa-bar-chart',
  279. ],
  280. [
  281. 'name' => '总提现笔数',
  282. 'field' => '笔',
  283. 'count' => self::where(['status' => 1])->distinct(true)->count(),
  284. 'content' => '',
  285. 'background_color' => 'layui-bg-cyan',
  286. 'sum' => '',
  287. 'class' => 'fa fa-line-chart',
  288. ],
  289. [
  290. 'name' => '本月提现人数',
  291. 'field' => '人',
  292. 'count' => $month,
  293. 'content' => '',
  294. 'background_color' => 'layui-bg-orange',
  295. 'sum' => '',
  296. 'class' => 'fa fa-line-chart',
  297. ],
  298. [
  299. 'name' => '本月提现笔数',
  300. 'field' => '笔',
  301. 'count' => $new_month,
  302. 'content' => '',
  303. 'background_color' => 'layui-bg-green',
  304. 'sum' => '',
  305. 'class' => 'fa fa-line-chart',
  306. ],
  307. [
  308. 'name' => '本月提现金额',
  309. 'field' => '元',
  310. 'count' => $extract_price,
  311. 'content' => '提现总金额',
  312. 'background_color' => 'layui-bg-cyan',
  313. 'sum' => self::where(['status' => 1])->sum('extract_price'),
  314. 'class' => 'fa fa-line-chart',
  315. ],
  316. [
  317. 'name' => '上月提现人数',
  318. 'field' => '个',
  319. 'count' => $last_month,
  320. 'content' => '环比增幅',
  321. 'background_color' => 'layui-bg-blue',
  322. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  323. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  324. ],
  325. [
  326. 'name' => '上月提现笔数',
  327. 'field' => '笔',
  328. 'count' => $last_count,
  329. 'content' => '环比增幅',
  330. 'background_color' => 'layui-bg-black',
  331. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  332. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  333. ],
  334. [
  335. 'name' => '上月提现金额',
  336. 'field' => '元',
  337. 'count' => $last_extract_price,
  338. 'content' => '环比增幅',
  339. 'background_color' => 'layui-bg-gray',
  340. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  341. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  342. ],
  343. ];
  344. }
  345. //获取提现分布图和提现人数金额曲线图
  346. public static function getExtractList($where, $limit = 15)
  347. {
  348. $legdata = ['提现人数', '提现金额'];
  349. $list = self::getModelTime($where, self::where('status', 1))
  350. ->field([
  351. 'FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time',
  352. 'count(uid) as count',
  353. 'sum(extract_price) as sum_price',
  354. ])->group('un_time')->order('un_time asc')->select();
  355. if (count($list)) $list = $list->toArray();
  356. $xdata = [];
  357. $itemList = [0 => [], 1 => []];
  358. $chatrList = [];
  359. $zoom = '';
  360. foreach ($list as $value) {
  361. $xdata[] = $value['un_time'];
  362. $itemList[0][] = $value['count'];
  363. $itemList[1][] = $value['sum_price'];
  364. }
  365. foreach ($legdata as $key => $name) {
  366. $item['name'] = $name;
  367. $item['type'] = 'line';
  368. $item['data'] = $itemList[$key];
  369. $chatrList[] = $item;
  370. }
  371. unset($item, $name, $key);
  372. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  373. //饼状图
  374. $cake = ['支付宝', '银行卡', '微信'];
  375. $fenbulist = self::getModelTime($where, self::where('status', 1))
  376. ->field(['count(uid) as count', 'extract_type'])->group('extract_type')->order('count asc')->select();
  377. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  378. $sum_count = self::getModelTime($where, self::where('status', 1))->count();
  379. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  380. $fenbudata = [];
  381. foreach ($fenbulist as $key => $item) {
  382. if ($item['extract_type'] == 'bank') {
  383. $item_date['name'] = '银行卡';
  384. } else if ($item['extract_type'] == 'alipay') {
  385. $item_date['name'] = '支付宝';
  386. } else if ($item['extract_type'] == 'weixin') {
  387. $item_date['name'] = '微信';
  388. }
  389. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  390. $item_date['itemStyle']['color'] = $color[$key];
  391. $fenbudata[] = $item_date;
  392. }
  393. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  394. }
  395. /**
  396. * 获取用户累计提现金额
  397. * @param int $uid
  398. * @return int|mixed
  399. */
  400. public static function getUserCountPrice($uid = 0)
  401. {
  402. if (!$uid) return 0;
  403. $price = self::where('uid', $uid)->where('status', 1)->field('sum(extract_price) as price')->find()['price'];
  404. return $price ? $price : 0;
  405. }
  406. /**
  407. * 获取用户累计提现次数
  408. * @param int $uid
  409. * @return int|string
  410. */
  411. public static function getUserCountNum($uid = 0)
  412. {
  413. if (!$uid) return 0;
  414. return self::where('uid', $uid)->count();
  415. }
  416. /**
  417. * 获得用户提现总金额
  418. * @param $uid
  419. * @return mixed
  420. */
  421. public static function userExtractTotalPrice($uid, $status = 1, $where = [])
  422. {
  423. return self::getModelTime($where, self::where('uid', 'in', $uid)->where('status', $status))->sum('extract_price') ?: 0;
  424. }
  425. }