UserExtract.php 13 KB

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