package com.gkhy.labRiskManage.domain.basic.service.impl; import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentStuffAppInsertBO; import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentStuffAppQueryBO; import com.gkhy.labRiskManage.application.basic.dto.bo.BasicExperimentStuffAppUpdateBO; 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.entity.BasicExperimentStuff; import com.gkhy.labRiskManage.domain.basic.model.dto.*; import com.gkhy.labRiskManage.domain.basic.repository.jpa.BasicExperimentStuffRepository; import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentStuffService; 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 BasicExperimentStuffServiceImpl implements BasicExperimentStuffService { @Autowired private BasicExperimentStuffRepository stuffRepository; @Autowired private UserDomainService userDomainService; /** * 基础实验耗材管理 - 新增 */ @Override public StuffInsertDTO insertBasicExperimentStuff(Long currentUserId, BasicExperimentStuffAppInsertBO insertParam) { if (currentUserId < 0){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆"); } //参数校验 if (ObjectUtils.isEmpty(insertParam)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数不能为空"); } if (ObjectUtils.isEmpty(insertParam.getStuffName())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料名称不能为空"); } BasicExperimentStuff stuffByName = stuffRepository.getStuffByName(insertParam.getStuffName()); // if (!ObjectUtils.isEmpty(stuffByName)){ // throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "该材料已存在,不需要重复添加"); // } if (ObjectUtils.isEmpty(insertParam.getStuffCode())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料编号不能为空"); } if (ObjectUtils.isEmpty(insertParam.getStuffType())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料类型不能为空"); } if (!(insertParam.getStuffType() == 1 || insertParam.getStuffType() == 2)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料类型符合要求"); } if (ObjectUtils.isEmpty(insertParam.getStuffUnit())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "计量单位不能为空"); } if (!(insertParam.getStuffUnit() == 1 || insertParam.getStuffUnit() == 2 || insertParam.getStuffUnit() == 3 || insertParam.getStuffUnit() == 4)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "计量单位不符合要求"); } if (ObjectUtils.isEmpty(insertParam.getStuffStorage())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "存储放方式不能为空"); } UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId); BasicExperimentStuff stuff = BeanCopyUtils.copyBean(insertParam, BasicExperimentStuff.class); //设置必要参数 LocalDateTime date = LocalDateTime.now(); stuff.setCreateTime(date); stuff.setCreateByUserId(userInfoById.getId()); stuff.setUpdateTime(date); stuff.setUpdateByUserId(userInfoById.getId()); stuff.setDeleteStatus(StatusEnum.DELETE_NOT.getCode().byteValue()); BasicExperimentStuff insertResult = stuffRepository.save(stuff); return BeanCopyUtils.copyBean(insertResult, StuffInsertDTO.class); } /** * 基础实验耗材管理 - 查询 */ @Override public SearchResult selectBasicExperimentStuffPage(Long currentUserId, BasicExperimentStuffAppQueryBO queryParam) { //校验参数 if (ObjectUtils.isEmpty(queryParam.getPageSize())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"分页信息不能为空"); } if (ObjectUtils.isEmpty(queryParam.getPageIndex())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode() ,"分页信息不能为空"); } if (queryParam.getStuffName() == ""){ queryParam.setStuffName(null); } if (queryParam.getStuffCode() == ""){ queryParam.setStuffCode(null); } SearchResult searchResult = new SearchResult<>(); searchResult.setPageIndex(queryParam.getPageIndex()); searchResult.setPageSize(queryParam.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 (queryParam.getSiteName() != null && !queryParam.getSiteName().equals("")){ // predicateList.add(criteriaBuilder.equal(root.get("siteName"), queryParam.getSiteName())); // } if (queryParam.getStuffName() != null && !queryParam.getStuffName().equals("")){ predicateList.add(criteriaBuilder.like(root.get("stuffName"),"%"+queryParam.getStuffName()+"%")); } if (queryParam.getStuffCode() != null && !queryParam.getStuffCode().equals("")){ predicateList.add(criteriaBuilder.like(root.get("stuffCode"), "%"+queryParam.getStuffCode()+"%")); } if (queryParam.getStuffType() != null && !queryParam.getStuffType().equals("")){ predicateList.add(criteriaBuilder.equal(root.get("stuffType"), queryParam.getStuffType())); } 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(new Predicate[0])); } }; PageRequest pageParam = PageRequest.of(queryParam.getPageIndex() - 1, queryParam.getPageSize(), Sort.Direction.DESC, "updateTime"); Page pageResult = stuffRepository.findAll(specification, pageParam); List stuffQueryDTOS = BeanCopyUtils.copyBeanList(pageResult.getContent(), StuffQueryDTO.class); List userList = userDomainService.getUserList(); for (StuffQueryDTO stuffQueryDTO : stuffQueryDTOS) { for (UserInfoDomainDTO userInfo : userList) { if (userInfo.getId() == stuffQueryDTO.getCreateByUserId()){ stuffQueryDTO.setCreateByUserName(userInfo.getRealName()); } if (userInfo.getId() == stuffQueryDTO.getUpdateByUserId()){ stuffQueryDTO.setUpdateByUserName(userInfo.getRealName()); } } } searchResult.setData(stuffQueryDTOS); searchResult.setTotal(pageResult.getTotalElements()); return searchResult; } /** * 基础实验耗材管理 - 修改 */ @Override public StuffUpdateDTO updateBasicExperimentSite(Long currentUserId, BasicExperimentStuffAppUpdateBO updateParam) { if (currentUserId < 0){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode() ,"当前用户无效,请重新登陆"); } //参数校验 if (ObjectUtils.isEmpty(updateParam)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数不能为空"); } if (ObjectUtils.isEmpty(updateParam.getStuffName())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料名称不能为空"); } if (ObjectUtils.isEmpty(updateParam.getStuffCode())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料编号不能为空"); } if (ObjectUtils.isEmpty(updateParam.getStuffType())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料类型不能为空"); } if (!(updateParam.getStuffType() == 1 || updateParam.getStuffType() == 2)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "材料类型不符合要求"); } if (ObjectUtils.isEmpty(updateParam.getStuffUnit())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "计量单位不能为空"); } if (!(updateParam.getStuffUnit() == 1 || updateParam.getStuffUnit() == 2 || updateParam.getStuffUnit() == 3 || updateParam.getStuffUnit() == 4)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "计量单位不符合要求"); } if (ObjectUtils.isEmpty(updateParam.getStuffStorage())){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "存储放方式不能为空"); } BasicExperimentStuff stuffByName = stuffRepository.getStuffByName(updateParam.getStuffName()); // if (!ObjectUtils.isEmpty(stuffByName) && !stuffByName.getId().equals(updateParam.getId())){ // throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "该材料已存在"); // } UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId); BasicExperimentStuff stuffById = stuffRepository.getStuffById(updateParam.getId()); BasicExperimentStuff stuff = BeanCopyUtils.copyBean(stuffById, BasicExperimentStuff.class); //设置必要参数 LocalDateTime date = LocalDateTime.now(); stuff.setUpdateTime(date); stuff.setUpdateByUserId(userInfoById.getId()); stuff.setStuffCode(updateParam.getStuffCode()); stuff.setStuffName(updateParam.getStuffName()); stuff.setStuffType(updateParam.getStuffType()); stuff.setStuffStorage(updateParam.getStuffStorage()); stuff.setStuffUnit(updateParam.getStuffUnit()); BasicExperimentStuff updateResult = stuffRepository.save(stuff); return BeanCopyUtils.copyBean(updateResult, StuffUpdateDTO.class); } /** * 基础实验耗材管理 - 删除 */ @Override public StuffDeleteDTO deleteBasicExperimentStuff(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(), "请选择正常的内容删除"); } BasicExperimentStuff stuffById = stuffRepository.getStuffById(id); if (ObjectUtils.isEmpty(stuffById)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请选择正确的内容删除,或确认该耗材是否已经被删除"); } UserInfoDomainDTO userInfoById = userDomainService.getUserInfoById(currentUserId); BasicExperimentStuff stuff = BeanCopyUtils.copyBean(stuffById, BasicExperimentStuff.class); //设置必要参数 LocalDateTime date = LocalDateTime.now(); stuff.setUpdateTime(date); stuff.setUpdateByUserId(userInfoById.getId()); stuff.setDeleteStatus(StatusEnum.DELETED.getCode().byteValue()); BasicExperimentStuff deleteResult = stuffRepository.save(stuff); return BeanCopyUtils.copyBean(deleteResult, StuffDeleteDTO.class); } /** * 基础实验耗材管理 - 列表 */ @Override public List listBasicExperimentStuff(Long currentUserId) { UserInfoDomainDTO user = userDomainService.getUserById(currentUserId); int roleTag = GetRoleTagUtils.GetRoleTagUtils(user); List listResult = new ArrayList<>(); if (SearchAuthUtils.basicSearchAuth() == 0){ listResult = stuffRepository.listStuff(currentUserId); return BeanCopyUtils.copyBeanList(listResult, StuffListDTO.class); } if (roleTag != UserTagEnum.USER_TAG_0.getCode()){ listResult = stuffRepository.listStuff(currentUserId); }else { listResult = stuffRepository.listStuffByUserId(currentUserId); } return BeanCopyUtils.copyBeanList(listResult, StuffListDTO.class); } /** * 基础实验耗材管理 - 查询 by id */ @Override public StuffQueryDTO getBasicExperimentStuffById(Long id) { if (ObjectUtils.isEmpty(id)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请求参数不能为空"); } BasicExperimentStuff stuffById = stuffRepository.getStuffById(id); if (ObjectUtils.isEmpty(stuffById)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "查询的耗材不存在"); } return BeanCopyUtils.copyBean(stuffById, StuffQueryDTO.class); } /** * 基础实验耗材管理 - 通过id列表查询 */ @Override public List getBasicExperimentStuffByIdList(List ids) { if (ids.size() < 1){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "请求参数不能为空"); } List listResult = stuffRepository.batchById(ids); if (listResult.size() < 1){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "查询结果为空"); } return BeanCopyUtils.copyBeanList(listResult, StuffQueryDTO.class); } }