UserExtract.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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, 'now_money', 'extract', $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. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  112. }
  113. public static function changeSuccess($id)
  114. {
  115. $data = self::get($id);
  116. $extractNumber = $data['extract_price'];
  117. $mark = '成功提现余额' . $extractNumber . '元';
  118. $wechatUserInfo = WechatUser::where('uid', $data['uid'])->field('openid,user_type,routine_openid,nickname')->find();
  119. $extract_type = '未知方式';
  120. switch ($data['extract_type']) {
  121. case 'alipay':
  122. $extract_type = '支付宝';
  123. break;
  124. case 'bank':
  125. $extract_type = '银行卡';
  126. break;
  127. case 'weixin':
  128. $extract_type = '微信';
  129. if ($wechatUserInfo) {
  130. if (strtolower($wechatUserInfo->user_type) == 'routine') {
  131. //直接到零钱
  132. $list[] = [
  133. 'out_detail_no' => 'kb' . date('YmdHis') . $data['uid'] . rand(1000, 9999),
  134. 'transfer_amount' => $extractNumber * 100,
  135. 'transfer_remark' => '用户提现' . $extractNumber . '元',
  136. 'openid' => $wechatUserInfo['routine_openid'],
  137. ];
  138. $batchesOn = 'bt' . date('YmdHis') . $data['uid'] . rand(1000, 9999);
  139. try {
  140. $res = MiniProgramService::batches($batchesOn, '用户提现', '用户提现 - ' . date('Y-m-d H:i:s'), $list);
  141. var_dump($res);
  142. } catch (\Exception $e) {
  143. var_dump($e->getMessage());
  144. }
  145. } else if (strtolower($wechatUserInfo->user_type) == 'wechat') {
  146. //直接到零钱
  147. $list[] = [
  148. 'out_detail_no' => 'kb' . date('YmdHis') . $data['uid'] . rand(1000, 9999),
  149. 'transfer_amount' => $extractNumber * 100,
  150. 'transfer_remark' => '用户提现' . $extractNumber . '元',
  151. 'openid' => $wechatUserInfo['openid'],
  152. ];
  153. $batchesOn = 'bt' . date('YmdHis') . $data['uid'] . rand(1000, 9999);
  154. try {
  155. $res = WechatService::batches($batchesOn, '用户提现', '用户提现 - ' . date('Y-m-d H:i:s'), $list);
  156. var_dump($res);
  157. } catch (\Exception $e) {
  158. var_dump($e->getMessage());
  159. }
  160. }
  161. }
  162. break;
  163. }
  164. if ($wechatUserInfo) {
  165. if (strtolower($wechatUserInfo->user_type) == 'routine') {
  166. RoutineTemplate::sendExtractSuccess($data['uid'], $extractNumber, $wechatUserInfo->nickname);
  167. } else if (strtolower($wechatUserInfo->user_type) == 'wechat') {
  168. WechatTemplateService::sendTemplate($wechatUserInfo->openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  169. 'first' => $mark,
  170. 'keyword1' => '余额提现',
  171. 'keyword2' => date('Y-m-d H:i:s', time()),
  172. 'keyword3' => $extractNumber,
  173. 'remark' => '点击查看我的余额明细'
  174. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  175. }
  176. }
  177. return self::edit(['status' => 1], $id);
  178. }
  179. //测试数据
  180. public static function test()
  181. {
  182. $uids = User::order('uid desc')->limit(2, 20)->field(['uid', 'nickname'])->select()->toArray();
  183. $type = ['bank', 'alipay', 'weixin'];
  184. foreach ($uids as $item) {
  185. $data = [
  186. 'uid' => $item['uid'],
  187. 'real_name' => $item['nickname'],
  188. 'extract_type' => isset($type[rand(0, 2)]) ? $type[rand(0, 2)] : 'alipay',
  189. 'bank_code' => rand(1000000, 999999999),
  190. 'bank_address' => '中国',
  191. 'alipay_code' => rand(1000, 9999999),
  192. 'extract_price' => rand(100, 9999),
  193. 'mark' => '测试数据',
  194. 'add_time' => time(),
  195. 'status' => 1,
  196. 'wechat' => rand(999, 878788) . $item['uid'],
  197. ];
  198. self::create($data);
  199. }
  200. }
  201. //获取头部提现信息
  202. public static function getExtractHead()
  203. {
  204. //本月提现人数
  205. $month = self::getModelTime(['data' => 'month'], self::where('status', 1))->group('uid')->count();
  206. //本月提现笔数
  207. $new_month = self::getModelTime(['data' => 'month'], self::where('status', 1))->distinct(true)->count();
  208. //上月提现人数
  209. $last_month = self::whereTime('add_time', 'last month')->where('status', 1)->group('uid')->distinct(true)->count();
  210. //上月提现笔数
  211. $last_count = self::whereTime('add_time', 'last month')->where('status', 1)->count();
  212. //本月提现金额
  213. $extract_price = self::getModelTime(['data' => 'month'], self::where('status', 1))->sum('extract_price');
  214. //上月提现金额
  215. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', 1)->sum('extract_price');
  216. return [
  217. [
  218. 'name' => '总提现人数',
  219. 'field' => '个',
  220. 'count' => self::where('status', 1)->group('uid')->count(),
  221. 'content' => '',
  222. 'background_color' => 'layui-bg-blue',
  223. 'sum' => '',
  224. 'class' => 'fa fa-bar-chart',
  225. ],
  226. [
  227. 'name' => '总提现笔数',
  228. 'field' => '笔',
  229. 'count' => self::where('status', 1)->distinct(true)->count(),
  230. 'content' => '',
  231. 'background_color' => 'layui-bg-cyan',
  232. 'sum' => '',
  233. 'class' => 'fa fa-line-chart',
  234. ],
  235. [
  236. 'name' => '本月提现人数',
  237. 'field' => '人',
  238. 'count' => $month,
  239. 'content' => '',
  240. 'background_color' => 'layui-bg-orange',
  241. 'sum' => '',
  242. 'class' => 'fa fa-line-chart',
  243. ],
  244. [
  245. 'name' => '本月提现笔数',
  246. 'field' => '笔',
  247. 'count' => $new_month,
  248. 'content' => '',
  249. 'background_color' => 'layui-bg-green',
  250. 'sum' => '',
  251. 'class' => 'fa fa-line-chart',
  252. ],
  253. [
  254. 'name' => '本月提现金额',
  255. 'field' => '元',
  256. 'count' => $extract_price,
  257. 'content' => '提现总金额',
  258. 'background_color' => 'layui-bg-cyan',
  259. 'sum' => self::where('status', 1)->sum('extract_price'),
  260. 'class' => 'fa fa-line-chart',
  261. ],
  262. [
  263. 'name' => '上月提现人数',
  264. 'field' => '个',
  265. 'count' => $last_month,
  266. 'content' => '环比增幅',
  267. 'background_color' => 'layui-bg-blue',
  268. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  269. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  270. ],
  271. [
  272. 'name' => '上月提现笔数',
  273. 'field' => '笔',
  274. 'count' => $last_count,
  275. 'content' => '环比增幅',
  276. 'background_color' => 'layui-bg-black',
  277. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  278. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  279. ],
  280. [
  281. 'name' => '上月提现金额',
  282. 'field' => '元',
  283. 'count' => $last_extract_price,
  284. 'content' => '环比增幅',
  285. 'background_color' => 'layui-bg-gray',
  286. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  287. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  288. ],
  289. ];
  290. }
  291. //获取提现分布图和提现人数金额曲线图
  292. public static function getExtractList($where, $limit = 15)
  293. {
  294. $legdata = ['提现人数', '提现金额'];
  295. $list = self::getModelTime($where, self::where('status', 1))
  296. ->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();
  297. if (count($list)) $list = $list->toArray();
  298. $xdata = [];
  299. $itemList = [0 => [], 1 => []];
  300. $chatrList = [];
  301. $zoom = '';
  302. foreach ($list as $value) {
  303. $xdata[] = $value['un_time'];
  304. $itemList[0][] = $value['count'];
  305. $itemList[1][] = $value['sum_price'];
  306. }
  307. foreach ($legdata as $key => $name) {
  308. $item['name'] = $name;
  309. $item['type'] = 'line';
  310. $item['data'] = $itemList[$key];
  311. $chatrList[] = $item;
  312. }
  313. unset($item, $name, $key);
  314. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  315. //饼状图
  316. $cake = ['支付宝', '银行卡', '微信'];
  317. $fenbulist = self::getModelTime($where, self::where('status', 1))
  318. ->field('count(uid) as count,extract_type')->group('extract_type')->order('count asc')->select();
  319. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  320. $sum_count = self::getModelTime($where, self::where('status', 1))->count();
  321. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  322. $fenbudata = [];
  323. foreach ($fenbulist as $key => $item) {
  324. if ($item['extract_type'] == 'bank') {
  325. $item_date['name'] = '银行卡';
  326. } else if ($item['extract_type'] == 'alipay') {
  327. $item_date['name'] = '支付宝';
  328. } else if ($item['extract_type'] == 'weixin') {
  329. $item_date['name'] = '微信';
  330. }
  331. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  332. $item_date['itemStyle']['color'] = $color[$key];
  333. $fenbudata[] = $item_date;
  334. }
  335. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  336. }
  337. /**
  338. * 获取用户累计提现金额
  339. * @param int $uid
  340. * @return int|mixed
  341. */
  342. public static function getUserCountPrice($uid = 0)
  343. {
  344. if (!$uid) return 0;
  345. $price = self::where('uid', $uid)->where('status', 1)->sum('extract_price');
  346. return $price ? $price : 0;
  347. }
  348. /**
  349. * 获取用户累计提现次数
  350. * @param int $uid
  351. * @return int|string
  352. */
  353. public static function getUserCountNum($uid = 0)
  354. {
  355. if (!$uid) return 0;
  356. return self::where('uid', $uid)->count();
  357. }
  358. }