admin/plugins/rebate/src/models/Rebate.php

239 lines
8.7 KiB
PHP
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 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;
use app\common\services\income\IncomeService;
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'] = time();
$currentMonthData['status'] = 1;
}
$insertData[] = $currentMonthData;
}
}
}
// 添加数据
if(count($insertData) > 0) {
self::insert($insertData);
self::rebateSettlement();
}
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/15 9:58
*/
public static function rebateThaw(){
$time = time();
// 预计解冻时间 低于当前时间 解冻
self::where('expect_thaw_time','<=', $time)->update([
'status' => 1,
'reality_thaw_time' => $time
]);
// 执行 已解冻佣金结算到账
self::rebateSettlement();
}
/**
* Common: 已解冻返利 结算到佣金
* Author: wu-hui
* Time: 2024/03/15 9:50
*/
public static function rebateSettlement(){
\Log::debug('--- 消费返利 - 结算到佣金 - 开始 ----');
// 获取可以结算但是未结算的数据
$list = self::select(['id','uid','money'])
->where('status', 1)
->get()
->toArray();
$incomeData = [];
foreach($list as $item){
$incomeData[] = [
'uniacid' => \YunShop::app()->uniacid,
'member_id' => $item['uid'],
'amount' => $item['money'],
'detail' => '',
'dividend_table_id' => $item['id'],
'dividend_code' => IncomeService::REBATE_MONEY,
'order_sn' => '',
];
}
if(count($incomeData) > 0){
// 添加结算信息
IncomeService::insertIncome($incomeData);
// 修改为已结算
$ids = array_column($list,'id');
self::whereIn('id',$ids)->update([
'status' => 2
]);
}
}
/**
* 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');
}
}