123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace app\admin\controller\dict;
- use app\admin\model\Admin;
- use app\common\controller\Backend;
- use think\facade\Db;
- /**
- * 字典-通用字典类型管理
- */
- class Commontable extends Backend
- {
- /**
- * Commontable模型对象
- * @var object
- * @phpstan-var \app\admin\model\dict\Commontable
- */
- protected object $model;
- protected string|array $defaultSortField = 'weigh,desc';
- protected array|string $preExcludeFields = ['id', 'update_time'];
- protected string|array $quickSearchField = ['id'];
- public function initialize(): void
- {
- parent::initialize();
- $this->model = new \app\admin\model\dict\Commontable();
- }
- /**
- * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
- */
- /**
- * 查看
- * @throws Throwable
- */
- public function index(): void
- {
- if ($this->request->param('select')) {
- $this->select();
- }
- list($where, $alias, $limit, $order) = $this->queryBuilder();
- $res = $this->model
- ->field($this->indexField)
- ->withJoin($this->withJoinTable, $this->withJoinType)
- ->alias($alias)
- ->where($where)
- ->order($order)
- ->paginate($limit);
- $list = $res->items();
- $common = Db::name('dict_common_data')->select();
- $arr = [];
- // $i = 1;
- foreach ($common as $k=>$v)
- {
- if($arr[$v['type']] ?? '')
- {
- $arr[$v['type']] .= '、'.$v['code'].'-'.$v['name'];
- // $i++;
- }else{
- // $i = 1;
- $arr[$v['type']] = $v['code'].'-'.$v['name'];
- // $i++;
- }
- }
- foreach ($list as $k=>$v)
- {
- $list[$k]['area'] = $arr[$v['code']] ?? [];
- }
- $this->success('', [
- 'list' => $list,
- 'total' => $res->total(),
- 'remark' => get_route_remark(),
- ]);
- }
- /**
- * 添加
- */
- public function add(): void
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- $data = $this->excludeFields($data);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $data[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- $this->model->startTrans();
- try {
- // 模型验证
- if ($this->modelValidate) {
- $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- if (class_exists($validate)) {
- $validate = new $validate();
- if ($this->modelSceneValidate) $validate->scene('add');
- $validate->check($data);
- }
- }
- if(!empty($data['create_user_id']))
- {
- $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
- }
- $result = $this->model->save($data);
- $this->model->commit();
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Added successfully'));
- } else {
- $this->error(__('No rows were added'));
- }
- }
- $this->error(__('Parameter error'));
- }
- /**
- * 编辑
- * @throws Throwable
- */
- public function edit(): void
- {
- $pk = $this->model->getPk();
- $id = $this->request->param($pk);
- $row = $this->model->find($id);
- if (!$row) {
- $this->error(__('Record not found'));
- }
- $dataLimitAdminIds = $this->getDataLimitAdminIds();
- if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
- $this->error(__('You have no permission'));
- }
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- $data = $this->excludeFields($data);
- $result = false;
- $this->model->startTrans();
- try {
- // 模型验证
- if ($this->modelValidate) {
- $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- if (class_exists($validate)) {
- $validate = new $validate();
- if ($this->modelSceneValidate) $validate->scene('edit');
- $data[$pk] = $row[$pk];
- $validate->check($data);
- }
- }
- if(!empty($data['create_user_id']))
- {
- $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
- }
- $result = $row->save($data);
- $this->model->commit();
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Update successful'));
- } else {
- $this->error(__('No rows updated'));
- }
- }
- $this->success('', [
- 'row' => $row
- ]);
- }
- }
|