package org.jeecg.modules.dnc.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.shiro.SecurityUtils; import org.jeecg.common.system.vo.LoginUser; import org.jeecg.modules.dnc.dto.ComponentExt; import org.jeecg.modules.dnc.exception.ExceptionCast; import org.jeecg.modules.dnc.mapper.ComponentInfoMapper; import org.jeecg.modules.dnc.mapper.ProductInfoMapper; import org.jeecg.modules.dnc.response.CommonCode; import org.jeecg.modules.dnc.response.ComponentInfoCode; import org.jeecg.modules.dnc.response.ProductInfoCode; import org.jeecg.modules.dnc.response.UcenterCode; import org.jeecg.modules.dnc.service.*; import org.jeecg.modules.dnc.ucenter.Department; import org.jeecg.modules.dnc.utils.ValidateUtil; import org.jeecg.modules.dnc.entity.*; import org.jeecg.modules.system.entity.SysUser; import org.jeecg.modules.dnc.entity.ComponentInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @Service public class ComponentInfoSeServiceImpl extends ServiceImpl implements IComponentInfoService { @Autowired private IComponentPermissionService componentPermissionService; @Autowired public ProductInfoMapper productInfoMapper; @Autowired private IPermissionStreamService permissionStreamService; @Autowired private IComponentDepartmentService componentDepartmentService; @Autowired @Lazy private IPartsInfoService partsInfoService; @Autowired private IProcessStreamService processStreamService; @Autowired private IDocInfoService docInfoService; @Autowired private INcLogInfoService iNcLogInfoService; @Override @Transactional(rollbackFor = {Exception.class}) public boolean addComponentInfo(ComponentInfo componentInfo) { if(componentInfo == null) ExceptionCast.cast(CommonCode.INVALID_PARAM); if(!ValidateUtil.validateString(componentInfo.getComponentName())) ExceptionCast.cast(ComponentInfoCode.COMPONENT_NAME_NONE); if(!ValidateUtil.validateString(componentInfo.getProductId())) ExceptionCast.cast(ComponentInfoCode.COMPONENT_PRODUCT_NONE); if(!ValidateUtil.validateString(componentInfo.getComponentCode())) ExceptionCast.cast(ComponentInfoCode.COMPONENT_CODE_NONE); ComponentInfo en = getByCode(componentInfo.getComponentCode()); if(en != null) ExceptionCast.cast(ComponentInfoCode.COMPONENT_IS_EXIST); LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST); List oldPermissionList; ProductInfo productInfo = productInfoMapper.selectById(componentInfo.getProductId()); if(productInfo == null) ExceptionCast.cast(ComponentInfoCode.COMPONENT_PRODUCT_NONE); if (ValidateUtil.validateString(componentInfo.getParentId())) { en = super.getById(componentInfo.getParentId()); if(en == null) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_PARENT_NOT_EXIST); } oldPermissionList = permissionStreamService.getByComponentId(en.getProductId(), en.getComponentId()); componentInfo.setRankLevel(en.getRankLevel() + 1); } else { oldPermissionList = permissionStreamService.getByProductId(productInfo.getProductId()); componentInfo.setParentId(null); componentInfo.setRankLevel(1); } componentInfo.setComponentStatus(1); //添加日志 NcLogInfo ncLogInfo = new NcLogInfo(); //模块 ncLogInfo.setModuleInfo("产品结构树"); //类型 ncLogInfo.setOperateType(2); //日志内容 ncLogInfo.setLogContent("部件名称:"+componentInfo.getComponentName()+",部件代号:"+componentInfo.getComponentCode()); iNcLogInfoService.saveLogNcInfos(ncLogInfo); boolean b = super.save(componentInfo); if(!b) ExceptionCast.cast(CommonCode.FAIL); //部门权限 List componentDepartmentList = new ArrayList<>(); List componentDepartPermList = new ArrayList<>(); if(oldPermissionList != null && !oldPermissionList.isEmpty()) { oldPermissionList.forEach(item -> { ComponentDepartment componentDepartment = new ComponentDepartment(); componentDepartment.setComponentId(componentInfo.getComponentId()); componentDepartment.setDepartId(item.getDepartId()); componentDepartmentList.add(componentDepartment); PermissionStream permissionStream = new PermissionStream(); permissionStream.setProductId(componentInfo.getProductId()); permissionStream.setComponentId(componentInfo.getComponentId()); permissionStream.setDepartId(item.getDepartId()); componentDepartPermList.add(permissionStream); }); } if(!componentDepartmentList.isEmpty()) { b = componentDepartmentService.saveBatch(componentDepartmentList); if(!b) ExceptionCast.cast(CommonCode.FAIL); } if(!componentDepartPermList.isEmpty()) { b = permissionStreamService.saveBatch(componentDepartPermList); if(!b) ExceptionCast.cast(CommonCode.FAIL); } ComponentPermission permission = new ComponentPermission(); permission.setComponentId(componentInfo.getComponentId()); permission.setUserId(userId); b = componentPermissionService.save(permission); if(!b) ExceptionCast.cast(CommonCode.FAIL); //添加默认工序 ProcessStream processStream = new ProcessStream(); processStream.setProductId(componentInfo.getProductId()); processStream.setComponentId(componentInfo.getComponentId()); processStream.setProcessCode("1"); //添加日志 NcLogInfo ncLogInfogx = new NcLogInfo(); //模块 ncLogInfogx.setModuleInfo("产品结构树"); //类型 ncLogInfogx.setOperateType(2); //日志内容 ncLogInfogx.setLogContent("部件名称:"+componentInfo.getComponentName()+"生成默认工序,工序号:"+processStream.getProcessCode()); iNcLogInfoService.saveLogNcInfos(ncLogInfogx); b = processStreamService.save(processStream); if(!b) ExceptionCast.cast(CommonCode.FAIL); //添加权限 PermissionStream stream = new PermissionStream(); stream.setUserId(userId); stream.setProductId(componentInfo.getProductId()); stream.setComponentId(componentInfo.getComponentId()); return permissionStreamService.save(stream); } @Override @Transactional(rollbackFor = {Exception.class}) public boolean editComponentInfo(String id, ComponentInfo componentInfo) { if(!ValidateUtil.validateString(id) || componentInfo == null) ExceptionCast.cast(CommonCode.INVALID_PARAM); LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = user.getId(); if(!ValidateUtil.validateString(userId)) ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST); ComponentInfo en = super.getById(id); if(en == null) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_NOT_EXIST); } componentInfo.setComponentId(id); //设置为空 则不会更新 componentInfo.setParentId(null); componentInfo.setComponentStatus(null); componentInfo.setRankLevel(null); componentInfo.setProductId(null); //添加日志 NcLogInfo ncLogInfo = new NcLogInfo(); //模块 ncLogInfo.setModuleInfo("产品结构树"); //类型 ncLogInfo.setOperateType(3); //日志内容 ncLogInfo.setLogContent("修改部件名称:"+productInfoMapper.selectById(en.getProductId()).getProductName()+"子集部件"); //修改保存备注 ncLogInfo.setRemark(JSONObject.toJSONString(en)); iNcLogInfoService.saveLogNcInfos(ncLogInfo); boolean b = super.updateById(componentInfo); if(!b) return false; ComponentPermission permission = componentPermissionService.getByComponentIdAndUserId(id, userId); if(permission == null) { permission = new ComponentPermission(); permission.setComponentId(id); permission.setUserId(userId); b = componentPermissionService.save(permission); if(!b) return false; } PermissionStream stream = permissionStreamService.getByComponentIdAndUserId(en.getProductId(), id, userId); if(stream == null) { stream = new PermissionStream(); stream.setUserId(userId); stream.setProductId(en.getProductId()); stream.setComponentId(id); return permissionStreamService.save(stream); } return b; } @Override public List findExtAll() { return super.getBaseMapper().findExtAll(); } @Override public List getByUserPerms(String userId) { if(!ValidateUtil.validateString(userId)) return Collections.emptyList(); return super.getBaseMapper().getByUserPerms(userId); } @Override public List getByUserPerms(String userId, String queryParam) { if(!ValidateUtil.validateString(userId)) return Collections.emptyList(); //去除权限 TODO LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); if(ValidateUtil.validateString(queryParam)) { queryWrapper.and(wrapper->wrapper.like(ComponentInfo::getComponentCode, queryParam) .or() .like(ComponentInfo::getComponentName, queryParam)); } return super.list(queryWrapper); } @Override public List getByUserPermsAs(String userId) { if(!ValidateUtil.validateString(userId)) return Collections.emptyList(); return super.getBaseMapper().getByUserPermsAs(userId); } @Override public List getByProductId(String productId) { if(!ValidateUtil.validateString(productId)) return null; return super.lambdaQuery().eq(ComponentInfo::getProductId, productId).list(); } @Override @Transactional(rollbackFor = {Exception.class}) public boolean deleteCollections(List componentInfoList) { componentInfoList.forEach(item -> { boolean b = componentPermissionService.deleteByComponentId(item.getComponentId()); if(!b) ExceptionCast.cast(ComponentInfoCode.COMPONENT_DELETE_PERM_NONE); b = componentDepartmentService.deleteByComponentId(item.getComponentId()); if(!b) ExceptionCast.cast(ComponentInfoCode.COMPONENT_DELETE_PERM_NONE); b = permissionStreamService.deleteUserPermsByComponentId(item.getProductId(), item.getComponentId()); if(!b) ExceptionCast.cast(ComponentInfoCode.COMPONENT_DELETE_PERM_NONE); b = permissionStreamService.deleteDepartPermsByComponentId(item.getProductId(), item.getComponentId()); if(!b) ExceptionCast.cast(ComponentInfoCode.COMPONENT_DELETE_PERM_NONE); b = super.removeById(item.getComponentId()); if(!b) ExceptionCast.cast(ComponentInfoCode.COMPONENT_DELETE_PERM_NONE); }); return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean deleteComponentInfo(String id) { if(!ValidateUtil.validateString(id)) ExceptionCast.cast(ComponentInfoCode.COMPONENT_DELETE_ERROR); ComponentInfo componentInfo = super.getById(id); if(componentInfo == null) ExceptionCast.cast(ComponentInfoCode.COMPONENT_DELETE_ERROR); List childrenList = getByParentId(id); if(childrenList != null && !childrenList.isEmpty()) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_CHILD_EXIST); } //验证产品下是否有零件 List partsInfoList = partsInfoService.getByComponentId(componentInfo.getProductId(), componentInfo.getComponentId()); if(partsInfoList != null && !partsInfoList.isEmpty()) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_PARTS_EXIST); } List processStreamList = processStreamService.findByComponentId(id); if(processStreamList != null && !processStreamList.isEmpty()) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_PROCESS_EXIST); } //添加日志 NcLogInfo ncLogInfo = new NcLogInfo(); //模块 ncLogInfo.setModuleInfo("产品结构树"); //类型 ncLogInfo.setOperateType(4); //日志内容 ncLogInfo.setLogContent("部件名称:"+componentInfo.getComponentName()); iNcLogInfoService.saveLogNcInfos(ncLogInfo); boolean b = docInfoService.deleteByComponentId(id); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = componentPermissionService.deleteByComponentId(id); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = componentDepartmentService.deleteByComponentId(id); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = permissionStreamService.deleteUserPermsByComponentId(componentInfo.getProductId(), componentInfo.getComponentId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); b = permissionStreamService.deleteDepartPermsByComponentId(componentInfo.getProductId(), componentInfo.getComponentId()); if(!b) ExceptionCast.cast(CommonCode.FAIL); return super.removeById(id); } @Override public List getByParentId(String componentId) { List componentExtList = super.getBaseMapper().findByParentId(componentId); if(componentExtList == null || componentExtList.isEmpty()) return null; List list = new ArrayList<>(); componentExtList.forEach(item -> { item.getAllChildren(list); }); return list; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignAddUser(ComponentInfo componentInfo, Collection userList) { if(componentInfo == null || userList == null || userList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); List permissionList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); userList.forEach(item -> { ComponentPermission en = componentPermissionService.getByComponentIdAndUserId(componentInfo.getComponentId(), item.getId()); if(en == null) { en = new ComponentPermission(); en.setUserId(item.getId()); en.setComponentId(componentInfo.getComponentId()); permissionList.add(en); } PermissionStream stream = permissionStreamService.getByComponentIdAndUserId(componentInfo.getProductId(), componentInfo.getComponentId(), item.getId()); if(stream == null) { stream = new PermissionStream(); stream.setUserId(item.getId()); stream.setProductId(componentInfo.getProductId()); stream.setComponentId(componentInfo.getComponentId()); permissionStreamList.add(stream); } }); if(!permissionList.isEmpty()) { boolean b = componentPermissionService.saveBatch(permissionList); if(!b) { ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = permissionStreamService.saveBatch(permissionStreamList); if(!b) { ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR); } } return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignRemoveUser(ComponentInfo componentInfo, Collection userList) { if(componentInfo == null || userList == null || userList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); List permissionList = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); userList.forEach(item -> { ComponentPermission en = componentPermissionService.getByComponentIdAndUserId(componentInfo.getComponentId(), item.getId()); if(en != null) { permissionList.add(en); } PermissionStream stream = permissionStreamService.getByComponentIdAndUserId(componentInfo.getProductId(), componentInfo.getComponentId(), item.getId()); if(stream != null) { permissionStreamList.add(stream); } }); //校验部件用户权限清空操作 List existList = componentPermissionService.getByComponentId(componentInfo.getComponentId()); if(existList.size() <= permissionList.size()) ExceptionCast.cast(ComponentInfoCode.COMPONENT_USER_NONE); if(!permissionList.isEmpty()) { boolean b = componentPermissionService.removeByCollection(permissionList); if(!b) { ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = permissionStreamService.removeByCollection(permissionStreamList); if(!b) { ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR); } } return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignAddDepart(ComponentInfo componentInfo, Collection departmentList) { if(componentInfo == null || departmentList == null || departmentList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); List componentDepartments = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); departmentList.forEach(item -> { ComponentDepartment en = componentDepartmentService.getByComponentIdAndDepartId(componentInfo.getComponentId(), item.getDepartId()); if(en == null) { en = new ComponentDepartment(); en.setDepartId(item.getDepartId()); en.setComponentId(componentInfo.getComponentId()); componentDepartments.add(en); } PermissionStream stream = permissionStreamService.getByComponentIdAndDepartId(componentInfo.getProductId(), componentInfo.getComponentId(), item.getDepartId()); if(stream == null) { stream = new PermissionStream(); stream.setDepartId(item.getDepartId()); stream.setProductId(componentInfo.getProductId()); stream.setComponentId(componentInfo.getComponentId()); permissionStreamList.add(stream); } }); if(!componentDepartments.isEmpty()) { boolean b = componentDepartmentService.saveBatch(componentDepartments); if(!b) { ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = permissionStreamService.saveBatch(permissionStreamList); if(!b) { ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR); } } return true; } @Override @Transactional(rollbackFor = {Exception.class}) public boolean assignRemoveDepart(ComponentInfo componentInfo, Collection departmentList) { if(componentInfo == null || departmentList == null || departmentList.isEmpty()) ExceptionCast.cast(CommonCode.INVALID_PARAM); List componentDepartments = new ArrayList<>(); List permissionStreamList = new ArrayList<>(); departmentList.forEach(item -> { ComponentDepartment en = componentDepartmentService.getByComponentIdAndDepartId(componentInfo.getComponentId(), item.getDepartId()); if(en != null) { componentDepartments.add(en); } PermissionStream stream = permissionStreamService.getByComponentIdAndDepartId(componentInfo.getProductId(), componentInfo.getComponentId(), item.getDepartId()); if(stream != null) { permissionStreamList.add(stream); } }); if(!componentDepartments.isEmpty()) { boolean b = componentDepartmentService.removeByCollection(componentDepartments); if(!b) { ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR); } } if(!permissionStreamList.isEmpty()) { boolean b = permissionStreamService.removeByCollection(permissionStreamList); if(!b) { ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR); } } return true; } @Override public ComponentInfo getByCode(String code) { if(ValidateUtil.validateString(code)) { List list = super.lambdaQuery().eq(ComponentInfo::getComponentCode, code).list(); if(list == null || list.isEmpty()) return null; return list.get(0); } return null; } @Override public PermissionStream validateComponentOrPartsPnCode(String pnCode) { if(!ValidateUtil.validateString(pnCode)) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_PN_NOT_VALID); } PermissionStream stream = new PermissionStream(); List componentInfos = super.lambdaQuery().eq(ComponentInfo::getComponentCode, pnCode).list(); if(componentInfos == null || componentInfos.isEmpty()) { List list = partsInfoService.lambdaQuery().eq(PartsInfo::getPartsCode, pnCode).list(); if(list == null || list.isEmpty()) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_PN_NOT_EXIST); }else if(list.size() != 1) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_PN_NOT_ONLY); }else { PartsInfo partsInfo = list.get(0); stream.setProductId(partsInfo.getProductId()); stream.setComponentId(partsInfo.getComponentId()); stream.setPartsId(partsInfo.getPartsId()); return stream; } }else if(componentInfos.size() != 1){ ExceptionCast.cast(ComponentInfoCode.COMPONENT_PN_NOT_ONLY); }else { List list = partsInfoService.lambdaQuery().eq(PartsInfo::getPartsCode, pnCode).list(); if(list != null && list.size() > 0) { ExceptionCast.cast(ComponentInfoCode.COMPONENT_PN_NOT_ONLY); } ComponentInfo componentInfo = componentInfos.get(0); stream.setProductId(componentInfo.getProductId()); stream.setComponentId(componentInfo.getComponentId()); return stream; } return null; } @Override public List getByProductIdAndUserId(String productId, String userId) { //去除权限 TODO //return super.getBaseMapper().selectByProductIdAndUserId(productId, userId); LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(ComponentInfo::getProductId, productId); queryWrapper.eq(ComponentInfo::getRankLevel, 1); queryWrapper.orderByAsc(ComponentInfo::getCreateTime); return super.list(queryWrapper); } @Override public List getByParentIdAndUserId(String parentId, String userId) { //去除权限 TODO //return super.getBaseMapper().selectByParentIdAndUserId(parentId, userId); LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(ComponentInfo::getParentId, parentId); queryWrapper.orderByAsc(ComponentInfo::getCreateTime); return super.list(queryWrapper); } }