178 lines
6.8 KiB
PHP
178 lines
6.8 KiB
PHP
<?php
|
|
namespace Yunshop\CulturalSpace\models;
|
|
use app\common\facades\Setting;
|
|
use app\common\models\BaseModel;
|
|
use app\common\models\OrderGoods;
|
|
use app\common\models\UniAccount;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
|
|
use Yunshop\TeamDividend\models\TeamDividendLevelModel;
|
|
|
|
class CulturalSpaceAreaCycle extends BaseModel
|
|
{
|
|
public $table = 'yz_cultural_space_area_cycle';
|
|
public $timestamps = false;
|
|
public $casts = [
|
|
'start_time' => 'datetime:Y-m-d H:i:s',
|
|
'end_time' => 'datetime:Y-m-d H:i:s'
|
|
];
|
|
protected $fillable = [
|
|
'uniacid',
|
|
'uid',
|
|
'total_price',
|
|
'allocation_price',
|
|
'total_amount',
|
|
'start_time',
|
|
'end_time',
|
|
'created_at',
|
|
];
|
|
private $startTime,$endTime,$set,$orderTotalMoney,$bonusTotalMoney;
|
|
/**
|
|
* Common: 获取列表
|
|
* Author: wu-hui
|
|
* Time: 2024/01/05 16:47
|
|
* @param int $limit
|
|
* @return array
|
|
*/
|
|
public function getList(){
|
|
$list = self::uniacid()
|
|
->orderBy('created_at','DESC')
|
|
->orderBy('id','DESC')
|
|
->paginate(10);
|
|
|
|
return $list ? $list->toArray() : [];
|
|
}
|
|
|
|
|
|
/**
|
|
* Common: 分红处理开始
|
|
* Author: wu-hui
|
|
* Time: 2024/01/05 16:22
|
|
*/
|
|
public function bonusInit(){
|
|
set_time_limit(0);
|
|
DB::beginTransaction();
|
|
try{
|
|
[$this->startTime,$this->endTime] = getTimeStamp('yesterday');// yesterday 计算前一天信息
|
|
// 循环平台 进行处理
|
|
$uniAccount = UniAccount::getEnable() ?: [];
|
|
foreach ($uniAccount as $u) {
|
|
Setting::$uniqueAccountId = \YunShop::app()->uniacid = $u->uniacid;
|
|
$this->set = Setting::get('plugin.cultural_space_set');
|
|
// 判断:是否符合执行条件
|
|
$isRun = (boolean)$this->isRun();
|
|
// 判断:允许下一步
|
|
if ($isRun) $this->bonusHandle();
|
|
}
|
|
|
|
\Log::debug('--- 文创空间 - 市场津贴 - 结束 end----');
|
|
DB::commit();
|
|
}catch(\Exception $e){
|
|
\Log::debug('--- 文创空间 - 市场津贴 - 错误:'.$e->getMessage());
|
|
DB::rollBack();
|
|
}
|
|
}
|
|
/**
|
|
* Common: 判断是否允许继续执行
|
|
* Author: wu-hui
|
|
* Time: 2024/01/05 16:24
|
|
* @return bool
|
|
*/
|
|
private function isRun(){
|
|
$time = time();
|
|
// 判断:设置是否开启市场津贴
|
|
if((int)$this->set['area_bonus_switch'] != 1 || (float)$this->set['area_bonus_ratio'] <= 0) {
|
|
\Log::debug("未开启市场津贴 || 总业绩比例为0或未设置",\YunShop::app()->uniacid);
|
|
return false;
|
|
}
|
|
// 判断:时间上是否允许继续执行(上一个周期结束时间存在 & 当前时间小于下一个周期开始时间 禁止继续执行)
|
|
$upCycleEndTime = self::uniacid()->max('end_time');
|
|
$lastRunTime = strtotime(date("Y-m-d H:i:s",$upCycleEndTime)." +1 day");// 下一个周期结束时间
|
|
if($upCycleEndTime > 0 && $time < $lastRunTime) {
|
|
\Log::debug("执行时间错误(上一个周期结束时间{$upCycleEndTime},下一个周期结束时间{$lastRunTime})",\YunShop::app()->uniacid);
|
|
return false;
|
|
}
|
|
// 判断:是否存在分红总金额 分红总金额,仅计算参与文创空间的商品的销售额
|
|
$this->orderTotalMoney = (float)OrderGoods::uniacid()
|
|
->leftjoin('yz_order','yz_order.id','=','yz_order_goods.order_id')
|
|
->leftjoin('yz_goods_cultural_space','yz_goods_cultural_space.goods_id','=','yz_order_goods.goods_id')
|
|
->where('yz_goods_cultural_space.is_open',1)
|
|
->where('yz_order.pay_time','>=',$this->startTime)
|
|
->where('yz_order.pay_time','<',$this->endTime)
|
|
->where('yz_order.price','>',0)
|
|
->where('yz_order.status','>',0)
|
|
->sum('yz_order_goods.payment_amount');
|
|
|
|
$this->bonusTotalMoney = (float)sprintf("%.2f",$this->orderTotalMoney * (float)$this->set['area_bonus_ratio'] / 100);
|
|
if($this->bonusTotalMoney <= 0) {
|
|
\Log::debug("分红总金额为0",\YunShop::app()->uniacid);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
/**
|
|
* Common: 处理每个站点所有用户的分红
|
|
* Author: wu-hui
|
|
* Time: 2024/01/05 16:24
|
|
* @return bool|void
|
|
*/
|
|
private function bonusHandle(){
|
|
// 获取当前站点参与分红的经销商等级id
|
|
$teamLevelIds = TeamDividendLevelModel::uniacid()->where('is_join_bonus',1)->pluck('id')->toArray();
|
|
if(count($teamLevelIds) <= 0) return true;
|
|
// 获取参与的经销商列表
|
|
$teamAgentIds = TeamDividendAgencyModel::uniacid()
|
|
->where(function($query) use ($teamLevelIds){
|
|
$query->whereIn('level',$teamLevelIds)
|
|
->orWhereIn('cultural_level_id',$teamLevelIds);
|
|
})
|
|
->pluck('uid')
|
|
->toArray();
|
|
if(count($teamAgentIds) <= 0) return true;
|
|
// 获取全部团队的小区业绩总和
|
|
$minAreaSumAmount = (float)TeamDividendAgencyModel::teamMinAreaSumAmount();
|
|
if($minAreaSumAmount <= 0) return true;
|
|
// 记录周期分红信息
|
|
$cycleId = self::insertGetId([
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'total_price' => $this->orderTotalMoney,
|
|
'allocation_price' => $this->bonusTotalMoney,
|
|
'total_amount' => $minAreaSumAmount,
|
|
'start_time' => $this->startTime,
|
|
'end_time' => $this->endTime,
|
|
'created_at' => time(),
|
|
]);
|
|
// 循环处理信息
|
|
$logInsertData = [];// 明细记录
|
|
$uniacid = \YunShop::app()->uniacid;
|
|
foreach($teamAgentIds as $currentUid){
|
|
// 获取当前用户小区业绩
|
|
$teamPerformance = (new CulturalSpace())->getTeamOrderAmount($currentUid);
|
|
if($teamPerformance['area_amount'] <= 0) continue;
|
|
// 添加市场津贴信息记录 这里必须进行100的偏移计算
|
|
$radio = (float)sprintf("%.2f",$teamPerformance['area_amount'] / $minAreaSumAmount * 100);// 佣金比例
|
|
$money = (float)sprintf("%.2f",$this->bonusTotalMoney * $radio / 100);// 实际获得佣金
|
|
if($money > 0){
|
|
$logInsertData[] = [
|
|
'uniacid' => $uniacid,
|
|
'uid' => $currentUid,
|
|
'cycle_id' => $cycleId,
|
|
'area_amount' => $teamPerformance['area_amount'],
|
|
'area_rate' => $radio,
|
|
'get_amount' => $money,
|
|
];
|
|
}
|
|
}
|
|
|
|
CulturalSpaceAreaBonus::insert($logInsertData);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|