diff --git a/application/admin/controller/kpi/Item.php b/application/admin/controller/kpi/Item.php index 4cddfc2..07eddaa 100644 --- a/application/admin/controller/kpi/Item.php +++ b/application/admin/controller/kpi/Item.php @@ -2,7 +2,15 @@ namespace app\admin\controller\kpi; +use app\admin\model\AuthGroup; use app\common\controller\Backend; +use Exception; +use fast\Tree; +use think\Db; +use think\exception\DbException; +use think\exception\PDOException; +use think\exception\ValidateException; +use think\response\Json; /** * kpi指标 @@ -22,8 +30,36 @@ class Item extends Backend { parent::_initialize(); $this->model = new \app\admin\model\kpi\Item; - $this->view->assign("typeList", $this->model->getTypeList()); $this->view->assign("unitList", $this->model->getUnitList()); + + $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin()); + $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin()); + + $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray(); + + Tree::instance()->init($groupList); + $groupdata = []; + if ($this->auth->isSuperAdmin()) { + $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0)); + foreach ($result as $k => $v) { + $groupdata[$v['id']] = $v['name']; + } + } else { + $result = []; + $groups = $this->auth->getGroups(); + foreach ($groups as $m => $n) { + $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id'])); + $temp = []; + foreach ($childlist as $k => $v) { + $temp[$v['id']] = $v['name']; + } + $result[__($n['name'])] = $temp; + } + $groupdata = $result; + } + + $this->view->assign('groupdata', $groupdata); + } /** @@ -43,4 +79,138 @@ class Item extends Backend */ + /** + * 查看 + * + * @return string|Json + * @throws \think\Exception + * @throws DbException + */ + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags', 'trim']); + if (false === $this->request->isAjax()) { + return $this->view->fetch(); + } + //如果发送的来源是 Selectpage,则转发到 Selectpage + if ($this->request->request('keyField')) { + return $this->selectpage(); + } + [$where, $sort, $order, $offset, $limit] = $this->buildparams(); + $list = $this->model + ->where($where) + ->with('admin') + ->with('authgroup') + ->order($sort, $order) + ->paginate($limit); + $result = ['total' => $list->total(), 'rows' => $list->items()]; + return json($result); + } + + + /** + * 添加 + * + * @return string + * @throws \think\Exception + */ + public function add() + { + if (false === $this->request->isPost()) { + return $this->view->fetch(); + } + $params = $this->request->post('row/a'); + if (empty($params)) { + $this->error(__('Parameter %s can not be empty', '')); + } + $params = $this->preExcludeFields($params); + + if ($this->dataLimit && $this->dataLimitFieldAutoFill) { + $params[$this->dataLimitField] = $this->auth->id; + } + $result = false; + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; + $this->model->validateFailException()->validate($validate); + } + + $group = $this->request->post("group/a"); + $params['admin_id'] = $this->auth->id; + $params['group_id'] = $group[0]; + $params['create_time'] = date('Y-m-d H:i:s'); + $params['update_time'] = date('Y-m-d H:i:s'); + + $result = $this->model->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if ($result === false) { + $this->error(__('No rows were inserted')); + } + $this->success(); + } + + /** + * 编辑 + * + * @param $ids + * @return string + * @throws DbException + * @throws \think\Exception + */ + public function edit($ids = null) + { + $row = $this->model->get($ids); + if (!$row) { + $this->error(__('No Results were found')); + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { + $this->error(__('You have no permission')); + } + if (false === $this->request->isPost()) { + $this->view->assign("groupid", $row['group_id']); + $this->view->assign('row', $row); + return $this->view->fetch(); + } + $params = $this->request->post('row/a'); + if (empty($params)) { + $this->error(__('Parameter %s can not be empty', '')); + } + $params = $this->preExcludeFields($params); + $result = false; + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; + $row->validateFailException()->validate($validate); + } + + $group = $this->request->post("group/a"); + $params['admin_id'] = $this->auth->id; + $params['group_id'] = $group[0]; + $params['update_time'] = date('Y-m-d H:i:s'); + + $result = $row->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if (false === $result) { + $this->error(__('No rows were updated')); + } + $this->success(); + } + + } diff --git a/application/admin/controller/kpi/Template.php b/application/admin/controller/kpi/Template.php index e5f52f4..e549e39 100644 --- a/application/admin/controller/kpi/Template.php +++ b/application/admin/controller/kpi/Template.php @@ -2,8 +2,10 @@ namespace app\admin\controller\kpi; +use app\admin\model\AuthGroup; use app\common\controller\Backend; use Exception; +use fast\Tree; use PDOException; use think\Db; use think\exception\ValidateException; @@ -27,6 +29,34 @@ class Template extends Backend parent::_initialize(); $this->model = new \app\admin\model\kpi\Template; + $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin()); + $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin()); + + $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray(); + + Tree::instance()->init($groupList); + $groupdata = []; + if ($this->auth->isSuperAdmin()) { + $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0)); + foreach ($result as $k => $v) { + $groupdata[$v['id']] = $v['name']; + } + } else { + $result = []; + $groups = $this->auth->getGroups(); + foreach ($groups as $m => $n) { + $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id'])); + $temp = []; + foreach ($childlist as $k => $v) { + $temp[$v['id']] = $v['name']; + } + $result[__($n['name'])] = $temp; + } + $groupdata = $result; + } + + $this->view->assign('groupdata', $groupdata); + } @@ -52,6 +82,8 @@ class Template extends Backend $list = $this->model ->where($where) ->with('kpiitem') + ->with('admin') + ->with('authgroup') ->order($sort, $order) ->paginate($limit); @@ -90,7 +122,9 @@ class Template extends Backend $this->model->validateFailException()->validate($validate); } - + $group = $this->request->post("group/a"); + $params['admin_id'] = $this->auth->id; + $params['group_id'] = $group[0]; $params['create_time'] = date('Y-m-d H:i:s'); $params['update_time'] = date('Y-m-d H:i:s'); @@ -153,6 +187,7 @@ class Template extends Backend $assignKpiitem = json_encode($assignKpiitem); $this->view->assign('assignkpiitem', $assignKpiitem); + $this->view->assign("groupid", $row['group_id']); $this->view->assign('row', $row); return $this->view->fetch(); } @@ -171,6 +206,9 @@ class Template extends Backend $row->validateFailException()->validate($validate); } + $group = $this->request->post("group/a"); + $params['admin_id'] = $this->auth->id; + $params['group_id'] = $group[0]; $params['update_time'] = date('Y-m-d H:i:s'); $result = $row->allowField(true)->save($params); diff --git a/application/admin/lang/zh-cn/kpi/item.php b/application/admin/lang/zh-cn/kpi/item.php index 02ac00d..76e8706 100644 --- a/application/admin/lang/zh-cn/kpi/item.php +++ b/application/admin/lang/zh-cn/kpi/item.php @@ -3,16 +3,14 @@ return [ 'Id' => 'ID', 'Admin_id' => '创建人id', - 'Type' => '指标类型', - 'Type 1' => '派单员', - 'Type 2' => '运营', - 'Type 3' => '售前', + 'Admin.nickname' => '创建人', + 'AuthGroup.name' => '角色', 'Name' => '名称', 'Desc' => '描述', 'Target_value' => '目标值', 'Unit' => '目标值单位', 'Unit 1' => '固定值', - 'Unit 2' => '百分比', + 'Unit 2' => '%', 'Create_time' => '创建时间', 'Update_time' => '编辑时间' ]; diff --git a/application/admin/lang/zh-cn/kpi/template.php b/application/admin/lang/zh-cn/kpi/template.php index 86306f6..ad6aeea 100644 --- a/application/admin/lang/zh-cn/kpi/template.php +++ b/application/admin/lang/zh-cn/kpi/template.php @@ -3,6 +3,8 @@ return [ 'Id' => 'ID', 'Admin_id' => '创建人id', + 'Admin.nickname' => '创建人', + 'AuthGroup.name' => '角色', 'Name' => '名称', 'Desc' => '描述', 'Create_time' => '创建时间', diff --git a/application/admin/model/kpi/Item.php b/application/admin/model/kpi/Item.php index 03fb6ed..c272fdd 100644 --- a/application/admin/model/kpi/Item.php +++ b/application/admin/model/kpi/Item.php @@ -2,6 +2,8 @@ namespace app\admin\model\kpi; +use app\admin\model\Admin; +use app\admin\model\AuthGroup; use think\Model; @@ -25,16 +27,9 @@ class Item extends Model // 追加属性 protected $append = [ - 'type_text', 'unit_text' ]; - - - public function getTypeList() - { - return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')]; - } public function getUnitList() { @@ -42,14 +37,6 @@ class Item extends Model } - public function getTypeTextAttr($value, $data) - { - $value = $value ?: ($data['type'] ?? ''); - $list = $this->getTypeList(); - return $list[$value] ?? ''; - } - - public function getUnitTextAttr($value, $data) { $value = $value ?: ($data['unit'] ?? ''); @@ -57,7 +44,16 @@ class Item extends Model return $list[$value] ?? ''; } + public function admin() + { + return $this->belongsTo(Admin::class, 'admin_id')->setEagerlyType(1); + } + public function authgroup() + { + return $this->belongsTo(AuthGroup::class, 'group_id')->setEagerlyType(1); + } + } diff --git a/application/admin/model/kpi/Template.php b/application/admin/model/kpi/Template.php index 92fa907..a9bb3a0 100644 --- a/application/admin/model/kpi/Template.php +++ b/application/admin/model/kpi/Template.php @@ -2,6 +2,8 @@ namespace app\admin\model\kpi; +use app\admin\model\Admin; +use app\admin\model\AuthGroup; use think\Model; @@ -39,7 +41,14 @@ class Template extends Model + public function admin() + { + return $this->belongsTo(Admin::class, 'admin_id')->setEagerlyType(1); + } - + public function authgroup() + { + return $this->belongsTo(AuthGroup::class, 'group_id')->setEagerlyType(1); + } } diff --git a/application/admin/model/oa/Schedule.php b/application/admin/model/oa/Schedule.php index 991b076..6a858a8 100644 --- a/application/admin/model/oa/Schedule.php +++ b/application/admin/model/oa/Schedule.php @@ -48,6 +48,6 @@ class Schedule extends Model public function admin() { - return $this->belongsTo('app\admin\model\Admin', 'exec_admin_id', 'id', [], 'LEFT')->setEagerlyType(0); + return $this->belongsTo('app\admin\model\Admin', 'exec_admin_id', 'id', [], 'LEFT')->setEagerlyType(1); } } diff --git a/application/admin/model/oa/Task.php b/application/admin/model/oa/Task.php index 43fa03e..459744a 100644 --- a/application/admin/model/oa/Task.php +++ b/application/admin/model/oa/Task.php @@ -62,6 +62,6 @@ class Task extends Model public function admin() { - return $this->belongsTo('app\admin\model\Admin', 'exec_admin_id', 'id', [], 'LEFT')->setEagerlyType(0); + return $this->belongsTo('app\admin\model\Admin', 'exec_admin_id', 'id', [], 'LEFT')->setEagerlyType(1); } } diff --git a/application/admin/view/kpi/item/add.html b/application/admin/view/kpi/item/add.html index f8f7c1f..2ef10fa 100644 --- a/application/admin/view/kpi/item/add.html +++ b/application/admin/view/kpi/item/add.html @@ -1,15 +1,9 @@