添加:消费返利 - 购买指定商品进行返利,返利记录列表查看

This commit is contained in:
wuhui_zzw 2024-03-13 17:56:10 +08:00
parent 36bb257eff
commit 5f1ad1487a
5 changed files with 263 additions and 25 deletions

View File

@ -1,6 +1,8 @@
<?php
namespace Yunshop\Rebate;
use Yunshop\Rebate\listener\OrderPaidListener;
class PluginApplication extends \app\common\services\PluginApplication{
protected function setMenuConfig(){
@ -90,7 +92,8 @@ class PluginApplication extends \app\common\services\PluginApplication{
public function boot(){
$events = app('events');
// 订单支付成功
$events->subscribe(OrderPaidListener::class);
}

View File

@ -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();
}

View File

@ -0,0 +1,28 @@
<?php
namespace Yunshop\Rebate\listener;
use app\common\events\order\AfterOrderPaidEvent;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Yunshop\Rebate\models\Rebate;
class OrderPaidListener{
use DispatchesJobs;
public function subscribe(Dispatcher $events){
$events->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());
}
}
}

View File

@ -0,0 +1,182 @@
<?php
namespace Yunshop\Rebate\models;
use app\common\facades\Setting;
use app\common\models\BaseModel;
use app\common\models\Goods;
use app\common\models\Member;
use app\common\models\Order;
use app\common\models\OrderGoods;
class Rebate extends BaseModel{
public $table = 'yz_rebate';
public $casts = [
'created_at' => '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');
}
}

View File

@ -35,6 +35,9 @@
overflow: hidden;
text-overflow: ellipsis;
}
.label{
font-size: 15px!important;
}
</style>
@section('content')
<div class="w1200 m0a" id="storeManagerIndex">
@ -46,13 +49,16 @@
<div class="col-sm-11 col-xs-12">
<div class="row row-fix tpl-category-container" >
<div class="col-xs-12 col-sm-8 col-lg-3">
<input class="form-control" name="search[member_id]" id="" type="text" value="{{ $search['member_id'] }}" placeholder="用户ID">
<input class="form-control" name="search[uid]" id="" type="text" value="{{ $search['uid'] }}" placeholder="用户ID">
</div>
<div class="col-xs-12 col-sm-8 col-lg-3">
<input class="form-control" name="search[nickname]" id="" type="text" value="{{ $search['nickname'] }}" placeholder="用户昵称|真实姓名">
</div>
<div class="col-xs-12 col-sm-8 col-lg-3">
<input class="form-control" name="search[unique_number]" id="" type="text" value="{{ $search['unique_number'] }}" placeholder="唯一编号">
<input class="form-control" name="search[order_id]" id="" type="text" value="{{ $search['order_id'] }}" placeholder="订单id">
</div>
<div class="col-xs-12 col-sm-8 col-lg-3">
<input class="form-control" name="search[goods_id]" id="" type="text" value="{{ $search['goods_id'] }}" placeholder="商品id">
</div>
</div>
</div>
@ -73,9 +79,13 @@
<tr>
<th style="text-align:center;width: 80px;">ID</th>
<th style="text-align:center;">用户信息</th>
<th style="text-align:center;">唯一编号</th>
<th style="text-align:center;">获得时间</th>
<th style="text-align:center;">操作</th>
<th style="text-align:center;width: 200px;">订单商品</th>
<th style="text-align:center;">阶段时间</th>
<th style="text-align:center;width: 90px;">状态</th>
<th style="text-align:center;">预计解冻时间<br />实际解冻时间</th>
<th style="text-align:center;width: 130px;">返利金额</th>
<th style="text-align:center;width: 150px;">解冻后是否需要复购</th>
<th style="text-align:center;">下单时间</th>
</tr>
</thead>
<tbody>
@ -93,11 +103,31 @@
</div>
</div>
</td>
<td style="text-align:center;">{{ $item['unique_number'] }}</td>
<td style="text-align:center;">{{ $item['created_at'] }}</td>
<td style="text-align:center;">
<button onclick="updateNumber({{$item['id']}})" type="button" class="btn btn-info btn-sm">修改编号</button>
{{ $item['goods']['title'] }}<br />
{{ $item['order']['order_sn'] }}
</td>
<td style="text-align:center;">{{ $item['quarter'] }}季度第{{ $item['month'] }}个月</td>
<td style="text-align:center;">
{{--状态0=冻结中1=已解冻(可提现),2=已提现3=已失效4=已退款(失效)--}}
@switch($item['status'])
@case(0)<span class="label label-default">冻结中</span>@break
@case(1)<span class="label label-primary">待提现</span>@break
@case(2)<span class="label label-success">已提现</span>@break
@case(3)<span class="label label-danger">已失效</span>@break
@case(4)<span class="label label-danger">已退款</span>@break
@endswitch
</td>
<td style="text-align:center;">{{ $item['expect_thaw_time'] ?? '-' }}<br />{{ $item['reality_thaw_time'] ?? '-' }}</td>
<td style="text-align:center;">{{ $item['money'] ?? 0 }}</td>
<td style="text-align:center;">
{{--领取后是否需要复购0=不需要1=需要--}}
@switch($item['is_repurchase'])
@case(0)<span class="label label-default">不需要</span>@break
@case(1)<span class="label label-success">需要</span>@break
@endswitch
</td>
<td style="text-align:center;">{{ $item['created_at'] }}</td>
</tr>
@endforeach
</tbody>
@ -107,14 +137,10 @@
</div>
</div>
<script type="text/javascript">
// 修改编号
function updateNumber(id){
let link = "{{yzWebUrl('plugin.collection-room.admin.index.update-number')}}" + `&id=${id}`;
let popup = util.ajaxshow(link,'修改唯一编号',{
width: $(window).width() * 0.8 > 1200 ? $(window).width() * 0.8 : 1200,
height: $(window).height() * 0.8 > 1200 ? $(window).height() * 0.8 : 1200,
});
}
</script>
@endsection