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 ShopAccountService() )->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_realname' ] = $shopAccount['bank_type'] == 1 ? $shopAccount['bank_account_name'] : $shopAccount['alipay_name'] ?? ''; $data[ 'transfer_mobile' ] = ''; $data[ 'transfer_bank' ] = $shopAccount['bank_type'] == 1 ? $shopAccount['bank_name'] ?? '' : ''; $data[ 'transfer_account' ] = $shopAccount['bank_type'] == 1 ? $shopAccount['bank_account_no'] : $shopAccount['alipay_account_no'] ?? ''; $data[ 'transfer_type' ] = $shopAccount['bank_type'] ?? ''; $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 ShopAccountService() )->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; } }