package com.gkhy.hazmat.system.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gkhy.hazmat.common.api.CommonPage; import com.gkhy.hazmat.common.exception.ApiException; import com.gkhy.hazmat.common.utils.PageUtils; import com.gkhy.hazmat.common.utils.SecurityUtils; import com.gkhy.hazmat.system.domain.HzHazmatBasic; import com.gkhy.hazmat.system.domain.Subscribe; import com.gkhy.hazmat.system.domain.SubscribeHazmat; import com.gkhy.hazmat.system.mapper.HzHazmatBasicMapper; import com.gkhy.hazmat.system.mapper.SubscribeHazmatMapper; import com.gkhy.hazmat.system.mapper.SubscribeMapper; import com.gkhy.hazmat.system.service.SubscribeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; @Service public class SubscribeServiceImpl extends ServiceImpl implements SubscribeService { @Resource private SubscribeMapper subscribeMapper; @Autowired private SubscribeHazmatMapper subscribeHazmatMapper; @Autowired private HzHazmatBasicMapper hazmatBasicMapper; @Override public CommonPage selectSubscribeList(Subscribe subscribe) { Integer currentUserId = SecurityUtils.getLoginUser().getUser().getUserType(); boolean isAdmin = SecurityUtils.isSystemUser(currentUserId); if (!isAdmin) { if (subscribe.getSubscribePersonId() == null) { subscribe.setSubscribePersonId(SecurityUtils.getUserId()); } else { if (!subscribe.getSubscribePersonId().equals(SecurityUtils.getUserId())) { throw new ApiException("无权限查看其他用户的申购单"); } } } PageUtils.startPage(); List subscribeList = subscribeMapper.selectSubscribeList(subscribe); for (Subscribe subscribe1 : subscribeList) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SubscribeHazmat::getSubscribeId, subscribe1.getId()); List hazmatList = subscribeHazmatMapper.selectList(wrapper); subscribe1.setSubscribeHazmats(hazmatList); } return CommonPage.restPage(subscribeList); } @Override public Subscribe selectSubscribeById(Long subscribeId) { Subscribe subscribe = subscribeMapper.selectSubscribeById(subscribeId); if (subscribe == null) { throw new ApiException("申购单不存在"); } Integer currentUserId = SecurityUtils.getLoginUser().getUser().getUserType(); boolean isAdmin = SecurityUtils.isSystemUser(currentUserId); if (!isAdmin && !subscribe.getSubscribePersonId().equals(SecurityUtils.getUserId()) && !SecurityUtils.getUserId().equals(subscribe.getCheckId())) { throw new ApiException("无权限查看该申购单"); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SubscribeHazmat::getSubscribeId, subscribeId); List hazmatList = subscribeHazmatMapper.selectList(wrapper); subscribe.setSubscribeHazmats(hazmatList); return subscribe; } @Override @Transactional(rollbackFor = RuntimeException.class) public int insertSubscribe(Subscribe subscribe) { Long currentUserId = SecurityUtils.getUserId(); String currentUserName = SecurityUtils.getLoginUser().getUser().getName(); subscribe.setSubscribePersonId(currentUserId); subscribe.setSubscribePersonName(currentUserName); subscribe.setCreateBy(currentUserName); subscribe.setCreateTime(LocalDateTime.now()); subscribe.setSubscribeNum(generateSubscribeNum()); int row = subscribeMapper.insertSubscribe(subscribe); if (row < 1) { throw new ApiException("新增申购单失败"); } if (subscribe.getSubscribeHazmats() != null && !subscribe.getSubscribeHazmats().isEmpty()) { for (SubscribeHazmat hazmat : subscribe.getSubscribeHazmats()) { HzHazmatBasic hazmatBasic=hazmatBasicMapper.selectById(hazmat.getBasicId()); if(hazmat.getHazmatCount()>hazmatBasic.getMaxEntry()){ throw new ApiException(hazmatBasic.getName()+"数量超过单次入库最大数量<"+hazmatBasic.getMaxEntry()+">"); } hazmat.setSubscribeId(subscribe.getId()); hazmat.setDelFlag(0); subscribeHazmatMapper.insert(hazmat); } } return row; } private String generateSubscribeNum() { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); return now.format(formatter); } @Override @Transactional(rollbackFor = RuntimeException.class) public int updateSubscribe(Subscribe subscribe) { Subscribe existSubscribe = subscribeMapper.selectById(subscribe.getId()); if (existSubscribe == null) { throw new ApiException("申购单不存在"); } if (existSubscribe.getStatus() != 0) { throw new ApiException("只能修改待审核状态的申购单"); } Long currentUserId = SecurityUtils.getUserId(); boolean isAdmin = SecurityUtils.isAdmin(currentUserId); if (!isAdmin && !existSubscribe.getSubscribePersonId().equals(currentUserId)) { throw new ApiException("无权限修改该申购单"); } subscribe.setUpdateBy(SecurityUtils.getUsername()); subscribe.setUpdateTime(LocalDateTime.now()); int row = subscribeMapper.updateSubscribe(subscribe); if (row < 1) { throw new ApiException("更新申购单失败"); } if (subscribe.getSubscribeHazmats() != null) { LambdaQueryWrapper deleteWrapper = new LambdaQueryWrapper<>(); deleteWrapper.eq(SubscribeHazmat::getSubscribeId, subscribe.getId()); subscribeHazmatMapper.delete(deleteWrapper); for (SubscribeHazmat hazmat : subscribe.getSubscribeHazmats()) { hazmat.setSubscribeId(subscribe.getId()); hazmat.setDelFlag(0); subscribeHazmatMapper.insert(hazmat); } } return row; } @Override @Transactional(rollbackFor = RuntimeException.class) public int deleteSubscribeById(Long subscribeId) { Subscribe subscribe = subscribeMapper.selectSubscribeById(subscribeId); if (subscribe == null) { throw new ApiException("申购单不存在"); } if (subscribe.getStatus() != 0) { throw new ApiException("只能删除待审核状态的申购单"); } Long currentUserId = SecurityUtils.getUserId(); boolean isAdmin = SecurityUtils.isAdmin(currentUserId); if (!isAdmin && !subscribe.getSubscribePersonId().equals(currentUserId)) { throw new ApiException("无权限删除该申购单"); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SubscribeHazmat::getSubscribeId, subscribeId); SubscribeHazmat updateHazmat = new SubscribeHazmat(); updateHazmat.setDelFlag(1); subscribeHazmatMapper.update(updateHazmat, wrapper); subscribe.setDelFlag(1); subscribe.setUpdateBy(SecurityUtils.getUsername()); subscribe.setUpdateTime(LocalDateTime.now()); return subscribeMapper.updateSubscribe(subscribe); } @Override @Transactional(rollbackFor = RuntimeException.class) public void auditSubscribe(Long subscribeId, Integer status,String opinion) { if (status == null || (status != 1 && status != 2)) { throw new ApiException("审核状态不正确,1为通过,2为驳回"); } if (status == 2 && opinion == null) { throw new ApiException("通过审核时,请填写审核意见"); } Subscribe subscribe = subscribeMapper.selectSubscribeById(subscribeId); if (subscribe == null) { throw new ApiException("申购单不存在"); } if (subscribe.getStatus() != 0) { throw new ApiException("该申购单已审核,不能重复审核"); } Long currentUserId = SecurityUtils.getUserId(); if (subscribe.getCheckId() != null && !currentUserId.equals(subscribe.getCheckId())) { throw new ApiException("非审批人不可审批"); } String currentUserName = SecurityUtils.getUsername(); subscribe.setStatus(status); subscribe.setOpinion(opinion); subscribe.setUpdateBy(currentUserName); subscribe.setUpdateTime(LocalDateTime.now()); int row = subscribeMapper.updateSubscribe(subscribe); if (row < 1) { throw new ApiException("审核操作失败"); } } @Override public CommonPage selectAuditList(Subscribe subscribe) { Long currentUserId = SecurityUtils.getUserId(); boolean isAdmin = SecurityUtils.isAdmin(currentUserId); if (!isAdmin) { subscribe.setCheckId(currentUserId); } PageUtils.startPage(); List subscribeList = subscribeMapper.selectSubscribeList(subscribe); for (Subscribe subscribe1 : subscribeList) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SubscribeHazmat::getSubscribeId, subscribe1.getId()); wrapper.eq(SubscribeHazmat::getDelFlag, 0); List hazmatList = subscribeHazmatMapper.selectList(wrapper); subscribe1.setSubscribeHazmats(hazmatList); } return CommonPage.restPage(subscribeList); } }