UserExtract.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\model\user;
  12. use app\merchant\model\merchant\Merchant;
  13. use app\merchant\model\merchant\MerchantBill;
  14. use app\merchant\model\user\User;
  15. use think\Url;
  16. use think\Db;
  17. use traits\ModelTrait;
  18. use basic\ModelBasic;
  19. use service\WechatTemplateService;
  20. use service\WechatService;
  21. /**
  22. * 用户提现管理 model
  23. * Class User
  24. * @package app\merchant\model\user
  25. */
  26. class UserExtract extends ModelBasic
  27. {
  28. use ModelTrait;
  29. //审核中
  30. const AUDIT_STATUS = 0;
  31. //未通过
  32. const FAIL_STATUS = -1;
  33. //已提现
  34. const SUCCESS_STATUS = 1;
  35. protected static $extractType = ['alipay', 'bank', 'weixin'];
  36. protected static $extractTypeMsg = ['alipay' => '支付宝', 'bank' => '银行卡', 'weixin' => '微信'];
  37. protected static $status = array(
  38. -1 => '未通过',
  39. 0 => '审核中',
  40. 1 => '已提现'
  41. );
  42. //商户提现
  43. public static function userExtract($mer_id, $data, $extract_price = 100)
  44. {
  45. $userInfo = Merchant::where('id', $mer_id)->find();
  46. if (!in_array($data['extract_type'], self::$extractType)) return self::setErrorInfo('提现方式不存在');
  47. if ($userInfo['now_money'] < $data['extract_price']) return self::setErrorInfo('余额不足');
  48. if ($data['extract_price'] < $extract_price) return self::setErrorInfo('提现金额不能小于' . $extract_price);
  49. $balance = bcsub($userInfo['now_money'], $data['extract_price'], 2);
  50. $insertData = [
  51. 'uid' => $userInfo['uid'],
  52. 'real_name' => $data['real_name'],
  53. 'extract_type' => $data['extract_type'],
  54. 'extract_price' => $data['extract_price'],
  55. 'add_time' => time(),
  56. 'balance' => $balance,
  57. 'status' => self::AUDIT_STATUS,
  58. 'mer_id' => $userInfo['id']
  59. ];
  60. if ($data['extract_type'] == 'weixin') {
  61. if (!$data['weixin']) return self::setErrorInfo('请输入微信账号');
  62. $insertData['wechat'] = $data['weixin'];
  63. $mark = '使用微信提现' . $insertData['extract_price'] . '元';
  64. } else if ($data['extract_type'] == 'alipay') {
  65. if (!$data['alipay_code']) return self::setErrorInfo('请输入支付宝账号');
  66. $insertData['alipay_code'] = $data['alipay_code'];
  67. $mark = '使用支付宝提现' . $insertData['extract_price'] . '元';
  68. } else {
  69. if (!$data['real_name']) return self::setErrorInfo('输入姓名有误');
  70. if (!$data['bank_code']) return self::setErrorInfo('请输入银行卡账号');
  71. if (!$data['bank_address']) return self::setErrorInfo('请输入开户地址');
  72. $insertData['bank_code'] = $data['bank_code'];
  73. $insertData['bank_address'] = $data['bank_address'];
  74. $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . '元';
  75. }
  76. self::beginTrans();
  77. $res1 = self::set($insertData);
  78. if (!$res1) return self::setErrorInfo('提现失败');
  79. Merchant::where(['id' => $userInfo['id']])->update(['now_money' => $balance]);
  80. $res2 = MerchantBill::expend('余额提现', 0, $userInfo['id'], 'now_money', 'extract', $data['extract_price'], $balance, $mark);
  81. $res = $res1 && $res2;
  82. if ($res) {
  83. self::commitTrans();
  84. return true;
  85. } else {
  86. self::rollbackTrans();
  87. return false;
  88. }
  89. }
  90. /**条件处理
  91. * @param $where
  92. */
  93. public static function setWhere($where)
  94. {
  95. $model = new self();
  96. $model = $model->alias('a');
  97. if (isset($where['mer_id']) && $where['mer_id'] != 0) $model = $model->where('a.mer_id', $where['mer_id']);
  98. if (isset($where['status']) && $where['status'] != '') $model = $model->where('a.status', $where['status']);
  99. if (isset($where['extract_type']) && $where['extract_type'] != '') $model = $model->where('a.extract_type', $where['extract_type']);
  100. return $model;
  101. }
  102. /**
  103. * @param $where
  104. * @return array
  105. */
  106. public static function systemPage($where)
  107. {
  108. $data = self::setWhere($where)->field('a.*')->order('a.id desc')->select();
  109. $data = count($data) > 0 ? $data->toArray() : [];
  110. $count = self::setWhere($where)->count();
  111. return compact('data', 'count');
  112. }
  113. }