new-admin-api/app/controller/admin/marketing/Agent.php

419 lines
21 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 app\controller\admin\marketing;
use app\common\repositories\marketing\AgentApplyRepository;
use app\common\repositories\marketing\AgentBrokerageRepository;
use app\common\repositories\marketing\AgentRepository;
use app\common\repositories\system\config\ConfigClassifyRepository;
use app\common\repositories\system\config\ConfigValueRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use crmeb\basic\BaseController;
use think\exception\ValidateException;
class Agent extends BaseController{
/**
* Common: 代理列表
* Author: wu-hui
* Time: 2024/01/24 15:40
* @return mixed
*/
public function agentList(){
[$page, $limit] = $this->getPage();
$params = $this->request->params(['uid','agent_type','contact_name','contact_phone','is_invite_supplier']);
$data = app()->make(AgentRepository::class)->getList((array)$params,(int)$page,(int)$limit);
return app('json')->success($data);
}
/**
* Common: 编辑信息
* Author: wu-hui
* Time: 2024/01/24 16:05
* @return mixed
*/
public function editInfo(){
// 参数获取
$params = $this->checkParams();
// 数据处理
app()->make(AgentRepository::class)->editInfo((int)$params['agentId'],(array)$params['data'],(array)$params['childrenList']);
return app('json')->success('操作成功');
}
/**
* Common: 编辑参数获取 & 参数校验
* Author: wu-hui
* Time: 2024/01/24 14:16
* @return array
*/
public function checkParams(){
// 参数获取
$data = $this->request->params([
['uid',0],
['pid',0],
['agent_type',0],
['agent_stock',0],
'contact_name',
'contact_phone',
['province_id',0],
['city_id', 0],
['area_id', 0],
['street_id', 0],
'address',
'lat',
'lng',
['mer_id', 0],
['mer_id_list', []],
['province_corporate_name','']
]);
$agentId = (int)$this->request->param('id');
$childrenList = $this->request->param('children_list');
// 判断:如果$agentId 为空或者0则当前操作为编辑发起人至少有一个发起人信息
if($agentId <= 0){
if(count($childrenList) <= 0) throw new ValidateException('请至少添加一个发起人!');
$agentStock = (float)array_sum(array_column($childrenList,'agent_stock'));
if($agentStock != 100) throw new ValidateException('所有发起人的股份总和必须等于100');
}
// 验证下级信息类型1=总部发起人,2=省公司发起人,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商,9=总部外勤,10=总部内勤
if(count($childrenList) > 0){
// 下级数据信息是否完善
foreach($childrenList as $childrenItem){
if(empty($childrenItem['contact_name'])) throw new ValidateException('请输入联系人姓名!');
if(empty($childrenItem['contact_phone'])) throw new ValidateException('请输入联系人电话!');
if(isPhone($childrenItem['contact_phone'])) throw new ValidateException('请输入正确的联系人电话!');
// 判断:根据当前角色判断 数据
switch((int)$childrenItem['agent_type']){
case 1:
// if((float)$childrenItem['agent_stock'] <= 0) throw new ValidateException('发起人的股份必须大于0');
break;
case 2:
if(empty($childrenItem['province_corporate_name'])) throw new ValidateException('请输入省公司名称!');
break;
case 3:
case 4:
// if((float)$childrenItem['province_id'] <= 0) throw new ValidateException('请完善地区信息!');
break;
case 5:
case 6:
if((float)$childrenItem['province_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$childrenItem['city_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$childrenItem['area_id'] <= 0) throw new ValidateException('请完善地区信息!');
break;
case 7:
if((float)$childrenItem['province_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$childrenItem['city_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$childrenItem['area_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$childrenItem['street_id'] <= 0) throw new ValidateException('请完善地区信息!');
if(empty($childrenItem['address'])) throw new ValidateException('请完善地区信息!');
if(((float)$childrenItem['lat'] <= 0 || (float)$childrenItem['lng'] <= 0)) throw new ValidateException('请选择定位信息!');
if((float)$childrenItem['mer_id'] <= 0) throw new ValidateException('餐厅必须关联商户!');
break;
case 8:
if(count($childrenItem['mer_id_list']) <= 0) throw new ValidateException('配送商至少关联一个商户!');
break;
}
}
// 判断:下级数量是否达到限制
$config = app()->make(AgentRepository::class)->getConfig();
$inviteLimit = $config['invite_limit'] ?? [];
$statisticNumber = (array)array_count_values(array_column($childrenList,'agent_type'));
switch($data['agent_type']){
case 1:
// 判断:[总部发起人] 邀请 [总部外勤]是否超出限制
if(($inviteLimit['invite_1_9'] ?? 0) < ($statisticNumber[9] ?? 0)) {
throw new ValidateException('【总部外勤】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_1_9'] ?? 0).' 人');
}
// 判断:[总部发起人] 邀请 [总部内勤]是否超出限制
if(($inviteLimit['invite_1_10'] ?? 0) < ($statisticNumber[10] ?? 0)) {
throw new ValidateException('【总部内勤】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_1_10'] ?? 0).' 人');
}
break;
case 2:
// 判断:[省公司发起人] 邀请 [省公司外勤]是否超出限制
if(($inviteLimit['invite_2_3'] ?? 0) < ($statisticNumber[3] ?? 0)) {
throw new ValidateException('【省公司外勤】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_2_3'] ?? 0).' 人');
}
// 判断:[省公司发起人] 邀请 [省公司内勤]是否超出限制
if(($inviteLimit['invite_2_4'] ?? 0) < ($statisticNumber[4] ?? 0)) {
throw new ValidateException('【省公司内勤】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_2_4'] ?? 0).' 人');
}
break;
case 3:
// 判断:[省公司外勤] 邀请 [区县运营商]是否超出限制
if(($inviteLimit['invite_3_5'] ?? 0) < ($statisticNumber[5] ?? 0)) {
throw new ValidateException('【区县运营商】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_3_5'] ?? 0).' 人');
}
break;
case 4: break;
case 5:
// 判断:[区县运营商] 邀请 [区县合伙人]是否超出限制
if(($inviteLimit['invite_5_6'] ?? 0) < ($statisticNumber[6] ?? 0)) {
throw new ValidateException('【区县合伙人】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_5_6'] ?? 0).' 人');
}
break;
case 6:
// 判断:[区县合伙人] 邀请 [餐厅]是否超出限制
if(($inviteLimit['invite_6_7'] ?? 0) < ($statisticNumber[7] ?? 0)) {
throw new ValidateException('【餐厅】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_6_7'] ?? 0).' 人');
}
// 判断:[区县合伙人] 邀请 [配送商]是否超出限制
if(($inviteLimit['invite_6_8'] ?? 0) < ($statisticNumber[8] ?? 0)) {
throw new ValidateException('【配送商】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_6_8'] ?? 0).' 人');
}
break;
// case 7: break;
// case 8: break;
case 9:
// 判断:[总部外勤] 邀请 [省公司发起人]是否超出限制
if(($inviteLimit['invite_9_2'] ?? 0) < ($statisticNumber[2] ?? 0)) {
throw new ValidateException('【省公司发起人】招募数量超出限制!仅允许招募 '.($inviteLimit['invite_9_2'] ?? 0).' 人');
}
break;
// case 10: break;
};
}
// 判断:餐厅关联的商户是否重复
$merIds = array_filter(array_column($childrenList,'mer_id'));
if(count($merIds) > 0){
// 判断:是否存在重复
if(count($merIds) != count(array_unique($merIds))) throw new ValidateException('每个商户只能关联一个餐厅,请勿重复关联!');
// 判断:当前关联商户中,是否存在其他已经被关联的商户
$ids = array_filter(array_column($childrenList,'id'));
$isHas = (int)app()->make(AgentRepository::class)
->getSearchModel([])
->whereNotIn('id',$ids)
->whereIn('mer_id',$merIds)
->count();
if($isHas > 0) throw new ValidateException('每个商户只能关联一个餐厅,请勿重复关联!');
}
// 判断:配送商关联的商户是否重复
$merIdList = array_filter(array_column($childrenList,'mer_id_list'));
if(count($merIdList) > 0){
// 判断:是否存在重复
$newMerIdList = call_user_func_array('array_merge', $merIdList);
if(count($newMerIdList) != count(array_unique($newMerIdList))) throw new ValidateException('每个商户只能关联一个配送商,请勿重复关联!');
// 判断:当前关联商户中,是否存在其他已经被关联的商户
$ids = array_filter(array_column($childrenList,'id'));
$isHas = (int)app()->make(MerchantRepository::class)
->getSearch([])
->hasWhere('agent', function ($query) {
$query->where('is_del', 0);
})
->whereNotIn('Merchant.agent_id',$ids)
->whereIn('Merchant.mer_id',$newMerIdList)
->count();
if($isHas > 0) throw new ValidateException('每个商户只能关联一个配送商,请勿重复关联!');
}
// 判断agentId 大于0验证data的信息
if($agentId > 0){
if(empty($data['contact_name'])) throw new ValidateException('请输入联系人姓名!');
if(empty($data['contact_phone'])) throw new ValidateException('请输入联系人电话!');
if(isPhone($data['contact_phone'])) throw new ValidateException('请输入正确的联系人电话!');
// 判断:根据当前角色判断 数据
switch((int)$data['agent_type']){
case 2:
if(empty($data['province_corporate_name'])) throw new ValidateException('请输入省公司名称!');
break;
case 3:
case 4:
// if((float)$data['province_id'] <= 0) throw new ValidateException('请完善地区信息!');
break;
case 5:
case 6:
if((float)$data['province_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$data['city_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$data['area_id'] <= 0) throw new ValidateException('请完善地区信息!');
break;
case 7:
if((float)$data['province_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$data['city_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$data['area_id'] <= 0) throw new ValidateException('请完善地区信息!');
if((float)$data['street_id'] <= 0) throw new ValidateException('请完善地区信息!');
if(empty($data['address'])) throw new ValidateException('请完善地区信息!');
if(((float)$data['lat'] <= 0 || (float)$data['lng'] <= 0)) throw new ValidateException('请选择定位信息!');
if((float)$data['mer_id'] <= 0) throw new ValidateException('餐厅必须关联商户!');
break;
case 8:
if(count($data['mer_id_list']) <= 0) throw new ValidateException('配送商至少关联一个商户!');
break;
}
// 判断:餐厅关联的商户是否重复
if((int)$data['mer_id'] > 0){
// 判断:当前关联商户中,是否存在其他已经被关联的商户
$isHas = (int)app()->make(AgentRepository::class)
->getSearchModel([])
->where('id','<>',$agentId)
->where('mer_id',(int)$data['mer_id'])
->count();
if($isHas > 0) throw new ValidateException('每个商户只能关联一个餐厅,请勿重复关联!');
}
// 判断:配送商关联的商户是否重复
$merIdList = is_array($data['mer_id_list']) ? $data['mer_id_list'] : [];
if(count($merIdList) > 0){
// 判断:当前关联商户中,是否存在其他已经被关联的商户
$isHas = (int)app()->make(MerchantRepository::class)
->getSearch([])
->hasWhere('agent', function ($query) {
$query->where('is_del', 0);
})
->where('Merchant.agent_id','<>',$agentId)
->whereIn('Merchant.mer_id',$data['mer_id_list'])
->count();
if($isHas > 0) throw new ValidateException('每个商户只能关联一个配送商,请勿重复关联!');
}
}
return compact("data", "agentId", "childrenList");
}
/**
* Common: 获取编辑信息
* Author: wu-hui
* Time: 2024/01/24 18:25
* @return mixed
*/
public function getEditInfo(){
// 参数获取
$params = $this->request->params(['agent_id']);
$data = app()->make(AgentRepository::class)->getEditInfo((array)$params);
return app('json')->success($data);
}
/**
* Common: 申请列表获取
* Author: wu-hui
* Time: 2024/02/01 15:48
* @return mixed
*/
public function applyList(){
$params = $this->request->params(['uid','agent_type','contact_name','contact_phone','mer_name','status']);
[$page, $limit] = $this->getPage();
$data = app()->make(AgentApplyRepository::class)->getList($params, $page, $limit);
return app('json')->success($data);
}
/**
* Common: 审核
* Author: wu-hui
* Time: 2024/02/01 18:00
* @return mixed
*/
public function toExamine(){
// 参数获取
$params = $this->request->params(['id','status','reason']);
if($params['id'] <= 0) throw new ValidateException('方法请求,信息不明确!');
// 审核通过 主动生成账号
if((int)$params['status'] === 1){
// 审核通过
app()->make(AgentApplyRepository::class)->toExaminePass((array)$params);
}else{
// 驳回
app()->make(AgentApplyRepository::class)->update($params['id'],['status'=>$params['status'],'reason'=>$params['reason']]);
}
return app('json')->success('操作成功');
}
/**
* Common: 配置信息 - 修改配置信息
* Author: wu-hui
* Time: 2024/01/31 14:15
* @return mixed
*/
public function setConfig(){
$config = $this->request->params([
['delivery_money',0],
['delivery_money_field_staff',0],
['delivery_money_initiator',0],
['delivery_money_operator',0],
['delivery_money_partner',0],
['delivery_money_platform',0],
['delivery_money_province',0],
['delivery_process',0],
['field_staff_money',0],
['field_staff_money_initiator',0],
['field_staff_money_platform',0],
['field_staff_money_province',0],
['field_staff_process',0],
['internal_staff_money',0],
['internal_staff_money_initiator',0],
['internal_staff_money_platform',0],
['internal_staff_money_province',0],
['internal_staff_process',0],
['mer_money',0],
['mer_money_field_staff',0],
['mer_money_initiator',0],
['mer_money_operator',0],
['mer_money_partner',0],
['mer_money_platform',0],
['mer_money_province',0],
['mer_process',0],
['operator_money',0],
['operator_money_field_staff',0],
['operator_money_initiator',0],
['operator_money_platform',0],
['operator_money_province',0],
['operator_process',0],
['partner_money',0],
['partner_money_field_staff',0],
['partner_money_initiator',0],
['partner_money_operator',0],
['partner_money_platform',0],
['partner_money_province',0],
['partner_process',0],
['province_money',0],
['province_money_initiator',0],
['province_money_platform',0],
['province_process',0],
// 总部外勤佣金
['province_money_field_personnel',0],
['field_staff_money_field_personnel',0],
['internal_staff_money_field_personnel',0],
['operator_money_field_personnel',0],
['partner_money_field_personnel',0],
['mer_money_field_personnel',0],
['delivery_money_field_personnel',0],
['field_personnel_money',0],
['field_personnel_process',0],
['field_personnel_money_platform',0],
['field_personnel_money_initiator',0],
['external_personnel_money',0],
['external_personnel_process',0],
['external_personnel_money_platform',0],
['external_personnel_money_initiator',0],
['invite_limit',[]],
]);
// 保存信息
$cid = app()->make(ConfigClassifyRepository::class)->getConfigClassifyKeyById('agent_config', '代理中心配置');
if (!$cid) return app('json')->fail('保存失败');
app()->make(ConfigValueRepository::class)->setFormData($config,$this->request->merId());
return app('json')->success('保存成功');
}
/**
* Common: 配置信息 - 获取配置信息
* Author: wu-hui
* Time: 2024/01/31 14:32
* @return mixed
*/
public function getConfig(){
$config = app()->make(AgentRepository::class)->getConfig();
return app('json')->success($config);
}
/**
* Common: 佣金列表
* Author: wu-hui
* Time: 2024/01/26 20:36
* @return mixed
*/
public function commissionList(){
[$page, $limit] = $this->getPage();
$params = $this->request->params(['staff_uid','store_uid','store_uid','area_store_uid','delivery_uid','province_uid']);
$data = app()->make(AgentBrokerageRepository::class)->getList((array)$params,(int)$page,(int)$limit);
return app('json')->success($data);
}
}