UserExtract.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 think\facade\Route as Url;
  12. use crmeb\traits\ModelTrait;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\services\WechatTemplateService;
  15. /**
  16. * 用户提现管理 model
  17. * Class User
  18. * @package app\admin\model\user
  19. */
  20. class UserExtract extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'id';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'user_extract';
  32. use ModelTrait;
  33. /**
  34. * 获得用户提现总金额
  35. * @param $uid
  36. * @return mixed
  37. */
  38. public static function userExtractTotalPrice($uid, $status = EXTRACT_SUC, $where = [])
  39. {
  40. return self::getModelTime($where, self::where('uid', 'in', $uid)->where('status', $status))->sum('extract_price') ?: 0;
  41. }
  42. public static function extractStatistics()
  43. {
  44. //待提现金额
  45. $data['price'] = floatval(self::where('status', EXTRACT_AUDITING)->sum('extract_price'));
  46. //总获得
  47. $sum = UserBill::getBrokerageCount();
  48. //退款扣除
  49. $lose = UserBill::loseBrokerageCount();
  50. //佣金总金额
  51. $data['brokerage_count'] = $sum > $lose ? floatval($sum - $lose) : 0;
  52. //已提现金额
  53. $data['priced'] = floatval(self::where('status', EXTRACT_SUC)->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');
  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. /**
  80. * 其他函數調用本函數需要放到事務處理的作用域內。
  81. *
  82. * 本函數內部不使用事務是因爲若外層調用方使用事物,會事務嵌套。也可能需要和其他邏輯組成一個大的事物
  83. */
  84. public static function changeFail($id, $fail_msg)
  85. {
  86. $fail_time = time();
  87. $data = self::get($id);
  88. $extract_number = $data['extract_price'];
  89. $mark = '提现失败,退回佣金' . $extract_number . '元';
  90. $uid = $data['uid'];
  91. $status = EXTRACT_FAILED;
  92. $User = User::where('uid', $uid)->find()->toArray();
  93. $res1 = UserBill::income('提现失败', $uid, 'now_money', 'extract', $extract_number, $id, bcadd($User['now_money'], $extract_number, 2), $mark);
  94. $res2 = User::bcInc($uid, 'brokerage_price', $extract_number, 'uid');
  95. // $extract_type = '未知方式';
  96. // switch ($data['extract_type']) {
  97. // case 'alipay':
  98. // $extract_type = '支付宝';
  99. // break;
  100. // case 'bank':
  101. // $extract_type = '银行卡';
  102. // break;
  103. // case 'weixin':
  104. // $extract_type = '微信';
  105. // break;
  106. // }
  107. // if (strtolower($User['user_type']) == 'wechat') {
  108. // WechatTemplateService::sendTemplate(WechatUser::where('uid', $uid)->value('openid'), WechatTemplateService::USER_BALANCE_CHANGE, [
  109. // 'first' => $mark,
  110. // 'keyword1' => '佣金提现',
  111. // 'keyword2' => date('Y-m-d H:i:s', time()),
  112. // 'keyword3' => $extract_number,
  113. // 'remark' => '错误原因:' . $fail_msg
  114. // ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  115. // } else if (strtolower($User['user_type']) == 'routine') {
  116. // RoutineTemplate::sendExtractFail($uid, $fail_msg, $extract_number, $User['nickname']);
  117. // }
  118. $res3 = self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  119. return $res1 && $res2 && $res3;
  120. }
  121. public static function changeSuccess($id)
  122. {
  123. // $data = self::get($id);
  124. // $extractNumber = $data['extract_price'];
  125. // $mark = '成功提现佣金' . $extractNumber . '元';
  126. // $wechatUserInfo = WechatUser::where('uid', $data['uid'])->field('openid,user_type,routine_openid,nickname')->find();
  127. // $extract_type = '未知方式';
  128. // switch ($data['extract_type']) {
  129. // case 'alipay':
  130. // $extract_type = '支付宝';
  131. // break;
  132. // case 'bank':
  133. // $extract_type = '银行卡';
  134. // break;
  135. // case 'weixin':
  136. // $extract_type = '微信';
  137. // break;
  138. // }
  139. // if ($wechatUserInfo) {
  140. // if (strtolower($wechatUserInfo->user_type) == 'routine') {
  141. // RoutineTemplate::sendExtractSuccess($data['uid'], $extractNumber, $wechatUserInfo->nickname);
  142. // } else if (strtolower($wechatUserInfo->user_type) == 'wechat') {
  143. // WechatTemplateService::sendTemplate($wechatUserInfo->openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  144. // 'first' => $mark,
  145. // 'keyword1' => '佣金提现',
  146. // 'keyword2' => date('Y-m-d H:i:s', time()),
  147. // 'keyword3' => $extractNumber,
  148. // 'remark' => '点击查看我的佣金明细'
  149. // ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  150. // }
  151. // }
  152. return self::edit(['status' => EXTRACT_SUC], $id);
  153. }
  154. //测试数据
  155. public static function test()
  156. {
  157. $uids = User::order('uid desc')->limit(2, 20)->field(['uid', 'nickname'])->select()->toArray();
  158. $type = ['bank', 'alipay', 'weixin'];
  159. foreach ($uids as $item) {
  160. $data = [
  161. 'uid' => $item['uid'],
  162. 'real_name' => $item['nickname'],
  163. 'extract_type' => isset($type[rand(0, 2)]) ? $type[rand(0, 2)] : 'alipay',
  164. 'bank_code' => rand(1000000, 999999999),
  165. 'bank_address' => '中国',
  166. 'alipay_code' => rand(1000, 9999999),
  167. 'extract_price' => rand(100, 9999),
  168. 'mark' => '测试数据',
  169. 'add_time' => time(),
  170. 'status' => EXTRACT_SUC,
  171. 'wechat' => rand(999, 878788) . $item['uid'],
  172. ];
  173. self::create($data);
  174. }
  175. }
  176. //获取头部提现信息
  177. public static function getExtractHead()
  178. {
  179. //本月提现人数
  180. $month = self::getModelTime(['data' => 'month'], self::where('status', EXTRACT_SUC))->group('uid')->count();
  181. //本月提现笔数
  182. $new_month = self::getModelTime(['data' => 'month'], self::where('status', EXTRACT_SUC))->distinct(true)->count();
  183. //上月提现人数
  184. $last_month = self::whereTime('add_time', 'last month')->where('status', EXTRACT_SUC)->group('uid')->distinct(true)->count();
  185. //上月提现笔数
  186. $last_count = self::whereTime('add_time', 'last month')->where('status', EXTRACT_SUC)->count();
  187. //本月提现金额
  188. $extract_price = self::getModelTime(['data' => 'month'], self::where('status', EXTRACT_SUC))->sum('extract_price');
  189. //上月提现金额
  190. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', EXTRACT_SUC)->sum('extract_price');
  191. return [
  192. [
  193. 'name' => '总提现人数',
  194. 'field' => '个',
  195. 'count' => self::where('status', EXTRACT_SUC)->group('uid')->count(),
  196. 'content' => '',
  197. 'background_color' => 'layui-bg-blue',
  198. 'sum' => '',
  199. 'class' => 'fa fa-bar-chart',
  200. ],
  201. [
  202. 'name' => '总提现笔数',
  203. 'field' => '笔',
  204. 'count' => self::where('status', EXTRACT_SUC)->distinct(true)->count(),
  205. 'content' => '',
  206. 'background_color' => 'layui-bg-cyan',
  207. 'sum' => '',
  208. 'class' => 'fa fa-line-chart',
  209. ],
  210. [
  211. 'name' => '本月提现人数',
  212. 'field' => '人',
  213. 'count' => $month,
  214. 'content' => '',
  215. 'background_color' => 'layui-bg-orange',
  216. 'sum' => '',
  217. 'class' => 'fa fa-line-chart',
  218. ],
  219. [
  220. 'name' => '本月提现笔数',
  221. 'field' => '笔',
  222. 'count' => $new_month,
  223. 'content' => '',
  224. 'background_color' => 'layui-bg-green',
  225. 'sum' => '',
  226. 'class' => 'fa fa-line-chart',
  227. ],
  228. [
  229. 'name' => '本月提现金额',
  230. 'field' => '元',
  231. 'count' => $extract_price,
  232. 'content' => '提现总金额',
  233. 'background_color' => 'layui-bg-cyan',
  234. 'sum' => self::where('status', EXTRACT_SUC)->sum('extract_price'),
  235. 'class' => 'fa fa-line-chart',
  236. ],
  237. [
  238. 'name' => '上月提现人数',
  239. 'field' => '个',
  240. 'count' => $last_month,
  241. 'content' => '环比增幅',
  242. 'background_color' => 'layui-bg-blue',
  243. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  244. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  245. ],
  246. [
  247. 'name' => '上月提现笔数',
  248. 'field' => '笔',
  249. 'count' => $last_count,
  250. 'content' => '环比增幅',
  251. 'background_color' => 'layui-bg-black',
  252. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  253. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  254. ],
  255. [
  256. 'name' => '上月提现金额',
  257. 'field' => '元',
  258. 'count' => $last_extract_price,
  259. 'content' => '环比增幅',
  260. 'background_color' => 'layui-bg-gray',
  261. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  262. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  263. ],
  264. ];
  265. }
  266. //获取提现分布图和提现人数金额曲线图
  267. public static function getExtractList($where, $limit = 15)
  268. {
  269. $legdata = ['提现人数', '提现金额'];
  270. $list = self::getModelTime($where, self::where('status', EXTRACT_SUC))
  271. ->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();
  272. if (count($list)) $list = $list->toArray();
  273. $xdata = [];
  274. $itemList = [0 => [], 1 => []];
  275. $chatrList = [];
  276. $zoom = '';
  277. foreach ($list as $value) {
  278. $xdata[] = $value['un_time'];
  279. $itemList[0][] = $value['count'];
  280. $itemList[1][] = $value['sum_price'];
  281. }
  282. foreach ($legdata as $key => $name) {
  283. $item['name'] = $name;
  284. $item['type'] = 'line';
  285. $item['data'] = $itemList[$key];
  286. $chatrList[] = $item;
  287. }
  288. unset($item, $name, $key);
  289. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  290. //饼状图
  291. $cake = ['支付宝', '银行卡', '微信'];
  292. $fenbulist = self::getModelTime($where, self::where('status', EXTRACT_SUC))
  293. ->field('count(uid) as count,extract_type')->group('extract_type')->order('count asc')->select();
  294. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  295. $sum_count = self::getModelTime($where, self::where('status', EXTRACT_SUC))->count();
  296. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  297. $fenbudata = [];
  298. foreach ($fenbulist as $key => $item) {
  299. if ($item['extract_type'] == 'bank') {
  300. $item_date['name'] = '银行卡';
  301. } else if ($item['extract_type'] == 'alipay') {
  302. $item_date['name'] = '支付宝';
  303. } else if ($item['extract_type'] == 'weixin') {
  304. $item_date['name'] = '微信';
  305. }
  306. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  307. $item_date['itemStyle']['color'] = $color[$key];
  308. $fenbudata[] = $item_date;
  309. }
  310. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  311. }
  312. /**
  313. * 获取用户累计提现金额
  314. * @param int $uid
  315. * @return int|mixed
  316. */
  317. public static function getUserCountPrice($uid = 0)
  318. {
  319. if (!$uid) return 0;
  320. $price = self::where('uid', $uid)->where('status', EXTRACT_SUC)->sum('extract_price');
  321. return $price ? $price : 0;
  322. }
  323. /**
  324. * 获取用户累计提现次数
  325. * @param int $uid
  326. * @return int|string
  327. */
  328. public static function getUserCountNum($uid = 0)
  329. {
  330. if (!$uid) return 0;
  331. return self::where('uid', $uid)->count();
  332. }
  333. }