Files
anmo/server/app/coachapi/logic/WithdrawLogic.php
2025-08-19 14:16:51 +08:00

425 lines
15 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\coachapi\logic;
use app\common\enum\accountLog\CoachAccountLogEnum;
use app\common\logic\BaseLogic;
use app\common\logic\CoachAccountLogLogic;
use app\common\model\coach\Coach;
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\coachapi\logic
*/
class WithdrawLogic extends BaseLogic
{
public function configLists(int $coachId)
{
$lists = WithdrawConfig::where(['relation_id'=>$coachId,'source'=>1])
->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 $coachId
* @param $type
* @return WithdrawConfig|array|\think\Model
* @author cjhao
* @date 2024/9/26 00:22
*/
public function getWithDrawWay($coachId,$type)
{
$detail = WithdrawConfig::where(['relation_id'=>$coachId,'type'=>$type,'source'=>1])
->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 $coachId
* @param $post
* @return string|true
* @author cjhao
* @date 2024/9/26 11:36
*/
public function setWithDrawWay($coachId,$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['coach_id'] = $coachId;
$config = WithdrawConfig::where(['type'=>$type,'relation_id'=>$coachId,'source'=>1])->findOrEmpty();
if($config->isEmpty()){
WithdrawConfig::create([
'relation_id' => $coachId,
'source' => 1,
'type' => $type,
'config' => $post['config'],
]);
}else{
$config->config = $post['config'];
$config->save();
}
return true;
}
/**
* @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['coach_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 int $coachId
* @return array
* @author cjhao
* @date 2024/10/29 17:54
*/
public function getWithdrawInfo(int $coachId){
$lists = WithdrawConfig::where(['relation_id'=>$coachId,'source'=>1])
->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]);
}
}
$coach = Coach::where(['id'=>$coachId])->field('money,deposit')->findOrEmpty();
$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' => $coach['money'],
'deposit' => $coach['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 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['coach_id'],'source'=>1,'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){
// $dayOfWeek = $dayOfWeek === 0 ? 7 : $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('请在每月'.$config['withdraw_cycle_date'].'号来申请提现');
}
}
$coach = Coach::where(['id'=> $params['coach_id']])->findOrEmpty();
$coachMoney = $coach->money;
if(2 == $applyType){
$coachMoney = $coach->deposit;
}
if($coachMoney < $money){
throw new Exception('当前可提现余额仅剩'.$coachMoney);
}
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);
}
//提现操作
$withdrawApply = WithdrawApply::create([
'sn' => generate_sn((new WithdrawApply()), 'sn'),
'relation_id' => $params['coach_id'],
'source' => 1,
'type' => $type,
'apply_type' => $applyType,
'money' => $money,
'left_money' => round($money - $serviceFree,2),
'handling_fee' => $serviceFree,
'service_charge' => $config['service_charge'],
'withdraw_config_snap' => $withdrawConfig,
]);
if(1 == $applyType){
$coach->money = round($coach->money - $money,2);
$coach->save();
CoachAccountLogLogic::add(
$coach->id,
CoachAccountLogEnum::MONEY,
CoachAccountLogEnum::WITHDRAW_DEC_MONEY,
2,
$money,
$withdrawApply['sn'],
);
}else{
$coach->deposit = round($coach->deposit - $money,2);
$coach->save();
CoachAccountLogLogic::add(
$coach->id,
CoachAccountLogEnum::DEPOSIT,
CoachAccountLogEnum::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,$coachId)
{
$detail = WithdrawApply::where(['id'=>$id,'relation_id'=>$coachId,'source'=>1])
->append(['status_desc','type_desc'])
->withoutField('delete_time')
->findOrEmpty()->toArray();
return $detail;
}
}