初始版本
This commit is contained in:
428
server/app/shopapi/logic/WithdrawLogic.php
Executable file
428
server/app/shopapi/logic/WithdrawLogic.php
Executable file
@@ -0,0 +1,428 @@
|
||||
<?php
|
||||
namespace app\shopapi\logic;
|
||||
use app\common\enum\accountLog\CoachAccountLogEnum;
|
||||
use app\common\enum\accountLog\ShopAccountLogEnum;
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\logic\ShopAccountLogLogic;
|
||||
use app\common\model\accountLog\CoachAccountLog;
|
||||
use app\common\model\accountLog\ShopAccountLog;
|
||||
use app\common\model\coach\Coach;
|
||||
use app\common\model\shop\Shop;
|
||||
use app\common\model\withdraw\WithdrawApply;
|
||||
use app\common\model\withdraw\WithdrawConfig;
|
||||
use app\common\service\ConfigService;
|
||||
use DateTime;
|
||||
use think\Exception;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 提现逻辑类
|
||||
* Class WithdrawLogic
|
||||
* @package app\shopapi\logic
|
||||
*/
|
||||
class WithdrawLogic extends BaseLogic
|
||||
{
|
||||
|
||||
public function lists(int $shopId)
|
||||
{
|
||||
$lists = WithdrawConfig::where(['relation_id'=>$shopId,'source'=>2])
|
||||
->column('type,config','type');
|
||||
$configDefault = [
|
||||
[
|
||||
'type' => 1,
|
||||
],
|
||||
[
|
||||
'type' => 2,
|
||||
],
|
||||
[
|
||||
'type' => 3,
|
||||
],
|
||||
];
|
||||
foreach ($configDefault as $default){
|
||||
$config = $lists[$default['type']] ?? [];
|
||||
if(empty($config)){
|
||||
$lists[$default['type']] = [
|
||||
'type' => $default['type'],
|
||||
'config' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
if(empty($lists)){
|
||||
$lists = [];
|
||||
}
|
||||
$wayLists = ConfigService::get('withdraw', 'way_list');
|
||||
foreach ($lists as $key => $config)
|
||||
{
|
||||
if(!in_array($config['type'],$wayLists)){
|
||||
unset($lists[$key]);
|
||||
}
|
||||
}
|
||||
return array_values($lists);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取提现方式配置
|
||||
* @param $shopId
|
||||
* @param $type
|
||||
* @return WithdrawConfig|array|\think\Model
|
||||
* @author cjhao
|
||||
* @date 2024/9/26 00:22
|
||||
*/
|
||||
public function getWithDrawWay($shopId,$type)
|
||||
{
|
||||
$detail = WithdrawConfig::where(['relation_id'=>$shopId,'type'=>$type,'source'=>2])
|
||||
->findOrEmpty()->toArray();
|
||||
if(empty($detail)){
|
||||
$detail = [
|
||||
'type' => $type,
|
||||
'config' => [],
|
||||
];
|
||||
}
|
||||
switch ($type){
|
||||
case 1:
|
||||
$detail['config']['name'] = $detail['config']['name'] ?? '';
|
||||
$detail['config']['mobile'] = $detail['config']['mobile'] ?? '';
|
||||
break;
|
||||
case 2:
|
||||
$detail['config']['name'] = $detail['config']['name'] ?? '';
|
||||
$detail['config']['account'] = $detail['config']['account'] ?? '';
|
||||
break;
|
||||
case 3:
|
||||
$detail['config']['name'] = $detail['config']['name'] ?? '';
|
||||
$detail['config']['bank'] = $detail['config']['bank'] ?? '';
|
||||
$detail['config']['bank_card'] = $detail['config']['bank_card'] ?? '';
|
||||
break;
|
||||
}
|
||||
return $detail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 设置提现方式
|
||||
* @param $shopId
|
||||
* @param $post
|
||||
* @return string|true
|
||||
* @author cjhao
|
||||
* @date 2024/9/26 11:36
|
||||
*/
|
||||
public function setWithDrawWay($shopId,$post)
|
||||
{
|
||||
$type = $post['type'] ?? '';
|
||||
if(empty($type)){
|
||||
return '提现配置错误';
|
||||
}
|
||||
switch ($type){
|
||||
case 1:
|
||||
$name = $post['config']['name'] ?? '';
|
||||
$mobile = $post['config']['mobile'] ?? '';
|
||||
if(empty($name)){
|
||||
return '请输入名称';
|
||||
}
|
||||
if(empty($mobile)){
|
||||
return '请输入手机号码';
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
$name = $post['config']['name'] ?? '';
|
||||
$account = $post['config']['account'] ?? '';
|
||||
if(empty($name)){
|
||||
return '请输入名称';
|
||||
}
|
||||
if(empty($account)){
|
||||
return '请输入账号';
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
$name = $post['config']['name'] ?? '';
|
||||
$bank = $post['config']['bank'] ?? '';
|
||||
$bankCard = $post['config']['bank_card'] ?? '';
|
||||
if(empty($name)){
|
||||
return '请输入名称';
|
||||
}
|
||||
if(empty($bank)){
|
||||
return '请输入开户名';
|
||||
}
|
||||
if(empty($bankCard)){
|
||||
return '请输入银行卡号';
|
||||
}
|
||||
break;
|
||||
}
|
||||
$post['shop_id'] = $shopId;
|
||||
$config = WithdrawConfig::where(['type'=>$type,'relation_id'=>$shopId,'source'=>2])->findOrEmpty();
|
||||
if($config->isEmpty()){
|
||||
WithdrawConfig::create([
|
||||
'relation_id' => $shopId,
|
||||
'type' => $type,
|
||||
'config' => $post['config'],
|
||||
'source' => 2,
|
||||
]);
|
||||
}else{
|
||||
$config->config = $post['config'];
|
||||
$config->save();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 提现信息
|
||||
* @param int $coachId
|
||||
* @return array
|
||||
* @author cjhao
|
||||
* @date 2024/10/29 17:54
|
||||
*/
|
||||
public function getWithdrawInfo(int $shopId){
|
||||
|
||||
$lists = WithdrawConfig::where(['relation_id'=>$shopId,'source'=>2])
|
||||
->column('type,config','type');
|
||||
$configDefault = [
|
||||
[
|
||||
'type' => 1,
|
||||
],
|
||||
[
|
||||
'type' => 2,
|
||||
],
|
||||
[
|
||||
'type' => 3,
|
||||
],
|
||||
];
|
||||
foreach ($configDefault as $default){
|
||||
$config = $lists[$default['type']] ?? [];
|
||||
if(empty($config)){
|
||||
$lists[$default['type']] = [
|
||||
'type' => $default['type'],
|
||||
'config' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
if(empty($lists)){
|
||||
$lists = [];
|
||||
}
|
||||
$wayLists = ConfigService::get('withdraw', 'way_list');
|
||||
foreach ($lists as $key => $config)
|
||||
{
|
||||
if(!in_array($config['type'],$wayLists)){
|
||||
unset($lists[$key]);
|
||||
}
|
||||
}
|
||||
$config = [
|
||||
'way_list' => array_values($lists),
|
||||
'min_money' => ConfigService::get('withdraw', 'min_money'),
|
||||
'max_money' => ConfigService::get('withdraw', 'max_money'),
|
||||
'service_charge' => ConfigService::get('withdraw', 'service_charge'),
|
||||
'money' => Shop::where(['id'=>$shopId])->value('money'),
|
||||
'deposit' => Shop::where(['id'=>$shopId])->value('deposit'),
|
||||
'withdraw_cycle_type' => ConfigService::get('withdraw', 'withdraw_cycle_type'),
|
||||
'withdraw_cycle_date' => ConfigService::get('withdraw', 'withdraw_cycle_date'),
|
||||
];
|
||||
$tips = '';
|
||||
if($config['withdraw_cycle_type']){
|
||||
// $dayOfWeek = date('w'); // 注意:'w'返回的是数字,其中0表示周日,6表示周六
|
||||
// $dayOfWeek = $dayOfWeek === 0 ? 7 : $dayOfWeek;
|
||||
$weekDay = getWeekdayByNumber($config['withdraw_cycle_date']);
|
||||
$tips = "平台设置每".$weekDay."可提现";
|
||||
}else{
|
||||
// 获取今天的日期(几号)
|
||||
// $dayOfMonth = date('j'); // 'j'返回不带前导零的日期
|
||||
$tips = "平台设置每月".$config['withdraw_cycle_date']."号可提现";
|
||||
}
|
||||
$config['tips'] = $tips;
|
||||
return $config;
|
||||
}
|
||||
/**
|
||||
* @notes 提现申请
|
||||
* @param $params
|
||||
* @return bool
|
||||
* @author cjhao
|
||||
* @date 2024/9/26 15:15
|
||||
*/
|
||||
public function withdrawalApply($params)
|
||||
{
|
||||
try {
|
||||
Db::startTrans();
|
||||
$cycleType = ConfigService::get('withdraw', 'withdraw_cycle_type');
|
||||
$cycleDate = ConfigService::get('withdraw', 'withdraw_cycle_date');
|
||||
$date = new DateTime();
|
||||
if(1 == $cycleType){
|
||||
$dayOfWeek =$date->format('N'); // 1(表示星期一)到 7(表示星期日)
|
||||
if($cycleDate != $dayOfWeek){
|
||||
throw new Exception('提现仅在每周'.getWeekdayByNumber($cycleDate).'可提现');
|
||||
}
|
||||
}else{
|
||||
$dayOfMonth =$date->format('j'); // 1 到 31
|
||||
if($cycleDate != $dayOfMonth){
|
||||
throw new Exception('提现仅在每月'.$cycleDate.'号可提现');
|
||||
}
|
||||
}
|
||||
$coach = Coach::where(['id'=>$params['coach_id']])->findOrEmpty();
|
||||
if($coach->money < $params['money']){
|
||||
throw new Exception('当前可提现金额仅剩:'.$coach->money.'元');
|
||||
}
|
||||
$minMoney = ConfigService::get('withdraw', 'min_money');
|
||||
$maxMoney = ConfigService::get('withdraw', 'max_money');
|
||||
$serviceCharge = ConfigService::get('withdraw', 'service_charge');
|
||||
if($maxMoney < $params['money']){
|
||||
throw new Exception('最高可提现'.$maxMoney.'元');
|
||||
}
|
||||
if($minMoney > $params['money']){
|
||||
throw new Exception('最低提现'.$minMoney.'元');
|
||||
}
|
||||
$applyType = $params['apply_type'];
|
||||
$config = WithdrawConfig::where(['type'=>$applyType,'coach_id'=>$params['coach_id']])->findOrEmpty();
|
||||
if($config->isEmpty()){
|
||||
throw new Exception('请配置提现账户');
|
||||
}
|
||||
$coach->money = round($coach->money - $params['money']);
|
||||
$coach->save();
|
||||
$handlingFee = round( ($params['money'] * $serviceCharge/100),2);
|
||||
(new WithdrawApply())->save([
|
||||
'sn' => generate_sn((new WithdrawApply()), 'sn'),
|
||||
'coach_id' => $params['shop_id'],
|
||||
'type' => $params['apply_type'],
|
||||
'money' => $params['money'],
|
||||
'left_money' => $coach->money,
|
||||
'handling_fee' => $handlingFee,
|
||||
'service_charge' => $serviceCharge,
|
||||
'withdraw_config_snap' => $config
|
||||
]);
|
||||
//提交事务
|
||||
Db::commit();
|
||||
return true;
|
||||
}catch (Exception $e) {
|
||||
Db::rollback();
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 提现接口
|
||||
* @param $params
|
||||
* @return string|true
|
||||
* @throws \Exception
|
||||
* @author cjhao
|
||||
* @date 2024/10/30 15:05
|
||||
*/
|
||||
public function apply($params)
|
||||
{
|
||||
try {
|
||||
Db::startTrans();
|
||||
|
||||
$money = $params['money'] ?? 0;
|
||||
$type = $params['type'] ?? '';
|
||||
$applyType = $params['apply_type'] ?? 1;
|
||||
if(empty($type)){
|
||||
throw new Exception('请选择提现账号');
|
||||
}
|
||||
$config = [
|
||||
'min_money' => ConfigService::get('withdraw', 'min_money'),
|
||||
'max_money' => ConfigService::get('withdraw', 'max_money'),
|
||||
'service_charge' => ConfigService::get('withdraw', 'service_charge'),
|
||||
'withdraw_cycle_type' => ConfigService::get('withdraw', 'withdraw_cycle_type'),
|
||||
'withdraw_cycle_date' => ConfigService::get('withdraw', 'withdraw_cycle_date'),
|
||||
];
|
||||
$withdrawConfig = WithdrawConfig::where(['relation_id'=>$params['shop_id'],'source'=>2,'type'=>$type])
|
||||
->value('config');
|
||||
if(empty($withdrawConfig)){
|
||||
throw new Exception('请先配置提现账号信息');
|
||||
}
|
||||
if($config['withdraw_cycle_type']){
|
||||
$dayOfWeek = date('w'); // 注意:'w'返回的是数字,其中0表示周日,6表示周六
|
||||
if($config['withdraw_cycle_date'] != $dayOfWeek){
|
||||
$weekDay = getWeekdayByNumber($config['withdraw_cycle_date']);
|
||||
throw new Exception('请在每'.$weekDay.'来申请提现');
|
||||
}
|
||||
}else{
|
||||
// 获取今天的日期(几号)
|
||||
$dayOfMonth = date('j'); // 'j'返回不带前导零的日期
|
||||
if($config['withdraw_cycle_date'] != $dayOfMonth){
|
||||
throw new Exception('请在每月'.$dayOfMonth.'号来申请提现');
|
||||
}
|
||||
}
|
||||
$shop = shop::where(['id'=> $params['shop_id']])->findOrEmpty();
|
||||
$shopMoney = $shop->money;
|
||||
if(2 == $applyType){
|
||||
$shopMoney = $shop->deposit;
|
||||
}
|
||||
if($shopMoney< $money){
|
||||
throw new Exception('当前可提现余额仅剩'.$shopMoney);
|
||||
}
|
||||
if($money < $config['min_money']){
|
||||
throw new Exception('最小提现额度不能小于'.$config['min_money']);
|
||||
}
|
||||
if($money > $config['max_money']){
|
||||
throw new Exception('最大提现额度不能小于'.$config['max_money']);
|
||||
}
|
||||
$serviceFree = 0;
|
||||
if(1 == $applyType){
|
||||
//手续费
|
||||
$serviceFree = round($money*($config['service_charge']/100),2);
|
||||
}
|
||||
//手续费
|
||||
// $serviceFree = round($money*($config['service_charge']/100),2);
|
||||
//提现操作
|
||||
$withdrawApply = WithdrawApply::create([
|
||||
'sn' => generate_sn((new WithdrawApply()), 'sn', 20),
|
||||
'relation_id' => $params['shop_id'],
|
||||
'source' => 2,
|
||||
'type' => $type,
|
||||
'money' => $money,
|
||||
'apply_type' => $applyType,
|
||||
'left_money' => round($money - $serviceFree,2),
|
||||
'handling_fee' => $serviceFree,
|
||||
'service_charge' => $config['service_charge'],
|
||||
'withdraw_config_snap' => $withdrawConfig,
|
||||
]);
|
||||
if(1 == $applyType){
|
||||
$shop->money = round($shop->money - $money,2);
|
||||
$shop->save();
|
||||
ShopAccountLogLogic::add(
|
||||
$shop->id,
|
||||
ShopAccountLogEnum::MONEY,
|
||||
ShopAccountLogEnum::WITHDRAW_DEC_MONEY,
|
||||
2,
|
||||
$money,
|
||||
$withdrawApply['sn'],
|
||||
);
|
||||
}else{
|
||||
$shop->deposit = round($shop->deposit - $money,2);
|
||||
$shop->save();
|
||||
ShopAccountLogLogic::add(
|
||||
$shop->id,
|
||||
ShopAccountLogEnum::DEPOSIT,
|
||||
ShopAccountLogEnum::WITHDRAW_DEC_DEPOSIT,
|
||||
2,
|
||||
$money,
|
||||
$withdrawApply['sn'],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Db::commit();
|
||||
|
||||
return true;
|
||||
}catch (Exception $e){
|
||||
Db::rollback();
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 详情
|
||||
* @param $id
|
||||
* @return WithdrawApply|array|\think\Model
|
||||
* @author cjhao
|
||||
* @date 2024/10/31 09:08
|
||||
*/
|
||||
public function detail($id,$shopId)
|
||||
{
|
||||
$detail = WithdrawApply::where(['id'=>$id,'source'=>2,'relation_id'=>$shopId])
|
||||
->append(['status_desc','type_desc'])
|
||||
->withoutField('delete_time')
|
||||
->findOrEmpty()->toArray();
|
||||
return $detail;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user