112 lines
4.7 KiB
PHP
112 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
|
|
* =========================================================
|
|
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
|
|
* ----------------------------------------------
|
|
* 官方网址: https://www.gobuysaas.com
|
|
* =========================================================
|
|
*/
|
|
namespace addon\fenxiao\model;
|
|
use app\model\BaseModel;
|
|
use app\model\message\Message;
|
|
use app\model\system\Stat;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 分销商品
|
|
*/
|
|
class WeightOrder extends BaseModel{
|
|
/**
|
|
* 加权订单计算
|
|
* @param $site_id
|
|
*/
|
|
public function calculate($site_id){
|
|
//获取分销基础配置
|
|
$config_model = new Config();
|
|
$fenxiao_basic_config = $config_model->getFenxiaoBasicsConfig($site_id)['data']['value'];
|
|
$time_type = $fenxiao_basic_config['time'];
|
|
$is_settlement = false;
|
|
switch($time_type){
|
|
//按天分配
|
|
case '1':
|
|
$is_settlement = true;
|
|
// $order_list = model('fenxiao_order')->getList([['is_weight' , '=' , 1] ,['weight_money' , '>' ,0] ,['weight_log_id' ,'=' ,0]]);
|
|
break;
|
|
//按周分配
|
|
case '2':
|
|
$week = date('w',time());
|
|
if($week){
|
|
$is_settlement = true;
|
|
}
|
|
break;
|
|
//按月分配
|
|
case '3':
|
|
$currentDate = date('Y-m-d'); // 获取当前日期
|
|
$firstDayOfMonth = date('Y-m-01');
|
|
if($currentDate == $firstDayOfMonth){
|
|
$is_settlement = true;
|
|
}
|
|
break;
|
|
}
|
|
//符合条件
|
|
if($is_settlement){
|
|
$total_weight_money = model('fenxiao_order')->getSum([['site_id' ,'=' , $site_id],['is_weight' ,'=' ,1],['weight_log_id' ,'=' ,0]],'weight_money');
|
|
$total_order_money = model('fenxiao_order')->getSum([['site_id' ,'=' , $site_id],['is_weight' ,'=' ,1],['weight_log_id' ,'=' ,0]],'real_goods_money');
|
|
$alias = 'f';
|
|
$join = [
|
|
['fenxiao_level l', 'f.level_id = l.level_id', 'right']
|
|
];
|
|
$fenxiao_list = model('fenxiao')->getList([ [ 'l.is_weight', '=', 1 ], [ 'f.is_delete', '=', 0 ] ],'f.fenxiao_id,f.fenxiao_name','',$alias,$join);
|
|
if($fenxiao_list && $total_weight_money){
|
|
$fenxiao_ids = $detail = $order_ids = [];
|
|
$total_commission = 0;
|
|
Db::startTrans();
|
|
try {
|
|
foreach($fenxiao_list as $key => $val){
|
|
$condition = [
|
|
['site_id', '=', $site_id],
|
|
['chain' , 'like' ,'%' . $val['fenxiao_id']. ',%'],
|
|
['is_delete' ,'=' ,0]
|
|
];
|
|
$ids = model('fenxiao')->getColumn($condition,'member_id');
|
|
$weight_con = [
|
|
['site_id' ,'=' , $site_id],
|
|
['is_weight' ,'=' ,1],
|
|
['weight_log_id' ,'=' ,0],
|
|
['member_id', 'in' ,$ids]
|
|
];
|
|
$weight_money = model('fenxiao_order')->getSum($weight_con,'weight_money');
|
|
$order_ids = $key === 0 ?? model('fenxiao_order')->getColumn($weight_con,'order_id');
|
|
$fenxiao_ids[] = $val['fenxiao_id'];
|
|
$commission = $weight_money / $total_weight_money * $total_order_money;
|
|
$total_commission += $commission;
|
|
$detail[] = [
|
|
'fenxiao_id' => $val['fenxiao_id'],
|
|
'name' => $val['fenxiao_name'],
|
|
'commission' => $commission
|
|
];
|
|
}
|
|
$add = [
|
|
'order_ids' => json_encode($order_ids),
|
|
'fenxiao_ids' => json_encode($fenxiao_ids),
|
|
'total_money' => $total_commission,
|
|
'detail' => $detail,
|
|
'create_time' => time()
|
|
];
|
|
$log_id = model('weight_order_log')->add($add);
|
|
model('fenxiao_order')->update(['weight_log_id' => $log_id],[['order_id', 'IN' , $order_ids]]);
|
|
Db::commit();
|
|
return $log_id;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
return error(-1, $e->getMessage());
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return $this->success();
|
|
}
|
|
}
|