feature: kpi template

This commit is contained in:
zhuyu 2025-05-14 09:50:18 +08:00
parent 3dfbbe52e0
commit 009c71b287
4 changed files with 154 additions and 28 deletions

View File

@ -73,8 +73,9 @@ class Template extends Backend
$result = $this->model->allowField(true)->save($params); $result = $this->model->allowField(true)->save($params);
if($result!==false) {
$templateId = $result; $templateId = $this->model->id; //新插入数据的ID
}
$templateItem = []; $templateItem = [];
$kpiItems = json_decode($params['kpiitem'], true); $kpiItems = json_decode($params['kpiitem'], true);
@ -89,7 +90,7 @@ class Template extends Backend
} }
Db::name('kpi_template_item') Db::name('kpi_template_item')
->insert($templateItem); ->insertAll($templateItem);
Db::commit(); Db::commit();
} catch (ValidateException|PDOException|Exception $e) { } catch (ValidateException|PDOException|Exception $e) {
@ -102,4 +103,121 @@ class Template extends Backend
$this->success(); $this->success();
} }
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()) {
$kpiitem = Db::name('kpi_template_item')
->join('kpi_item', 'item_id = kpi_item.id')
->where('template_id', $row->id)
->select();
$assignKpiitem = array_map(function ($item) {
return [
'id' => (string)$item['id'],
'rate' => (string)$item['rate'],
];
}, $kpiitem);
$assignKpiitem = json_encode($assignKpiitem);
$this->view->assign('assignkpiitem', $assignKpiitem);
$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);
}
$params['update_time'] = date('Y-m-d H:i:s');
$result = $row->allowField(true)->save($params);
$templateId = $row->id;
$templateItem = [];
$kpiItems = json_decode($params['kpiitem'], true);
foreach ($kpiItems as $kpiItem) {
$templateItem[] = [
'admin_id' => $this->auth->id,
'template_id' => $templateId,
'item_id' => $kpiItem['id'],
'rate' => $kpiItem['rate'],
];
}
Db::name('kpi_template_item')
->where('template_id', $templateId)
->delete();
Db::name('kpi_template_item')
->insertAll($templateItem);
Db::commit();
} catch (ValidateException| \think\exception\PDOException |Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (false === $result) {
$this->error(__('No rows were updated'));
}
$this->success();
}
public function del($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post("ids");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $item) {
Db::name('kpi_template_item')
->where('template_id', $item->id)
->delete();
$count += $item->delete();
}
Db::commit();
} catch (\think\exception\PDOException |Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
}
$this->error(__('No rows were deleted'));
}
} }

View File

@ -12,21 +12,9 @@
<input id="c-desc" class="form-control" name="row[desc]" type="text" value=""> <input id="c-desc" class="form-control" name="row[desc]" type="text" value="">
</div> </div>
</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 row"> <div class="form-group row">
<div class="col-xs-12"> <div class="col-xs-12">
<table class="table fieldlist" data-template="kpiitemtpl" data-name="row[kpiitem]" id="second-table"> <table class="table fieldlist" data-template="kpiitemtpl" data-name="row[kpiitem]" id="fieldlist-table">
<tr> <tr>
<td>{:__('指标')}</td> <td>{:__('指标')}</td>
<td>{:__('比例')}</td> <td>{:__('比例')}</td>
@ -38,7 +26,7 @@
</table> </table>
<!--请注意实际开发中textarea应该添加个hidden进行隐藏--> <!--请注意实际开发中textarea应该添加个hidden进行隐藏-->
<textarea name="row[kpiitem]" class="form-control" cols="30" rows="5">[{"id":"1","rate":"20"}]</textarea> <textarea name="row[kpiitem]" class="form-control" cols="30" rows="5" style="display:none;"></textarea>
<script id="kpiitemtpl" type="text/html"> <script id="kpiitemtpl" type="text/html">
<tr class="form-inline"> <tr class="form-inline">
<td><input type="text" name="<%=name%>[<%=index%>][id]" class="form-control selectpage" data-source="kpi/item/selectpage" data-field="name" value="<%=row.id%>" placeholder="指标"/></td> <td><input type="text" name="<%=name%>[<%=index%>][id]" class="form-control selectpage" data-source="kpi/item/selectpage" data-field="name" value="<%=row.id%>" placeholder="指标"/></td>

View File

@ -12,16 +12,32 @@
<input id="c-desc" class="form-control" name="row[desc]" type="text" value="{$row.desc|htmlentities}"> <input id="c-desc" class="form-control" name="row[desc]" type="text" value="{$row.desc|htmlentities}">
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group row">
<label class="control-label col-xs-12 col-sm-2">{:__('Create_time')}:</label> <div class="col-xs-12">
<div class="col-xs-12 col-sm-8"> <table class="table fieldlist" data-template="kpiitemtpl" data-name="row[kpiitem]" id="fieldlist-table">
<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}"> <tr>
</div> <td>{:__('指标')}</td>
</div> <td>{:__('比例')}</td>
<div class="form-group"> <td width="100"></td>
<label class="control-label col-xs-12 col-sm-2">{:__('Update_time')}:</label> </tr>
<div class="col-xs-12 col-sm-8"> <tr>
<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}"> <td colspan="4"><a href="javascript:;" class="btn btn-sm btn-success btn-append"><i class="fa fa-plus"></i> {:__('Append')}</a></td>
</tr>
</table>
<!--请注意实际开发中textarea应该添加个hidden进行隐藏-->
<textarea name="row[kpiitem]" class="form-control" cols="30" rows="5" style="display:none;">{$assignkpiitem}</textarea>
<script id="kpiitemtpl" type="text/html">
<tr class="form-inline">
<td><input type="text" name="<%=name%>[<%=index%>][id]" class="form-control selectpage" data-source="kpi/item/selectpage" data-field="name" value="<%=row.id%>" placeholder="指标"/></td>
<td><input type="text" name="<%=name%>[<%=index%>][rate]" class="form-control" value="<%=row['rate']%>" size="30"></td>
<td>
<!--下面的两个按钮务必保留-->
<span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span>
<span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span>
</td>
</tr>
</script>
</div> </div>
</div> </div>
<div class="form-group layer-footer"> <div class="form-group layer-footer">

View File

@ -40,13 +40,17 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
Table.api.bindevent(table); Table.api.bindevent(table);
}, },
add: function () { add: function () {
$(document).on("fa.event.appendfieldlist", "#second-table .btn-append", function (e, obj) { $(document).on("fa.event.appendfieldlist", "#fieldlist-table .btn-append", function (e, obj) {
//绑定动态下拉组件 //绑定动态下拉组件
Form.events.selectpage(obj); Form.events.selectpage(obj);
}); });
Controller.api.bindevent(); Controller.api.bindevent();
}, },
edit: function () { edit: function () {
$(document).on("fa.event.appendfieldlist", "#fieldlist-table .btn-append", function (e, obj) {
//绑定动态下拉组件
Form.events.selectpage(obj);
});
Controller.api.bindevent(); Controller.api.bindevent();
}, },
api: { api: {