UserExtract.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 crmeb\services\SystemConfigService;
  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('real_price'));
  47. //佣金总金额
  48. $data['brokerage_count'] = floatval(UserBill::getBrokerageCount());
  49. //已提现金额
  50. $data['priced'] = floatval(self::where('status', 1)->sum('real_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', '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 . '元';
  82. $uid = $data['uid'];
  83. $status = -1;
  84. $User = User::where('uid', $uid)->find()->toArray();
  85. UserBill::income('提现失败', $uid, $data['money_type'] == 0 ? 'brokerage_price' : 'now_money', 'extract', $extract_number, $id, bcadd($User[$data['money_type'] == 0 ? 'brokerage_price' : 'now_money'], $extract_number, 2), $mark);
  86. User::bcInc($uid, $data['money_type'] == 0 ? 'brokerage_price' : 'now_money', $extract_number, 'uid');
  87. $extract_type = '未知方式';
  88. switch ($data['extract_type']) {
  89. case 'alipay':
  90. $extract_type = '支付宝';
  91. break;
  92. case 'bank':
  93. $extract_type = '银行卡';
  94. break;
  95. case 'weixin':
  96. $extract_type = '微信';
  97. break;
  98. }
  99. if (strtolower($User['user_type']) == 'wechat') {
  100. WechatTemplateService::sendTemplate(WechatUser::where('uid', $uid)->value('openid'), WechatTemplateService::USER_BALANCE_CHANGE, [
  101. 'first' => $mark,
  102. 'keyword1' => $data['money_type'] == 0 ?'佣金提现':'余额提现',
  103. 'keyword2' => date('Y-m-d H:i:s', time()),
  104. 'keyword3' => $extract_number,
  105. 'remark' => '错误原因:' . $fail_msg
  106. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  107. } else if (strtolower($User['user_type']) == 'routine') {
  108. RoutineTemplate::sendExtractFail($uid, $fail_msg, $extract_number, $User['nickname']);
  109. }
  110. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  111. }
  112. public static function changeSuccess($id)
  113. {
  114. $data = self::get($id);
  115. if (!$data)
  116. return false;
  117. /*
  118. $openid = WechatUser::where('uid', $data['uid'])->value('routine_openid');
  119. $res = self::sendMoney($data['real_price'], $openid);
  120. if (!$res['flag']) {
  121. return self::setErrorInfo($res['msg']);
  122. // return self::setErrorInfo(public_path());
  123. }*/
  124. $extractNumber = $data['real_price'];
  125. $mark = '成功提现佣金' . $extractNumber . '元';
  126. $wechatUserInfo = WechatUser::where('uid', $data['uid'])->field('openid,user_type,routine_openid,nickname')->find();
  127. $extract_type = '未知方式';
  128. switch ($data['extract_type']) {
  129. case 'alipay':
  130. $extract_type = '支付宝';
  131. break;
  132. case 'bank':
  133. $extract_type = '银行卡';
  134. break;
  135. case 'weixin':
  136. $extract_type = '微信';
  137. break;
  138. }
  139. if ($wechatUserInfo) {
  140. if (strtolower($wechatUserInfo->user_type) == 'routine') {
  141. RoutineTemplate::sendExtractSuccess($data['uid'], $extractNumber, $wechatUserInfo->nickname);
  142. } else if (strtolower($wechatUserInfo->user_type) == 'wechat') {
  143. WechatTemplateService::sendTemplate($wechatUserInfo->openid, WechatTemplateService::USER_BALANCE_CHANGE, [
  144. 'first' => $mark,
  145. 'keyword1' => '佣金提现',
  146. 'keyword2' => date('Y-m-d H:i:s', time()),
  147. 'keyword3' => $extractNumber,
  148. 'remark' => '点击查看我的佣金明细'
  149. ], Url::buildUrl('/user/cashrecord')->suffix('')->domain(true)->build());
  150. }
  151. }
  152. return self::edit(['status' => 1], $id);
  153. }
  154. protected static function sendMoney($amount, $re_openid, $desc = '佣金提现', $check_name = '')
  155. {
  156. $wechat = SystemConfigService::more(['site_url', 'routine_appId', 'routine_appsecret']);
  157. $payment = SystemConfigService::more(['pay_routine_mchid', 'pay_routine_key', 'pay_routine_client_cert', 'pay_routine_client_key', 'pay_weixin_open']);
  158. $total_amount = (100) * $amount;
  159. $data = array(
  160. 'mch_appid' => isset($wechat['routine_appId']) ? trim($wechat['routine_appId']) : '',//商户账号appid
  161. 'mchid' => trim($payment['pay_routine_mchid']),//商户号
  162. 'nonce_str' => createNoncestr(),//随机字符串
  163. 'partner_trade_no' => date('YmdHis') . rand(1000, 9999),//商户订单号
  164. 'openid' => $re_openid,//用户openid
  165. 'check_name' => 'NO_CHECK',//校验用户姓名选项,
  166. 're_user_name' => '匿名',//收款用户姓名
  167. 'amount' => $total_amount,//金额
  168. 'desc' => $desc,//企业付款描述信息
  169. 'spbill_create_ip' => request()->ip(),//Ip地址
  170. );
  171. //生成签名算法
  172. $secrect_key = trim($payment['pay_routine_key']);///这个就是个API密码。MD5 32位。
  173. $data = array_filter($data);
  174. ksort($data);
  175. $str = '';
  176. foreach ($data as $k => $v) {
  177. $str .= $k . '=' . $v . '&';
  178. }
  179. $str .= 'key=' . $secrect_key;
  180. $data['sign'] = strtoupper(md5($str));
  181. //生成签名算法
  182. $xml = arraytoxml($data);
  183. $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //调用接口
  184. $res = curl_post_ssl($url, $xml);
  185. $return = xmltoarray($res);
  186. if ($return['return_code'] == 'SUCCESS' && $return['result_code'] == 'SUCCESS') {
  187. //付款成功逻辑
  188. return [
  189. 'flag' => true
  190. ];
  191. } else {
  192. return [
  193. 'flag' => false,
  194. 'msg' => $return['err_code'] . ":" . $return['err_code_des']
  195. ];
  196. }
  197. }
  198. //测试数据
  199. public static function test()
  200. {
  201. $uids = User::order('uid desc')->limit(2, 20)->field(['uid', 'nickname'])->select()->toArray();
  202. $type = ['bank', 'alipay', 'weixin'];
  203. foreach ($uids as $item) {
  204. $data = [
  205. 'uid' => $item['uid'],
  206. 'real_name' => $item['nickname'],
  207. 'extract_type' => isset($type[rand(0, 2)]) ? $type[rand(0, 2)] : 'alipay',
  208. 'bank_code' => rand(1000000, 999999999),
  209. 'bank_address' => '中国',
  210. 'alipay_code' => rand(1000, 9999999),
  211. 'extract_price' => rand(100, 9999),
  212. 'mark' => '测试数据',
  213. 'add_time' => time(),
  214. 'status' => 1,
  215. 'wechat' => rand(999, 878788) . $item['uid'],
  216. ];
  217. self::create($data);
  218. }
  219. }
  220. //获取头部提现信息
  221. public static function getExtractHead()
  222. {
  223. //本月提现人数
  224. $month = self::getModelTime(['data' => 'month'], self::where('status', 1))->group('uid')->count();
  225. //本月提现笔数
  226. $new_month = self::getModelTime(['data' => 'month'], self::where('status', 1))->distinct(true)->count();
  227. //上月提现人数
  228. $last_month = self::whereTime('add_time', 'last month')->where('status', 1)->group('uid')->distinct(true)->count();
  229. //上月提现笔数
  230. $last_count = self::whereTime('add_time', 'last month')->where('status', 1)->count();
  231. //本月提现金额
  232. $extract_price = self::getModelTime(['data' => 'month'], self::where('status', 1))->sum('extract_price');
  233. //上月提现金额
  234. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', 1)->sum('extract_price');
  235. return [
  236. [
  237. 'name' => '总提现人数',
  238. 'field' => '个',
  239. 'count' => self::where('status', 1)->group('uid')->count(),
  240. 'content' => '',
  241. 'background_color' => 'layui-bg-blue',
  242. 'sum' => '',
  243. 'class' => 'fa fa-bar-chart',
  244. ],
  245. [
  246. 'name' => '总提现笔数',
  247. 'field' => '笔',
  248. 'count' => self::where('status', 1)->distinct(true)->count(),
  249. 'content' => '',
  250. 'background_color' => 'layui-bg-cyan',
  251. 'sum' => '',
  252. 'class' => 'fa fa-line-chart',
  253. ],
  254. [
  255. 'name' => '本月提现人数',
  256. 'field' => '人',
  257. 'count' => $month,
  258. 'content' => '',
  259. 'background_color' => 'layui-bg-orange',
  260. 'sum' => '',
  261. 'class' => 'fa fa-line-chart',
  262. ],
  263. [
  264. 'name' => '本月提现笔数',
  265. 'field' => '笔',
  266. 'count' => $new_month,
  267. 'content' => '',
  268. 'background_color' => 'layui-bg-green',
  269. 'sum' => '',
  270. 'class' => 'fa fa-line-chart',
  271. ],
  272. [
  273. 'name' => '本月提现金额',
  274. 'field' => '元',
  275. 'count' => $extract_price,
  276. 'content' => '提现总金额',
  277. 'background_color' => 'layui-bg-cyan',
  278. 'sum' => self::where('status', 1)->sum('extract_price'),
  279. 'class' => 'fa fa-line-chart',
  280. ],
  281. [
  282. 'name' => '上月提现人数',
  283. 'field' => '个',
  284. 'count' => $last_month,
  285. 'content' => '环比增幅',
  286. 'background_color' => 'layui-bg-blue',
  287. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  288. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  289. ],
  290. [
  291. 'name' => '上月提现笔数',
  292. 'field' => '笔',
  293. 'count' => $last_count,
  294. 'content' => '环比增幅',
  295. 'background_color' => 'layui-bg-black',
  296. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  297. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  298. ],
  299. [
  300. 'name' => '上月提现金额',
  301. 'field' => '元',
  302. 'count' => $last_extract_price,
  303. 'content' => '环比增幅',
  304. 'background_color' => 'layui-bg-gray',
  305. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  306. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  307. ],
  308. ];
  309. }
  310. //获取提现分布图和提现人数金额曲线图
  311. public static function getExtractList($where, $limit = 15)
  312. {
  313. $legdata = ['提现人数', '提现金额'];
  314. $list = self::getModelTime($where, self::where('status', 1))
  315. ->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();
  316. if (count($list)) $list = $list->toArray();
  317. $xdata = [];
  318. $itemList = [0 => [], 1 => []];
  319. $chatrList = [];
  320. $zoom = '';
  321. foreach ($list as $value) {
  322. $xdata[] = $value['un_time'];
  323. $itemList[0][] = $value['count'];
  324. $itemList[1][] = $value['sum_price'];
  325. }
  326. foreach ($legdata as $key => $name) {
  327. $item['name'] = $name;
  328. $item['type'] = 'line';
  329. $item['data'] = $itemList[$key];
  330. $chatrList[] = $item;
  331. }
  332. unset($item, $name, $key);
  333. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  334. //饼状图
  335. $cake = ['支付宝', '银行卡', '微信'];
  336. $fenbulist = self::getModelTime($where, self::where('status', 1))
  337. ->field('count(uid) as count,extract_type')->group('extract_type')->order('count asc')->select();
  338. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  339. $sum_count = self::getModelTime($where, self::where('status', 1))->count();
  340. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  341. $fenbudata = [];
  342. foreach ($fenbulist as $key => $item) {
  343. if ($item['extract_type'] == 'bank') {
  344. $item_date['name'] = '银行卡';
  345. } else if ($item['extract_type'] == 'alipay') {
  346. $item_date['name'] = '支付宝';
  347. } else if ($item['extract_type'] == 'weixin') {
  348. $item_date['name'] = '微信';
  349. }
  350. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  351. $item_date['itemStyle']['color'] = $color[$key];
  352. $fenbudata[] = $item_date;
  353. }
  354. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  355. }
  356. /**
  357. * 获取用户累计提现金额
  358. * @param int $uid
  359. * @return int|mixed
  360. */
  361. public static function getUserCountPrice($uid = 0)
  362. {
  363. if (!$uid) return 0;
  364. $price = self::where('uid', $uid)->where('status', 1)->sum('extract_price');
  365. return $price ? $price : 0;
  366. }
  367. /**
  368. * 获取用户累计提现次数
  369. * @param int $uid
  370. * @return int|string
  371. */
  372. public static function getUserCountNum($uid = 0)
  373. {
  374. if (!$uid) return 0;
  375. return self::where('uid', $uid)->count();
  376. }
  377. }