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

233 lines
11 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\AgentBrokerageRepository;
use app\common\repositories\marketing\AgentRepository;
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', []],
]);
$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');
}
// 循环判断:$childrenList 的数据是否完善 类型1=发起人,2=省公司,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商
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:
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;
}
}
// 判断:餐厅关联的商户是否重复
$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)
->getSearch([])
->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([])
->whereNotIn('agent_id',$ids)
->whereIn('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:
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)
->getSearch([])
->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([])
->where('agent_id','<>',$agentId)
->whereIn('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/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);
}
}