UserExtract.php 13 KB

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