new-admin-api/app/common/repositories/marketing/AgentApplyRepository.php

320 lines
15 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\common\repositories\marketing;
use app\common\dao\marketing\AgentApplyDao;
use app\common\model\marketing\Agent;
use app\common\model\marketing\agent\AgentDelivery;
use app\common\model\marketing\AgentApply;
use app\common\model\user\User;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\order\StoreOrderCreateRepository;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\service\StoreServiceRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use crmeb\services\LockService;
use think\exception\ValidateException;
use think\facade\Db;
class AgentApplyRepository extends BaseRepository{
protected $dao;
public function __construct(AgentApplyDao $dao){
$this->dao = $dao;
}
/**
* Common: 公共查询模型
* Author: wu-hui
* Time: 2024/02/01 14:56
* @param $search
* @return \app\common\model\marketing\AgentApply
*/
public function getSearchModel($search){
return $this->dao->searchList($search);
}
/**
* Common: 列表获取
* Author: wu-hui
* Time: 2024/02/01 15:47
* @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 11:21
* @param $params
* @return mixed
*/
public function editApplyInfo($params){
return Db::transaction(function() use ($params){
// 生成申请信息
$applyInfoKeys = array_flip((array)[
"uid",
"pid",
"agent_type",
"contact_name",
"contact_phone",
"province_id",
"city_id",
"area_id",
"street_id",
"address",
"mer_name",
"mer_class_id",
"mer_type_id",
"mer_images",
'enterprise_name'
]);
$applyInfo = array_intersect_key($params['data'], $applyInfoKeys);
$applyInfo['mer_images'] = implode(',',$applyInfo['mer_images']);
// 支付信息
$payInfo = array_intersect_key($params['data'],array_flip((array)["pay_type","return_url"]));
$payResult = [];
// 根据操作类型进行对应的处理
if($params['agentApplyId'] > 0){
// 编辑
$applyInfo['status'] = 0;
$applyInfo['reason'] = '';
$this->dao->update($params['agentApplyId'], $applyInfo);
}
else{
// 添加 是否需要支付
$payMoney = 0;// 默认 无需支付
$config = app()->make(AgentRepository::class)->getConfig();
$agentBaseSet = $config['agent_base_set'] ?? [];
$currentSet = $agentBaseSet[$applyInfo['agent_type']] ?? [];
debug($currentSet);
switch ((int)$applyInfo['agent_type']) {
case 2: $payMoney = $config['province_money'] ?? 0;break;
case 3: $payMoney = $config['field_staff_money'] ?? 0;break;
case 4: $payMoney = $config['internal_staff_money'] ?? 0;break;
case 5: $payMoney = $config['operator_money'] ?? 0;break;
case 6: $payMoney = $config['partner_money'] ?? 0;break;
case 7: $payMoney = $config['mer_money'] ?? 0;break;
case 8: $payMoney = $config['delivery_money'] ?? 0;break;
case 9: $payMoney = $config['field_personnel_money'] ?? 0;break;
case 10: $payMoney = $config['external_personnel_money'] ?? 0;break;
}
// 生成申请信息
$applyInfoId = AgentApply::insertGetId($applyInfo);
// 判断:是否需要支付 需要支付生成订单并且获取支付信息
if((float)$payMoney > 0){
$userInfo = $params['user_info'] ?? [];
$payInfo['money'] = (float)$payMoney;
// 发起支付
$groupOrder = app()
->make(LockService::class)
->exec('online_order.create',function() use ($payInfo,$userInfo){
$payType = array_search($payInfo['pay_type'],StoreOrderRepository::PAY_TYPE);
return app()
->make(StoreOrderCreateRepository::class)
->onlinePayment($payType,$payInfo,$userInfo, 32);
});
// 子订单只存在一个 直接查询即可
$orderId = app()->make(StoreOrderRepository::class)
->getSearch([])
->where('group_order_id',$groupOrder->group_order_id)
->value('order_id');
AgentApply::update(['order_id'=>$orderId],['id'=>$applyInfoId]);
$payResult = app()
->make(StoreOrderRepository::class)
->pay($payInfo['pay_type'],$userInfo,$groupOrder,$payInfo['return_url'],$params['is_app']);
}
}
return $payResult;
});
}
/**
* Common: 审核通过
* Author: wu-hui
* Time: 2024/02/01 17:10
* @param $params
* @return mixed
*/
public function toExaminePass($params){
// 判断:上级邀请人员是否已经达到限制 类型1=总部发起人,2=省公司发起人,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商,9=总部外勤,10=总部内勤
$config = app()->make(AgentRepository::class)->getConfig();
$agentBaseSet = $config['agent_base_set'] ?? [];
$inviteLimit = $config['invite_limit'] ?? [];
$applyInfo = $this->getSearchModel(['id'=>$params['id']])->findOrEmpty()->toArray();
$giveTitleQuota = 0;// 赠送冠名品牌额度
$giveOtherQuota = 0;// 赠送其他品牌额度
switch($applyInfo['agent_type']){
case 2:
// 判断:当前上级招募 [省公司发起人] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>2])->count();
if(($inviteLimit['invite_9_2'] ?? 0) < $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_9_2'] ?? 0).' 人');
}
break;
case 3:
// 判断:当前上级招募 [省公司外勤] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>3])->count();
if(($inviteLimit['invite_2_3'] ?? 0) <= $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_2_3'] ?? 0).' 人');
}
break;
case 4:
// 判断:当前上级招募 [省公司内勤] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>4])->count();
if(($inviteLimit['invite_2_4'] ?? 0) <= $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_2_4'] ?? 0).' 人');
}
break;
case 5:
// 判断:当前上级招募 [区县运营商] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>5])->count();
if(($inviteLimit['invite_3_5'] ?? 0) <= $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_3_5'] ?? 0).' 人');
}
break;
case 6:
// 判断:当前上级招募 [区县合伙人] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>6])->count();
if(($inviteLimit['invite_5_6'] ?? 0) <= $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_5_6'] ?? 0).' 人');
}
break;
case 7:
// 判断:当前上级招募 [餐厅] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>7])->count();
if(($inviteLimit['invite_6_7'] ?? 0) <= $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_6_7'] ?? 0).' 家餐厅');
}
break;
case 8:
// 判断:当前上级招募 [配送商] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>8])->count();
if(($inviteLimit['invite_6_8'] ?? 0) <= $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_6_8'] ?? 0).' 家配送商');
}
// 赠送品牌额度
$currentInfo = $agentBaseSet[8] ?? [];
$giveTitleQuota = $currentInfo['title_quota'] ?? 0;
$giveOtherQuota = $currentInfo['other_quota'] ?? 0;
break;
case 9:
// 判断:当前上级招募 [总部外勤] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>9])->count();
if(($inviteLimit['invite_1_9'] ?? 0) <= $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_1_9'] ?? 0).' 人');
}
break;
case 10:
// 判断:当前上级招募 [总部内勤] 数量是否超出限制
$hasCount = app()->make(AgentRepository::class)->getSearchModel(['pid'=>$applyInfo['pid'],'agent_type'=>10])->count();
if(($inviteLimit['invite_1_10'] ?? 0) <= $hasCount) {
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_1_10'] ?? 0).' 人');
}
break;
}
// 通过处理
return Db::transaction(function() use ($params, $applyInfo, $giveTitleQuota, $giveOtherQuota){
// 修改状态
$this->dao->update($params['id'],['status'=>$params['status']]);
// 生成代理信息
$keys = array_flip((array)[
"uid",
"pid",
"agent_type",
"contact_name",
"contact_phone",
"province_id",
"city_id",
"area_id",
"street_id",
"address",
]);
$insertInfo = array_intersect_key($applyInfo, $keys);
$insertInfo['apply_id'] = $applyInfo['id'];
$agentId = Agent::insertGetId($insertInfo);
// 判断:如果是餐厅 生成商户信息
if((int)$insertInfo['agent_type'] == 7){
$password = substr($applyInfo['contact_phone'],-6);
$config = systemConfig(['broadcast_room_type', 'broadcast_goods_type']);
// 商户账号 默认随机生成
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$merAccount = substr(str_shuffle($characters), 0, 11);
$merchant = app()->make(MerchantRepository::class)->createMerchant([
'mer_name' => $applyInfo['mer_name'],
'mer_phone' => $applyInfo['contact_phone'],
'mer_account' => $merAccount,
'category_id' => $applyInfo['mer_class_id'],
'type_id' => $applyInfo['mer_type_id'],
'real_name' => $applyInfo['contact_name'],
'status' => 1,
'is_audit' => 1,
'is_bro_room' => $config['broadcast_room_type'] == 1 ? 0 : 1,
'is_bro_goods' => $config['broadcast_goods_type'] == 1 ? 0 : 1,
'mer_password' => $password,
'is_margin' => $margin['is_margin'] ?? -1,
'margin' => $margin['margin'] ?? 0
]);
// 关联商户信息
Agent::update(['mer_id'=>$merchant->mer_id],['id'=>$agentId]);
// 存在默认管理员信息 生成管理员
$staffUserInfo = User::where('uid', $applyInfo['uid'])->findOrEmpty()->toArray();
$staffData = [
'uid' => $applyInfo['uid'],
'nickname' => $staffUserInfo['nickname'] ?? $staffUserInfo['real_name'],
'account' => $applyInfo['contact_phone'],
'pwd' => $password,
'is_open' => 1,
'status' => 1,
'customer' => 1,
'is_verify' => 1,
'is_goods' => 1,
'is_user' => 1,
'staff_manage' => 1,
'notify' => 1,
'avatar' => $staffUserInfo['avatar'] ?? '',
'phone' => $applyInfo['contact_phone'],
'sort' => 1,
'mer_id' => $merchant->mer_id,
'is_manage' => 1
];
app()->make(StoreServiceRepository::class)->createInfo($staffData);
}
// 判断:如果是配送商 赠送相关品牌额度
if((float)$giveTitleQuota > 0 || (float)$giveOtherQuota > 0){
AgentDelivery::insert([
'agent_id' => $agentId,
'price' => $applyInfo['orderInfo']['pay_price'] ?? 0,
'title_quota' => (float)$giveTitleQuota,
'other_quota' => (float)$giveOtherQuota,
'order_id' => $applyInfo['order_id'] > 0 ? $applyInfo['order_id'] : NULL,
'status' => 1
]);
}
});
}
}