UserExtract.php 14 KB

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