Accept Merge Request #51: (feature/zy -> develop)

Merge Request: feature: kpi item template

Created By: @zhuyu
Accepted By: @zhuyu
URL: https://g-bcrc3009.coding.net/p/allocatr/d/allocatr/git/merge/51
This commit is contained in:
zhuyu 2025-05-15 17:22:29 +08:00 committed by Coding
commit 396d569913
15 changed files with 259 additions and 69 deletions

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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' => '编辑时间'
];

View File

@ -3,6 +3,8 @@
return [
'Id' => 'ID',
'Admin_id' => '创建人id',
'Admin.nickname' => '创建人',
'AuthGroup.name' => '角色',
'Name' => '名称',
'Desc' => '描述',
'Create_time' => '创建时间',

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1,15 +1,9 @@
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
<label class="control-label col-xs-12 col-sm-2">{:__('Group')}:</label>
<div class="col-xs-12 col-sm-8">
<select id="c-type" data-rule="required" class="form-control selectpicker" name="row[type]">
{foreach name="typeList" item="vo"}
<option value="{$key}" {in name="key" value="0"}selected{/in}>{$vo}</option>
{/foreach}
</select>
{:build_select('group', $groupdata, null, ['class'=>'form-control selectpicker', 'data-rule'=>'required'])}
</div>
</div>
<div class="form-group">
@ -42,18 +36,6 @@
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Create_time')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-create_time" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[create_time]" type="text" value="{:date('Y-m-d H:i:s')}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Update_time')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-update_time" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[update_time]" type="text" value="{:date('Y-m-d H:i:s')}">
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">

View File

@ -1,15 +1,9 @@
<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
<label class="control-label col-xs-12 col-sm-2">{:__('Group')}:</label>
<div class="col-xs-12 col-sm-8">
<select id="c-type" data-rule="required" class="form-control selectpicker" name="row[type]">
{foreach name="typeList" item="vo"}
<option value="{$key}" {in name="key" value="$row.type"}selected{/in}>{$vo}</option>
{/foreach}
</select>
{:build_select('group', $groupdata, $groupid, ['class'=>'form-control selectpicker', 'data-rule'=>'required'])}
</div>
</div>
<div class="form-group">
@ -42,18 +36,6 @@
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Create_time')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-create_time" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[create_time]" type="text" value="{$row.create_time}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Update_time')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-update_time" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[update_time]" type="text" value="{$row.update_time}">
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">

View File

@ -1,5 +1,11 @@
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Group')}:</label>
<div class="col-xs-12 col-sm-8">
{:build_select('group', $groupdata, null, ['class'=>'form-control selectpicker', 'data-rule'=>'required'])}
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
<div class="col-xs-12 col-sm-8">

View File

@ -1,5 +1,11 @@
<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Group')}:</label>
<div class="col-xs-12 col-sm-8">
{:build_select('group', $groupdata, $groupid, ['class'=>'form-control selectpicker', 'data-rule'=>'required'])}
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
<div class="col-xs-12 col-sm-8">

View File

@ -26,8 +26,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'admin_id', title: __('Admin_id')},
{field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
{field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
{field: 'authgroup.name', title: __('AuthGroup.name'), operate: 'LIKE'},
{field: 'name', title: __('Name'), operate: 'LIKE'},
{field: 'desc', title: __('Desc'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
{field: 'target_value', title: __('Target_value')},

View File

@ -26,7 +26,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'admin_id', title: __('Admin_id')},
{field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
{field: 'authgroup.name', title: __('AuthGroup.name'), operate: 'LIKE'},
{field: 'name', title: __('Name'), operate: 'LIKE'},
{field: 'desc', title: __('Desc'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
{

View File

@ -36,7 +36,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
{field: 'expire_start_time', title: __('Expire_start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
{field: 'expire_end_time', title: __('Expire_end_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
{field: 'complete_time', title: __('Complete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
{field: 'admin.username', title: __('Admin.username'), operate: 'LIKE'},
{field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
buttons:[
{