Commontable.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\controller\dict;
  3. use app\admin\model\Admin;
  4. use app\common\controller\Backend;
  5. use think\facade\Db;
  6. /**
  7. * 字典-通用字典类型管理
  8. */
  9. class Commontable extends Backend
  10. {
  11. /**
  12. * Commontable模型对象
  13. * @var object
  14. * @phpstan-var \app\admin\model\dict\Commontable
  15. */
  16. protected object $model;
  17. protected string|array $defaultSortField = 'weigh,desc';
  18. protected array|string $preExcludeFields = ['id', 'update_time'];
  19. protected string|array $quickSearchField = ['id'];
  20. public function initialize(): void
  21. {
  22. parent::initialize();
  23. $this->model = new \app\admin\model\dict\Commontable();
  24. }
  25. /**
  26. * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
  27. */
  28. /**
  29. * 查看
  30. * @throws Throwable
  31. */
  32. public function index(): void
  33. {
  34. if ($this->request->param('select')) {
  35. $this->select();
  36. }
  37. list($where, $alias, $limit, $order) = $this->queryBuilder();
  38. $res = $this->model
  39. ->field($this->indexField)
  40. ->withJoin($this->withJoinTable, $this->withJoinType)
  41. ->alias($alias)
  42. ->where($where)
  43. ->order($order)
  44. ->paginate($limit);
  45. $list = $res->items();
  46. $common = Db::name('dict_common_data')->select();
  47. $arr = [];
  48. // $i = 1;
  49. foreach ($common as $k=>$v)
  50. {
  51. if($arr[$v['type']] ?? '')
  52. {
  53. $arr[$v['type']] .= '、'.$v['code'].'-'.$v['name'];
  54. // $i++;
  55. }else{
  56. // $i = 1;
  57. $arr[$v['type']] = $v['code'].'-'.$v['name'];
  58. // $i++;
  59. }
  60. }
  61. foreach ($list as $k=>$v)
  62. {
  63. $list[$k]['area'] = $arr[$v['code']] ?? [];
  64. }
  65. $this->success('', [
  66. 'list' => $list,
  67. 'total' => $res->total(),
  68. 'remark' => get_route_remark(),
  69. ]);
  70. }
  71. /**
  72. * 添加
  73. */
  74. public function add(): void
  75. {
  76. if ($this->request->isPost()) {
  77. $data = $this->request->post();
  78. if (!$data) {
  79. $this->error(__('Parameter %s can not be empty', ['']));
  80. }
  81. $data = $this->excludeFields($data);
  82. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  83. $data[$this->dataLimitField] = $this->auth->id;
  84. }
  85. $result = false;
  86. $this->model->startTrans();
  87. try {
  88. // 模型验证
  89. if ($this->modelValidate) {
  90. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  91. if (class_exists($validate)) {
  92. $validate = new $validate();
  93. if ($this->modelSceneValidate) $validate->scene('add');
  94. $validate->check($data);
  95. }
  96. }
  97. if(!empty($data['create_user_id']))
  98. {
  99. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  100. }
  101. $result = $this->model->save($data);
  102. $this->model->commit();
  103. } catch (Throwable $e) {
  104. $this->model->rollback();
  105. $this->error($e->getMessage());
  106. }
  107. if ($result !== false) {
  108. $this->success(__('Added successfully'));
  109. } else {
  110. $this->error(__('No rows were added'));
  111. }
  112. }
  113. $this->error(__('Parameter error'));
  114. }
  115. /**
  116. * 编辑
  117. * @throws Throwable
  118. */
  119. public function edit(): void
  120. {
  121. $pk = $this->model->getPk();
  122. $id = $this->request->param($pk);
  123. $row = $this->model->find($id);
  124. if (!$row) {
  125. $this->error(__('Record not found'));
  126. }
  127. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  128. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  129. $this->error(__('You have no permission'));
  130. }
  131. if ($this->request->isPost()) {
  132. $data = $this->request->post();
  133. if (!$data) {
  134. $this->error(__('Parameter %s can not be empty', ['']));
  135. }
  136. $data = $this->excludeFields($data);
  137. $result = false;
  138. $this->model->startTrans();
  139. try {
  140. // 模型验证
  141. if ($this->modelValidate) {
  142. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  143. if (class_exists($validate)) {
  144. $validate = new $validate();
  145. if ($this->modelSceneValidate) $validate->scene('edit');
  146. $data[$pk] = $row[$pk];
  147. $validate->check($data);
  148. }
  149. }
  150. if(!empty($data['create_user_id']))
  151. {
  152. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  153. }
  154. $result = $row->save($data);
  155. $this->model->commit();
  156. } catch (Throwable $e) {
  157. $this->model->rollback();
  158. $this->error($e->getMessage());
  159. }
  160. if ($result !== false) {
  161. $this->success(__('Update successful'));
  162. } else {
  163. $this->error(__('No rows updated'));
  164. }
  165. }
  166. $this->success('', [
  167. 'row' => $row
  168. ]);
  169. }
  170. }