From d57e84fc09c6c9a3048bb49e1779cdd87b98d5b5 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Tue, 19 Mar 2024 17:40:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E5=A4=8D=E8=B4=AD?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=88=A4=E6=96=AD=E9=87=8D=E6=9E=84=E3=80=81?= =?UTF-8?q?=E8=BF=94=E5=88=A9=E6=98=8E=E7=BB=86=E5=88=A4=E6=96=AD=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=AF=BC=E8=87=B4=E6=98=BE=E7=A4=BA=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E3=80=81=E5=95=86=E5=93=81=E5=A4=8D=E8=B4=AD=E4=BB=B7=E6=A0=BC?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=94=99=E8=AF=AF=E5=AF=BC=E8=87=B4=E4=BB=B7?= =?UTF-8?q?=E6=A0=BC=E4=B8=BA0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/models/goods/Privilege.php | 66 +++++++------- .../price/option/BaseOrderGoodsPrice.php | 35 +++---- plugins/rebate/src/api/IndexController.php | 91 +++++++++++++------ 3 files changed, 116 insertions(+), 76 deletions(-) diff --git a/app/common/models/goods/Privilege.php b/app/common/models/goods/Privilege.php index 3eb0a1d4..e514b566 100644 --- a/app/common/models/goods/Privilege.php +++ b/app/common/models/goods/Privilege.php @@ -602,44 +602,42 @@ class Privilege extends BaseModel // 判断:是否开启消费返利 $goodsRebate = GoodsRebate::where('goods_id',$this->goods_id)->first(); if($goodsRebate->is_open == 1){ - // 判断:当前返利记录是否已经失效 最后一条返利信息已失效或者已结算 则允许重新购买 状态:0=冻结中,1=待结算,2=已结算,3=已失效,4=已退款(失效) - $endTime = Rebate::where('uid', $member->uid) + // 判断:当前返利记录是否存在 + $rebateInfo = Rebate::uniacid() + ->selectRaw('max(reality_thaw_time) as last_reality_thaw_time,max(status) as max_status') + ->where('uid', $member->uid) ->where('goods_id', $this->goods_id) - ->max('expect_thaw_time'); - if(empty($endTime)) return false; - $endInfo = Rebate::where('uid', $member->uid) - ->where('goods_id', $this->goods_id) - ->where('expect_thaw_time', $endTime) ->first(); - if(!$endInfo || in_array($endInfo->status,[2,3,4])) return false; - // 获取已经返利的 最大时间 - $maxTime = Rebate::where('uid', $member->uid) + // 不存在返利记录,从未购买 允许购买 + if(empty($rebateInfo)) return; + $rebateInfo = $rebateInfo->toArray(); + // 判断:最大状态是否为1或者2 不是则失效,允许购买 + if(!in_array($rebateInfo['max_status'],[1,2])) return; + // 判断:最后结算信息获取 并且判断是否需要复购,需要则返回复购价格 + $endInfo = Rebate::uniacid() + ->where('uid', $member->uid) ->where('goods_id', $this->goods_id) - ->where('status', 2) - ->max('reality_thaw_time'); - if(empty($maxTime)) return false; - $lastInfo = Rebate::where('uid', $member->uid) - ->where('goods_id', $this->goods_id) - ->where('status', 2) - ->where('reality_thaw_time', $maxTime) + ->where('reality_thaw_time', $rebateInfo['last_reality_thaw_time']) ->first(); - if(!$lastInfo) return false; - $lastInfo = $lastInfo->toArray(); - if($lastInfo['is_repurchase'] != 1) throw new AppException('未到复购时间'); - // 判断:需要复购 最后一次结算时间N天内 - $set = Setting::get('plugin.rebate'); - $repurchase_day = $set['repurchase_day'] ?? 0; - $limitTime = strtotime(date("Y-m-d H:i:s",$maxTime)." +{$repurchase_day} day"); - // 判断:最后复购时间 小于等于 当前时间,超出复购时间,禁止复购 - if($limitTime <= time()) throw new AppException('已超过复购时间'); - // 是否已经存在复购订单 - $isHas = (int)OrderGoods::where('is_rebate',1) - ->where('created_at', '>', $maxTime)// 在最后一次返利后的订单 - ->where('created_at', '<=', $limitTime)// 在复购时间内的订单 - ->where('goods_id', $this->goods_id)// 同一个商品 - ->where('uid', $member->uid)// 同一个用户 - ->value('id'); - if($isHas > 0) throw new AppException('请勿重复复购!'); + // 是否需要复购 需要则判断是否已经复购,不需要则报错 未到复购时间 + if($endInfo->is_repurchase == 1){ + // 判断:需要复购 最后一次结算时间N天内 + $set = Setting::get('plugin.rebate'); + $repurchaseDay = $set['repurchase_day'] ?? 0;// 复购限制天数 + $limitTime = strtotime(date("Y-m-d H:i:s",$rebateInfo['last_reality_thaw_time'])." +{$repurchaseDay} day"); + // 判断:最后复购时间 小于等于 当前时间,超出复购时间,禁止复购 + if($limitTime <= time()) throw new AppException('已超过复购时间'); + // 是否已经存在复购订单 + $isHas = (int)OrderGoods::where('is_rebate',1) + ->where('created_at', '>', $rebateInfo['last_reality_thaw_time'])// 在最后一次返利后的订单 + ->where('created_at', '<=', $limitTime)// 在复购时间内的订单 + ->where('goods_id', $this->goods_id)// 同一个商品 + ->where('uid', $member->uid)// 同一个用户 + ->value('id'); + if($isHas > 0) throw new AppException('请勿重复复购!'); + }else{ + throw new AppException('未到复购时间'); + } } } diff --git a/app/frontend/modules/orderGoods/price/option/BaseOrderGoodsPrice.php b/app/frontend/modules/orderGoods/price/option/BaseOrderGoodsPrice.php index 306ed5f3..8d553b3a 100644 --- a/app/frontend/modules/orderGoods/price/option/BaseOrderGoodsPrice.php +++ b/app/frontend/modules/orderGoods/price/option/BaseOrderGoodsPrice.php @@ -107,26 +107,29 @@ abstract class BaseOrderGoodsPrice extends OrderGoodsPrice $this->price = $this->getGoodsPrice(); $this->price = max($this->price, 0); // 判断:是否开启消费返利 - $goodsRebate = GoodsRebate::where('goods_id',$this->orderGoods->goods_id)->first(); + $goodsRebate = GoodsRebate::uniacid()->where('goods_id',$this->orderGoods->goods_id)->first(); if($goodsRebate->is_open == 1){ - // 判断:当前返利记录是否已经失效 - $endTime = Rebate::where('uid', $this->orderGoods->uid) + // 判断:当前返利记录是否存在 + $rebateInfo = Rebate::uniacid() + ->selectRaw('max(reality_thaw_time) as last_reality_thaw_time,max(status) as max_status') + ->where('uid', $this->orderGoods->uid) ->where('goods_id', $this->orderGoods->goods_id) - ->max('reality_thaw_time'); - // 不存在返利记录 原价购买 - if(empty($endTime)) return $this->price; - // 存在返利记录 判断最后一条信息的状态 - $endInfo = Rebate::where('uid', $this->orderGoods->uid) - ->where('goods_id', $this->orderGoods->goods_id) - ->where('reality_thaw_time', $endTime) ->first(); - // 已失效 原价购买 - if($endInfo->status == 2){ - // 复购 + // 不存在返利记录,从未购买 原价购买 + if(empty($rebateInfo)) return $this->price; + $rebateInfo = $rebateInfo->toArray(); + // 判断:最大状态是否为1或者2 不是则失效,原价购买 + if(!in_array($rebateInfo['max_status'],[1,2])) return $this->price; + // 判断:最后结算信息获取 并且判断是否需要复购,需要则返回复购价格 + $endInfo = Rebate::uniacid() + ->where('uid', $this->orderGoods->uid) + ->where('goods_id', $this->orderGoods->goods_id) + ->where('reality_thaw_time', $rebateInfo['last_reality_thaw_time']) + ->first(); + // 是否需要复购 需要则返回复购价格,否则无操作 + if($endInfo->is_repurchase == 1){ $endInfo = $endInfo->toArray(); - $goodsRebate = $goodsRebate->toArray(); - $quarter = $goodsRebate['quarter_list'][$endInfo['quarter']]; - $this->price = $quarter['repurchase_money'] ?? 0; + $this->price = $endInfo['repurchase_money'] ?? $this->price; } } diff --git a/plugins/rebate/src/api/IndexController.php b/plugins/rebate/src/api/IndexController.php index 7ca91eec..61d65f63 100644 --- a/plugins/rebate/src/api/IndexController.php +++ b/plugins/rebate/src/api/IndexController.php @@ -7,6 +7,7 @@ use app\common\facades\Setting; use app\common\models\Goods; use app\common\models\Income; use app\common\models\Member; +use Illuminate\Support\Facades\DB; use Yunshop\Rebate\models\Rebate; use Yunshop\TeamDividend\models\TeamDividendAgencyModel; use Yunshop\TeamDividend\models\TeamDividendLevelModel; @@ -76,12 +77,23 @@ class IndexController extends ApiController{ ])->first(); $isRepurchase = $lastSettlement->is_repurchase ?? 0; if($isRepurchase == 1){ - $good = Goods::where('id',$rebateInfo['goods_id']) - ->select(['id','title','thumb']) - ->first(); - if($good) { - $good->price = $lastSettlement['repurchase_money']; - $goods[] = $good->toArray(); + $set = Setting::get('plugin.rebate'); + $repurchaseDay = $set['repurchase_day'] ?? 0; + $startTime = strtotime(date("Y-m-d H:i:s",time())." -{$repurchaseDay} day"); + $isRepurchase = (int)Db::table('yz_order_goods') + ->join('yz_order','yz_order.id','=','yz_order_goods.order_id') + ->where('yz_order_goods.is_rebate',1) + ->where('yz_order.pay_time','>',$startTime) + ->where('yz_order.uid',$uid) + ->value('yz_order_goods.id'); + if($isRepurchase <= 0){ + $good = Goods::where('id',$rebateInfo['goods_id']) + ->select(['id','title','thumb']) + ->first(); + if($good) { + $good->price = $lastSettlement['repurchase_money']; + $goods[] = $good->toArray(); + } } } } @@ -126,8 +138,8 @@ class IndexController extends ApiController{ ->sum('yz_member_income.amount'); // 获取全部返利信息 并且循环处理 $set = Setting::get('plugin.rebate'); - $repurchase_day = $set['repurchase_day'] ?? 0; - $monthList = Rebate::select(['id','quarter','month','expect_thaw_time','money','status','is_repurchase','repurchase_money','reality_thaw_time']) + $repurchaseDay = $set['repurchase_day'] ?? 0; + $monthList = Rebate::select(['id','quarter','month','expect_thaw_time','money','status','is_repurchase','repurchase_money','reality_thaw_time','goods_id']) ->where([ 'uid' => $uid, 'order_id' => $lastRebateInfo['order_id'], @@ -135,31 +147,58 @@ class IndexController extends ApiController{ ]) ->get() ->toArray(); + // 循环每月信息 生成季度信息 foreach($monthList as $monthInfo){ + // 时间 + $monthInfo['expect_thaw_time'] = strtotime($monthInfo['expect_thaw_time']); + $monthInfo['reality_thaw_time'] = strtotime($monthInfo['reality_thaw_time']); + // 信息赋值及处理 $quarterList[$monthInfo['quarter']]['quarter'] = (int)$monthInfo['quarter']; $quarterList[$monthInfo['quarter']]['list'][$monthInfo['month']] = $monthInfo; - // 复购信息 - if($quarterList[$monthInfo['quarter']]['is_repurchase'] != 1) { - $quarterList[$monthInfo['quarter']]['is_repurchase'] = (int)$monthInfo['is_repurchase']; - $quarterList[$monthInfo['quarter']]['repurchase_money'] = (float)$monthInfo['repurchase_money']; - } // 返利时间 开始 ~ 结束 - if($monthInfo['month'] == 1) $quarterList[$monthInfo['quarter']]['start_time'] = date("Y.m", strtotime($monthInfo['expect_thaw_time'])); - $quarterList[$monthInfo['quarter']]['end_time'] = date("Y.m", strtotime($monthInfo['expect_thaw_time'])); + if($monthInfo['month'] == 1) $quarterList[$monthInfo['quarter']]['start_time'] = date("Y.m", $monthInfo['expect_thaw_time']); + $quarterList[$monthInfo['quarter']]['end_time'] = date("Y.m", $monthInfo['expect_thaw_time']); // 本季度总金额 $quarterList[$monthInfo['quarter']]['total_money'] += (float)$monthInfo['money']; - // 本季度是否在结算中 不是结算中 = 最新状态,是结算中 = 状态不变 - if($quarterList[$monthInfo['quarter']]['status'] != 2) $quarterList[$monthInfo['quarter']]['status'] = (int)$monthInfo['status']; - // 当前记录最后一次结算时间 - $reality_thaw_time = $monthInfo['reality_thaw_time'] ? strtotime($monthInfo['reality_thaw_time']) : 0; - if(!array_key_exists('last_reality_thaw_time',$quarterList[$monthInfo['quarter']])) $quarterList[$monthInfo['quarter']]['last_reality_thaw_time'] = 0; - if((int)$reality_thaw_time > 0) { - $quarterList[$monthInfo['quarter']]['last_reality_thaw_time'] = (int)$reality_thaw_time; - $quarterList[$monthInfo['quarter']]['repurchase_end_time'] = (int)strtotime(date("Y-m-d H:i:s",$reality_thaw_time)." +{$repurchase_day} day");; - } - // 最后一次预计结算时间 - $quarterList[$monthInfo['quarter']]['last_expect_thaw_time'] = $monthInfo['expect_thaw_time'] ? strtotime($monthInfo['expect_thaw_time']) : 0; } + // 循环处理季度信息 + $upQuarter = []; + foreach($quarterList as &$quarterInfo){ + $quarterInfo['goods_id'] = max(array_column($quarterInfo['list'], 'goods_id')); + // 获取本季度状态 状态值最高则为本季度最高状态 + $quarterInfo['status'] = max(array_column($quarterInfo['list'], 'status')); + // 获取本季度最后预计结算时间 + $quarterInfo['last_expect_thaw_time'] = (int)max(array_column($quarterInfo['list'], 'expect_thaw_time')); + // 获取本季度最后已结算时间 + $quarterInfo['last_reality_thaw_time'] = (int)max(array_column($quarterInfo['list'], 'reality_thaw_time')); + // 复购信息:当前季度复购信息 = 上一个季度复购信息 + $quarterInfo['is_repurchase'] = (int)max(array_column($upQuarter['list'], 'is_repurchase')); + $quarterInfo['repurchase_money'] = (float)max(array_column($upQuarter['list'], 'repurchase_money')); + // 判断:本季度最后结算时间=0;且上一个季度结束需要复购;进入复购判断流程 + $quarterInfo['repurchase_type'] = 0;// 0=无复购;1=有复购,未到复购时间;2=有复购,未复购;3=有复购,已复购 + if($quarterInfo['last_reality_thaw_time'] == 0 && $quarterInfo['is_repurchase'] == 1){ + // 且上一个季最后结算时间大于等于上一个季度最后预计结算时间 已到复购时间内 + if($upQuarter['last_reality_thaw_time'] >= $upQuarter['last_expect_thaw_time']){ + $quarterInfo['repurchase_type'] = 2;// 有复购 未复购 + $quarterInfo['repurchase_end_time'] = (int)strtotime(date("Y-m-d H:i:s",$realityThawTime)." +{$repurchaseDay} day");; + // 判断:N天内是否存在 该商品和用户的复购订单 存在则已复购 + $startTime = strtotime(date("Y-m-d H:i:s",time())." -{$repurchaseDay} day"); + $isRepurchase = (int)Db::table('yz_order_goods') + ->join('yz_order','yz_order.id','=','yz_order_goods.order_id') + ->where('yz_order_goods.is_rebate',1) + ->where('yz_order.pay_time','>',$startTime) + ->where('yz_order.uid',$uid) + ->value('yz_order_goods.id'); + if($isRepurchase > 0) $quarterInfo['repurchase_type'] = 3;// 有复购 已复购 + }else{ + $quarterInfo['repurchase_type'] = 1;// 有复购 未到复购时间 + } + } + + // 刷新上一个季度信息 + $upQuarter = $quarterInfo; + } + } return $this->successJson('success',compact('statistics', 'quarterList', 'realityThawTime'));