From 77ee4bbf347afe35a59be3b5ba4893751aea6e97 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Fri, 15 Mar 2024 17:11:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E7=BB=8F=E9=94=80?= =?UTF-8?q?=E5=95=86=E4=BD=A3=E9=87=91=E5=92=8C=E5=B9=B3=E7=BA=A7=E5=A5=96?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9C=88=E6=9C=88=E8=BF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../team-dividend/src/PluginApplication.php | 6 + .../src/admin/TeamDividendController.php | 25 ++ .../src/models/TeamDividendModel.php | 9 + .../src/services/TeamReturnService.php | 234 ++++++++++++------ .../src/services/TimedTaskService.php | 15 ++ .../views/admin/dividend-list.blade.php | 6 +- 6 files changed, 219 insertions(+), 76 deletions(-) diff --git a/plugins/team-dividend/src/PluginApplication.php b/plugins/team-dividend/src/PluginApplication.php index 3205fcfd..c2234c23 100644 --- a/plugins/team-dividend/src/PluginApplication.php +++ b/plugins/team-dividend/src/PluginApplication.php @@ -733,6 +733,12 @@ class PluginApplication extends \app\common\services\PluginApplication (new \Yunshop\TeamDividend\services\TimedTaskService)->handle(); return; }); + + // 经销商佣金解冻 每5分钟执行一次 + \Cron::add('Team-dividend', '*/5 * * * *', function () { + (new \Yunshop\TeamDividend\services\TimedTaskService)->rebateThaw(); + return; + }); }); \Event::listen('cron.collectJobs', function () { \Cron::add('Team-dividend-send-redpack', '0 3 * * *', function () { diff --git a/plugins/team-dividend/src/admin/TeamDividendController.php b/plugins/team-dividend/src/admin/TeamDividendController.php index d0fc79a3..26bb82d2 100644 --- a/plugins/team-dividend/src/admin/TeamDividendController.php +++ b/plugins/team-dividend/src/admin/TeamDividendController.php @@ -6,7 +6,9 @@ namespace Yunshop\TeamDividend\admin; use app\common\components\BaseController; use app\common\helpers\PaginationHelper; +use app\common\models\Order; use app\common\services\ExportService; +use Yunshop\TeamDividend\Listener\OrderCreatedListener; use Yunshop\TeamDividend\models\Lose; use Yunshop\TeamDividend\models\TeamDividendLevelModel; use Yunshop\TeamDividend\models\TeamDividendModel; @@ -114,4 +116,27 @@ class TeamDividendController extends BaseController } $exportService->export($file_name, $exportData, \Request::query('route')); } + + public function test(){ + // date_default_timezone_set("PRC"); + // //订单model + // $model = Order::find(81); + // (new OrderCreatedListener())->handle($model); + // + // + // debug("结束"); + + // (new \Yunshop\TeamDividend\services\TimedTaskService)->handle(); + + + debug("结束"); + + + + + + } + + + } \ No newline at end of file diff --git a/plugins/team-dividend/src/models/TeamDividendModel.php b/plugins/team-dividend/src/models/TeamDividendModel.php index 70275715..1ef71c3d 100644 --- a/plugins/team-dividend/src/models/TeamDividendModel.php +++ b/plugins/team-dividend/src/models/TeamDividendModel.php @@ -200,6 +200,12 @@ class TeamDividendModel extends BaseModel case 3: $this->TypeService = '额外分红'; break; + case 7: + $this->TypeService = '月月返'; + break; + case 8: + $this->TypeService = '分红奖'; + break; } } return $this->TypeService; @@ -225,6 +231,9 @@ class TeamDividendModel extends BaseModel case 1: $this->StatusService = '已结算'; break; + case 2: + $this->StatusService = '冻结中'; + break; case -1: $this->StatusService = '已失效'; break; diff --git a/plugins/team-dividend/src/services/TeamReturnService.php b/plugins/team-dividend/src/services/TeamReturnService.php index db390f43..eb3e6224 100644 --- a/plugins/team-dividend/src/services/TeamReturnService.php +++ b/plugins/team-dividend/src/services/TeamReturnService.php @@ -59,6 +59,8 @@ class TeamReturnService protected $flat_prize_limit; + + private $team_levels; // public function __construct($order, $set, $buyMember, $order_goods, $agents) // { // $this->order = $order; @@ -81,6 +83,11 @@ class TeamReturnService $this->team_goods = GoodsTeamDividend::getGoodsByGoodsId($this->order_goods->goods_id)->first(); $this->levels = TeamDividendLevelModel::uniacid()->orderBy('level_weight','asc')->get()->toArray(); $this->flat_prize_limit = 0; + $this->team_levels = TeamDividendLevelModel::uniacid() + ->select(['id','level_weight']) + ->get() + ->toArray(); + \Log::debug('经销商创建-team_goods:' . $this->order_goods->goods_id, $this->team_goods ? $this->team_goods->toArray() : []); $this->init(); @@ -191,42 +198,77 @@ class TeamReturnService // 经销商奖励 private function dividendAward($agent) { - //平级跳出 if ($this->dividend_level_ids[$agent['level']]) { return; } // 平级奖处理初始化 - $this->dividend_level_ids[$agent['level']]['hierarchy'] = 0; + $this->hierarchyHandle($agent); + // $this->dividend_level_ids[$agent['level']]['hierarchy'] = 0; $this->hierarchy_uid = $agent['uid']; // 分红比例 $ratio = $this->amount_service->getRatio(); - // 分红金额 + // 分红金额 判断:是否开启分期 + if($this->team_goods->is_stages == 1){ + $levelName = 'level_'.$agent['level']; + $stages_num = unserialize($this->team_goods->stages_num);//分期数量 + $stages_type = unserialize($this->team_goods->stages_type);//分期类型 + $stages_month_list = unserialize($this->team_goods->stages_month_list);//每期金额(比例) + $immediately = unserialize($this->team_goods->immediately);//每月立即返佣(和每期第一个月一起返佣,但是为独立奖) + $currentStagesType = (int)$stages_type[$levelName] ?? 0;//当前等级分期类型 + $currentStagesMonthList = (array)$stages_month_list[$levelName] ?? [];//当前等级每期金额(比例) + $currentImmediately = (float)$immediately[$levelName] ?? 0;//当前等级每月立即返佣(和每期第一个月一起返佣,但是为独立奖) + $moneyList = (array)array_merge([$currentImmediately],$currentStagesMonthList);// 立即结算金额 + // 循环处理 + $expectThawTime = time();// 每个商品的预计解冻时间 = 当前时间 + $expectThawTimeMonth = 0; - $dividend_amount = $this->amount_service->getDividendAmount($ratio); + foreach(array_values($moneyList) as $monthIndex => $monthInfo){ + // 预计解冻时间 增加 + $currentExpectThawTime = getNextMonthDays($expectThawTime, $expectThawTimeMonth); + if($monthIndex > 0) $expectThawTimeMonth++; - if ($this->amount_service->fixed == 1) { - $ratio = $this->amount_service->ratio; + if($currentStagesType == 1) $dividend_amount = (float)sprintf("%.2f",$monthInfo * $this->order_goods->total);// 固定金额 + else $dividend_amount = $this->amount_service->getDividendAmount($monthInfo);// 百分比 + if($dividend_amount <= 0) continue; + // 计算基数 + $dividendData = [ + 'dividend_amount' => $dividend_amount, + 'type' => $monthIndex == 0 ? '8' : '7',// 7=分月返佣,8=立即返佣 + 'amount' => $dividend_amount, + 'dividend_rate' => 0, + 'lower_level_rate' => 0, + 'status' => 2, + 'expect_thaw_time' => $currentExpectThawTime, + 'month' => $expectThawTimeMonth + ]; + // 0,1 立即获取,第一个月返佣 + if($monthIndex == 0 || $monthIndex == 1){ + $dividendData['status'] = 0; + $dividendData['reality_thaw_time'] = time(); + } + // 分红 + $dividend_model = $this->addDividend($agent, $dividendData); + } } - if ($dividend_amount <= 0) { - return; + else{ + $dividend_amount = $this->amount_service->getDividendAmount($ratio); + if ($this->amount_service->fixed == 1) $ratio = $this->amount_service->ratio; + if ($dividend_amount <= 0) return; + // 计算基数 + $amount = $this->amount_service->amount; + $dividendData = [ + 'dividend_amount' => $dividend_amount, + 'type' => 0, + 'amount' => $amount, + 'dividend_rate' => $ratio, + 'lower_level_rate' => $this->finish_ratio ?: 0 + ]; + // 分红 + $dividend_model = $this->addDividend($agent, $dividendData); } - - // 计算基数 - $amount = $this->amount_service->amount; - - $dividendData = [ - 'dividend_amount' => $dividend_amount, - 'type' => 0, - 'amount' => $amount, - 'dividend_rate' => $ratio, - 'lower_level_rate' => $this->finish_ratio ?: 0 - ]; - // 分红 - $dividend_model = $this->addDividend($agent, $dividendData); - $member = Member::getMemberByUid($agent['uid'])->with('hasOneFans')->first(); $notice = [ 'lower_level_name' => $this->buyMember->nickname, @@ -256,15 +298,18 @@ class TeamReturnService // 平级奖 private function hierarchyAward($agent) { + // 是否开启平级奖 if (!$this->set['is_flat_prize']) { return; } + // 商品是否开启平级奖 if (!$this->amount_service->isHierarchy()) { return; } + if ($this->flat_prize_limit) { \Log::debug('经销商平级奖限制,取消平级奖', $this->order->id.'_'.$this->order_goods->id.'_'.$agent['uid']); return; @@ -277,76 +322,100 @@ class TeamReturnService } // 平级奖奖励层级 $award_hierarchy = $this->amount_service->getAwardHierarchy(); + // 平级奖信息存在 并且 (平级奖奖励层级 小于等于 当前层级 或者 平级奖用户和代理用户为同一个人 ) 不参加平级奖 if ($dividend_level && ($award_hierarchy <= $dividend_level['hierarchy'] || $this->hierarchy_uid == $agent['uid'])) { return; } // 平级奖奖励比例 $award_ratio = $this->amount_service->getAwardHierarchyRatio(); - if (!isset($award_ratio) || $award_ratio <= 0) { - return; - } - $amount = $this->amount_service->getAwardHierarchyAmount(); - if ($amount <= 0) { - return; - } - $dividend_amount = proportionMath($amount, $award_ratio); + if (!isset($award_ratio) || $award_ratio <= 0) return; - $dividendData = [ - 'amount' => $amount, - 'dividend_rate' => $award_ratio, - 'lower_level_rate' => 0, - 'type' => 1, - 'hierarchy' => $dividend_level['hierarchy'] + 1, - 'dividend_amount' => $dividend_amount - ]; - // 是否开启平级奖下级获得经销商奖励计算方式 - if ($this->set['calculate_formula'] == 1) { - $finishPrice = $this->obtainInfos[$agent['level']]; - if (!$finishPrice) { - return; + // 分红金额 判断:是否开启分期 + if($this->team_goods->is_stages == 1){ + $amountList = TeamDividendModel::where('order_sn',$this->order->order_sn) + ->select(['amount','status','expect_thaw_time','reality_thaw_time','month']) + ->whereIn('type',[7,8]) + ->get()->toArray(); + + foreach($amountList as $amountInfo){ + // 计算基数 + $dividendData = [ + 'dividend_amount' => (float)sprintf("%.2f",$amountInfo['amount'] * $award_ratio / 100), + 'amount' => (float)sprintf("%.2f",$amountInfo['amount'] * $award_ratio / 100), + 'dividend_rate' => $award_ratio, + 'lower_level_rate' => 0, + 'type' => 1, + 'hierarchy' => $dividend_level['hierarchy'] + 1, + 'status' => $amountInfo['status'] == 2 ? 2 : 0, + 'expect_thaw_time' => $amountInfo['expect_thaw_time'], + 'reality_thaw_time' => $amountInfo['reality_thaw_time'], + 'month' => $amountInfo['month'] + ]; + + // 分红 + $dividend_model = $this->addDividend($agent, $dividendData); } - $dividendData['amount'] = $finishPrice; - $dividendData['dividend_amount'] = proportionMath($finishPrice, $award_ratio); } - - if ($dividendData['dividend_amount'] <= 0) { - return; - } - - if ($this->set['flat_prize_limit']) { - \Log::debug('经销商平级奖限制开始',$agent['uid']); - - $this->flat_prize_limit = 1; - - $last_agent = null; - foreach ($this->agents as $v) { - if ($v['uid'] == $agent['uid']) { - break; + else{ + $amount = $this->amount_service->getAwardHierarchyAmount(); + if ($amount <= 0) return; + $dividend_amount = proportionMath($amount, $award_ratio); + $dividendData = [ + 'amount' => $amount, + 'dividend_rate' => $award_ratio, + 'lower_level_rate' => 0, + 'type' => 1, + 'hierarchy' => $dividend_level['hierarchy'] + 1, + 'dividend_amount' => $dividend_amount + ]; + // 是否开启平级奖下级获得经销商奖励计算方式 + if ($this->set['calculate_formula'] == 1) { + $finishPrice = $this->obtainInfos[$agent['level']]; + if (!$finishPrice) { + return; } - $last_agent = $v; + $dividendData['amount'] = $finishPrice; + $dividendData['dividend_amount'] = proportionMath($finishPrice, $award_ratio); } - - if (!$child_uid = $last_agent['uid']){ - \Log::debug('经销商平级奖限制,获取下级会员ID为空',$this->order->id.'_'.$this->order_goods->id.'_'.$agent['uid']); + if ($dividendData['dividend_amount'] <= 0) { return; } + if ($this->set['flat_prize_limit']) { + \Log::debug('经销商平级奖限制开始',$agent['uid']); + + $this->flat_prize_limit = 1; + + $last_agent = null; + foreach ($this->agents as $v) { + if ($v['uid'] == $agent['uid']) { + break; + } + $last_agent = $v; + } + + if (!$child_uid = $last_agent['uid']){ + \Log::debug('经销商平级奖限制,获取下级会员ID为空',$this->order->id.'_'.$this->order_goods->id.'_'.$agent['uid']); + return; + } + + if ($last_agent['has_one_level']['id'] != $agent['has_one_level']['id']){ + \Log::debug('经销商平级奖限制,下级经销商等级与当前会员不相等',$this->order->id.'_'.$this->order_goods->id.'_'.$agent['uid']); + return; + } + + $yz_member = MemberShopInfo::where('member_id',$last_agent['uid'])->first(); + if (!$yz_member || $yz_member->parent_id != $agent['uid']){ + \Log::debug('经销商平级奖限制,下级经销商非当前会员直属下级',$this->order->id.'_'.$this->order_goods->id.'_'.$agent['uid']); + return; + } - if ($last_agent['has_one_level']['id'] != $agent['has_one_level']['id']){ - \Log::debug('经销商平级奖限制,下级经销商等级与当前会员不相等',$this->order->id.'_'.$this->order_goods->id.'_'.$agent['uid']); - return; - } - - $yz_member = MemberShopInfo::where('member_id',$last_agent['uid'])->first(); - if (!$yz_member || $yz_member->parent_id != $agent['uid']){ - \Log::debug('经销商平级奖限制,下级经销商非当前会员直属下级',$this->order->id.'_'.$this->order_goods->id.'_'.$agent['uid']); - return; } + $dividend_model = $this->addDividend($agent, $dividendData); } - $dividend_model = $this->addDividend($agent, $dividendData); $member = Member::getMemberByUid($agent['uid'])->with('hasOneFans')->first(); $notice = [ 'lower_level_name' => $this->buyMember->nickname, @@ -369,9 +438,26 @@ class TeamReturnService } // 平级奖层数增加 - $this->dividend_level_ids[$agent['level']]['hierarchy'] += 1; + $this->hierarchyHandle($agent); + // $this->dividend_level_ids[$agent['level']]['hierarchy'] += 1; } + private function hierarchyHandle($agent){ + // 平级奖层数增加 修改:等级权重大于等于当前经销商用户 都拿平级奖处理 2024-3-15 + foreach($this->team_levels as $levelInfo){ + if($agent['has_one_level']['level_weight'] <= $levelInfo['level_weight']){ + if($this->dividend_level_ids[$levelInfo['id']]['hierarchy'] != ''){ + $this->dividend_level_ids[$levelInfo['id']]['hierarchy'] += 1; + }else{ + $this->dividend_level_ids[$levelInfo['id']]['hierarchy'] = 0; + } + } + } + } + + + + //额外分红 private function extraDividend($agent) { diff --git a/plugins/team-dividend/src/services/TimedTaskService.php b/plugins/team-dividend/src/services/TimedTaskService.php index fc7af6b9..9f88268c 100644 --- a/plugins/team-dividend/src/services/TimedTaskService.php +++ b/plugins/team-dividend/src/services/TimedTaskService.php @@ -390,5 +390,20 @@ class TimedTaskService (new BalanceChange())->universal($data); } + /** + * Common: 经销商佣金解冻 + * Author: wu-hui + * Time: 2024/03/15 17:07 + */ + public static function rebateThaw(){ + $time = time(); + // 预计解冻时间 低于当前时间 解冻 + TeamDividendModel::where('expect_thaw_time','<=', $time)->update([ + 'status' => 0, + 'reality_thaw_time' => $time + ]); + } + + } \ No newline at end of file diff --git a/plugins/team-dividend/views/admin/dividend-list.blade.php b/plugins/team-dividend/views/admin/dividend-list.blade.php index 9dcb3652..a82adc17 100644 --- a/plugins/team-dividend/views/admin/dividend-list.blade.php +++ b/plugins/team-dividend/views/admin/dividend-list.blade.php @@ -167,8 +167,10 @@ {{$row['level_name']}}
{{ $row['type_name'] }} - - - 比例:{{$row['dividend_rate']}}% + @if($row['type'] != 7 && $row['type'] != 8 ) + - 比例:{{$row['dividend_rate']}}% + @endif + {{$row['order_amount']}} {{$row['amount']}} {{$row['lower_level_rate']}}%