diff --git a/plugins/rebate/src/PluginApplication.php b/plugins/rebate/src/PluginApplication.php index 887be665..835c75b0 100644 --- a/plugins/rebate/src/PluginApplication.php +++ b/plugins/rebate/src/PluginApplication.php @@ -1,6 +1,8 @@ subscribe(OrderPaidListener::class); } diff --git a/plugins/rebate/src/admin/IndexController.php b/plugins/rebate/src/admin/IndexController.php index 8119dd85..f60725ce 100644 --- a/plugins/rebate/src/admin/IndexController.php +++ b/plugins/rebate/src/admin/IndexController.php @@ -6,6 +6,7 @@ use app\common\components\BaseController; use app\common\facades\Setting; use app\common\helpers\PaginationHelper; use Yunshop\CollectionRoom\models\CollectionRoomModel; +use Yunshop\Rebate\models\Rebate; class IndexController extends BaseController{ // 进入列表 @@ -14,14 +15,12 @@ class IndexController extends BaseController{ $pageSize = request()->input('page_size',10); $search = request()->input('search'); // 获取列表信息 - // $field = ['id','member_id','unique_number','created_at']; - // $result = CollectionRoomModel::getList($pageSize,$search,$field); - // $data = [ - // 'list' => $result['data'], - // 'pager' => PaginationHelper::show($result['total'],$result['current_page'],$result['per_page']), - // 'search' => $search - // ]; - + $result = Rebate::getList($pageSize,$search); + $data = [ + 'list' => $result['data'], + 'pager' => PaginationHelper::show($result['total'],$result['current_page'],$result['per_page']), + 'search' => $search + ]; return view('Yunshop\Rebate::index.index',$data)->render(); } diff --git a/plugins/rebate/src/listener/OrderPaidListener.php b/plugins/rebate/src/listener/OrderPaidListener.php new file mode 100644 index 00000000..d02e9080 --- /dev/null +++ b/plugins/rebate/src/listener/OrderPaidListener.php @@ -0,0 +1,28 @@ +listen(AfterOrderPaidEvent::class, self::class . '@handle'); + } + + + public function handle(AfterOrderPaidEvent $event){ + try { + $model = $event->getOrderModel(); + $orderId = $model->id ?? 0; + if($orderId > 0) Rebate::createRebateInit((int)$orderId,(int)$model->uid); + }catch (\Exception $e){ + \Log::debug('--- 消费返利 - 支付成功事件处理 - 失败 ---'.$e->getMessage()); + } + } +} diff --git a/plugins/rebate/src/models/Rebate.php b/plugins/rebate/src/models/Rebate.php new file mode 100644 index 00000000..5fddaf03 --- /dev/null +++ b/plugins/rebate/src/models/Rebate.php @@ -0,0 +1,182 @@ + 'datetime:Y-m-d H:i:s', + 'expect_thaw_time' => 'datetime:Y-m-d H:i:s', + 'reality_thaw_time' => 'datetime:Y-m-d H:i:s' + ]; + + /** + * Common: 列表获取 + * Author: wu-hui + * Time: 2024/03/13 16:38 + * @param $pageSize + * @param $search + * @param string[] $field + * @return array + */ + public function getList($pageSize,$search,$field = ['*']){ + // 条件生成 + $where = []; + if($search['uid'] > 0) $where[] = ['uid','=',$search['uid']]; + if($search['order_id'] > 0) $where[] = ['order_id','=',$search['order_id']]; + if($search['goods_id'] > 0) $where[] = ['goods_id','=',$search['goods_id']]; + // 列表获取 + $model = self::uniacid() + ->select($field) + ->where($where) + ->when(!empty($search['nickname']),function($query) use ($search){ + // 昵称|真实姓名搜索 + $ids = Member::where('nickname','like',"%{$search['nickname']}%") + ->orwhere('realname','like',"%{$search['nickname']}%") + ->pluck('uid'); + if($ids) $ids = $ids->toArray(); + $query->whereIn('uid',(array)$ids); + }) + ->with([ + 'member' => function($query){ + $query->select(['uid','nickname','realname','avatar']); + }, + 'order' => function($query){ + $query->select(['id','order_sn']); + }, + 'goods' => function($query){ + $query->select(['id','title']); + } + ]) + // ->orderBy('expect_thaw_time','DESC') + ->orderBy('id','DESC'); + $list = $model->paginate($pageSize); + + return $list ? $list->toArray() : []; + } + + + /** + * Common: 增加返利信息 - 开始处理 + * Author: wu-hui + * Time: 2024/03/13 16:32 + * @param int $orderId + * @param int $uid + * @return bool + */ + public static function createRebateInit(int $orderId,int $uid){ + // 基本设置 并且判断是否开启插件 + $set = Setting::get('plugin.rebate'); + if($set['is_switch'] != 1) return false; + // 获取当前订单所有商品 + $goodsList = self::createRebateGetOrderGoods($orderId); + // 循环处理每个商品 + $insertData = []; + $uniacid = \YunShop::app()->uniacid; + foreach($goodsList as $goodsInfo){ + // 判断:使用通用默认设置 or 商品独立规则;is_alone:是否独立设置:0=不是,1=是 + $quarterList = $goodsInfo['is_alone'] == 1 ? json_decode($goodsInfo['quarter_list'], true) : $set['quarter_list']; + $expectThawTime = time();// 每个商品的预计解冻时间 = 当前时间 + $expectThawTimeMonth = 0; + foreach($quarterList as $quarterIndex => $quarterInfo){ + $endKey = max(array_keys($quarterInfo['month_list']));// 最后一个元素的键 + foreach($quarterInfo['month_list'] as $monthIndex => $monthInfo){ + // 预计解冻时间 增加 + $currentExpectThawTime = getNextMonthDays($expectThawTime, $expectThawTimeMonth); + $expectThawTimeMonth++; + // 生成返利信息 + $currentMonthData = [ + 'uniacid' => $uniacid, + 'uid' => $uid, + 'order_id' => $orderId, + 'goods_id' => $goodsInfo['goods_id'], + 'quarter' => $quarterIndex, + 'month' => $monthIndex, + 'money' => (float)sprintf("%.2f",$monthInfo * $goodsInfo['total']), + 'expect_thaw_time' => $currentExpectThawTime, + 'reality_thaw_time' => NULL, + 'status' => 0, + 'is_repurchase' => $monthIndex == $endKey ? (int)$quarterInfo['is_repurchase'] : 0, + 'repurchase_money' => $monthIndex == $endKey ? (float)sprintf("%.2f",$quarterInfo['repurchase_money'] * $goodsInfo['total']) : 0, + 'created_at' => time(), + 'updated_at' => time(), + ]; + // 判断:第一季度第一个月 并且 (不是最后一个月 或者 本季度不需要复购):立即解冻 + if($quarterIndex == 1 && $monthIndex == 1 && ($monthIndex != $endKey || (int)$quarterInfo['is_repurchase'] != 1)){ + $currentMonthData['reality_thaw_time'] = $currentExpectThawTime; + $currentMonthData['status'] = 1; + } + + $insertData[] = $currentMonthData; + } + } + } + + // 添加数据 + if(count($insertData) > 0) self::insert($insertData); + + return true; + } + /** + * Common: 增加返利信息 - 获取商品信息 + * Author: wu-hui + * Time: 2024/03/13 15:18 + * @param $orderId + * @return array + */ + private static function createRebateGetOrderGoods($orderId){ + return OrderGoods::select([ + 'yz_order_goods.total', + 'yz_goods_rebate.goods_id', + 'yz_goods_rebate.is_open', + 'yz_goods_rebate.is_alone', + 'yz_goods_rebate.total_quarter', + 'yz_goods_rebate.quarter_list' + ]) + ->leftjoin('yz_goods_rebate','yz_goods_rebate.goods_id','=','yz_order_goods.goods_id') + ->where('yz_order_goods.order_id',$orderId) + ->where('yz_goods_rebate.is_open', 1) + ->get() + ->makeHidden(['order','after_sales','buttons']) + ->toArray(); + } + + + + /** + * Common: 一对一关联 - 用户信息表 + * Author: wu-hui + * Time: 2024/03/13 16:37 + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function member(){ + return $this->hasOne(Member::class, 'uid', 'uid'); + } + /** + * Common: 一对一关联 - 订单表 + * Author: wu-hui + * Time: 2024/03/13 16:41 + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function order(){ + return $this->hasOne(Order::class,'id', 'order_id'); + } + /** + * Common: 一对一关联 - 商品表 + * Author: wu-hui + * Time: 2024/03/13 17:01 + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function goods(){ + return $this->hasOne(Goods::class,'id', 'goods_id'); + } + + +} diff --git a/plugins/rebate/views/index/index.blade.php b/plugins/rebate/views/index/index.blade.php index 8e47de19..bede5a65 100644 --- a/plugins/rebate/views/index/index.blade.php +++ b/plugins/rebate/views/index/index.blade.php @@ -35,6 +35,9 @@ overflow: hidden; text-overflow: ellipsis; } + .label{ + font-size: 15px!important; + } @section('content')