package com.gkhy.labRiskManage.domain.basic.service.impl; import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentDeviceAppInsertBO; import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentDeviceAppQueryBO; import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentDeviceAppUpdateBO; import com.gkhy.labRiskManage.application.basic.dto.dto.BasicExperimentDeviceAppListDTO; import com.gkhy.labRiskManage.commons.domain.SearchResult; import com.gkhy.labRiskManage.commons.enums.ResultCode; import com.gkhy.labRiskManage.commons.enums.StatusEnum; import com.gkhy.labRiskManage.commons.enums.UserTagEnum; import com.gkhy.labRiskManage.commons.exception.BusinessException; import com.gkhy.labRiskManage.commons.utils.BeanCopyUtils; import com.gkhy.labRiskManage.domain.account.model.dto.UserInfoDomainDTO; import com.gkhy.labRiskManage.domain.account.service.UserDomainService; import com.gkhy.labRiskManage.domain.basic.converter.ToBasicDeviceInfoConverter; import com.gkhy.labRiskManage.domain.basic.converter.ToQueryDeviceBOConverter; import com.gkhy.labRiskManage.domain.basic.converter.ToUpdateDeviceBOConverter; import com.gkhy.labRiskManage.domain.basic.entity.BasicExperimentDevice; import com.gkhy.labRiskManage.domain.basic.entity.BasicExperimentDeviceType; import com.gkhy.labRiskManage.domain.basic.model.bo.DeviceInsertBO; import com.gkhy.labRiskManage.domain.basic.model.bo.DeviceQueryBO; import com.gkhy.labRiskManage.domain.basic.model.bo.DeviceUpdateBO; import com.gkhy.labRiskManage.domain.basic.model.dto.*; import com.gkhy.labRiskManage.domain.basic.repository.jpa.BasicExperimentDeviceRepository; import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentDeviceService; import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentDeviceTypeService; import com.gkhy.labRiskManage.domain.riskReport.utils.GetRoleTagUtils; import com.gkhy.labRiskManage.domain.riskReport.utils.SearchAuthUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; /** * 基础仪器设备表 */ @Service public class BasicExperimentDeviceServiceImpl implements BasicExperimentDeviceService { @Autowired private BasicExperimentDeviceRepository basicExperimentDeviceRepository; @Autowired private UserDomainService userDomainService; @Autowired private BasicExperimentDeviceTypeService deviceTypeService; /** * 基础仪器设备表 - 新增 * */ @Override public DeviceInsertDTO insertBasicExperimentDevice(Long currentUserId, BasicExperimentDeviceAppInsertBO basicExperimentDeviceAppInsertBO) { if (currentUserId < 0){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆"); } //接收、转换参数 ToBasicDeviceInfoConverter deviceParam = new ToBasicDeviceInfoConverter(); DeviceInsertBO deviceInsertBO = deviceParam.toBasicDeviceInfoConvert(basicExperimentDeviceAppInsertBO); //参数校验 if (ObjectUtils.isEmpty(deviceInsertBO.getDeviceCode())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备编码不能为空"); } BasicExperimentDevice deviceByCode = basicExperimentDeviceRepository.getDeviceByCode(deviceInsertBO.getDeviceCode()); // if (!ObjectUtils.isEmpty(deviceByCode)){ // throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"设备编码已存在"); // } if (ObjectUtils.isEmpty(deviceInsertBO.getDeviceName())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备名称不能为空"); } BasicExperimentDevice deviceByName = basicExperimentDeviceRepository.getDeviceByName(deviceInsertBO.getDeviceName()); // if (!ObjectUtils.isEmpty(deviceByName)){ // throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"设备名称已存在"); // } if (ObjectUtils.isEmpty(deviceInsertBO.getSpecialDevice())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备类型不能为空"); } if (ObjectUtils.isEmpty(deviceInsertBO.getDevicePower())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备功率不能为空"); } if (ObjectUtils.isEmpty(deviceInsertBO.getDeviceUnit())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备计量单位不能为空"); } if (ObjectUtils.isEmpty(deviceInsertBO.getDeviceTypeId())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备类型不能为空"); } BasicExperimentDeviceType deviceTypeById = deviceTypeService.getDeviceTypeById(deviceInsertBO.getDeviceTypeId()); if (ObjectUtils.isEmpty(deviceTypeById)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"找不到该类型的设备,请检查输入是否正确,或者先添加设备类型"); } if (ObjectUtils.isEmpty(deviceInsertBO.getDeviceDesc())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备型号不能为空"); } if (ObjectUtils.isEmpty(deviceInsertBO.getDeviceStatus())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备状态不能为空"); } if (ObjectUtils.isEmpty(deviceInsertBO.getBuyTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备购买时间不能为空"); } if (ObjectUtils.isEmpty(deviceInsertBO.getAdjustTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备校准时间不能为空"); } //获取需要的参数 LocalDateTime date = LocalDateTime.now(); if (date.isBefore(deviceInsertBO.getBuyTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备购买时间不能在当前时间之后"); } if (date.isBefore(deviceInsertBO.getAdjustTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备校准时间不能在当前时间之后"); } if (deviceInsertBO.getAdjustTime().isBefore(deviceInsertBO.getBuyTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备校准时间不能在购买时间之后"); } //获取需要的参数 BasicExperimentDevice device = BeanCopyUtils.copyBean(deviceInsertBO, BasicExperimentDevice.class); UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId); device.setCreateTime(date); device.setUpdateTime(date); device.setCreateByUserId(userInfoById.getId()); device.setUpdateByUserId(userInfoById.getId()); device.setDeleteStatus(StatusEnum.DELETE_NOT.getCode().byteValue()); device.setDeviceType(deviceTypeById.getDeviceType()); BasicExperimentDevice saveResult = basicExperimentDeviceRepository.save(device); DeviceInsertDTO deviceInsertDTO = BeanCopyUtils.copyBean(saveResult, DeviceInsertDTO.class); return deviceInsertDTO; } /** * 基础仪器设备表 - 分页查询 * */ @Override public SearchResult getBasicExperimentDevicePage(Long currentUserId, BasicExperimentDeviceAppQueryBO deviceQueryBO) { //校验参数 if (ObjectUtils.isEmpty(deviceQueryBO.getPageSize())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"分页信息不能为空"); } if (ObjectUtils.isEmpty(deviceQueryBO.getPageIndex())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"分页信息不能为空"); } if (deviceQueryBO.getDeviceCode() == ""){ deviceQueryBO.setDeviceCode(null); } if (deviceQueryBO.getDevicePower() == ""){ deviceQueryBO.setDevicePower(null); } if (deviceQueryBO.getDeviceName() == ""){ deviceQueryBO.setDeviceName(null); } ToQueryDeviceBOConverter toQueryDeviceBOConverter = new ToQueryDeviceBOConverter(); DeviceQueryBO queryPageParam = toQueryDeviceBOConverter.ToQueryDeviceDTOConverter(deviceQueryBO); SearchResult searchResult = new SearchResult<>(); searchResult.setPageIndex(queryPageParam.getPageIndex()); searchResult.setPageSize(queryPageParam.getPageSize()); UserInfoDomainDTO user = userDomainService.getUserById(currentUserId); int roleTag = GetRoleTagUtils.GetRoleTagUtils(user); //组装查询条件 Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { List predicateList = new ArrayList<>(); if (deviceQueryBO.getDeviceCode() != null && !deviceQueryBO.getDeviceCode().equals("")){ predicateList.add(criteriaBuilder.equal(root.get("deviceCode"), deviceQueryBO.getDeviceCode())); } if (deviceQueryBO.getDeviceName() != null && !deviceQueryBO.getDeviceName().equals("")){ predicateList.add(criteriaBuilder.equal(root.get("deviceName"), deviceQueryBO.getDeviceName())); } if (deviceQueryBO.getDevicePower() != null && !deviceQueryBO.getDevicePower().equals("")){ predicateList.add(criteriaBuilder.equal(root.get("devicePower"), deviceQueryBO.getDevicePower())); } if (deviceQueryBO.getSpecialDevice() != null && !deviceQueryBO.getSpecialDevice().equals("")){ predicateList.add(criteriaBuilder.equal(root.get("specialDevice"), deviceQueryBO.getSpecialDevice())); } if (SearchAuthUtils.basicSearchAuth() == 1){ if (roleTag == UserTagEnum.USER_TAG_0.getCode()){ predicateList.add(criteriaBuilder.equal(root.get("createByUserId"), currentUserId)); } } predicateList.add(criteriaBuilder.equal(root.get("deleteStatus"),StatusEnum.DELETE_NOT.getCode())); //返回组装的条件 return criteriaBuilder.and(predicateList.toArray(predicateList.toArray(new Predicate[0]))); } }; PageRequest page = PageRequest.of(queryPageParam.getPageIndex() - 1, queryPageParam.getPageSize(), Sort.Direction.DESC, "updateTime"); Page pageResult = basicExperimentDeviceRepository.findAll(specification, page); List deviceQueryDTO = BeanCopyUtils.copyBeanList(pageResult.getContent(), DeviceQueryDTO.class); List userList = userDomainService.getUserList(); for (DeviceQueryDTO queryDTO : deviceQueryDTO) { for (UserInfoDomainDTO userInfo : userList) { if (userInfo.getId() == queryDTO.getCreateByUserId()){ queryDTO.setCreateByUserName(userInfo.getRealName()); } if (userInfo.getId() == queryDTO.getUpdateByUserId()){ queryDTO.setUpdateByUserName(userInfo.getRealName()); } } } searchResult.setTotal(pageResult.getTotalElements()); searchResult.setData(deviceQueryDTO); return searchResult; } /** * 基础仪器设备表 - 修改 * */ @Override public DeviceUpdateDTO updateBasicExperimentDevice(Long currentUserId, BasicExperimentDeviceAppUpdateBO deviceAppUpdateDO) { if (currentUserId < 0){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆"); } //接收、转换参数 ToUpdateDeviceBOConverter converter = new ToUpdateDeviceBOConverter(); DeviceUpdateBO updateDeviceParam = converter.toUpdateDeviceDO(deviceAppUpdateDO); //参数校验 if (ObjectUtils.isEmpty(updateDeviceParam.getId())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "修改参数错误"); } BasicExperimentDevice deviceById = basicExperimentDeviceRepository.getDeviceById(updateDeviceParam.getId()); if (ObjectUtils.isEmpty(deviceById)){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR.getCode(), "要修改的数据不存在,或已被删除"); } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getDeviceCode())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备编码不能为空"); } BasicExperimentDevice deviceByCode = basicExperimentDeviceRepository.getDeviceByCode(deviceAppUpdateDO.getDeviceCode()); // if (!ObjectUtils.isEmpty(deviceByCode) && !deviceByCode.getId().equals(updateDeviceParam.getId())){ // throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"设备编码已存在"); // } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getDeviceName()) && !deviceByCode.getId().equals(updateDeviceParam.getId())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备名称不能为空"); } // BasicExperimentDevice deviceByName = basicExperimentDeviceRepository.getDeviceByName(deviceAppUpdateDO.getDeviceName()); // if (!ObjectUtils.isEmpty(deviceByName)){ // throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"设备名称已存在"); // } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getSpecialDevice())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备类型不能为空"); } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getDevicePower())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备功率不能为空"); } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getDeviceUnit())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备计量单位不能为空"); } if (ObjectUtils.isEmpty(deviceAppUpdateDO)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"找不到该类型的设备,请检查输入是否正确,或者先添加设备类型"); } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getDeviceDesc())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备型号不能为空"); } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getDeviceStatus())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备状态不能为空"); } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getBuyTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备购买时间不能为空"); } if (ObjectUtils.isEmpty(deviceAppUpdateDO.getAdjustTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备校准时间不能为空"); } //获取需要的参数 LocalDateTime date = LocalDateTime.now(); if (date.isBefore(deviceAppUpdateDO.getBuyTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备购买时间不能在当前时间之后"); } if (date.isBefore(deviceAppUpdateDO.getAdjustTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备校准时间不能在当前时间之后"); } if (deviceAppUpdateDO.getAdjustTime().isBefore(deviceAppUpdateDO.getBuyTime())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"设备校准时间不能在购买时间之后"); } BasicExperimentDevice device = BeanCopyUtils.copyBean(deviceById, BasicExperimentDevice.class); UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId); device.setUpdateTime(date); device.setUpdateByUserId(userInfoById.getId()); device.setDeviceCode(updateDeviceParam.getDeviceCode()); device.setDeviceName(updateDeviceParam.getDeviceName()); device.setDevicePower(updateDeviceParam.getDevicePower()); device.setDeviceUnit(updateDeviceParam.getDeviceUnit()); device.setSpecialDevice(updateDeviceParam.getSpecialDevice()); device.setSafeProtect(updateDeviceParam.getSafeProtect()); device.setDeviceTypeId(updateDeviceParam.getDeviceTypeId()); device.setDeviceType(updateDeviceParam.getDeviceType()); device.setDeviceDesc(updateDeviceParam.getDeviceDesc()); device.setDeviceStatus(updateDeviceParam.getDeviceStatus()); device.setBuyTime(updateDeviceParam.getBuyTime()); device.setAdjustTime(updateDeviceParam.getAdjustTime()); BasicExperimentDevice saveResult = basicExperimentDeviceRepository.save(device); DeviceUpdateDTO deviceUpdateDTO = BeanCopyUtils.copyBean(saveResult, DeviceUpdateDTO.class); return deviceUpdateDTO; } /** * 基础仪器设备表 - 删除 * */ @Override public DeviceDeleteDTO deleteBasicExperimentDevice(Long currentUserId, Long id) { if (currentUserId < 0){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆"); } if (ObjectUtils.isEmpty(id)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode() ,"请选择正常的设备进行删除"); } BasicExperimentDevice deviceById = basicExperimentDeviceRepository.getDeviceById(id); if (ObjectUtils.isEmpty(deviceById)){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR.getCode() ,"要删除的设备不存在,或已被删除"); } UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId); LocalDateTime date = LocalDateTime.now(); //设置数据为删除 deviceById.setDeleteStatus(StatusEnum.DELETED.getCode().byteValue()); deviceById.setUpdateByUserId(userInfoById.getId()); deviceById.setUpdateTime(date); BasicExperimentDevice deleteResult = basicExperimentDeviceRepository.save(deviceById); return BeanCopyUtils.copyBean(deleteResult, DeviceDeleteDTO.class); } /** * 基础仪器设备表 - 设备列表 * */ @Override public List listBasicExperimentDevice(Long currentUserId) { UserInfoDomainDTO user = userDomainService.getUserById(currentUserId); int roleTag = GetRoleTagUtils.GetRoleTagUtils(user); List deviceList = new ArrayList<>(); if (SearchAuthUtils.basicSearchAuth() == 0){ deviceList = basicExperimentDeviceRepository.listDevice(); return BeanCopyUtils.copyBeanList(deviceList, BasicExperimentDeviceAppListDTO.class); } if (roleTag != UserTagEnum.USER_TAG_0.getCode()){ deviceList = basicExperimentDeviceRepository.listDevice(); }else { deviceList = basicExperimentDeviceRepository.listDeviceByUserId(currentUserId); } return BeanCopyUtils.copyBeanList(deviceList, BasicExperimentDeviceAppListDTO.class); } /** * 基础仪器设备表 - 通过id查询 * */ @Override public DeviceQueryDTO getBasicExperimentDeviceById(Long id) { if (ObjectUtils.isEmpty(id)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请求参数不能为空"); } BasicExperimentDevice deviceById = basicExperimentDeviceRepository.getDeviceById(id); if (ObjectUtils.isEmpty(deviceById)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "风险源信息不存在,请检查是否输入有误或已被删除"); } return BeanCopyUtils.copyBean(deviceById, DeviceQueryDTO.class); } /** * 基础仪器设备表 - 通过id列表查询 * */ @Override public List getBasicExperimentDeviceByIdList(List ids) { if (ids.size() < 1){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "请求参数不能为空"); } List listResult = basicExperimentDeviceRepository.batchById(ids); if (listResult.size() < 1){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "查询结果为空"); } return BeanCopyUtils.copyBeanList(listResult, DeviceQueryDTO.class); } }