Withdraw.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\api\model\service;
  3. use think\Exception;
  4. use think\Model;
  5. class Withdraw extends Model
  6. {
  7. // 表名
  8. protected $name = 'service_withdraw';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. public static function apply($params)
  15. {
  16. //方式,金额,信息,提现日期
  17. $userInfo = UserInfo::getUserMoney($params['user_id']);
  18. self::compareNum(['type'=>$params['type'],'num'=>$params['num']],$userInfo);
  19. self::compareWithdrawType($params['withdrawtype'],$userInfo);
  20. //平台还是商户
  21. if($params['type'] == 1){
  22. //找商户
  23. $params['shop_id'] = self::shopWithdrawDay($params['user_id']);
  24. }
  25. switch ($params['withdrawtype'])
  26. {
  27. case 0:
  28. $params['withdraw_info'] = json_encode('支付宝账号:'.$userInfo['alipay_account'].',支付宝姓名:'.$userInfo['alipay_name'],JSON_UNESCAPED_UNICODE);
  29. break;
  30. case 1:
  31. $params['withdraw_info'] = json_encode('开户行:'.$userInfo['bank_name'].',开户人:'.$userInfo['bank_user'].',银行卡号:'.$userInfo['bank_card'],JSON_UNESCAPED_UNICODE);
  32. break;
  33. }
  34. (new Withdraw)->allowField(true)->data($params)->save();
  35. switch ($params['type'])
  36. {
  37. case 0:
  38. UserInfo::money(-$params['num'],$params['user_id'],'平台余额提现扣除');
  39. break;
  40. case 1:
  41. UserInfo::shopMoney(-$params['num'],$params['user_id'],'商户余额提现扣除');
  42. break;
  43. case 2:
  44. UserInfo::shopUserMoney(-$params['num'],$params['user_id'],'商户余额提现扣除');
  45. break;
  46. case 3:
  47. Skill::money(-$params['num'],$params['user_id'],'保证金提现扣除');
  48. $skill = Skill::where('user_id', $params['user_id'])->field('user_id,id,ensure_price')->find();
  49. SkillEnsurePay::reduceSkillEnsure($skill['id']);
  50. break;
  51. case 4:
  52. Shop::money(-$params['num'],$params['user_id'],'保证金提现扣除');
  53. $shop = Shop::where('user_id', $params['user_id'])->field('user_id,id,ensure_price')->find();
  54. ShopEnsurePay::reduceShopEnsure($shop['id']);
  55. break;
  56. }
  57. return true;
  58. }
  59. public static function platformWithdrawDay()
  60. {
  61. $config = ProjectConfig::where(['state'=>1])->field('withdrawtype,day')->find();
  62. if(($config['withdrawtype'] == 0 && date("d") != $config['day']) || ($config['withdrawtype'] == 1 && date('w') != $config['day']))
  63. {
  64. throw new Exception('未到提现日期');
  65. }
  66. return true;
  67. }
  68. public static function shopWithdrawDay($uid)
  69. {
  70. $shopId = Skill::where(['user_id'=>$uid])->value('shop_id');
  71. $shop = Shop::where(['id'=>$shopId])->field('withdrawtype,day')->find();
  72. if(!$shop)
  73. {
  74. throw new Exception('所属商户信息异常,请联系管理员');
  75. }
  76. if(($shop['withdrawtype'] == 0 && date("d") != $shop['day']) || ($shop['withdrawtype'] == 1 && date('w') != $shop['day']))
  77. {
  78. throw new Exception('未到提现日期');
  79. }
  80. return $shopId;
  81. }
  82. public static function compareWithdrawType($type,$userInfo)
  83. {
  84. if(($type == 0 && (!$userInfo['alipay_account'] || !$userInfo['alipay_name'])) || ($type == 1 && (!$userInfo['bank_name'] || !$userInfo['bank_user'] || !$userInfo['bank_card'])))
  85. {
  86. throw new Exception('请完善提现账户信息');
  87. }
  88. return true;
  89. }
  90. public static function compareNum($params,$userInfo)
  91. {
  92. if(($params['type'] == 0 && $userInfo['money']<$params['num']) || ($params['type'] == 1 && $userInfo['shop_money']<$params['num']) || ($params['type'] == 2 && $userInfo['shop_user_money']<$params['num']) || ($params['type'] == 3 && $userInfo['skillEnsurePrice']<$params['num']) || ($params['type'] == 4 && $userInfo['shopEnsurePrice']<$params['num']))
  93. {
  94. throw new Exception('提现金额大于当前金额,请重新填写');
  95. }
  96. return true;
  97. }
  98. public static function getList($params)
  99. {
  100. extract($params);
  101. $where = [];
  102. if(isset($user_id) && $user_id != '')
  103. {
  104. $where['user_id'] = $user_id;
  105. }
  106. if(isset($shop_id) && $shop_id != '')
  107. {
  108. $where['shop_id'] = $shop_id;
  109. }
  110. if(isset($withdrawtype) && $withdrawtype != '')
  111. {
  112. $where['withdrawtype'] = $withdrawtype;
  113. }
  114. if(isset($state) && is_numeric($state))
  115. {
  116. $where['state'] = $state;
  117. }
  118. if(isset($type) && is_numeric($type))
  119. {
  120. $where['type'] = $type;
  121. }
  122. $list = self::where($where)->order('id desc')->page($page)->limit(10)->select();
  123. foreach ($list as $key=>$value)
  124. {
  125. $list[$key]['withdraw_info'] = json_decode($value['withdraw_info'],true);
  126. $list[$key]['images'] = $value['images']?explode(',',$value['images']):'';
  127. }
  128. return $list;
  129. }
  130. }