170 lines
7.8 KiB
PHP
170 lines
7.8 KiB
PHP
<?php
|
||
|
||
namespace app\common\repositories\marketing;
|
||
|
||
use app\common\dao\marketing\AgentBrokerageDao;
|
||
use app\common\model\marketing\AgentBrokerage;
|
||
use app\common\repositories\BaseRepository;
|
||
use think\exception\ValidateException;
|
||
use think\facade\Db;
|
||
use think\facade\Log;
|
||
|
||
class AgentBrokerageRepository extends BaseRepository{
|
||
|
||
protected $dao;
|
||
|
||
public function __construct(AgentBrokerageDao $dao){
|
||
$this->dao = $dao;
|
||
}
|
||
/**
|
||
* Common: 获取信息列表
|
||
* Author: wu-hui
|
||
* Time: 2024/01/26 20:38
|
||
* @param array $params
|
||
* @param int $page
|
||
* @param int $limit
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getList(array $params,int $page,int $limit):array{
|
||
$query = $this->dao->searchList($params);
|
||
$count = $query->count();
|
||
$list = $query->page($page,$limit)->select();
|
||
|
||
return compact('count','list');
|
||
}
|
||
/**
|
||
* Common: 获取单个代理人员的佣金明细
|
||
* Author: wu-hui
|
||
* Time: 2024/02/02 17:41
|
||
* @param int $agentId
|
||
* @param int $page
|
||
* @param int $limit
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getAgentCommissionList(int $agentId,int $page,int $limit){
|
||
// 类型:1=发起人,2=省公司,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商
|
||
$agentInfo = app()->make(AgentRepository::class)->getSearchModel(['id'=>$agentId])->findOrEmpty()->toArray();
|
||
$totalMoney = 0;
|
||
switch((int)$agentInfo['agent_type']){
|
||
case 1:
|
||
$query = $this->dao->searchList(['initiator_uid'=>$agentInfo['uid']])
|
||
->field(['*','initiator_brokerage as brokerage']);
|
||
$totalMoney = $query->sum('initiator_brokerage');
|
||
break;
|
||
case 2:
|
||
$query = $this->dao->searchList(['province_uid'=>$agentInfo['uid']])
|
||
->field(['*','province_brokerage as brokerage']);
|
||
$totalMoney = $query->sum('province_brokerage');
|
||
break;
|
||
case 3:
|
||
$query = $this->dao->searchList(['field_staff_uid'=>$agentInfo['uid']])
|
||
->field(['*','field_staff_brokerage as brokerage']);
|
||
$totalMoney = $query->sum('field_staff_brokerage');
|
||
break;
|
||
// case 4: break;
|
||
case 5:
|
||
$query = $this->dao->searchList(['area_store_uid'=>$agentInfo['uid']])
|
||
->field(['*','area_store_brokerage as brokerage']);
|
||
$totalMoney = $query->sum('area_store_brokerage');
|
||
break;
|
||
case 6:
|
||
$query = $this->dao->searchList(['area_uid'=>$agentInfo['uid']])
|
||
->field(['*','area_brokerage as brokerage']);
|
||
$totalMoney = $query->sum('area_brokerage');
|
||
break;
|
||
case 7:
|
||
$query = $this->dao->searchList(['store_uid'=>$agentInfo['uid']])
|
||
->field(['*','store_brokerage as brokerage']);
|
||
$totalMoney = $query->sum('store_brokerage');
|
||
break;
|
||
case 8:
|
||
$query = $this->dao->searchList(['delivery_uid'=>$agentInfo['uid']])
|
||
->field(['*','delivery_brokerage as brokerage']);
|
||
$totalMoney = $query->sum('delivery_brokerage');
|
||
break;
|
||
}
|
||
// 列表信息获取
|
||
$count = $query->count();
|
||
$list = $query->page($page,$limit)->select();
|
||
|
||
return compact('count','list', 'totalMoney');
|
||
}
|
||
/**
|
||
* Common: 邀请代理人员奖励
|
||
* Author: wu-hui
|
||
* Time: 2024/02/02 14:20
|
||
* @param int $orderId
|
||
* @return mixed
|
||
*/
|
||
public function inviteAgentGive(int $orderId){
|
||
Log::info('支付成功 - 邀请代理人员奖励及免审核 - 开始处理: '.$orderId);
|
||
return Db::transaction(function() use ($orderId){
|
||
// 获取配置信息
|
||
$config = app()->make(AgentRepository::class)->getConfig();
|
||
$agentBaseSet = $config['agent_base_set'] ?? [];
|
||
// 获取申请信息
|
||
$applyInfo = app()->make(AgentApplyRepository::class)->getSearchModel(['order_id'=>$orderId])->findOrEmpty()->toArray();
|
||
if(!$applyInfo) throw new ValidateException('信息不存在!');
|
||
// 获取全部上级
|
||
$upAllList = app()->make(AgentRepository::class)->getAllUp($applyInfo['pid']);
|
||
$upAllList = array_column($upAllList, null, 'agent_type');
|
||
// 获取佣金信息 类型:1=总部发起人,2=省公司发起人,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商,9=总部外勤,10=总部内勤
|
||
$initiator = $upAllList[1] ?? [];
|
||
$province = $upAllList[2] ?? [];
|
||
$fieldStaff = $upAllList[3] ?? [];
|
||
$areaStore = $upAllList[5] ?? [];
|
||
$area = $upAllList[6] ?? [];
|
||
$fieldPersonnel = $upAllList[9] ?? [];
|
||
$data = [
|
||
'source' => 1,
|
||
'user_order_id' => $orderId,
|
||
// 发起人
|
||
'initiator_uid' => $initiator ? $initiator['uid'] : 0,
|
||
'initiator_agent_id' => $initiator ? $initiator['id'] : 0,
|
||
// 省公司
|
||
'province_uid' => $province ? $province['uid'] : 0,
|
||
'province_agent_id' => $province ? $province['id'] : 0,
|
||
// 外勤
|
||
'field_staff_uid' => $fieldStaff ? $fieldStaff['uid'] : 0,
|
||
'field_staff_agent_id' => $fieldStaff ? $fieldStaff['id'] : 0,
|
||
// 运营商
|
||
'area_store_uid' => $areaStore ? $areaStore['uid'] : 0,
|
||
'area_store_agent_id' => $areaStore ? $areaStore['id'] : 0,
|
||
// 合伙人
|
||
'area_uid' => $area ? $area['uid'] : 0,
|
||
'area_agent_id' => $area ? $area['id'] : 0,
|
||
// 总部外勤
|
||
'field_personnel_uid' => $fieldPersonnel ? $fieldPersonnel['uid'] : 0,
|
||
'field_personnel_agent_id' => $fieldPersonnel ? $fieldPersonnel['id'] : 0,
|
||
];
|
||
// 佣金处理
|
||
$currentSet = $agentBaseSet[$applyInfo['agent_type']] ?? [];
|
||
$isToExamine = (int)($currentSet['is_examine'] ?? 0);// 是否免审核 0=需要审核,1=无需审核
|
||
$commissionList = (array)($currentSet['commission_list'] ?? []);
|
||
$data['platform_brokerage'] = $commissionList['platform'] ?? 0;// 平台奖励
|
||
$data['initiator_brokerage'] = $commissionList['initiator'] ?? 0;//总部发起人奖励
|
||
$data['field_personnel_brokerage'] = $commissionList['field_personnel'] ?? 0;// 总部外勤奖励
|
||
$data['province_brokerage'] = $commissionList['province'] ?? 0;//省公司发起人奖励
|
||
$data['field_staff_brokerage'] = $commissionList['field_staff'] ?? 0;//省公司外勤奖励
|
||
$data['area_store_brokerage'] = $commissionList['operator'] ?? 0;//区县运营商奖励
|
||
$data['area_brokerage'] = $commissionList['partner'] ?? 0;// 区县合伙人奖励
|
||
AgentBrokerage::insert($data);
|
||
// 判断:是否免审核
|
||
if($isToExamine){
|
||
app()->make(AgentApplyRepository::class)->toExaminePass([
|
||
'id' => $applyInfo['id'],
|
||
'status' => 1,
|
||
]);
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
}
|