123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace app\service\admin\shop\site;
- use app\dict\shop\ShopCashOutDict;
- use app\dict\shop\ShopTransferDict;
- use app\model\shop\ShopCashOut;
- use app\service\core\sys\CoreSysConfigService;
- use core\exception\CommonException;
- use app\dict\member\MemberAccountTypeDict;
- use core\base\BaseAdminService;
- use think\facade\Cache;
- use think\facade\Db;
- class ShopCashOutService extends BaseAdminService
- {
- public function __construct()
- {
- parent::__construct();
- $this->model = new ShopCashOut();
- }
- public function getPage(array $where = [])
- {
- $field = 'apply_money, money, service_money, shop_cash_out.status, shop_cash_out.create_time, audit_time, transfer_time';
- $order = 'shop_cash_out.create_time desc';
- $search_model = $this->model
- ->withJoin(['siteInfo'])
- ->withSearch(['cash_out_no', 'create_time', 'audit_time', 'transfer_time', 'transfer_type', 'status'], $where)
- ->where([['shop_cash_out.site_id', '=', $this->site_id]])
- ->append([ 'status_name', 'transfer_type_name' ])
- ->field($field)->order($order);
- return $this->pageQuery($search_model);
- }
- /**
- * 店铺提现详情
- * @param int $id
- * @return array
- */
- public function getInfo(int $id)
- {
- $field = 'apply_money, money, service_money, shop_cash_out.status, shop_cash_out.create_time, audit_time, transfer_type, transfer_time';
- return $this->model
- ->withJoin(['siteInfo'])
- ->append([ 'status_name', 'transfer_type_name' ])
- ->field($field)->where([ [ 'id|cash_out_no', '=', $id ] ])->findOrEmpty()->toArray();
- }
- /**
- * 申请提现
- * @param array $data
- * @return true
- */
- public function apply(array $data)
- {
- $cash_out_config = ( new ShopConfigService() )->getCashOutConfig();
- if ($cash_out_config['is_open'] == 0) throw new CommonException('SHOP_CASH_OUT_NOT_START');//店铺提现未开启
- if ($cash_out_config['min'] > $data['apply_money']) throw new CommonException(get_lang('MIN_CASH_OUT_MONEY_CANNOT_UNDER').$cash_out_config['min']);//最低提现金额不能小于
- $shopAccount = ( new ShopService() )->find();
- if (!in_array($shopAccount['bank_type'], $cash_out_config['transfer_type'])) throw new CommonException('TRANSFER_TYPE_NOT_SUPPORT');//系统暂不支持该转账方式
- if (empty($shopAccount)) throw new CommonException('CASH_OUT_ACCOUNT_NOT_EXIST');
- if ($data['apply_money'] <= 0) throw new CommonException('CASH_OUT_MONEY_MUST_BE_GREATER_THAN_ZERO');
- if ($shopAccount['money'] < $data['apply_money']) throw new CommonException('EXCEEDING_MAXIMUM_WITHDRAWABLE_BALANCE');
- $service_money = format_round_money($data['apply_money'] * $cash_out_config['rate']/100);
- $money = $data['apply_money'] - $service_money;
- Db::startTrans();
- try {
- //生成提现交易号
- $cash_out_no = $this->createCashOutNo($this->site_id);
- //创建提现订单
- $data[ 'site_id' ] = $this->site_id;
- $data[ 'cash_out_no' ] = $cash_out_no;
- $data[ 'account_type' ] = MemberAccountTypeDict::MONEY;
- $data[ 'service_money' ] = $service_money;
- $data[ 'money' ] = $money;
- $data[ 'rate' ] = $cash_out_config['rate'];
- $data[ 'transfer_mobile' ] = '';
- $data[ 'transfer_bank' ] = $shopAccount['bank_type'] == ShopTransferDict::BANK ? $shopAccount['bank_name'] : '';
- $data[ 'transfer_type' ] = $shopAccount['bank_type'];
- if ($data[ 'transfer_type' ] == ShopTransferDict::BANK) {
- $data[ 'transfer_realname' ] = $shopAccount['bank_account_name'];
- $data[ 'transfer_account' ] = $shopAccount['bank_account_no'];
- } elseif ($data[ 'transfer_type' ] == ShopTransferDict::ALIPAY) {
- $data[ 'transfer_realname' ] = $shopAccount['alipay_name'];
- $data[ 'transfer_account' ] = $shopAccount['alipay_account_no'];
- $data[ 'transfer_payment_code' ] = $shopAccount['alipay_payment_code'];//支付宝收款码
- } elseif ($data[ 'transfer_type' ] == ShopTransferDict::WECHAT_CODE) {
- $data[ 'transfer_realname' ] = $shopAccount['wechat_name'];
- $data[ 'transfer_account' ] = $shopAccount['wechat_account_no'];
- $data[ 'transfer_payment_code' ] = $shopAccount['wechat_payment_code'];//微信收款码
- }
- $data[ 'status' ] = $cash_out_config['is_auto_verify'] == 1 ? 2 : 1;
- $data[ 'create_time' ] = time();
- $res = $this->model->create($data);
- //更新店铺信息
- $shopAccount->save([
- 'money_cash_outing' => $shopAccount['money_cash_outing'] + $data['apply_money'], //提现中金额计算
- 'money' => $shopAccount['money'] - $data['apply_money'], //当前余额计算
- ]);
- //添加账户收支记录
- $log_data['site_id'] = $this->site_id;
- $log_data['money'] = -1 * $data['apply_money'];
- $log_data['money_sum'] = $shopAccount['money'] - $data['apply_money'];
- $log_data['from_type'] = 'cash_out';
- $log_data['related_id'] = $cash_out_no;
- $log_data['memo'] = get_lang('SHOP_CASH_OUT');
- ( new ShopAccountLogService() )->add($log_data);
- Db::commit();
- } catch ( Exception $e) {
- Db::rollback();
- throw new CommonException($e->getMessage());
- }
- return true;
- }
- /**
- * 取消申请
- * @return true
- */
- public function cancel(int $id, array $data)
- {
- $info = $this->model->where([ [ 'id', '=', $id ] ])->findOrEmpty()->toArray();
- if(empty($info)) return true;
- $data[ 'update_time' ] = time();
- $data[ 'cancel_time'] = time();
- $data[ 'status' ] = -2;
- Db::startTrans();
- try {
- $this->model->where([['id', '=', $id]])->update($data);
- //审核取消,退回余额及减去提现中金额
- $shopAccount = ( new ShopService() )->find();
- $shopAccount->where([ [ 'site_id', '=', $info[ 'site_id' ] ] ])->update([
- 'money_cash_outing' => $shopAccount['money_cash_outing'] - $info['apply_money'],
- 'money' => $shopAccount['money'] + $info['apply_money']
- ]);
- $shopAccountLogService = new ShopAccountLogService();
- //添加账户收支记录
- $log_data['site_id'] = $info[ 'site_id' ];
- $log_data['money'] = $info['apply_money'];
- $log_data['money_sum'] = $shopAccount['money'] + $info['apply_money'];
- $log_data['from_type'] = 'cash_out';
- $log_data['related_id'] = $info['cash_out_no'];
- $log_data['memo'] = get_lang('USER_CANCEL_CASH_OUT_BACK_MONEY');
- $shopAccountLogService->add($log_data);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- throw new CommonException($e->getMessage());
- }
- return true;
- }
- /**
- * 创建订单编号
- * @param int $site_id
- * @return string
- */
- public function createCashOutNo(int $site_id)
- {
- $time_str = date('YmdHi');
- $max_no = Cache::get('cash_out_no_' . $site_id . '_' . $time_str);
- if (!isset($max_no) || empty($max_no)) {
- $max_no = 1;
- } else {
- ++$max_no;
- }
- $cash_out_no = $time_str . $site_id . sprintf('%03d', $max_no);
- Cache::set('cash_out_no_' . $site_id . '_' . $time_str, $max_no);
- return $cash_out_no;
- }
- /**
- * 转账
- * @param int $id
- * @return array
- */
- public function transfer(int $id)
- {
- $info = $this->model->field('status,transfer_type')->where([ [ 'id', '=', $id ] ])->findOrEmpty()->toArray();
- if (empty($info)) throw new CommonException('CASHOUT_LOG_NOT_EXIST');
- if (!in_array($info['status'], [ ShopCashOutDict::WAIT_TRANSFER, ShopCashOutDict::TRANSFER_ING ])) throw new CommonException('CASH_OUT_APPLY_NOT_PASS_OR_TRANSFERED');//审核未通过或已转账
- if ($info['transfer_type'] != ShopTransferDict::WECHAT) throw new CommonException('THIS_TRANSFER_TYPE_IS_NOT_SUPPORTED');//不支持该提现方式
- $check_code = substr(md5(uniqid('', true)), 0, 5);
- Cache::tag('transfer_'.$info['transfer_type'].'_'.$id)->set($check_code, $id, 900);
- return [
- 'page' => 'app/pages/site/transfer?cod='. $check_code,
- 'qrcode' => $this->getQrcode($check_code)
- ];
- }
- /**
- * 生成转账小程序二维码
- * @param string $check_code
- * @return string
- */
- public function getQrcode(string $check_code)
- {
- $url = ( new CoreSysConfigService() )->getSceneDomain($this->site_id)[ 'wap_url' ];
- $page = 'app/pages/site/transfer';
- $data = [
- [
- 'key' => 'cod',
- 'value' => $check_code
- ]
- ];
- $dir = 'upload/qrcode/' . $this->site_id . '/transfer';
- $channel = 'weapp';
- return qrcode($url, $page, $data, $this->site_id, $dir, $channel);
- }
- }
|