247 lines
8.5 KiB
PHP
247 lines
8.5 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/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/29 17:40
|
||
* @param array $data
|
||
* @param string $type
|
||
* @return mixed
|
||
*/
|
||
public function createQrCode(array $data,string $type = 'inviteSupplier'){
|
||
// 参数获取
|
||
switch($type){
|
||
case 'inviteSupplier':
|
||
$valueData = 'agent_id=' . $data['agent_id'];
|
||
$path = 'pages/supplier/apply_join';
|
||
return app()->make(QrcodeService::class)->createQrCode($valueData,$path);
|
||
break;
|
||
}
|
||
|
||
throw new ValidateException('小程序码生成失败!');
|
||
}
|
||
|
||
|
||
/**
|
||
* 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);
|
||
// 处理子类信息
|
||
if(count($childrenList) > 0) $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"
|
||
]);
|
||
$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"
|
||
]);
|
||
// 循环:区分对应的操作
|
||
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)->where('agent_type','<>',8)->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
|
||
];
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|