diff --git a/app/common/dao/system/admin/PartnerDao.php b/app/common/dao/system/admin/PartnerDao.php index ba7f240..171f476 100644 --- a/app/common/dao/system/admin/PartnerDao.php +++ b/app/common/dao/system/admin/PartnerDao.php @@ -17,6 +17,7 @@ namespace app\common\dao\system\admin; use app\common\dao\BaseDao; use app\common\model\BaseModel; use app\common\model\system\admin\Partner; +use app\common\model\user\UserPartner; use app\common\model\system\admin\Admin; use think\db\BaseQuery; use think\db\exception\DataNotFoundException; @@ -55,58 +56,12 @@ class PartnerDao extends BaseDao return $query; } - public function exists(int $id) + public function existsUser(int $id) { - $query = ($this->getModel())::getDB()->where($this->getPk(), $id)->where('is_del', 0); - return $query->count() > 0; - } - - - /** - * @param int $id - * @return array|Model|null - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020-04-09 - */ - public function get( $id) - { - return Admin::getInstance()->where('is_del', 0)->find($id); - } - - - /** - * @param $field - * @param $value - * @param int|null $except - * @return bool - * @author xaboy - * @day 2020-03-30 - */ - public function fieldExists($field, $value, ?int $except = null): bool - { - $query = ($this->getModel())::getDB()->where($field, $value)->where('is_del', 0); - if (!is_null($except)) $query->where($this->getPk(), '<>', $except); - return $query->count() > 0; - } - - /** - * @param string $account - * @return array|Model|null - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException - * @author xaboy - * @day 2020-04-09 - */ - public function accountByAdmin(string $account) - { - return Admin::getInstance()->where('account', $account) - ->where('is_del', 0) - ->field(['account', 'pwd', 'real_name', 'login_count', 'admin_id', 'status']) + return UserPartner::getInstance()->where('partner_id', $id) + ->field(['id']) ->find(); } + } diff --git a/app/common/model/user/UserPartner.php b/app/common/model/user/UserPartner.php new file mode 100644 index 0000000..f8c8911 --- /dev/null +++ b/app/common/model/user/UserPartner.php @@ -0,0 +1,42 @@ + +// +---------------------------------------------------------------------- + + +namespace app\common\model\user; + + +use app\common\model\BaseModel; + +class UserPartner extends BaseModel +{ + + /** + * @return string + * @author xaboy + * @day 2020-03-30 + */ + public static function tablePk(): string + { + return 'id'; + } + + /** + * @return string + * @author xaboy + * @day 2020-03-30 + */ + public static function tableName(): string + { + return 'user_partner'; + } + +} diff --git a/app/common/repositories/system/admin/PartnerRepository.php b/app/common/repositories/system/admin/PartnerRepository.php index 00f4170..924a3ea 100644 --- a/app/common/repositories/system/admin/PartnerRepository.php +++ b/app/common/repositories/system/admin/PartnerRepository.php @@ -49,6 +49,7 @@ class PartnerRepository extends BaseRepository $this->dao = $dao; } + /** * @param array $where * @param $page @@ -73,6 +74,22 @@ class PartnerRepository extends BaseRepository } /** + * @param int $id + * @return Form + * @throws DataNotFoundException + * @throws DbException + * @throws FormBuilderException + * @throws ModelNotFoundException + * @author xaboy + * @day 2020-04-09 + */ + public function getDetail(int $id) + { + return $this->dao->get($id)->toArray(); + } + + /** + * * 更新 * @param int $id id * @param array $data 数组 @@ -83,14 +100,20 @@ class PartnerRepository extends BaseRepository */ public function update(int $id, array $data) { - if (isset($data['roles'])) - $data['roles'] = implode(',', $data['roles']); return $this->dao->update($id, $data); } + //删除 + public function delete(int $id) + { + return $this->dao->delete($id); + } - - + //检测角色绑定用户 + public function existsUser(int $id) + { + return $this->dao->existsUser($id); + } } diff --git a/app/controller/admin/system/admin/Partner.php b/app/controller/admin/system/admin/Partner.php index 88275bc..71bc956 100644 --- a/app/controller/admin/system/admin/Partner.php +++ b/app/controller/admin/system/admin/Partner.php @@ -45,11 +45,56 @@ class Partner extends BaseController return app('json')->success($this->repository->getList($where, $page, $limit)); } + public function add(PartnerRoleValidate $validate) { $data = $this->request->params(['name', 'ratio', 'is_area', 'area_level', 'area_id', ['status', 1]]); $validate->check($data); + if ($this->repository->fieldExists('area_id', $data['area_id'])) { + return app('json')->fail('该区域已经有了代理角色,请勿重复添加'); + } $this->repository->create($data); return app('json')->success('添加成功'); } + + public function update(PartnerRoleValidate $validate) + { + $data = $this->request->params(['name', 'ratio', 'is_area', 'area_level', 'area_id', 'id', ['status', 1]]); + + $validate->check($data); + + if ($data['is_area'] == 1) { + if (empty($data['area_id']) && empty($data['area_level'])) { + return app('json')->fail('请选择所属区域'); + } + if ($this->repository->fieldExists('area_id', $data['area_id'], $data['id'])) { + return app('json')->fail('该区域已经有了代理角色,请勿重复添加'); + } + } else { + $data['area_level'] = 0; + $data['area_id'] = 0; + } + + $this->repository->update($data['id'], $data); + + return app('json')->success('编辑成功'); + } + + public function detail($id) + { + if (!$this->repository->exists($id)) + return app('json')->fail('数据不存在'); + return app('json')->success($this->repository->getDetail($id)); + } + + + public function delete($id) + { + if (!$this->repository->exists($id)) + return app('json')->fail('数据不存在'); + if ($this->repository->existsUser($id)) + return app('json')->fail('该角色下存在绑定用,请先取消绑定'); + $this->repository->delete($id); + return app('json')->success('删除成功'); + } } diff --git a/route/admin/role.php b/route/admin/role.php index 372fed6..0172283 100644 --- a/route/admin/role.php +++ b/route/admin/role.php @@ -143,6 +143,15 @@ Route::group(function () { Route::post('add', '.Partner/add')->name('systemPartnerAdd')->option([ '_alias' => '添加合作人角色', ]); + Route::post('update', '.Partner/update')->name('systemPartnerUpdate')->option([ + '_alias' => '编辑合作人角色', + ]); + Route::get('detail/:id', '.Partner/detail')->name('systemPartnerDetail')->option([ + '_alias' => '读取合作人角色详情', + ]); + Route::get('delete/:id', '.Partner/delete')->name('systemPartnerDel')->option([ + '_alias' => '删除合作人角色', + ]); })->prefix('admin.system.admin')->option([ '_path' => 'self', '_auth' => true,