UserExtract.php 13 KB

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