395 lines
14 KiB
PHP
395 lines
14 KiB
PHP
<?php
|
||
|
||
namespace app\common\repositories\marketing;
|
||
|
||
use app\common\dao\marketing\AgentDao;
|
||
use app\common\model\marketing\Agent;
|
||
use app\common\model\system\merchant\Merchant;
|
||
use app\common\repositories\BaseRepository;
|
||
use crmeb\services\QrcodeService;
|
||
use think\exception\ValidateException;
|
||
use think\facade\Db;
|
||
|
||
class AgentRepository extends BaseRepository{
|
||
|
||
protected $dao;
|
||
|
||
public function __construct(AgentDao $dao){
|
||
$this->dao = $dao;
|
||
}
|
||
/**
|
||
* Common: 公共查询模型
|
||
* Author: wu-hui
|
||
* Time: 2024/02/01 15:01
|
||
* @param $search
|
||
* @return Agent
|
||
*/
|
||
public function getSearchModel($search){
|
||
return $this->dao->searchList($search);
|
||
}
|
||
|
||
/**
|
||
* Common: 获取信息列表
|
||
* Author: wu-hui
|
||
* Time: 2024/01/24 15:41
|
||
* @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/01/29 16:00
|
||
* @param array $params
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getAllList(array $params):array{
|
||
$query = $this->dao->searchList($params);
|
||
return $query->select()->toArray();
|
||
}
|
||
/**
|
||
* Common: 获取单条代理人员信息
|
||
* Author: wu-hui
|
||
* Time: 2024/01/30 9:27
|
||
* @param $id
|
||
* @return array
|
||
*/
|
||
public function getSingleInfo($id){
|
||
return $this->dao->searchList(['id'=>$id])->findOrEmpty()->toArray();
|
||
}
|
||
/**
|
||
* Common: 生成对应的二维码
|
||
* Author: wu-hui
|
||
* Time: 2024/01/30 18:39
|
||
* @param array $params
|
||
* @return mixed
|
||
*/
|
||
public function createQrCode(array $params){
|
||
// 参数获取
|
||
switch($params['type']){
|
||
case 'supplier':
|
||
$valueData = 'agent_id=' . $params['agent_id'];
|
||
$path = 'pages/supplier/apply/apply_join';
|
||
return app()->make(QrcodeService::class)->createQrCode($valueData,$path);
|
||
break;
|
||
case 'subordinate':
|
||
// 参数长度超过 简写:lv=level;agent_id=aid
|
||
$valueData = 'lv=' . $params['level'] . '&aid=' . $params['agent_id'];
|
||
$path = 'pages/agent/invite/index';
|
||
return app()->make(QrcodeService::class)->createQrCode($valueData,$path);
|
||
break;
|
||
}
|
||
|
||
throw new ValidateException('小程序码生成失败!');
|
||
}
|
||
/**
|
||
* Common: 获取用户指定身份列表
|
||
* Author: wu-hui
|
||
* Time: 2024/02/19 17:47
|
||
* @param int $uid
|
||
* @param string $type
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getIdentityList(int $uid,string $type = 'headquarters'):array{
|
||
// 根据身份类型,获取代理中心角色类型数组:headquarters=总部,province=省公司,county=区县
|
||
// 类型:1=总部发起人,2=省公司发起人,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商,9=总部外勤,10=总部内勤
|
||
switch($type){
|
||
case 'headquarters':
|
||
$agentType = [1,9,10];
|
||
break;
|
||
case 'province':
|
||
$agentType = [2,3,4];
|
||
break;
|
||
case 'county':
|
||
$agentType = [5,6,7,8];
|
||
break;
|
||
default:
|
||
throw new ValidateException('身份查询失败!');
|
||
}
|
||
// 获取对应的角色身份列表
|
||
return $this->dao->searchList(['uid'=>$uid])
|
||
->whereIn('agent_type', $agentType)
|
||
->whereNotIn('agent_type', [4,7,8,10])// 忽略无管理菜单的角色
|
||
->field(['id','uid','agent_type','province_id','city_id','area_id','street_id','address','mer_id','contact_name'])
|
||
->select()
|
||
->toArray();
|
||
}
|
||
|
||
|
||
/**
|
||
* Common: 代理添加/编辑
|
||
* Author: wu-hui
|
||
* Time: 2024/01/25 9:03
|
||
* @param int $agentId
|
||
* @param array $data
|
||
* @param array $childrenList
|
||
* @return mixed
|
||
*/
|
||
public function editInfo(int $agentId, array $data, array $childrenList){
|
||
return Db::transaction(function() use ($agentId, $data, $childrenList){
|
||
// 处理当前角色用户信息
|
||
if($agentId > 0) $this->currentRoleHandle($agentId, $data);
|
||
// 处理子类信息
|
||
$this->childrenListHandle($agentId, $childrenList);
|
||
});
|
||
}
|
||
/**
|
||
* Common: 代理添加/编辑 —— 处理当前角色信息
|
||
* Author: wu-hui
|
||
* Time: 2024/01/25 9:03
|
||
* @param int $agentId
|
||
* @param array $data
|
||
*/
|
||
private function currentRoleHandle(int $agentId,array $data){
|
||
// 修改基本信息
|
||
$keys = array_flip((array)[
|
||
"uid",
|
||
"pid",
|
||
"agent_type",
|
||
"agent_stock",
|
||
"contact_name",
|
||
"contact_phone",
|
||
"province_id",
|
||
"city_id",
|
||
"area_id",
|
||
"street_id",
|
||
"address",
|
||
"lat",
|
||
"lng",
|
||
"mer_id",
|
||
'is_headquarters'
|
||
]);
|
||
$updateInfo = array_intersect_key($data, $keys);
|
||
Agent::update($updateInfo,['id'=>$agentId]);
|
||
// 修改配送商 - 商户关联信息
|
||
$merIdList = is_array($data['mer_id_list']) ? $data['mer_id_list'] : [];
|
||
if(count($merIdList) > 0){
|
||
// 删除已经存在的关联信息
|
||
Merchant::update(['agent_id' => null],['agent_id'=>$agentId]);
|
||
// 建立新的关联信息
|
||
Merchant::whereIn('mer_id',$data['mer_id_list'])->update(['agent_id' => $agentId]);
|
||
}
|
||
}
|
||
/**
|
||
* Common: 代理添加/编辑 —— 处理子类信息
|
||
* Author: wu-hui
|
||
* Time: 2024/01/25 17:45
|
||
* @param int $agentId
|
||
* @param array $childrenList
|
||
* @throws \think\db\exception\DbException
|
||
*/
|
||
private function childrenListHandle(int $agentId,array $childrenList){
|
||
$insertData = [];
|
||
$updateData = [];
|
||
$merUpdateData = [];
|
||
$keys = array_flip((array)[
|
||
"id",
|
||
"uid",
|
||
"pid",
|
||
"agent_type",
|
||
"agent_stock",
|
||
"contact_name",
|
||
"contact_phone",
|
||
"province_id",
|
||
"city_id",
|
||
"area_id",
|
||
"street_id",
|
||
"address",
|
||
"lat",
|
||
"lng",
|
||
"mer_id",
|
||
'is_headquarters'
|
||
]);
|
||
// 循环:区分对应的操作
|
||
foreach($childrenList as $childrenItem){
|
||
unset($childrenItem['user']);
|
||
$childrenItemId = $childrenItem['id'] ?? 0;
|
||
$handleData = array_intersect_key($childrenItem, $keys);
|
||
$handleData['pid'] = $agentId;
|
||
// 判断:如果为配送商 处理商户关联信息
|
||
if($childrenItem['agent_type'] == 8){
|
||
// 判断:应该修改还是编辑
|
||
if((int)$childrenItemId > 0) $this->dao->update($childrenItemId,$handleData);
|
||
else $childrenItemId = Agent::insertGetId($handleData);
|
||
// 处理商户关联信息
|
||
foreach($childrenItem['mer_id_list'] as $merId){
|
||
$merUpdateData[] = [
|
||
'mer_id' => $merId,
|
||
'agent_id' => $childrenItemId
|
||
];
|
||
}
|
||
}else{
|
||
// 判断:应该修改还是编辑
|
||
if((int)$childrenItemId > 0) $updateData[] = $handleData;
|
||
else $insertData[] = $handleData;
|
||
}
|
||
}
|
||
// 获取需要删除的数据的id 删除思路:先获取总数据、对修改数据比较、删除缺少的项
|
||
$allIds = $this->dao->searchList([])->where('pid',$agentId)->column('id');
|
||
$updateIds = array_column($updateData,'id');
|
||
$delIds = array_diff($allIds,$updateIds);
|
||
|
||
// 处理结果 进行对应的操作;必须按照先删除、在修改、最后添加的顺序进行
|
||
if(count($delIds) > 0) Agent::whereIn('id',$delIds)->update(['is_del'=>1]);
|
||
if(count($updateData) > 0) Agent::batchUpdate(array_values($updateData));
|
||
if(count($insertData) > 0) $this->dao->insertAll($insertData);
|
||
if(count($merUpdateData) > 0) {
|
||
// 删除已经存在的关联信息
|
||
Merchant::whereIn('agent_id',array_column($childrenList,'id'))->update(['agent_id' => null]);
|
||
// 建立新的关联信息
|
||
Merchant::batchUpdate(array_values($merUpdateData),'mer_id','mer_id');
|
||
}
|
||
}
|
||
/**
|
||
* Common: 代理添加/编辑 —— 获取需要编辑的信息
|
||
* Author: wu-hui
|
||
* Time: 2024/01/24 16:57
|
||
* @param $params
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getEditInfo($params){
|
||
// 获取当前信息 编辑类型:0=发起人管理,1=发起人,2=省公司,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商
|
||
if((int)$params['agent_id'] > 0) {
|
||
// 非发起人管理:存在info及children_list
|
||
$info = $this->dao->searchList(['id' => $params['agent_id']])->findOrEmpty();
|
||
$childrenList = $this->dao
|
||
->searchList(['pid' => $params['agent_id']])
|
||
->select()
|
||
->toArray();
|
||
}
|
||
else{
|
||
// 发起人管理:仅存在children_list
|
||
$childrenList = $this->dao
|
||
->searchList(['pid' => 0,'agent_type' => 1])
|
||
->select()
|
||
->toArray();
|
||
}
|
||
// 信息处理及返回
|
||
$childrenList = array_column($childrenList,null,'only_key');
|
||
|
||
return [
|
||
'info' => $info ?? [],
|
||
'children_list' => $childrenList
|
||
];
|
||
}
|
||
/**
|
||
* Common: 获取配置
|
||
* Author: wu-hui
|
||
* Time: 2024/01/31 14:43
|
||
* @return mixed
|
||
*/
|
||
public function getConfig(){
|
||
$config = systemConfig([
|
||
'delivery_money',
|
||
'delivery_money_field_staff',
|
||
'delivery_money_initiator',
|
||
'delivery_money_operator',
|
||
'delivery_money_partner',
|
||
'delivery_money_platform',
|
||
'delivery_money_province',
|
||
'delivery_process',
|
||
'field_staff_money',
|
||
'field_staff_money_initiator',
|
||
'field_staff_money_platform',
|
||
'field_staff_money_province',
|
||
'field_staff_process',
|
||
'internal_staff_money',
|
||
'internal_staff_money_initiator',
|
||
'internal_staff_money_platform',
|
||
'internal_staff_money_province',
|
||
'internal_staff_process',
|
||
'mer_money',
|
||
'mer_money_field_staff',
|
||
'mer_money_initiator',
|
||
'mer_money_operator',
|
||
'mer_money_partner',
|
||
'mer_money_platform',
|
||
'mer_money_province',
|
||
'mer_process',
|
||
'operator_money',
|
||
'operator_money_field_staff',
|
||
'operator_money_initiator',
|
||
'operator_money_platform',
|
||
'operator_money_province',
|
||
'operator_process',
|
||
'partner_money',
|
||
'partner_money_field_staff',
|
||
'partner_money_initiator',
|
||
'partner_money_operator',
|
||
'partner_money_platform',
|
||
'partner_money_province',
|
||
'partner_process',
|
||
'province_money',
|
||
'province_money_initiator',
|
||
'province_money_platform',
|
||
'province_process',
|
||
// 总部外勤相关
|
||
'province_money_field_personnel',
|
||
'field_staff_money_field_personnel',
|
||
'internal_staff_money_field_personnel',
|
||
'operator_money_field_personnel',
|
||
'partner_money_field_personnel',
|
||
'mer_money_field_personnel',
|
||
'delivery_money_field_personnel',
|
||
'field_personnel_money',
|
||
'field_personnel_process',
|
||
'field_personnel_money_platform',
|
||
'field_personnel_money_initiator',
|
||
'external_personnel_money',
|
||
'external_personnel_process',
|
||
'external_personnel_money_platform',
|
||
'external_personnel_money_initiator',
|
||
]);
|
||
$config['delivery_process'] = (int)$config['delivery_process'];
|
||
$config['field_staff_process'] = (int)$config['field_staff_process'];
|
||
$config['internal_staff_process'] = (int)$config['internal_staff_process'];
|
||
$config['mer_process'] = (int)$config['mer_process'];
|
||
$config['operator_process'] = (int)$config['operator_process'];
|
||
$config['partner_process'] = (int)$config['partner_process'];
|
||
$config['province_process'] = (int)$config['province_process'];
|
||
$config['field_personnel_process'] = (int)$config['field_personnel_process'];
|
||
$config['external_personnel_process'] = (int)$config['external_personnel_process'];
|
||
|
||
return $config;
|
||
}
|
||
/**
|
||
* Common: 获取全部的上级信息
|
||
* Author: wu-hui
|
||
* Time: 2024/02/02 13:52
|
||
* @param $pid
|
||
* @param array $data
|
||
* @return array|mixed
|
||
*/
|
||
public function getAllUp($pid,$data = []){
|
||
$info = $this->getSingleInfo($pid);
|
||
if($info['pid'] > 0) $data = $this->getAllUp($info['pid'], $data);
|
||
else $data[] = $info;
|
||
|
||
|
||
return $data;
|
||
}
|
||
|
||
|
||
|
||
}
|