Dic.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\admin\controller\base\dic;
  3. /**
  4. * @title : 字典控制器
  5. * @desc :
  6. * @Author : Rock
  7. * @Date : 2021-12-04 10:12:21
  8. * @LastEditTime: 2023-03-11 14:06:11
  9. */
  10. use think\facade\Db;
  11. use app\admin\controller\Base;
  12. use app\common\model\base\dic\DicGroup as dicGroupModel;
  13. use app\common\model\base\dic\Dic as dicModel;
  14. class Dic extends Base
  15. {
  16. protected $dicGroupModel;
  17. protected $dicModel;
  18. protected $noNeedAuth = ['getList','getDic'];
  19. protected $noNeedLogin = ['getDic'];
  20. public function initialize()
  21. {
  22. parent::initialize();
  23. $this->dicGroupModel = new dicGroupModel;
  24. $this->dicModel = new dicModel;
  25. }
  26. /**
  27. * @title: 统一创建查询条件
  28. * @desc:
  29. * @param {*}
  30. * @return {*}
  31. * @Author: Rock
  32. * @Date: 2021-12-04 10:58:55
  33. * @LastEditTime: Do not edit
  34. */
  35. private function createWhere()
  36. {
  37. $data = $this->request->param();
  38. $where = [];
  39. if (!empty($data['type'])) {
  40. if ($data['type'] == 'GOODTYPE') {
  41. // 货物分类
  42. $group_codes = $this->dicGroupModel->where('pid', 1)->column('group_code');
  43. $where[] = ['group_code', 'IN', $group_codes];
  44. } elseif ($data['type'] == 'CARTYPE') {
  45. // 车辆分类
  46. $where[] = ['group_code', '=', 'CARTYPE'];
  47. } elseif ($data['type'] == 'INDUSTRY') {
  48. $group_codes = $this->dicGroupModel->where('pid', 12)->column('group_code');
  49. $where[] = ['group_code', 'IN', $group_codes];
  50. } elseif ($data['type'] == 'OPERATE') {
  51. $where[] = ['group_code', '=', 'OPERATE'];
  52. }
  53. }
  54. if (!empty($data['group_id'])) {
  55. $groupInfo = $this->dicGroupModel->where('group_id', $data['group_id'])->find();
  56. $where[] = ['group_code', '=', $groupInfo->group_code];
  57. }
  58. if (!empty($data['group_code'])) {
  59. $where[] = ['group_code', '=', $data['group_code']];
  60. }
  61. if (!empty($data['status'])) {
  62. $where[] = ['status', '=', $data['status']];
  63. }
  64. if (!empty($data['keyword'])) {
  65. $keyword = $data['keyword'];
  66. $where[] = ['title|code', 'LIKE', "%$keyword%"];
  67. }
  68. return $where;
  69. }
  70. /**
  71. * @title: 获取字典列表
  72. * @desc:
  73. * @param {int} {pageNo} {0} {页码,页码为0时不分页返回全部}
  74. * @param {int} {pageSize} {10} {每页数量}
  75. * @param {int} {group_id} {} {分组ID}
  76. * @param {int} {status} {} {字典状态}
  77. * @param {int} {group_code} {} {分组编码}
  78. * @return {*}
  79. * @Author: Rock
  80. * @Date: 2021-12-04 10:16:04
  81. * @LastEditTime: Do not edit
  82. */
  83. public function getList($pageNo = 0, $pageSize = 10)
  84. {
  85. $where = $this->createWhere();
  86. if ($pageNo) {
  87. $list = $this->dicModel->where($where)->order('sort')->paginate(['page' => $pageNo, 'list_rows' => $pageSize]);
  88. return pageRes(1, "获取成功", $list->total(), $list->items());
  89. } else {
  90. $list = $this->dicModel->where($where)->select();
  91. return res(1, "获取成功", $list);
  92. }
  93. }
  94. /**
  95. * @title: 新增/编辑字典
  96. * @desc:
  97. * @param {*}
  98. * @return {*}
  99. * @Author: Rock
  100. * @Date: 2021-12-04 10:52:22
  101. * @LastEditTime: Do not edit
  102. */
  103. public function doEdit()
  104. {
  105. $data = $this->request->param();
  106. if (!empty($data['dic_id'])) {
  107. $res = $this->dicModel->replace()->save($data);
  108. } else {
  109. $res = dicModel::createDic($data);
  110. }
  111. // 更新缓存内容
  112. $group_code = $data['group_code'];
  113. $list = dicModel::where('group_code', $group_code)->where('status', 1)->select()->toArray();
  114. $list = array_column($list, 'title', 'code');
  115. cache($group_code, $list, 86400);
  116. slog(1, "编辑了字典" . $data['title'], $data);
  117. return res(1, "保存成功", $res);
  118. }
  119. /**
  120. * @title: 删除字典
  121. * @desc:
  122. * @param {int} {dic_id} {必填} {分组ID}
  123. * @return {*}
  124. * @Author: Rock
  125. * @Date: 2021-12-04 10:52:53
  126. * @LastEditTime: Do not edit
  127. */
  128. public function doDelete($dic_id)
  129. {
  130. $where = [];
  131. $where[] = ['dic_id', '=', $dic_id];
  132. $info = $this->dicModel->where($where)->find();
  133. try {
  134. dicModel::startTrans();
  135. dicModel::where($where)->delete();
  136. dicModel::commit();
  137. // 更新缓存内容
  138. $group_code = $info->group_code;
  139. $list = dicModel::where('group_code', $group_code)->where('status', 1)->select()->toArray();
  140. $list = array_column($list, 'title', 'code');
  141. cache($group_code, $list, 86400);
  142. slog(1, "删除了字典" . $info->title);
  143. return res(1, "删除成功");
  144. } catch (\Exception $e) {
  145. dicModel::rollback();
  146. slog(2, "删除了字典" . $info->title);
  147. return res(2, "删除失败");
  148. }
  149. }
  150. /**
  151. * @title: 改变字典状态
  152. * @desc:
  153. * @param {string/array} {ids} {必填} {字典IDS,以字符串传递时用','隔开}
  154. * @return {*}
  155. * @Author: Rock
  156. * @Date: 2021-12-04 11:11:19
  157. * @LastEditTime: Do not edit
  158. */
  159. public function changeStatus($ids)
  160. {
  161. $where = [];
  162. if (is_string($ids)) {
  163. $ids = explode(',', $ids);
  164. } elseif (is_int($ids)) {
  165. $ids = [$ids];
  166. }
  167. $where[] = ['dic_id', 'IN', $ids];
  168. $this->dicModel->where($where)->update(['status' => Db::raw('ABS(3 * `status` - 5)')]);
  169. // 更新缓存内容
  170. $codeList = dicModel::where($where)->column('group_code');
  171. foreach ($codeList as $group_code) {
  172. $list = dicModel::where('group_code', $group_code)->where('status', 1)->select()->toArray();
  173. $list = array_column($list, 'title', 'code');
  174. cache($group_code, $list, 86400);
  175. }
  176. slog(1, "变更了字典状态");
  177. return res(1, "操作成功");
  178. }
  179. /**
  180. * Desc :获取字典
  181. * User : zwq
  182. * Date : 2025-02-05 09:26
  183. */
  184. public function getDic()
  185. {
  186. $group_code = input('group_code/s', "");
  187. $list = dicModel::where('group_code', $group_code)
  188. ->where('status', 1)
  189. ->order('sort')
  190. ->select();
  191. return res(1, "获取成功", $list ?? []);
  192. }
  193. }