UserExtract.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/3/3
  6. */
  7. namespace app\models\user;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\workerman\ChannelService;
  10. use crmeb\traits\ModelTrait;
  11. use crmeb\services\SystemConfigService;
  12. /**
  13. * TODO 用户提现
  14. * Class UserExtract
  15. * @package app\models\user
  16. */
  17. class UserExtract extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'user_extract';
  29. use ModelTrait;
  30. //审核中
  31. const AUDIT_STATUS = 0;
  32. //未通过
  33. const FAIL_STATUS = -1;
  34. //已提现
  35. const SUCCESS_STATUS = 1;
  36. protected static $extractType = ['alipay','bank','weixin'];
  37. protected static $extractTypeMsg = ['alipay'=>'支付宝','bank'=>'银行卡','weixin'=>'微信'];
  38. protected static $status = array(
  39. -1=>'未通过',
  40. 0 =>'审核中',
  41. 1 =>'已提现'
  42. );
  43. /**
  44. * 用户自主提现记录提现记录,后台执行审核
  45. * @param array $userInfo 用户个人信息
  46. * @param array $data 提现详细信息
  47. * @return bool
  48. */
  49. public static function userExtract($userInfo,$data){
  50. if(!in_array($data['extract_type'],self::$extractType))
  51. return self::setErrorInfo('提现方式不存在');
  52. $userInfo = User::get($userInfo['uid']);
  53. $sxf = 0;
  54. $js = '';
  55. $bl = '';
  56. if ($data['type'] == 1){
  57. if ($data['money'] > $userInfo['white_integral']) return self::setErrorInfo('肥料不足');
  58. $extractPrice = $userInfo['white_integral'];
  59. $js = '肥料';
  60. $bl = 'white_integral';
  61. }elseif ($data['type'] == 2){
  62. if ($userInfo['is_merchant'] == 0) return self::setErrorInfo('你不是商户,无法提现');
  63. if ($data['money'] > $userInfo['business_integral']) return self::setErrorInfo('商家积分不足');
  64. $extractPrice = $userInfo['business_integral'];
  65. $js = '商家积分';
  66. $bl = 'business_integral';
  67. }
  68. // $extractPrice = $userInfo['brokerage_price'];
  69. if($extractPrice < 0) return self::setErrorInfo('提现积分不足'.$data['money']);
  70. if($data['money'] > $extractPrice) return self::setErrorInfo('提现肥料不足'.$data['money']);
  71. if($data['money'] <= 0) return self::setErrorInfo('提现积分大于0');
  72. $balance = bcsub($extractPrice,$data['money'],2);
  73. if($balance < 0) $balance=0;
  74. if ($userInfo['integral'] > 0 and $data['status'] == 1){
  75. $tx = SystemConfigService::get('deduction_tx');
  76. $sxf = ($data['money'] - (($data['money']*0.95) - 5));
  77. $dk = $userInfo['integral']/$tx;
  78. if($dk > $sxf){
  79. $ydk = ($dk - $sxf) * $tx;
  80. $money = $data['money'];
  81. $inte = $sxf;
  82. }else{
  83. $inte = $sxf - $dk;
  84. $ydk = 0;
  85. $money = $data['money'] - ($sxf - $dk);
  86. }
  87. }else{
  88. $money = ($data['money']*0.95) - 5;
  89. }
  90. $insertData = [
  91. 'uid' => $userInfo['uid'],
  92. 'extract_type' => $data['extract_type'],
  93. 'extract_price' => $money,
  94. 'add_time' => time(),
  95. 'balance' => $balance,
  96. 'status' => self::AUDIT_STATUS,
  97. 'money' => $data['money'],
  98. 'integral' => $data['status'] == 1? $inte : 0
  99. ];
  100. if(isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  101. else $insertData['real_name'] = $userInfo['nickname'];
  102. if(isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  103. else $insertData['bank_code'] = '';
  104. if(isset($data['bankname'])) $insertData['bank_address']=$data['bankname'];
  105. else $insertData['bank_address']='';
  106. if(isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  107. else $insertData['wechat'] = $userInfo['nickname'];
  108. if($data['extract_type'] == 'alipay'){
  109. if(!$data['alipay_code']) return self::setErrorInfo('请输入支付宝账号');
  110. $insertData['alipay_code'] = $data['alipay_code'];
  111. $mark = '提现'.$js.',使用支付宝提现'.$insertData['extract_price'].'元';
  112. }else if($data['extract_type'] == 'bank'){
  113. if(!$data['cardnum']) return self::setErrorInfo('请输入银行卡账号');
  114. if(!$data['bankname']) return self::setErrorInfo('请输入开户行信息');
  115. $mark = '提现'.$js.',使用银联卡'.$insertData['bank_code'].'提现'.$insertData['extract_price'].'元';
  116. }else if($data['extract_type'] == 'weixin'){
  117. if(!$data['weixin']) return self::setErrorInfo('请输入微信账号');
  118. $mark = '提现'.$js.',使用微信提现'.$insertData['extract_price'].'元';
  119. }
  120. self::beginTrans();
  121. try{
  122. $res1 = self::create($insertData);
  123. if(!$res1) return self::setErrorInfo('提现失败');
  124. if ($userInfo['integral'] > 0 and $data['status'] == 1){
  125. if($dk > $sxf){
  126. $res2 = User::edit([$bl => $balance, 'integral' => $ydk],$userInfo['uid'],'uid');
  127. UserBill::expend('提现抵扣',$userInfo['uid'],'integral','extract',($sxf*$tx),$res1['id'],$ydk,'提现使用'.($sxf*$tx).'阳光抵扣'.$sxf.'手续费');
  128. }else{
  129. $res2 = User::edit([$bl => $balance, 'integral' => $ydk],$userInfo['uid'],'uid');
  130. UserBill::expend('提现抵扣',$userInfo['uid'],'integral','extract',($dk*$tx),$res1['id'],$ydk,'提现使用'.($dk*$tx).'阳光抵扣'.$dk.'手续费');
  131. }
  132. }else{
  133. $res2 = User::edit([$bl => $balance],$userInfo['uid'],'uid');
  134. }
  135. $res3 = UserBill::expend('提现',$userInfo['uid'],$bl,'extract_tx',$data['money'],$res1['id'],$balance,$mark);
  136. $res = $res2 && $res3;
  137. if($res){
  138. self::commitTrans();
  139. try{
  140. ChannelService::instance()->send('WITHDRAW', ['id'=>$res1->id]);
  141. }catch (\Exception $e){}
  142. event('AdminNewPush');
  143. //发送模板消息
  144. return true;
  145. }else return self::setErrorInfo('提现失败!');
  146. }catch (\Exception $e){
  147. self::rollbackTrans();
  148. return self::setErrorInfo('提现失败!');
  149. }
  150. }
  151. /**
  152. * 获得用户最后一次提现信息
  153. * @param $openid
  154. * @return mixed
  155. */
  156. public static function userLastInfo($uid)
  157. {
  158. return self::where(compact('uid'))->order('add_time DESC')->find();
  159. }
  160. /**
  161. * 获得用户提现总金额
  162. * @param $uid
  163. * @return mixed
  164. */
  165. public static function userExtractTotalPrice($uid,$status=self::SUCCESS_STATUS)
  166. {
  167. return self::where('uid',$uid)->where('status',$status)->value('SUM(extract_price)')?:0;
  168. }
  169. /**
  170. * 用户提现记录列表
  171. * @param int $uid 用户uid
  172. * @param int $first 截取行数
  173. * @param int $limit 截取数
  174. * @return \think\Collection
  175. * @throws \think\db\exception\DataNotFoundException
  176. * @throws \think\db\exception\ModelNotFoundException
  177. * @throws \think\exception\DbException
  178. */
  179. public static function extractList($uid,$first = 0,$limit = 8)
  180. {
  181. $list=UserExtract::where('uid',$uid)->order('add_time desc')->limit($first,$limit)->select();
  182. foreach($list as &$v){
  183. $v['add_time']=date('Y/m/d',$v['add_time']);
  184. }
  185. return $list;
  186. }
  187. /**
  188. * 获取累计已提现佣金
  189. * @param $uid
  190. * @return float
  191. */
  192. public static function extractSum($uid)
  193. {
  194. return self::where('uid',$uid)->where('status',1)->sum('extract_price');
  195. }
  196. }