package com.ydtech.modules.admin.controller; import com.ydtech.config.DictEnumConfig; import com.ydtech.constants.enums.dict.DictionaryManagement; import com.ydtech.core.page.BaseController; import com.ydtech.core.page.HttpResult; import com.ydtech.core.page.PageRequest; import com.ydtech.modules.admin.model.SysAccount; import com.ydtech.modules.admin.model.SysDict; import com.ydtech.modules.admin.model.SysDictType; import com.ydtech.modules.admin.model.SysUser; import com.ydtech.modules.admin.service.SysAccountService; import com.ydtech.modules.admin.service.SysDeptService; import com.ydtech.modules.admin.service.SysDictService; import com.ydtech.modules.admin.service.SysDictTypeService; import com.ydtech.modules.admin.vo.TreeSysDeptVO; import com.ydtech.modules.esm.model.EsmInsCompany; import com.ydtech.modules.esm.model.EsmProduct; import com.ydtech.modules.esm.service.EsmInsCompanyService; import com.ydtech.modules.esm.service.EsmProductService; import com.ydtech.security.utils.JwtTokenUtils; import com.ydtech.utils.StringUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.collections4.list.TreeList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * 字典控制器 * * @author Yasepix * @date Oct 29, 2018 */ @Api(tags = "字典管理") @RestController @RequestMapping("dict") public class SysDictController extends BaseController { @Autowired private SysDictService sysDictService; @Autowired private SysDictTypeService sysDictTypeService; @Autowired private SysDeptService sysDeptService; @Autowired private EsmInsCompanyService esmInsCompanyService; @Autowired private EsmProductService esmProductService; @Autowired private SysAccountService sysAccountService; // @PreAuthorize("hasAuthority('sys:dict:add') AND hasAuthority('sys:dict:edit')") // @PostMapping(value="/save") // public HttpResult save(@RequestBody SysDict record) { // return HttpResult.ok(sysDictService.save(record)); // } // @PreAuthorize("hasAuthority('sys:dict:add')") @PostMapping(value = "/add") public HttpResult add(@RequestBody SysDict record) { // return HttpResult.ok(sysDictService.save(record)); Integer result = sysDictService.insertSelective(record); return HttpResult.ok(result); } // @PreAuthorize("hasAuthority('sys:dict:edit')") @PostMapping(value = "/edit") public HttpResult edit(@RequestBody SysDict record) { // return HttpResult.ok(sysDictService.save(record)); Integer result = sysDictService.updateSelective(record); return HttpResult.ok(result); } // @PreAuthorize("hasAuthority('sys:dict:add') AND hasAuthority('sys:dict:edit')") @PostMapping(value = "/savetype") public HttpResult savetype(@RequestBody SysDictType record) { return HttpResult.ok(sysDictTypeService.insertSelective(record)); } // @PreAuthorize("hasAuthority('sys:dict:delete')") @PostMapping(value = "/delete") public HttpResult delete(@RequestBody List records) { return HttpResult.ok(sysDictService.delete(records)); } // @PreAuthorize("hasAuthority('sys:dict:view')") @PostMapping(value = "/findPage") public HttpResult findPage(@RequestBody PageRequest pageRequest) { return HttpResult.ok(sysDictService.findPage(pageRequest)); } // // @PreAuthorize("hasAuthority('sys:dict:view')") @GetMapping(value = "/findByLable") public HttpResult findByLable(@RequestParam String lable, HttpServletRequest request) { List byLable = sysDictService.findByLable(lable); if (byLable == null || byLable.size() == 0) { // 获取用户名 String deptId = JwtTokenUtils.getDeptIdFromRequest(request); lable = lable.toLowerCase(); switch (lable) { case "inscompanycmain": List lst = esmInsCompanyService.list(); /*// 查询当前登录的用户对应的角色是不是管理员,是管理员的话显示全部供应商,否则显示当前用户对于的供应商 if (!StringUtils.isNullOrEmpty(deptId)){ if (SysConstants.DEPT.equals(deptId)){ lst = esmInsCompanyService.selectAll(); }else{ deptId=deptId.substring(0,3); lst = esmInsCompanyService.selectList(" where parent_id like ? and (parent_id='0' or LENGTH(t.parent_id)=3) ",new Object[]{deptId+"%"}); } }*/ byLable = new TreeList<>(); for (EsmInsCompany esmInsCompany : lst) { SysDict sysDict = new SysDict(); sysDict.setValue(esmInsCompany.getId()); sysDict.setLabel(esmInsCompany.getName()); byLable.add(sysDict); } break; case "comcode": case "ydcomcode": // 下拉框,要改成层级的那样 List treeSysDeptVOList = null; /*if (SysConstants.DEPT.equals(deptId)){ deptId=""; }else{ if (deptId.contains("D")){ deptId = deptId.substring(0,deptId.indexOf("D")); } if (3 == deptId.length()){ // 商户管理员 deptId = deptId; }else{ // 其他分支机构负责人 deptId = deptId.substring(0,5); } }*/ deptId = ""; treeSysDeptVOList = sysDeptService.selectListWhere("and id like ? order by id ", new Object[]{deptId + "%"}); for (TreeSysDeptVO treeSysDeptVO : treeSysDeptVOList) { if ("0".equals(treeSysDeptVO.getParentId())) { SysDict sysDict = new SysDict(); sysDict.setValue(treeSysDeptVO.getId()); sysDict.setLabel(treeSysDeptVO.getName()); sysDict.setDeptId(treeSysDeptVO.getId()); sysDict.setDeptParentId(treeSysDeptVO.getParentId()); sysDict.setDeptLevel(treeSysDeptVO.getComlevel()); byLable.add(sysDict); } } getTree(byLable, treeSysDeptVOList); break; case "comcode1and2": List comcode1and2List = null; /*if (SysConstants.DEPT.equals(deptId)){ deptId=""; }else{ if (deptId.length()!=3 && deptId.contains("D")){ deptId=deptId.substring(0,deptId.indexOf("D")); }else{ deptId = deptId.substring(0,3); } }*/ deptId = ""; 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)}); for (TreeSysDeptVO treeSysDeptVO : comcode1and2List) { // 设置第一级 if ("0".equals(treeSysDeptVO.getComlevel())) { SysDict sysDict = new SysDict(); sysDict.setValue(treeSysDeptVO.getId()); sysDict.setLabel(treeSysDeptVO.getName()); sysDict.setDeptId(treeSysDeptVO.getId()); sysDict.setDeptParentId(treeSysDeptVO.getParentId()); sysDict.setDeptLevel(treeSysDeptVO.getComlevel()); byLable.add(sysDict); } } for (TreeSysDeptVO treeSysDeptVO : comcode1and2List) { if ("2".equals(treeSysDeptVO.getComlevel())) { for (SysDict dict : byLable) { // 设置第二级 if (dict.getDeptId().equals(treeSysDeptVO.getParentId())) { SysDict sysDict = new SysDict(); sysDict.setValue(treeSysDeptVO.getId()); sysDict.setLabel(treeSysDeptVO.getName()); sysDict.setDeptId(treeSysDeptVO.getId()); sysDict.setDeptParentId(treeSysDeptVO.getParentId()); sysDict.setDeptLevel(treeSysDeptVO.getComlevel()); dict.getChildren().add(sysDict); } } } } break; case "classcode": StringBuffer whereStr = new StringBuffer(" where 1=1 and del_flag=0"); //不含已删除 ArrayList params = new ArrayList(); whereStr.append(" and parent_id=?"); params.add("0"); List esmProducts = esmProductService.selectList(whereStr.toString(), params.toArray()); byLable = new TreeList<>(); for (EsmProduct product : esmProducts) { SysDict sysDict = new SysDict(); sysDict.setValue(product.getId()); sysDict.setLabel(product.getName()); byLable.add(sysDict); } break; case "account": deptId = deptId.substring(0, 3); List list = sysAccountService.findByDeptId(deptId); byLable = new TreeList<>(); for (SysAccount sysAccount : list) { SysDict sysDict = new SysDict(); sysDict.setValue(sysAccount.getId()); sysDict.setLabel(sysAccount.getName()); byLable.add(sysDict); } break; default: break; } } return HttpResult.ok(byLable); } private void getTree(List byLable, List treeSysDeptVOList) { if (!CollectionUtils.isEmpty(byLable)) { for (SysDict sysDict : byLable) { for (TreeSysDeptVO treeSysDeptVO : treeSysDeptVOList) { if (treeSysDeptVO.getParentId().equals(sysDict.getValue())) { // 第二层 SysDict sysDict1 = new SysDict(); sysDict1.setValue(treeSysDeptVO.getId()); sysDict1.setLabel(treeSysDeptVO.getName()); sysDict1.setDeptId(treeSysDeptVO.getId()); sysDict1.setDeptParentId(treeSysDeptVO.getParentId()); sysDict1.setDeptLevel(treeSysDeptVO.getComlevel()); sysDict.getChildren().add(sysDict1); } } getTree(sysDict.getChildren(), treeSysDeptVOList); } } } @GetMapping(value = "/findMyDictTypeTree") public HttpResult findMyDictTypeTree() { SysUser sysUser = getUser(); return HttpResult.ok(sysDictTypeService.selectAll()); } @GetMapping(value = "/findLeftDictTree") public HttpResult findLeftDictTree(String deptCode) { //return HttpResult.ok(sysDictTypeServicexx.selectListTreeSysDictVO(deptCode)); List lst = sysDictTypeService.selectAll(); return HttpResult.ok(lst); } /** * 枚举类字典 * * @author: lig * @date: 2023年04月10日 0010 */ @ApiOperation(value = "根据多个字典类型查询字典数据") @GetMapping("/listAllDataByTypes") public List listAllDataByTypes(@RequestParam Set dictTypeList) { List dmList = new ArrayList<>(); Map dmMap = DictEnumConfig.getDmMap(); Set dictTypeListRemain = new HashSet<>(); for (String dictType : dictTypeList) { if (dmMap.containsKey(dictType)) { DictionaryManagement dictionaryManagement = dmMap.get(dictType); dmList.add(dictionaryManagement); } else { dictTypeListRemain.add(dictType); } } // List dictionaryManagements = dictionaryManagementService.listAllDataByTypes(dictTypeListRemain); // dmList.addAll(dictionaryManagements); return dmList; } /** * * 枚举类 * @author: lig * @date: 2023年04月10日 0010 */ @ApiOperation(value = "获取字典列表") @GetMapping("/dictList") public List dictTypeList(String type) { List dictList = DictEnumConfig.dictManageList; if (StringUtils.isNotEmpty(type)) { List result = new ArrayList<>(); dictList.stream().forEach(a -> { if(a.getDictType().equals(type)){ result.add(a); } }); return result; } return dictList; } }