UserExtract.php 16 KB

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