UserExtract.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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\wechat\WechatUser;
  10. use app\models\routine\RoutineTemplate;
  11. use crmeb\services\MiniProgramService;
  12. use crmeb\services\WechatService;
  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(UserBill::getBrokerageCount());
  50. //已提现金额
  51. $data['priced'] = floatval(self::where('status', 1)->sum('extract_price'));
  52. //未提现金额
  53. $data['brokerage_not'] = bcsub(bcsub($data['brokerage_count'], $data['priced'], 2), $data['price'], 2);
  54. return compact('data');
  55. }
  56. /**
  57. * @param $where
  58. * @return array
  59. */
  60. public static function systemPage($where)
  61. {
  62. $model = new self;
  63. if ($where['date'] != '') {
  64. list($startTime, $endTime) = explode(' - ', $where['date']);
  65. $model = $model->where('a.add_time', '>', strtotime($startTime));
  66. $model = $model->where('a.add_time', '<', (int)bcadd(strtotime($endTime), 86400, 0));
  67. }
  68. if ($where['status'] != '') $model = $model->where('a.status', $where['status']);
  69. if ($where['extract_type'] != '') $model = $model->where('a.extract_type', $where['extract_type']);
  70. if ($where['nireid'] != '') $model = $model->where('a.real_name|a.id|b.nickname|a.bank_code|a.alipay_code', 'like', "%$where[nireid]%");
  71. $model = $model->alias('a');
  72. $model = $model->field('a.*,b.nickname');
  73. $model = $model->join('user b', 'b.uid=a.uid', 'LEFT');
  74. $model = $model->order('a.id desc');
  75. return self::page($model, $where);
  76. }
  77. public static function changeFail($id, $fail_msg)
  78. {
  79. $fail_time = time();
  80. $data = self::get($id);
  81. $extract_number = $data['extract_price'];
  82. $mark = '提现失败,退回佣金' . $extract_number . '元';
  83. $uid = $data['uid'];
  84. $status = -1;
  85. $User = User::where('uid', $uid)->find()->toArray();
  86. UserBill::income('提现失败', $uid, 'brokerage_price', 'extract_fail', $extract_number, $id, bcadd($User['now_money'], $extract_number, 2), $mark);
  87. User::bcInc($uid, 'brokerage_price', $extract_number, 'uid');
  88. $extract_type = '未知方式';
  89. switch ($data['extract_type']) {
  90. case 'alipay':
  91. $extract_type = '支付宝';
  92. break;
  93. case 'bank':
  94. $extract_type = '银行卡';
  95. break;
  96. case 'weixin':
  97. $extract_type = '微信';
  98. break;
  99. }
  100. if (strtolower($User['user_type']) == 'wechat') {
  101. WechatTemplateService::sendTemplate(WechatUser::where('uid', $uid)->value('openid'), WechatTemplateService::USER_BALANCE_CHANGE, [
  102. 'first' => $mark,
  103. 'keyword1' => '佣金提现',
  104. 'keyword2' => date('Y-m-d H:i:s', time()),
  105. 'keyword3' => $extract_number,
  106. 'remark' => '错误原因:' . $fail_msg
  107. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  108. } else if (strtolower($User['user_type']) == 'routine') {
  109. RoutineTemplate::sendExtractFail($uid, $fail_msg, $extract_number, $User['nickname']);
  110. }
  111. // 通知提现用户
  112. $openid = WechatUser::where('uid', $uid)->value('openid');
  113. if (!empty($openid)) {
  114. $group = [
  115. 'first' => '提现审核处理通知',
  116. 'remark' => '点击查看订单详情'
  117. ];
  118. $time = date('Y-m-d H:i:s', time());
  119. $group = array_merge($group, [
  120. 'thing3' => $User['nickname'], //客户名称
  121. 'amount6' => $extract_number, //提现金额
  122. 'const15' => '驳回', //审核结果
  123. 'thing4' => $extract_type, //提现分类
  124. 'time8' => $time, //提现时间
  125. ]);
  126. @file_put_contents('quanju.txt', json_encode($group) . "-模版消息4\r\n", 8);
  127. $res = WechatTemplateService::sendTemplate($openid, WechatTemplateService::Extract_SUCCESS, $group);
  128. @file_put_contents('quanju.txt', json_encode($res) . "-模版消息返回4\r\n", 8);
  129. }
  130. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  131. }
  132. public static function changeSuccess($id)
  133. {
  134. $data = self::get($id);
  135. $extractNumber = $data['real_get'];
  136. $mark = '成功提现余额' . $extractNumber . '元';
  137. $wechatUserInfo = WechatUser::where('uid', $data['uid'])->field('openid,user_type,routine_openid,nickname')->find();
  138. $extract_type = '未知方式';
  139. switch ($data['extract_type']) {
  140. case 'alipay':
  141. $extract_type = '支付宝';
  142. break;
  143. case 'bank':
  144. $extract_type = '银行卡';
  145. break;
  146. case 'weixin':
  147. $extract_type = '微信';
  148. if ($wechatUserInfo) {
  149. if (strtolower($wechatUserInfo->user_type) == 'routine') {
  150. //直接到零钱
  151. $list[] = [
  152. 'out_detail_no' => 'kb' . date('YmdHis') . $data['uid'] . rand(1000, 9999),
  153. 'transfer_amount' => $extractNumber * 100,
  154. 'transfer_remark' => '用户提现' . $extractNumber . '元',
  155. 'openid' => $wechatUserInfo['routine_openid'],
  156. ];
  157. $batchesOn = 'bt' . date('YmdHis') . $data['uid'] . rand(1000, 9999);
  158. try {
  159. $res = MiniProgramService::batches($batchesOn, '用户提现', '用户提现 - ' . date('Y-m-d H:i:s'), $list);
  160. var_dump($res);
  161. } catch (\Exception $e) {
  162. var_dump($e->getMessage());
  163. }
  164. } else if (strtolower($wechatUserInfo->user_type) == 'wechat') {
  165. //直接到零钱
  166. $list[] = [
  167. 'out_detail_no' => 'kb' . date('YmdHis') . $data['uid'] . rand(1000, 9999),
  168. 'transfer_amount' => $extractNumber * 100,
  169. 'transfer_remark' => '用户提现' . $extractNumber . '元',
  170. 'openid' => $wechatUserInfo['openid'],
  171. ];
  172. $batchesOn = 'bt' . date('YmdHis') . $data['uid'] . rand(1000, 9999);
  173. try {
  174. $res = WechatService::batches($batchesOn, '用户提现', '用户提现 - ' . date('Y-m-d H:i:s'), $list);
  175. var_dump($res);
  176. } catch (\Exception $e) {
  177. var_dump($e->getMessage());
  178. }
  179. }
  180. }
  181. break;
  182. }
  183. if ($wechatUserInfo) {
  184. if (strtolower($wechatUserInfo->user_type) == 'routine') {
  185. RoutineTemplate::sendExtractSuccess($data['uid'], $extractNumber, $wechatUserInfo->nickname);
  186. } else if (strtolower($wechatUserInfo->user_type) == 'wechat') {
  187. WechatTemplateService::sendTemplate($wechatUserInfo->openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  188. 'first' => $mark,
  189. 'keyword1' => '余额提现',
  190. 'keyword2' => date('Y-m-d H:i:s', time()),
  191. 'keyword3' => $extractNumber,
  192. 'remark' => '点击查看我的余额明细'
  193. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  194. }
  195. // 通知提现用户
  196. $openid = WechatUser::where('uid', $data['uid'])->value('openid');
  197. $nickname = User::where('uid', $data['uid'])->value('nickname');
  198. if (!empty($openid)) {
  199. $group = [
  200. 'first' => '提现审核处理通知',
  201. 'remark' => '点击查看订单详情'
  202. ];
  203. $time = date('Y-m-d H:i:s', time());
  204. $group = array_merge($group, [
  205. 'thing3' => $nickname, //客户名称
  206. 'amount6' => $extractNumber, //提现金额
  207. 'const15' => '通过', //审核结果
  208. 'thing4' => $extract_type, //提现分类
  209. 'time8' => $time, //提现时间
  210. ]);
  211. @file_put_contents('quanju.txt', json_encode($group) . "-模版消息5\r\n", 8);
  212. $res = WechatTemplateService::sendTemplate($openid, WechatTemplateService::Extract_SUCCESS, $group);
  213. @file_put_contents('quanju.txt', json_encode($res) . "-模版消息返回5\r\n", 8);
  214. }
  215. }
  216. return self::edit(['status' => 1], $id);
  217. }
  218. //测试数据
  219. public static function test()
  220. {
  221. $uids = User::order('uid desc')->limit(2, 20)->field(['uid', 'nickname'])->select()->toArray();
  222. $type = ['bank', 'alipay', 'weixin'];
  223. foreach ($uids as $item) {
  224. $data = [
  225. 'uid' => $item['uid'],
  226. 'real_name' => $item['nickname'],
  227. 'extract_type' => isset($type[rand(0, 2)]) ? $type[rand(0, 2)] : 'alipay',
  228. 'bank_code' => rand(1000000, 999999999),
  229. 'bank_address' => '中国',
  230. 'alipay_code' => rand(1000, 9999999),
  231. 'extract_price' => rand(100, 9999),
  232. 'mark' => '测试数据',
  233. 'add_time' => time(),
  234. 'status' => 1,
  235. 'wechat' => rand(999, 878788) . $item['uid'],
  236. ];
  237. self::create($data);
  238. }
  239. }
  240. //获取头部提现信息
  241. public static function getExtractHead()
  242. {
  243. //本月提现人数
  244. $month = self::getModelTime(['data' => 'month'], self::where('status', 1))->group('uid')->count();
  245. //本月提现笔数
  246. $new_month = self::getModelTime(['data' => 'month'], self::where('status', 1))->distinct(true)->count();
  247. //上月提现人数
  248. $last_month = self::whereTime('add_time', 'last month')->where('status', 1)->group('uid')->distinct(true)->count();
  249. //上月提现笔数
  250. $last_count = self::whereTime('add_time', 'last month')->where('status', 1)->count();
  251. //本月提现金额
  252. $extract_price = self::getModelTime(['data' => 'month'], self::where('status', 1))->sum('extract_price');
  253. //上月提现金额
  254. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', 1)->sum('extract_price');
  255. return [
  256. [
  257. 'name' => '总提现人数',
  258. 'field' => '个',
  259. 'count' => self::where('status', 1)->group('uid')->count(),
  260. 'content' => '',
  261. 'background_color' => 'layui-bg-blue',
  262. 'sum' => '',
  263. 'class' => 'fa fa-bar-chart',
  264. ],
  265. [
  266. 'name' => '总提现笔数',
  267. 'field' => '笔',
  268. 'count' => self::where('status', 1)->distinct(true)->count(),
  269. 'content' => '',
  270. 'background_color' => 'layui-bg-cyan',
  271. 'sum' => '',
  272. 'class' => 'fa fa-line-chart',
  273. ],
  274. [
  275. 'name' => '本月提现人数',
  276. 'field' => '人',
  277. 'count' => $month,
  278. 'content' => '',
  279. 'background_color' => 'layui-bg-orange',
  280. 'sum' => '',
  281. 'class' => 'fa fa-line-chart',
  282. ],
  283. [
  284. 'name' => '本月提现笔数',
  285. 'field' => '笔',
  286. 'count' => $new_month,
  287. 'content' => '',
  288. 'background_color' => 'layui-bg-green',
  289. 'sum' => '',
  290. 'class' => 'fa fa-line-chart',
  291. ],
  292. [
  293. 'name' => '本月提现金额',
  294. 'field' => '元',
  295. 'count' => $extract_price,
  296. 'content' => '提现总金额',
  297. 'background_color' => 'layui-bg-cyan',
  298. 'sum' => self::where('status', 1)->sum('extract_price'),
  299. 'class' => 'fa fa-line-chart',
  300. ],
  301. [
  302. 'name' => '上月提现人数',
  303. 'field' => '个',
  304. 'count' => $last_month,
  305. 'content' => '环比增幅',
  306. 'background_color' => 'layui-bg-blue',
  307. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  308. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  309. ],
  310. [
  311. 'name' => '上月提现笔数',
  312. 'field' => '笔',
  313. 'count' => $last_count,
  314. 'content' => '环比增幅',
  315. 'background_color' => 'layui-bg-black',
  316. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  317. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  318. ],
  319. [
  320. 'name' => '上月提现金额',
  321. 'field' => '元',
  322. 'count' => $last_extract_price,
  323. 'content' => '环比增幅',
  324. 'background_color' => 'layui-bg-gray',
  325. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  326. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  327. ],
  328. ];
  329. }
  330. //获取提现分布图和提现人数金额曲线图
  331. public static function getExtractList($where, $limit = 15)
  332. {
  333. $legdata = ['提现人数', '提现金额'];
  334. $list = self::getModelTime($where, self::where('status', 1))
  335. ->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();
  336. if (count($list)) $list = $list->toArray();
  337. $xdata = [];
  338. $itemList = [0 => [], 1 => []];
  339. $chatrList = [];
  340. $zoom = '';
  341. foreach ($list as $value) {
  342. $xdata[] = $value['un_time'];
  343. $itemList[0][] = $value['count'];
  344. $itemList[1][] = $value['sum_price'];
  345. }
  346. foreach ($legdata as $key => $name) {
  347. $item['name'] = $name;
  348. $item['type'] = 'line';
  349. $item['data'] = $itemList[$key];
  350. $chatrList[] = $item;
  351. }
  352. unset($item, $name, $key);
  353. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  354. //饼状图
  355. $cake = ['支付宝', '银行卡', '微信'];
  356. $fenbulist = self::getModelTime($where, self::where('status', 1))
  357. ->field('count(uid) as count,extract_type')->group('extract_type')->order('count asc')->select();
  358. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  359. $sum_count = self::getModelTime($where, self::where('status', 1))->count();
  360. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  361. $fenbudata = [];
  362. foreach ($fenbulist as $key => $item) {
  363. if ($item['extract_type'] == 'bank') {
  364. $item_date['name'] = '银行卡';
  365. } else if ($item['extract_type'] == 'alipay') {
  366. $item_date['name'] = '支付宝';
  367. } else if ($item['extract_type'] == 'weixin') {
  368. $item_date['name'] = '微信';
  369. }
  370. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  371. $item_date['itemStyle']['color'] = $color[$key];
  372. $fenbudata[] = $item_date;
  373. }
  374. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  375. }
  376. /**
  377. * 获取用户累计提现金额
  378. * @param int $uid
  379. * @return int|mixed
  380. */
  381. public static function getUserCountPrice($uid = 0)
  382. {
  383. if (!$uid) return 0;
  384. $price = self::where('uid', $uid)->where('status', 1)->sum('extract_price');
  385. return $price ? $price : 0;
  386. }
  387. /**
  388. * 获取用户累计提现次数
  389. * @param int $uid
  390. * @return int|string
  391. */
  392. public static function getUserCountNum($uid = 0)
  393. {
  394. if (!$uid) return 0;
  395. return self::where('uid', $uid)->count();
  396. }
  397. }