SysDictController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package com.ydtech.modules.admin.controller;
  2. import com.ydtech.config.DictEnumConfig;
  3. import com.ydtech.constants.enums.dict.DictionaryManagement;
  4. import com.ydtech.core.page.HttpResult;
  5. import com.ydtech.core.page.PageRequest;
  6. import com.ydtech.modules.admin.model.SysAccount;
  7. import com.ydtech.modules.admin.model.SysDict;
  8. import com.ydtech.modules.admin.model.SysDictType;
  9. import com.ydtech.modules.admin.model.SysUser;
  10. import com.ydtech.modules.admin.service.SysAccountService;
  11. import com.ydtech.modules.admin.service.SysDeptService;
  12. import com.ydtech.modules.admin.service.SysDictService;
  13. import com.ydtech.modules.admin.service.SysDictTypeService;
  14. import com.ydtech.modules.admin.model.vo.TreeSysDeptVO;
  15. import com.ydtech.modules.base.controller.BaseController;
  16. import com.ydtech.modules.esm.model.EsmInsCompany;
  17. import com.ydtech.modules.esm.model.EsmProduct;
  18. import com.ydtech.modules.esm.service.EsmInsCompanyService;
  19. import com.ydtech.modules.esm.service.EsmProductService;
  20. import com.ydtech.utils.StringUtils;
  21. import io.swagger.annotations.Api;
  22. import io.swagger.annotations.ApiOperation;
  23. import org.apache.commons.collections4.list.TreeList;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.util.CollectionUtils;
  26. import org.springframework.web.bind.annotation.*;
  27. import javax.servlet.http.HttpServletRequest;
  28. import java.util.*;
  29. /**
  30. * 字典控制器
  31. *
  32. * @author Yasepix
  33. * @date Oct 29, 2018
  34. */
  35. @Api(tags = "字典管理")
  36. @RestController
  37. @RequestMapping("dict")
  38. public class SysDictController extends BaseController {
  39. @Autowired
  40. private SysDictService sysDictService;
  41. @Autowired
  42. private SysDictTypeService sysDictTypeService;
  43. @Autowired
  44. private SysDeptService sysDeptService;
  45. @Autowired
  46. private EsmInsCompanyService esmInsCompanyService;
  47. @Autowired
  48. private EsmProductService esmProductService;
  49. @Autowired
  50. private SysAccountService sysAccountService;
  51. // @PreAuthorize("hasAuthority('sys:dict:add') AND hasAuthority('sys:dict:edit')")
  52. // @PostMapping(value="/save")
  53. // public HttpResult save(@RequestBody SysDict record) {
  54. // return HttpResult.ok(sysDictService.save(record));
  55. // }
  56. // @PreAuthorize("hasAuthority('sys:dict:add')")
  57. @PostMapping(value = "/add")
  58. public HttpResult add(@RequestBody SysDict record) {
  59. // return HttpResult.ok(sysDictService.save(record));
  60. Integer result = sysDictService.insertSelective(record);
  61. return HttpResult.ok(result);
  62. }
  63. // @PreAuthorize("hasAuthority('sys:dict:edit')")
  64. @PostMapping(value = "/edit")
  65. public HttpResult edit(@RequestBody SysDict record) {
  66. // return HttpResult.ok(sysDictService.save(record));
  67. Integer result = sysDictService.updateSelective(record);
  68. return HttpResult.ok(result);
  69. }
  70. // @PreAuthorize("hasAuthority('sys:dict:add') AND hasAuthority('sys:dict:edit')")
  71. @PostMapping(value = "/savetype")
  72. public HttpResult savetype(@RequestBody SysDictType record) {
  73. return HttpResult.ok(sysDictTypeService.insertSelective(record));
  74. }
  75. // @PreAuthorize("hasAuthority('sys:dict:delete')")
  76. @PostMapping(value = "/delete")
  77. public HttpResult delete(@RequestBody List<SysDict> records) {
  78. return HttpResult.ok(sysDictService.delete(records));
  79. }
  80. // @PreAuthorize("hasAuthority('sys:dict:view')")
  81. @PostMapping(value = "/findPage")
  82. public HttpResult findPage(@RequestBody PageRequest pageRequest) {
  83. return HttpResult.ok(sysDictService.findPage(pageRequest));
  84. }
  85. // // @PreAuthorize("hasAuthority('sys:dict:view')")
  86. @GetMapping(value = "/findByLable")
  87. public HttpResult findByLable(@RequestParam String lable, HttpServletRequest request) {
  88. List<SysDict> byLable = sysDictService.findByLable(lable);
  89. if (byLable == null || byLable.size() == 0) {
  90. // 获取部门ID
  91. String deptId = "";
  92. // String deptId = JwtTokenUtils.getDeptIdFromRequest(request);
  93. lable = lable.toLowerCase();
  94. switch (lable) {
  95. case "inscompanycmain":
  96. List<EsmInsCompany> lst = esmInsCompanyService.list();
  97. /*// 查询当前登录的用户对应的角色是不是管理员,是管理员的话显示全部供应商,否则显示当前用户对于的供应商
  98. if (!StringUtils.isNullOrEmpty(deptId)){
  99. if (SysConstants.DEPT.equals(deptId)){
  100. lst = esmInsCompanyService.selectAll();
  101. }else{
  102. deptId=deptId.substring(0,3);
  103. lst = esmInsCompanyService.selectList(" where parent_id like ? and (parent_id='0' or LENGTH(t.parent_id)=3) ",new Object[]{deptId+"%"});
  104. }
  105. }*/
  106. byLable = new TreeList<>();
  107. for (EsmInsCompany esmInsCompany : lst) {
  108. SysDict sysDict = new SysDict();
  109. sysDict.setValue(esmInsCompany.getId());
  110. sysDict.setLabel(esmInsCompany.getName());
  111. byLable.add(sysDict);
  112. }
  113. break;
  114. case "comcode":
  115. case "ydcomcode": // 下拉框,要改成层级的那样
  116. List<TreeSysDeptVO> treeSysDeptVOList = null;
  117. /*if (SysConstants.DEPT.equals(deptId)){
  118. deptId="";
  119. }else{
  120. if (deptId.contains("D")){
  121. deptId = deptId.substring(0,deptId.indexOf("D"));
  122. }
  123. if (3 == deptId.length()){ // 商户管理员
  124. deptId = deptId;
  125. }else{ // 其他分支机构负责人
  126. deptId = deptId.substring(0,5);
  127. }
  128. }*/
  129. deptId = "";
  130. treeSysDeptVOList = sysDeptService.selectListWhere("and id like ? order by id ", new Object[]{deptId + "%"});
  131. for (TreeSysDeptVO treeSysDeptVO : treeSysDeptVOList) {
  132. if ("0".equals(treeSysDeptVO.getParentId())) {
  133. SysDict sysDict = new SysDict();
  134. sysDict.setValue(treeSysDeptVO.getId());
  135. sysDict.setLabel(treeSysDeptVO.getName());
  136. sysDict.setDeptId(treeSysDeptVO.getId());
  137. sysDict.setDeptParentId(treeSysDeptVO.getParentId());
  138. sysDict.setDeptLevel(treeSysDeptVO.getComlevel());
  139. byLable.add(sysDict);
  140. }
  141. }
  142. getTree(byLable, treeSysDeptVOList);
  143. break;
  144. case "comcode1and2":
  145. List<TreeSysDeptVO> comcode1and2List = null;
  146. /*if (SysConstants.DEPT.equals(deptId)){
  147. deptId="";
  148. }else{
  149. if (deptId.length()!=3 && deptId.contains("D")){
  150. deptId=deptId.substring(0,deptId.indexOf("D"));
  151. }else{
  152. deptId = deptId.substring(0,3);
  153. }
  154. }*/
  155. deptId = "";
  156. comcode1and2List = sysDeptService.selectListWhere("and (id like ? or id = ? )and comlevel in('0','2') order by id ", new Object[]{deptId + "%", StringUtils.isNullOrEmpty(deptId) ? deptId : deptId.substring(0, 3)});
  157. for (TreeSysDeptVO treeSysDeptVO : comcode1and2List) {
  158. // 设置第一级
  159. if ("0".equals(treeSysDeptVO.getComlevel())) {
  160. SysDict sysDict = new SysDict();
  161. sysDict.setValue(treeSysDeptVO.getId());
  162. sysDict.setLabel(treeSysDeptVO.getName());
  163. sysDict.setDeptId(treeSysDeptVO.getId());
  164. sysDict.setDeptParentId(treeSysDeptVO.getParentId());
  165. sysDict.setDeptLevel(treeSysDeptVO.getComlevel());
  166. byLable.add(sysDict);
  167. }
  168. }
  169. for (TreeSysDeptVO treeSysDeptVO : comcode1and2List) {
  170. if ("2".equals(treeSysDeptVO.getComlevel())) {
  171. for (SysDict dict : byLable) {
  172. // 设置第二级
  173. if (dict.getDeptId().equals(treeSysDeptVO.getParentId())) {
  174. SysDict sysDict = new SysDict();
  175. sysDict.setValue(treeSysDeptVO.getId());
  176. sysDict.setLabel(treeSysDeptVO.getName());
  177. sysDict.setDeptId(treeSysDeptVO.getId());
  178. sysDict.setDeptParentId(treeSysDeptVO.getParentId());
  179. sysDict.setDeptLevel(treeSysDeptVO.getComlevel());
  180. dict.getChildren().add(sysDict);
  181. }
  182. }
  183. }
  184. }
  185. break;
  186. case "classcode":
  187. StringBuffer whereStr = new StringBuffer(" where 1=1 and del_flag=0"); //不含已删除
  188. ArrayList<String> params = new ArrayList<String>();
  189. whereStr.append(" and parent_id=?");
  190. params.add("0");
  191. List<EsmProduct> esmProducts = esmProductService.selectList(whereStr.toString(), params.toArray());
  192. byLable = new TreeList<>();
  193. for (EsmProduct product : esmProducts) {
  194. SysDict sysDict = new SysDict();
  195. sysDict.setValue(product.getId());
  196. sysDict.setLabel(product.getName());
  197. byLable.add(sysDict);
  198. }
  199. break;
  200. case "account":
  201. deptId = deptId.substring(0, 3);
  202. List<SysAccount> list = sysAccountService.findByDeptId(deptId);
  203. byLable = new TreeList<>();
  204. for (SysAccount sysAccount : list) {
  205. SysDict sysDict = new SysDict();
  206. sysDict.setValue(sysAccount.getId());
  207. sysDict.setLabel(sysAccount.getName());
  208. byLable.add(sysDict);
  209. }
  210. break;
  211. default:
  212. break;
  213. }
  214. }
  215. return HttpResult.ok(byLable);
  216. }
  217. private void getTree(List<SysDict> byLable, List<TreeSysDeptVO> treeSysDeptVOList) {
  218. if (!CollectionUtils.isEmpty(byLable)) {
  219. for (SysDict sysDict : byLable) {
  220. for (TreeSysDeptVO treeSysDeptVO : treeSysDeptVOList) {
  221. if (treeSysDeptVO.getParentId().equals(sysDict.getValue())) { // 第二层
  222. SysDict sysDict1 = new SysDict();
  223. sysDict1.setValue(treeSysDeptVO.getId());
  224. sysDict1.setLabel(treeSysDeptVO.getName());
  225. sysDict1.setDeptId(treeSysDeptVO.getId());
  226. sysDict1.setDeptParentId(treeSysDeptVO.getParentId());
  227. sysDict1.setDeptLevel(treeSysDeptVO.getComlevel());
  228. sysDict.getChildren().add(sysDict1);
  229. }
  230. }
  231. getTree(sysDict.getChildren(), treeSysDeptVOList);
  232. }
  233. }
  234. }
  235. @GetMapping(value = "/findMyDictTypeTree")
  236. public HttpResult findMyDictTypeTree() {
  237. SysUser sysUser = getUser();
  238. return HttpResult.ok(sysDictTypeService.selectAll());
  239. }
  240. @GetMapping(value = "/findLeftDictTree")
  241. public HttpResult findLeftDictTree(String deptCode) {
  242. //return HttpResult.ok(sysDictTypeServicexx.selectListTreeSysDictVO(deptCode));
  243. List<SysDictType> lst = sysDictTypeService.selectAll();
  244. return HttpResult.ok(lst);
  245. }
  246. /**
  247. * 枚举类字典
  248. *
  249. * @author: lig
  250. * @date: 2023年04月10日 0010
  251. */
  252. @ApiOperation(value = "根据多个字典类型查询字典数据")
  253. @GetMapping("/listAllDataByTypes")
  254. public List<DictionaryManagement> listAllDataByTypes(@RequestParam Set<String> dictTypeList) {
  255. List<DictionaryManagement> dmList = new ArrayList<>();
  256. Map<String, DictionaryManagement> dmMap = DictEnumConfig.getDmMap();
  257. Set<String> dictTypeListRemain = new HashSet<>();
  258. for (String dictType : dictTypeList) {
  259. if (dmMap.containsKey(dictType)) {
  260. DictionaryManagement dictionaryManagement = dmMap.get(dictType);
  261. dmList.add(dictionaryManagement);
  262. } else {
  263. dictTypeListRemain.add(dictType);
  264. }
  265. }
  266. // List<DictionaryManagement> dictionaryManagements = dictionaryManagementService.listAllDataByTypes(dictTypeListRemain);
  267. // dmList.addAll(dictionaryManagements);
  268. return dmList;
  269. }
  270. /**
  271. * 枚举类
  272. *
  273. * @author: lig
  274. * @date: 2023年04月10日 0010
  275. */
  276. @ApiOperation(value = "获取字典列表")
  277. @GetMapping("/dictList")
  278. public List<DictionaryManagement> dictTypeList(String type) {
  279. List<DictionaryManagement> dictList = DictEnumConfig.dictManageList;
  280. if (StringUtils.isNotEmpty(type)) {
  281. List<DictionaryManagement> result = new ArrayList<>();
  282. dictList.stream().forEach(a -> {
  283. if (a.getDictType().equals(type)) {
  284. result.add(a);
  285. }
  286. });
  287. return result;
  288. }
  289. return dictList;
  290. }
  291. }