diff --git a/app/common/dao/system/admin/PartnerDao.php b/app/common/dao/system/admin/PartnerDao.php index a633b3c..1bfe22b 100644 --- a/app/common/dao/system/admin/PartnerDao.php +++ b/app/common/dao/system/admin/PartnerDao.php @@ -59,6 +59,7 @@ class PartnerDao extends BaseDao public function existsUser(int $id) { return UserPartner::getInstance()->where('partner_id', $id) + ->where('uid','<>','') ->field(['id']) ->find(); } diff --git a/app/common/repositories/system/admin/PartnerRepository.php b/app/common/repositories/system/admin/PartnerRepository.php index 7a96764..6ffa04d 100644 --- a/app/common/repositories/system/admin/PartnerRepository.php +++ b/app/common/repositories/system/admin/PartnerRepository.php @@ -18,6 +18,7 @@ namespace app\common\repositories\system\admin; use app\common\dao\system\admin\PartnerDao; use app\common\model\system\admin\Partner; use app\common\repositories\BaseRepository; +use app\common\model\user\UserPartner; use app\common\repositories\system\auth\RoleRepository; use crmeb\exceptions\AuthException; use crmeb\services\JwtTokenService; @@ -30,6 +31,7 @@ use think\db\exception\ModelNotFoundException; use think\exception\ValidateException; use think\facade\Cache; use think\facade\Config; +use think\facade\Db; use think\facade\Route; use think\Model; @@ -70,7 +72,21 @@ class PartnerRepository extends BaseRepository public function create(array $data) { - return $this->dao->create($data); + return Db::transaction(function () use ($data) { + //生成角色 + $info = $this->dao->create($data); + $userPartner = []; + for ($i = 1;$i <= $data['max_role_num']; $i++ ) { + $name = $i == 1 ? $data['name'] : $data['name'] . '_' . $i; + $userPartner[] = [ + 'partner_name' => $name, + 'partner_id' => $info->id, + ]; + } + UserPartner::getDB()->insertAll($userPartner); + + }); + } /** @@ -100,12 +116,22 @@ class PartnerRepository extends BaseRepository */ public function update(int $id, array $data) { + + $old = $this->dao->get($id)->toArray(); + if($old['name'] != $data['name']){ + $list = UserPartner::getDB()->where('partner_id',$id)->select(); + foreach($list as $k=>$v){ + $name = str_replace($old['name'],$data['name'],$v['partner_name']); + UserPartner::getDB()->where('id', $v['id'])->update(['partner_name' => $name]); + } + } return $this->dao->update($id, $data); } //删除 public function delete(int $id) { + UserPartner::getDB()->where('partner_id', $id)->delete(); return $this->dao->delete($id); } @@ -116,7 +142,10 @@ class PartnerRepository extends BaseRepository } - + public function roleExists(int $id) + { + return UserPartner::getDB()->where('id', '=', $id)->where('uid','=','')->find(); + } } diff --git a/app/common/repositories/user/UserPartnerRepository.php b/app/common/repositories/user/UserPartnerRepository.php index 4a4d278..cb9d52a 100644 --- a/app/common/repositories/user/UserPartnerRepository.php +++ b/app/common/repositories/user/UserPartnerRepository.php @@ -75,8 +75,8 @@ class UserPartnerRepository extends BaseRepository /** @var UserLabelRepository $make */ $list = $this->dao->getAll($uid)->column('partner_id') ?? []; $data = ['partner_id' => $list, 'uid' => $uid]; - $url = 'sys/user/partner/'.$uid; - $res = Elm::createForm($url, [ + $url = 'sys/user/partner/' . $uid; + $res = Elm::createForm($url, [ Elm::input('uid', '用户 ID', '')->disabled(true)->required(true), Elm::selectMultiple('partner_id', '用户合作人角色')->options(function () { $partnerForm = new PartnerDao(); @@ -92,18 +92,17 @@ class UserPartnerRepository extends BaseRepository return $res; } - public function editPartner(int $uid, array $partner_id = []) + public function editPartner(int $uid, array $user_role_id = []) { - return Db::transaction(function () use ($uid, $partner_id) { + return Db::transaction(function () use ($uid, $user_role_id) { //先清空 - UserPartner::getInstance()->where('uid', $uid)->delete(); - //再插入 - if (!empty($partner_id)) { - $data = []; - foreach ($partner_id as $k => $v) { - $data[] = ['uid' => $uid, 'partner_id' => $v, 'create_time' => date("Y-m-d H:i:s")]; + UserPartner::getInstance()->where('uid', $uid)->update(['partner_id' => '']); + //再更新 + if (!empty($user_role_id)) { + foreach ($user_role_id as $k => $v) { + UserPartner::getInstance()->where('id', '=', $v) + ->update(['uid' => $uid, 'create_time' => date("Y-m-d H:i:s")]); } - UserPartner::getInstance()->insertAll($data); } }); } diff --git a/app/controller/admin/system/admin/Partner.php b/app/controller/admin/system/admin/Partner.php index 06622b1..21a9380 100644 --- a/app/controller/admin/system/admin/Partner.php +++ b/app/controller/admin/system/admin/Partner.php @@ -49,12 +49,16 @@ class Partner extends BaseController public function add(PartnerRoleValidate $validate) { $data = $this->request->params(['name', 'ratio', 'is_area', 'area_level', - 'area_id', ['status', 1], ['type', 1], ['max_role_num', 0], 'brand_id', 'trade_id']); + 'area_id', ['status', 1], ['type', 1], ['max_role_num', 1], 'brand_id', 'trade_id', 'region_id']); $validate->check($data); if ($data['is_area'] == 1 && $this->repository->fieldExists('area_id', $data['area_id'])) { return app('json')->fail('该区域已经有了代理角色,请勿重复添加'); } - if (($data['type'] == 3 && empty($data['brand_id']) || ($data['type'] == 4 && empty($data['trade_id'])))) { + if ( + ($data['type'] == 3 && empty($data['brand_id'])) + || ($data['type'] == 4 && empty($data['trade_id'])) + || ($data['type'] == 2 && empty($data['region_id'])) + ) { return app('json')->fail('请补全对应角色类型的关联数据'); } $this->repository->create($data); @@ -65,7 +69,7 @@ class Partner extends BaseController { $data = $this->request->params(['name', 'ratio', 'is_area', 'area_level', - 'area_id', ['status', 1], ['type', 1], ['max_role_num', 0], 'brand_id', 'trade_id','id']); + 'area_id', ['status', 1], ['type', 1], 'brand_id', 'trade_id', 'id', 'region_id']); $validate->check($data); if ($data['is_area'] == 1) { @@ -79,7 +83,11 @@ class Partner extends BaseController $data['area_level'] = 0; $data['area_id'] = 0; } - if (($data['type'] == 3 && empty($data['brand_id']) || ($data['type'] == 4 && empty($data['trade_id'])))) { + if ( + ($data['type'] == 3 && empty($data['brand_id'])) + || ($data['type'] == 4 && empty($data['trade_id'])) + || ($data['type'] == 2 && empty($data['region_id'])) + ) { return app('json')->fail('请补全对应角色类型的关联数据'); } @@ -105,4 +113,10 @@ class Partner extends BaseController $this->repository->delete($id); return app('json')->success('删除成功'); } + + public function getArea() + { + $data = config('partner.region_type'); + return app('json')->success($data); + } } diff --git a/app/controller/admin/user/User.php b/app/controller/admin/user/User.php index 13540bf..4c4e2d2 100644 --- a/app/controller/admin/user/User.php +++ b/app/controller/admin/user/User.php @@ -91,7 +91,7 @@ class User extends BaseController 'group_id', 'phone', 'uid', - ]); + ]); [$page, $limit] = $this->getPage(); return app('json')->success($this->repository->getList($where, $page, $limit)); } @@ -142,7 +142,7 @@ class User extends BaseController $data['pwd'] = $this->repository->encodePassword($data['repwd']); unset($data['repwd']); if ($data['is_promoter']) $data['promoter_time'] = date('Y-m-d H:i:s'); - $this->repository->create('h5',$data); + $this->repository->create('h5', $data); return app('json')->success('添加成功'); } @@ -164,7 +164,7 @@ class User extends BaseController return app('json')->fail('密码不一致'); $data['pwd'] = $this->repository->encodePassword($data['repwd']); unset($data['repwd']); - $this->repository->update($id,$data); + $this->repository->update($id, $data); return app('json')->success('修改成功'); } @@ -219,7 +219,7 @@ class User extends BaseController $data['label_id'] = implode(',', $label_id); if ($data['is_promoter']) $data['promoter_time'] = date('Y-m-d H:i:s'); - if(!$data['birthday']) unset($data['birthday']); + if (!$data['birthday']) unset($data['birthday']); $this->repository->update($id, $data); @@ -562,7 +562,7 @@ class User extends BaseController public function memberForm($id) { - return app('json')->success(formToData($this->repository->memberForm($id,1))); + return app('json')->success(formToData($this->repository->memberForm($id, 1))); } public function memberSave($id) @@ -576,7 +576,7 @@ class User extends BaseController public function spreadLevelForm($id) { - return app('json')->success(formToData($this->repository->memberForm($id,0))); + return app('json')->success(formToData($this->repository->memberForm($id, 0))); } public function spreadLevelSave($id) @@ -595,8 +595,8 @@ class User extends BaseController public function svipUpdate($id) { - $data = $this->request->params(['is_svip','add_time','type']); - $this->repository->svipUpdate($id, $data,$this->request->adminId()); + $data = $this->request->params(['is_svip', 'add_time', 'type']); + $this->repository->svipUpdate($id, $data, $this->request->adminId()); return app('json')->success('修改成功'); } @@ -608,18 +608,25 @@ class User extends BaseController return app('json')->success(formToData($partnerRepository->changePartnerForm($id))); } - public function changePartner(PartnerRepository $partnerRepository,UserPartnerRepository $userPartnerRepository) + public function changePartner(PartnerRepository $partnerRepository, UserPartnerRepository $userPartnerRepository) { $partner_id = (array)$this->request->param('partner_id', []); + $user_role_id = (array)$this->request->param('user_role_id', []); + $partner_ids = $user_role_ids = []; $id = (int)$this->request->param('uid', ''); if (!$this->repository->exists($id)) return app('json')->fail('数据不存在'); foreach ($partner_id as $k => $value) { - $partner_id[$k] = (int)$value; + $partner_ids[$k] = (int)$value; if (!$partnerRepository->exists((int)$value)) return app('json')->fail('所选合作人角色不存在'); } - $userPartnerRepository->editPartner($id, $partner_id); + foreach ($user_role_id as $k => $value) { + $user_role_id[$k] = (int)$value; + if (!$partnerRepository->roleExists((int)$value)) + return app('json')->fail('该位置已分配,请重新选择'); + } + $userPartnerRepository->editPartner($id, $user_role_id); return app('json')->success('修改成功'); } diff --git a/app/validate/admin/PartnerRoleValidate.php b/app/validate/admin/PartnerRoleValidate.php index 4431268..ce4d67c 100644 --- a/app/validate/admin/PartnerRoleValidate.php +++ b/app/validate/admin/PartnerRoleValidate.php @@ -25,5 +25,6 @@ class PartnerRoleValidate extends Validate 'ratio|比例' => 'require', 'status|启用状态' => 'require|in:1,2', 'type|角色类型' => 'require|in:1,2,3,4', + 'max_role_num|配置人数' => 'require|min:1' ]; } diff --git a/config/partner.php b/config/partner.php index 92b3727..2db7a0c 100644 --- a/config/partner.php +++ b/config/partner.php @@ -13,6 +13,8 @@ // | 控制台配置 // +---------------------------------------------------------------------- return [ + //贡献池系统用户id + 'gongxian_admin_id' => 9, // 社区等级配置 'community_level' => [ [ @@ -56,56 +58,42 @@ return [ 'pay_community' => '0.03', ], ], - //品牌代理商等级配置 - 'brand_level' => [ + //大区配置 + 'region_type' => [ [ - 'alias' => '全球品牌代言人', - 'level_name' => 'B7', - 'min_price' => '1000000', - 'pay_community' => '0.3', - 'promotion_community' => '0.1' + 'id' => 1, + 'name' => '华东地区', + 'province_ids' => [13, 12, 14, 15, 2, 16, 2768, 21], ], [ - 'alias' => '亚太区品牌代言人', - 'level_name' => 'B6', - 'min_price' => '500000', - 'pay_community' => '0.3', - 'promotion_community' => '0.1' + 'id' => 2, + 'name' => '华南地区', + 'province_ids' => [19, 20, 52993, 23], ], [ - 'alias' => '中国区品牌代言人', - 'level_name' => 'B5', - 'min_price' => '300000', - 'pay_community' => '0.3', - 'promotion_community' => '0.1' + 'id' => 3, + 'name' => '华北地区', + 'province_ids' => [1, 3, 5, 6, 11], ], [ - 'alias' => '品牌形象大使', - 'level_name' => 'B4', - 'min_price' => '100000', - 'pay_community' => '0.3', - 'promotion_community' => '0.1' + 'id' => 4, + 'name' => '华中地区', + 'province_ids' => [7, 17, 18], ], [ - 'alias' => '品牌推广大使', - 'level_name' => 'B3', - 'min_price' => '50000', - 'pay_community' => '0.3', - 'promotion_community' => '0.1' + 'id' => 5, + 'name' => '西南地区', + 'province_ids' => [25, 22, 24, 26, 4], ], [ - 'alias' => '品牌推荐官', - 'level_name' => 'B2', - 'min_price' => '30000', - 'pay_community' => '0.3', - 'promotion_community' => '0.1' + 'id' => 6, + 'name' => '西北地区', + 'province_ids' => [27, 28, 29, 30, 31], ], [ - 'alias' => '品牌体验官', - 'level_name' => 'B1', - 'min_price' => '10000', - 'pay_community' => '0.3', - 'promotion_community' => '0.1' + 'id' => 7, + 'name' => '东北地区', + 'province_ids' => [8, 9, 10], ], ], ]; diff --git a/crmeb/jobs/OrderPartnerJob.php b/crmeb/jobs/OrderPartnerJob.php index 9e177a7..90c4098 100644 --- a/crmeb/jobs/OrderPartnerJob.php +++ b/crmeb/jobs/OrderPartnerJob.php @@ -34,12 +34,13 @@ class OrderPartnerJob implements JobInterface public function fire($job, $data) { - file_put_contents('/tmp/debug_partner', '开始处理贡献值订单:'.var_export($data, 1), 8); + file_put_contents('/tmp/debug_partner', '开始处理贡献值订单:' . var_export($data, 1), 8); try { //初始化贡献值 $bao_price = $mian_price = $ping_price = $total_price = 0; $bao_flowing = $mian_flowing = $ping_flowing = 0; $brand_ids = $trade_ids = $user_log_data = []; + $admin_id = config('partner.gongxian_admin_id') ?? ''; //查询商品类型 -报单区 100% 免单区66.7% 平价区66.7% $order_product = StoreOrderProduct::getDB() ->where('order_id', $data['order_id']) @@ -95,7 +96,6 @@ class OrderPartnerJob implements JobInterface throw new Exception('订单没有指定三种类型商品,不计入贡献值统计'); } //读取品牌所属分类 - if (!empty($brand_ids)) { $brand_ids = array_unique($brand_ids); $cat_ids = StoreBrand::getDB()->whereIn('brand_id', $brand_ids)->column('brand_category_id'); @@ -163,20 +163,20 @@ class OrderPartnerJob implements JobInterface $role_ids = array_column($brand_roles, 'id'); $brand_brand_data = array_column($brand_roles, null, 'id'); - $user_list = User::getDB()->alias('u') - ->leftJoin('UserPartner p', 'u.uid = p.uid') - ->whereIn('p.partner_id', $role_ids) - ->where('u.uid', '<>', $data['uid']) - ->field('u.*,p.partner_id') + $role_list = UserPartner::getDB() + ->whereIn('partner_id', $role_ids) + ->where('uid', '<>', $data['uid']) ->select()->toArray(); - $user_count = count($user_list); - foreach ($user_list as $buk => $buv) { + foreach ($role_list as $buk => $buv) { $role_data = $brand_brand_data[$buv['partner_id']] ?? []; if (!empty($role_data)) { $role_value = bcmul($total_price, $role_data['ratio'] / 100, 2); - $role_value = bcdiv($role_value, $user_count, 2); - $user = User::getDB()->where('uid', $buv['uid'])->find(); + if (empty($buv['uid'])) { + $user = User::getDB()->where('uid', $admin_id)->find();; + } else { + $user = User::getDB()->where('uid', $buv['uid'])->find(); + } $user->all_value = bcadd($user->all_value, $role_value, 2); $user->save(); $user_log_data[] = [ @@ -201,19 +201,20 @@ class OrderPartnerJob implements JobInterface $role_ids = array_column($trade_roles, 'id'); $trade_trade_data = array_column($trade_roles, null, 'id'); - $user_list = User::getDB()->alias('u') - ->leftJoin('UserPartner p', 'u.uid = p.uid') - ->whereIn('p.partner_id', $role_ids) - ->where('u.uid', '<>', $data['uid']) - ->field('u.*,p.partner_id') + $role_list = UserPartner::getDB() + ->whereIn('partner_id', $role_ids) + ->where('uid', '<>', $data['uid']) ->select()->toArray(); - $user_count = count($user_list); - foreach ($user_list as $tuk => $tuv) { + + foreach ($role_list as $tuk => $tuv) { $role_data = $trade_trade_data[$tuv['partner_id']] ?? []; if (!empty($role_data)) { $role_value = bcmul($total_price, $role_data['ratio'] / 100, 2); - $role_value = bcdiv($role_value, $user_count, 2); - $user = User::getDB()->where('uid', $tuv['uid'])->find(); + if (empty($tuv['uid'])) { + $user = User::getDB()->where('uid', $admin_id)->find();; + } else { + $user = User::getDB()->where('uid', $tuv['uid'])->find(); + } $user->all_value = bcadd($user->all_value, $role_value, 2); $user->save(); $user_log_data[] = [ @@ -245,19 +246,21 @@ class OrderPartnerJob implements JobInterface $role_ids = array_column($area_roles, 'id'); $trade_trade_data = array_column($area_roles, null, 'id'); - $user_list = User::getDB()->alias('u') - ->leftJoin('UserPartner p', 'u.uid = p.uid') - ->whereIn('p.partner_id', $role_ids) - ->where('u.uid', '<>', $data['uid']) - ->field('u.*,p.partner_id') + + $role_list = UserPartner::getDB() + ->whereIn('partner_id', $role_ids) + ->where('uid', '<>', $data['uid']) ->select()->toArray(); - $user_count = count($user_list); - foreach ($user_list as $auk => $auv) { + + foreach ($role_list as $auk => $auv) { $role_data = $trade_trade_data[$auv['partner_id']] ?? []; if (!empty($role_data)) { $role_value = bcmul($total_price, $role_data['ratio'] / 100, 2); - $role_value = bcdiv($role_value, $user_count, 2); - $user = User::getDB()->where('uid', $auv['uid'])->find(); + if (empty($auv['uid'])) { + $user = User::getDB()->where('uid', $admin_id)->find();; + } else { + $user = User::getDB()->where('uid', $auv['uid'])->find(); + } $user->all_value = bcadd($user->all_value, $role_value, 2); $user->save(); $user_log_data[] = [ @@ -271,6 +274,52 @@ class OrderPartnerJob implements JobInterface } } } + //查询大区角色 + $region_list = config('partner.region_type'); + $region_ids = []; + if (!empty($province_id) && !empty($region_list)) { + foreach ($region_list as $rv) { + if (in_array($province_id, $rv['province_ids'])) { + $region_ids[] = $rv['id']; + } + } + if(!empty($region_ids)){ + $region_roles = Partner::getDB() + ->where('status', 1) + ->whereNotIn('type', 2) + ->whereIn('region_id', $region_ids) + ->select()->toArray(); + $role_ids = array_column($region_roles, 'id'); + $regin_trade_data = array_column($region_roles, null, 'id'); + + $role_list = UserPartner::getDB() + ->whereIn('partner_id', $role_ids) + ->where('uid', '<>', $data['uid']) + ->select()->toArray(); + + foreach ($role_list as $ruk => $ruv) { + $role_data = $regin_trade_data[$ruv['partner_id']] ?? []; + if (!empty($role_data)) { + $role_value = bcmul($total_price, $role_data['ratio'] / 100, 2); + if (empty($ruv['uid'])) { + $user = User::getDB()->where('uid', $admin_id)->find();; + } else { + $user = User::getDB()->where('uid', $ruv['uid'])->find(); + } + $user->all_value = bcadd($user->all_value, $role_value, 2); + $user->save(); + $user_log_data[] = [ + 'type' => 6, + 'types_of' => 1, + 'num' => $role_value, + 'ctime' => date('Y-m-d H:i:s'), + 'memo' => '订单消费大区贡献值', + 'user_id' => $ruv['uid'], + ]; + } + } + } + } //查询通用角色 $common_roles = Partner::getDB() @@ -280,22 +329,22 @@ class OrderPartnerJob implements JobInterface ->select()->toArray(); $role_ids = array_column($common_roles, 'id'); $common_trade_data = array_column($common_roles, null, 'id'); - $user_list = User::getDB()->alias('u') - ->leftJoin('UserPartner p', 'u.uid = p.uid') - ->field('u.*,p.partner_id') - ->whereIn('p.partner_id', $role_ids) - ->where('u.uid', '<>', $data['uid']) + $role_list = UserPartner::getDB() + ->whereIn('partner_id', $role_ids) + ->where('uid', '<>', $data['uid']) ->select(); - if (!empty($user_list)) { - $user_list = $user_list->toArray(); - $user_count = count($user_list); - foreach ($user_list as $cuk => $cuv) { + if (!empty($role_list)) { + $role_list = $role_list->toArray(); + foreach ($role_list as $cuk => $cuv) { $role_data = $common_trade_data[$cuv['partner_id']] ?? []; if (!empty($role_data)) { $role_value = bcmul($total_price, $role_data['ratio'] / 100, 2); - $role_value = bcdiv($role_value, $user_count, 2); - $user = User::getDB()->where('uid', $cuv['uid'])->find(); + if (empty($cuv['uid'])) { + $user = User::getDB()->where('uid', $admin_id)->find();; + } else { + $user = User::getDB()->where('uid', $cuv['uid'])->find(); + } $user->all_value = bcadd($user->all_value, $role_value, 2); $user->save(); $user_log_data[] = [ @@ -310,7 +359,6 @@ class OrderPartnerJob implements JobInterface } } - //批量增加用户贡献值日志 ValueContributionLog::getDB()->insertAll($user_log_data); //社区等级判断 diff --git a/route/admin/role.php b/route/admin/role.php index 0172283..6ccb315 100644 --- a/route/admin/role.php +++ b/route/admin/role.php @@ -152,6 +152,9 @@ Route::group(function () { Route::get('delete/:id', '.Partner/delete')->name('systemPartnerDel')->option([ '_alias' => '删除合作人角色', ]); + Route::get('region', '.Partner/getArea')->name('systemPartnerArea')->option([ + '_alias' => '读取合作人大区', + ]); })->prefix('admin.system.admin')->option([ '_path' => 'self', '_auth' => true,