UserExtract.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 = 1, $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', 0)->sum('extract_price'));
  46. //佣金总金额
  47. $data['brokerage_count'] = floatval(UserBill::getBrokerageCount());
  48. //已提现金额
  49. $data['priced'] = floatval(self::where('status', 1)->sum('extract_price'));
  50. //未提现金额
  51. $data['brokerage_not'] = bcsub(bcsub($data['brokerage_count'], $data['priced'], 2), $data['price'], 2);
  52. return compact('data');
  53. }
  54. /**
  55. * @param $where
  56. * @return array
  57. */
  58. public static function systemPage($where)
  59. {
  60. $model = new self;
  61. if ($where['date'] != '') {
  62. list($startTime, $endTime) = explode(' - ', $where['date']);
  63. $model = $model->where('a.add_time', '>', strtotime($startTime));
  64. $model = $model->where('a.add_time', '<', (int)bcadd(strtotime($endTime), 86400, 0));
  65. }
  66. if ($where['status'] != '') $model = $model->where('a.status', $where['status']);
  67. if ($where['extract_type'] != '') $model = $model->where('a.extract_type', $where['extract_type']);
  68. if ($where['nireid'] != '') $model = $model->where('a.real_name|a.id|b.nickname|a.bank_code|a.alipay_code', 'like', "%$where[nireid]%");
  69. $model = $model->alias('a');
  70. $model = $model->field('a.*,b.nickname');
  71. $model = $model->join('user b', 'b.uid=a.uid', 'LEFT');
  72. $model = $model->order('a.id desc');
  73. return self::page($model, $where);
  74. }
  75. public static function changeFail($id, $fail_msg)
  76. {
  77. $fail_time = time();
  78. $data = self::get($id);
  79. $extract_number = $data['extract_price'];
  80. $mark = '提现失败,退回佣金' . $extract_number . '元';
  81. $uid = $data['uid'];
  82. $status = -1;
  83. $User = User::where('uid', $uid)->find()->toArray();
  84. UserBill::income('提现失败', $uid, 'now_money', 'extract', $extract_number, $id, bcadd($User['now_money'], $extract_number, 2), $mark);
  85. User::bcInc($uid, 'brokerage_price', $extract_number, 'uid');
  86. $extract_type = '未知方式';
  87. switch ($data['extract_type']) {
  88. case 'alipay':
  89. $extract_type = '支付宝';
  90. break;
  91. case 'bank':
  92. $extract_type = '银行卡';
  93. break;
  94. case 'weixin':
  95. $extract_type = '微信';
  96. break;
  97. }
  98. if (strtolower($User['user_type']) == 'wechat') {
  99. WechatTemplateService::sendTemplate(WechatUser::where('uid', $uid)->value('openid'), WechatTemplateService::USER_BALANCE_CHANGE, [
  100. 'first' => $mark,
  101. 'keyword1' => '佣金提现',
  102. 'keyword2' => date('Y-m-d H:i:s', time()),
  103. 'keyword3' => $extract_number,
  104. 'remark' => '错误原因:' . $fail_msg
  105. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  106. } else if (strtolower($User['user_type']) == 'routine') {
  107. RoutineTemplate::sendExtractFail($uid, $fail_msg, $extract_number, $User['nickname']);
  108. }
  109. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  110. }
  111. public static function changeSuccess($id)
  112. {
  113. $data = self::get($id);
  114. $extractNumber = $data['extract_price'];
  115. $mark = '成功提现佣金' . $extractNumber . '元';
  116. $wechatUserInfo = WechatUser::where('uid', $data['uid'])->field('openid,user_type,routine_openid,nickname')->find();
  117. $extract_type = '未知方式';
  118. switch ($data['extract_type']) {
  119. case 'alipay':
  120. $extract_type = '支付宝';
  121. break;
  122. case 'bank':
  123. $extract_type = '银行卡';
  124. break;
  125. case 'weixin':
  126. $extract_type = '微信';
  127. break;
  128. }
  129. if ($wechatUserInfo) {
  130. if (strtolower($wechatUserInfo->user_type) == 'routine') {
  131. RoutineTemplate::sendExtractSuccess($data['uid'], $extractNumber, $wechatUserInfo->nickname);
  132. } else if (strtolower($wechatUserInfo->user_type) == 'wechat') {
  133. WechatTemplateService::sendTemplate($wechatUserInfo->openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  134. 'first' => $mark,
  135. 'keyword1' => '佣金提现',
  136. 'keyword2' => date('Y-m-d H:i:s', time()),
  137. 'keyword3' => $extractNumber,
  138. 'remark' => '点击查看我的佣金明细'
  139. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  140. }
  141. }
  142. return self::edit(['status' => 1], $id);
  143. }
  144. //测试数据
  145. public static function test()
  146. {
  147. $uids = User::order('uid desc')->limit(2, 20)->field(['uid', 'nickname'])->select()->toArray();
  148. $type = ['bank', 'alipay', 'weixin'];
  149. foreach ($uids as $item) {
  150. $data = [
  151. 'uid' => $item['uid'],
  152. 'real_name' => $item['nickname'],
  153. 'extract_type' => isset($type[rand(0, 2)]) ? $type[rand(0, 2)] : 'alipay',
  154. 'bank_code' => rand(1000000, 999999999),
  155. 'bank_address' => '中国',
  156. 'alipay_code' => rand(1000, 9999999),
  157. 'extract_price' => rand(100, 9999),
  158. 'mark' => '测试数据',
  159. 'add_time' => time(),
  160. 'status' => 1,
  161. 'wechat' => rand(999, 878788) . $item['uid'],
  162. ];
  163. self::create($data);
  164. }
  165. }
  166. //获取头部提现信息
  167. public static function getExtractHead()
  168. {
  169. //本月提现人数
  170. $month = self::getModelTime(['data' => 'month'], self::where('status', 1))->group('uid')->count();
  171. //本月提现笔数
  172. $new_month = self::getModelTime(['data' => 'month'], self::where('status', 1))->distinct(true)->count();
  173. //上月提现人数
  174. $last_month = self::whereTime('add_time', 'last month')->where('status', 1)->group('uid')->distinct(true)->count();
  175. //上月提现笔数
  176. $last_count = self::whereTime('add_time', 'last month')->where('status', 1)->count();
  177. //本月提现金额
  178. $extract_price = self::getModelTime(['data' => 'month'], self::where('status', 1))->sum('extract_price');
  179. //上月提现金额
  180. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', 1)->sum('extract_price');
  181. return [
  182. [
  183. 'name' => '总提现人数',
  184. 'field' => '个',
  185. 'count' => self::where('status', 1)->group('uid')->count(),
  186. 'content' => '',
  187. 'background_color' => 'layui-bg-blue',
  188. 'sum' => '',
  189. 'class' => 'fa fa-bar-chart',
  190. ],
  191. [
  192. 'name' => '总提现笔数',
  193. 'field' => '笔',
  194. 'count' => self::where('status', 1)->distinct(true)->count(),
  195. 'content' => '',
  196. 'background_color' => 'layui-bg-cyan',
  197. 'sum' => '',
  198. 'class' => 'fa fa-line-chart',
  199. ],
  200. [
  201. 'name' => '本月提现人数',
  202. 'field' => '人',
  203. 'count' => $month,
  204. 'content' => '',
  205. 'background_color' => 'layui-bg-orange',
  206. 'sum' => '',
  207. 'class' => 'fa fa-line-chart',
  208. ],
  209. [
  210. 'name' => '本月提现笔数',
  211. 'field' => '笔',
  212. 'count' => $new_month,
  213. 'content' => '',
  214. 'background_color' => 'layui-bg-green',
  215. 'sum' => '',
  216. 'class' => 'fa fa-line-chart',
  217. ],
  218. [
  219. 'name' => '本月提现金额',
  220. 'field' => '元',
  221. 'count' => $extract_price,
  222. 'content' => '提现总金额',
  223. 'background_color' => 'layui-bg-cyan',
  224. 'sum' => self::where('status', 1)->sum('extract_price'),
  225. 'class' => 'fa fa-line-chart',
  226. ],
  227. [
  228. 'name' => '上月提现人数',
  229. 'field' => '个',
  230. 'count' => $last_month,
  231. 'content' => '环比增幅',
  232. 'background_color' => 'layui-bg-blue',
  233. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  234. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  235. ],
  236. [
  237. 'name' => '上月提现笔数',
  238. 'field' => '笔',
  239. 'count' => $last_count,
  240. 'content' => '环比增幅',
  241. 'background_color' => 'layui-bg-black',
  242. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  243. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  244. ],
  245. [
  246. 'name' => '上月提现金额',
  247. 'field' => '元',
  248. 'count' => $last_extract_price,
  249. 'content' => '环比增幅',
  250. 'background_color' => 'layui-bg-gray',
  251. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  252. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  253. ],
  254. ];
  255. }
  256. //获取提现分布图和提现人数金额曲线图
  257. public static function getExtractList($where, $limit = 15)
  258. {
  259. $legdata = ['提现人数', '提现金额'];
  260. $list = self::getModelTime($where, self::where('status', 1))
  261. ->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();
  262. if (count($list)) $list = $list->toArray();
  263. $xdata = [];
  264. $itemList = [0 => [], 1 => []];
  265. $chatrList = [];
  266. $zoom = '';
  267. foreach ($list as $value) {
  268. $xdata[] = $value['un_time'];
  269. $itemList[0][] = $value['count'];
  270. $itemList[1][] = $value['sum_price'];
  271. }
  272. foreach ($legdata as $key => $name) {
  273. $item['name'] = $name;
  274. $item['type'] = 'line';
  275. $item['data'] = $itemList[$key];
  276. $chatrList[] = $item;
  277. }
  278. unset($item, $name, $key);
  279. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  280. //饼状图
  281. $cake = ['支付宝', '银行卡', '微信'];
  282. $fenbulist = self::getModelTime($where, self::where('status', 1))
  283. ->field('count(uid) as count,extract_type')->group('extract_type')->order('count asc')->select();
  284. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  285. $sum_count = self::getModelTime($where, self::where('status', 1))->count();
  286. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  287. $fenbudata = [];
  288. foreach ($fenbulist as $key => $item) {
  289. if ($item['extract_type'] == 'bank') {
  290. $item_date['name'] = '银行卡';
  291. } else if ($item['extract_type'] == 'alipay') {
  292. $item_date['name'] = '支付宝';
  293. } else if ($item['extract_type'] == 'weixin') {
  294. $item_date['name'] = '微信';
  295. }
  296. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  297. $item_date['itemStyle']['color'] = $color[$key];
  298. $fenbudata[] = $item_date;
  299. }
  300. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  301. }
  302. /**
  303. * 获取用户累计提现金额
  304. * @param int $uid
  305. * @return int|mixed
  306. */
  307. public static function getUserCountPrice($uid = 0)
  308. {
  309. if (!$uid) return 0;
  310. $price = self::where('uid', $uid)->where('status', 1)->sum('extract_price');
  311. return $price ? $price : 0;
  312. }
  313. /**
  314. * 获取用户累计提现次数
  315. * @param int $uid
  316. * @return int|string
  317. */
  318. public static function getUserCountNum($uid = 0)
  319. {
  320. if (!$uid) return 0;
  321. return self::where('uid', $uid)->count();
  322. }
  323. }