SysDictController.java 14 KB

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