|  |  | 
 |  |  | import com.vincent.rsf.server.manager.service.MatnrGroupService; | 
 |  |  | import com.vincent.rsf.server.system.controller.BaseController; | 
 |  |  | import io.swagger.annotations.Api; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.security.access.prepost.PreAuthorize; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  |  | 
 |  |  | import javax.servlet.http.HttpServletResponse; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | @Api(tags = "物料分组") | 
 |  |  | @RestController | 
 |  |  | 
 |  |  |     @PostMapping("/matnrGroup/page") | 
 |  |  |     public R page(@RequestBody Map<String, Object> map) { | 
 |  |  |         BaseParam baseParam = buildParam(map, BaseParam.class); | 
 |  |  |         baseParam.setOrderBy("code asc"); | 
 |  |  |         PageParam<MatnrGroup, BaseParam> pageParam = new PageParam<>(baseParam, MatnrGroup.class); | 
 |  |  |         return R.ok().add(matnrGroupService.page(pageParam, pageParam.buildWrapper(true))); | 
 |  |  |     } | 
 |  |  | 
 |  |  |     @PostMapping("/matnrGroup/save") | 
 |  |  |     public R save(@RequestBody MatnrGroup matnrGroup) { | 
 |  |  |         if (Objects.isNull(matnrGroup)) { | 
 |  |  |             throw new CoolException("参数不能为空!!"); | 
 |  |  |             return R.error("参数不能为空!!"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if (Objects.isNull(matnrGroup.getName())) { | 
 |  |  |             throw new CoolException("分组名称不能为空!!"); | 
 |  |  |             return R.error("分组名称不能为空!!"); | 
 |  |  |         } | 
 |  |  |         if (Objects.isNull(matnrGroup.getCode())) { | 
 |  |  |             throw new CoolException("分组编码不能为空!!"); | 
 |  |  |             return R.error("分组编码不能为空!!"); | 
 |  |  |         } | 
 |  |  |         matnrGroup.setCreateBy(getLoginUserId()); | 
 |  |  |         matnrGroup.setUpdateBy(getLoginUserId()); | 
 |  |  |  | 
 |  |  |         List<MatnrGroup> list = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>() | 
 |  |  |                 .eq(!Objects.isNull(matnrGroup.getParCode()), MatnrGroup::getParCode, matnrGroup.getParCode()) | 
 |  |  |                 .eq(MatnrGroup::getCode, matnrGroup.getCode())); | 
 |  |  |         if (!list.isEmpty()) { | 
 |  |  |             throw new CoolException("物料分组编码不能重复!!"); | 
 |  |  |             return R.error("物料分组编码不能重复!!"); | 
 |  |  |         } | 
 |  |  |         List<MatnrGroup> groups = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>() | 
 |  |  |                 .eq(MatnrGroup::getName, matnrGroup.getName())); | 
 |  |  |         if (!groups.isEmpty()) { | 
 |  |  |             throw new CoolException("分组已存在,请勿重复添加!!"); | 
 |  |  |             return R.error("分组已存在,请勿重复添加!!"); | 
 |  |  |         } | 
 |  |  |         //判断上级编码是否为空 | 
 |  |  |         if (!Objects.isNull(matnrGroup.getParCode()) && !StringUtils.isBlank(matnrGroup.getParCode())) { | 
 |  |  |             matnrGroup.setParCode(matnrGroup.getParCode() + matnrGroup.getCode()); | 
 |  |  |         } | 
 |  |  |         if (!matnrGroupService.save(matnrGroup)) { | 
 |  |  |             return R.error("Save Fail"); | 
 |  |  | 
 |  |  |     @OperationLog("Update 物料分类表") | 
 |  |  |     @PostMapping("/matnrGroup/update") | 
 |  |  |     public R update(@RequestBody MatnrGroup matnrGroup) { | 
 |  |  |  | 
 |  |  |         matnrGroup.setUpdateBy(getLoginUserId()); | 
 |  |  |         MatnrGroup matGroup = matnrGroupService.getById(matnrGroup.getId()); | 
 |  |  |         if (Objects.isNull(matGroup)) { | 
 |  |  |             return R.error("物料分组不存在!!"); | 
 |  |  |         } | 
 |  |  |         if (!matnrGroup.getName().equals(matGroup.getName())) { | 
 |  |  |             List<MatnrGroup> areasList = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>().eq(MatnrGroup::getName, matnrGroup.getName())); | 
 |  |  |             if (!areasList.isEmpty()) { | 
 |  |  |                 throw new CoolException("仓库名已存在!!"); | 
 |  |  |                 return R.error("仓库名已存在!!"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         if (!matnrGroup.getCode().equals(matGroup.getCode())) { | 
 |  |  |             List<MatnrGroup> areasList = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>().eq(MatnrGroup::getCode, matnrGroup.getCode())); | 
 |  |  |         if (!Objects.isNull(matnrGroup.getCode()) && !matnrGroup.getCode().equals(matGroup.getCode())) { | 
 |  |  |             List<MatnrGroup> areasList = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>() | 
 |  |  |                             .eq(!Objects.isNull(matnrGroup.getParCode()), MatnrGroup::getParCode, matnrGroup.getParCode()) | 
 |  |  |                     .eq(MatnrGroup::getCode, matnrGroup.getCode())); | 
 |  |  |             if (!areasList.isEmpty()) { | 
 |  |  |                 throw new CoolException("仓库编码已存在!!"); | 
 |  |  |                 return R.error("仓库编码已存在!!"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  | 
 |  |  |     @PreAuthorize("hasAuthority('manager:matnrGroup:list')") | 
 |  |  |     @PostMapping("/matnrGroup/tree") | 
 |  |  |     public R tree(@RequestBody(required = false) Map<String, Object> map) { | 
 |  |  |         List<MatnrGroup> matnrs = new ArrayList<>(); | 
 |  |  |         if (Objects.isNull(map)) { | 
 |  |  |             matnrs = matnrGroupService.list(new LambdaQueryWrapper<>()); | 
 |  |  |         } else { | 
 |  |  |             if (Objects.isNull(map.get("condition"))) { | 
 |  |  |                 return R.ok("condition参数不能为空!!"); | 
 |  |  |             } | 
 |  |  |             matnrs = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>().like(MatnrGroup::getName, map.get("condition"))); | 
 |  |  |             return R.error("参数不能为空!!"); | 
 |  |  |         } | 
 |  |  |         List<MatnrGroup> treeData = Utils.toTreeData(matnrs, 0L, MatnrGroup::getParentId, MatnrGroup::getId, MatnrGroup::setChildren); | 
 |  |  |         return R.ok().add(treeData); | 
 |  |  |         List<MatnrGroup> matnrs = matnrGroupService.getTreeData(map); | 
 |  |  |  | 
 |  |  |         return R.ok().add(matnrs); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  |