UserExtract.php 14 KB

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