UserExtract.php 13 KB

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