<?php
|
|
namespace app\common\model\plus\bonus;
|
|
use app\common\model\BaseModel;
|
use app\common\enum\order\OrderTypeEnum;
|
use app\common\model\plus\bonus\User as UserModel;
|
/**
|
* 队长订单模型
|
*/
|
class Order extends BaseModel
|
{
|
protected $name = 'bonus_order';
|
protected $pk = 'id';
|
|
/**
|
* 获取用户订单数
|
* @param $user_id
|
* @return int
|
*/
|
public static function getUserOrderCounti($user_id)
|
{
|
return (new static())
|
->where('is_invalid', '=', 0)
|
->where('user_id', '=', $user_id)
|
->count()+1;
|
}
|
|
/**
|
* 订单所属用户
|
* @return \think\model\relation\BelongsTo
|
*/
|
public function user()
|
{
|
return $this->belongsTo('app\common\model\user\User');
|
}
|
|
/**
|
* 一级队长用户
|
* @return \think\model\relation\BelongsTo
|
*/
|
public function bonusFirst()
|
{
|
return $this->belongsTo('app\common\model\user\User', 'first_user_id');
|
}
|
|
/**
|
* 二级队长用户
|
* @return \think\model\relation\BelongsTo
|
*/
|
public function bonusSecond()
|
{
|
return $this->belongsTo('app\common\model\user\User', 'second_user_id');
|
}
|
|
/**
|
* 三级队长用户
|
* @return \think\model\relation\BelongsTo
|
*/
|
public function bonusThird()
|
{
|
return $this->belongsTo('app\common\model\user\User', 'third_user_id');
|
}
|
/**
|
* 收益补贴用户
|
* @return \think\model\relation\BelongsTo
|
*/
|
public function subsidy()
|
{
|
return $this->belongsTo('app\common\model\user\User', 'subsidy_user_id');
|
}
|
|
|
/**
|
* 订单类型
|
* @param $value
|
* @return array
|
*/
|
public function getOrderTypeAttr($value)
|
{
|
$types = OrderTypeEnum::getTypeName();
|
return ['text' => $types[$value], 'value' => $value];
|
}
|
|
/**
|
* 订单详情
|
*/
|
public static function getDetailByOrderId($orderId, $orderType)
|
{
|
return (new static())->where('order_id', '=', $orderId)
|
->where('order_type', '=', $orderType)
|
->find();
|
}
|
/**
|
* 获取订单所有同样订单id详情
|
*/
|
public static function getDetailByOrderIdAll($orderId, $orderType)
|
{
|
return (new static())->where('order_id', '=', $orderId)
|
->where('order_type', '=', $orderType)
|
->select();
|
}
|
|
/**
|
* 发放分销订单佣金
|
* @param $order
|
* @param int $orderType
|
* @return bool
|
* @throws \think\db\exception\DataNotFoundException
|
* @throws \think\db\exception\DbException
|
* @throws \think\db\exception\ModelNotFoundException
|
*/
|
public static function grantMoney($order, $orderType = OrderTypeEnum::MASTER)
|
{
|
// 订单是否已完成
|
if ($order['order_status']['value'] != 30) {
|
return false;
|
}
|
|
// 分销订单详情
|
$models = self::getDetailByOrderIdAll($order['order_id'], $orderType);
|
foreach ($models as $model) {
|
if (!$model || $model['is_settled'] == 1) {
|
return false;
|
}
|
// 重新计算分红
|
$capital = $model->getCapitalByOrder($order,$model);
|
// 发放直推分红
|
$model['first_user_id'] > 0 && User::grantMoney($model['first_user_id'], $capital['first_money'], 10);
|
// 发放业绩分红
|
$model['second_user_id'] > 0 && User::grantMoney($model['second_user_id'], $capital['second_money'], 20,$model['purchase_count']);
|
// 发放补贴分红
|
$model['subsidy_user_id'] > 0 && User::grantMoney($model['subsidy_user_id'], $capital['subsidy_money'], 30);
|
// 更新分销订单记录
|
$model->save([
|
'order_price' => $capital['orderPrice'],
|
'first_money' => $model['first_user_id'] > 0? $capital['first_money']:0,
|
'second_money' => $model['second_user_id'] > 0? $capital['second_money']:0,
|
'is_settled' => 1,
|
'settle_time' => time()
|
]);
|
}
|
return true;
|
}
|
|
/**
|
* 计算订单分销佣金
|
* @param $order
|
* @return array
|
*/
|
protected function getCapitalByOrder($order,$teamOrder)
|
{
|
$capital=[
|
'orderPrice'=>$teamOrder['order_price'],
|
'first_money'=>$teamOrder['first_money'],
|
'second_money'=>$teamOrder['second_money'],
|
'subsidy_money'=>$teamOrder['subsidy_money']
|
];
|
return $capital;
|
}
|
|
/**
|
* 计算商品实际佣金
|
* @param $setting
|
* @param $product
|
* @param $productPrice
|
* @return float[]|int[]
|
*/
|
private function calculateProductCapital($setting, $product, $productPrice)
|
{
|
|
// 分红佣金类型:百分比
|
return [
|
'first_money' => $productPrice,
|
'second_money' => $productPrice
|
];
|
}
|
|
/**
|
* 验证商品是否存在售后
|
* @param $product
|
* @return bool
|
*/
|
private function checkProductRefund($product)
|
{
|
return !empty($product['refund'])
|
&& $product['refund']['type']['value'] != 20
|
&& $product['refund']['is_agree']['value'] != 20;
|
}
|
|
public static function getUserOrderCount($user_id)
|
{
|
return (new static())->where(['second_user_id'=>$user_id,'is_invalid'=>0])
|
->count();
|
}
|
|
/**
|
* 创建分红订单记录
|
*/
|
public static function createOrder($order, $order_type = OrderTypeEnum::MASTER)
|
{
|
// 分销订单模型
|
$model = new self;
|
// 分销商基本设置
|
$setting = Setting::getItem('basic', $order['app_id']);
|
// 是否开启分红功能
|
if (!$setting['is_open']) {
|
return false;
|
}
|
// 获取当前买家的直推奖用户id和业绩奖用户id
|
$agentUser = $model->getAgentUserId($order, $setting['begin_num']);
|
// 无分红用户
|
if (!$agentUser['first_user_id'] && !$agentUser['second_user_id']) {
|
return false;
|
}
|
$purchase_count=self::getUserOrderCount($order['user_id']);
|
$user=User::detail($order['user_id']);
|
if ($user['purchase_count']<=$purchase_count&&$user['purchase_count']!=11){
|
return false;
|
}
|
// 检查商品是否设置独立分红
|
$one_money = $setting['one_money'];
|
$two_money = $setting['two_money'];
|
if ($order['product'][0]['is_ind_bonus']) {
|
$one_money = $order['product'][0]['one_money'];
|
$two_money = $order['product'][0]['two_money'];
|
|
}
|
$subsidy_money=0;
|
if ($agentUser['subsidy_user_id']>0){
|
$subsidy_money = bcmul(bcadd($one_money,$two_money,2),bcdiv($setting['operating_subsidy'],100,2),2);
|
}
|
// 保存分红订单记录
|
return $model->save([
|
'user_id' => $order['user_id'],
|
'order_id' => $order['order_id'],
|
'order_type' => $order_type,
|
'order_price' => bcsub($order['pay_price'], $order['express_price'], 2),
|
'first_money' => $one_money,
|
'second_money' => $two_money,
|
'first_user_id' => $one_money > 0 ?$agentUser['first_user_id']:0,
|
'second_user_id' => $agentUser['second_user_id'],
|
'subsidy_user_id'=>$agentUser['subsidy_user_id'],
|
'subsidy_money'=>$subsidy_money,
|
'purchase_count'=>$purchase_count,
|
'is_settled' => 0,
|
'app_id' => $order['app_id']
|
]);
|
}
|
|
/**
|
* 获取当前买家的所有上级分销商用户id
|
*/
|
private function getAgentUserId($order, $begin_num)
|
{
|
$user = UserModel::detail($order['user_id']);
|
if (empty($user['parent_id'])) {
|
$second_user_id = 0;
|
} else {
|
$second_user_id = UserModel::getSecondUserId($user['user_id'], $begin_num, $order);
|
}
|
if ($second_user_id==0){
|
$subsidy_user_id = 0;
|
}else{
|
$subsidy_user_id = UserModel::getSubsidyUserId($second_user_id); //补贴用户
|
}
|
$agentUser = [
|
'first_user_id' => UserModel::getFirstUserId($order['user_id']), //直推用户
|
'second_user_id' => $second_user_id, //业绩奖用户
|
'subsidy_user_id'=>$subsidy_user_id, //补贴用户
|
];
|
return $agentUser;
|
}
|
|
}
|