package org.jeecg.modules.spare.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.system.api.ISysBaseAPI; import org.jeecg.common.system.vo.DictModel; import org.jeecg.modules.spare.entity.*; import org.jeecg.modules.spare.mapper.SparePartOutboundDetailMapper; import org.jeecg.modules.spare.service.ISparePartOutboundDetailService; import org.jeecg.modules.spare.service.ISparePartOutboundService; import org.jeecg.modules.spare.service.ISparePartService; import org.jeecg.modules.spare.service.ISparesPartInventoryService; import org.jeecg.modules.system.entity.SysRole; import org.jeecg.modules.system.entity.SysUser; import org.jeecg.modules.system.service.ISysDictService; import org.jeecg.modules.system.service.ISysRoleService; import org.jeecg.modules.system.service.ISysUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * @Description: mom_eam_spare_part_outbound_deatil * @Author: jeecg-boot * @Date: 2023-06-30 * @Version: V1.0 */ @Service public class SparePartOutboundDetailServiceImpl extends ServiceImpl implements ISparePartOutboundDetailService { @Autowired @Lazy private ISparesPartInventoryService sparesPartInventoryService; @Autowired @Lazy private ISparePartService sparePartService; @Autowired @Lazy private ISparePartOutboundService sparePartOutboundService; @Autowired @Lazy private ISysDictService sysDictService; @Autowired @Lazy private ISysRoleService sysRoleService; @Autowired @Lazy private ISysUserService sysUserService; @Autowired @Lazy private ISysBaseAPI sysBaseAPI; @Override public List> getSparePartOutboundDeatilList(String sparePartOutboundId) { return this.baseMapper.getSparePartOutboundDeatilList(sparePartOutboundId); } @Override public IPage> getSparePartOutboundDetailsById(Integer pageNo, Integer pageSize, Map params) { IPage pageData = new Page(pageNo, pageSize); return super.getBaseMapper().getSparePartOutboundDetailsById(pageData,params); } @Override @Transactional(rollbackFor = { Exception.class }) public boolean sparePartOutbound(SparePartOutboundDetail sparePartOutboundDetail) { //1.获取库存信息及库存总数量 String sparePartOutboundId = sparePartOutboundDetail.getSparePartOutboundId(); SparePartOutbound sparePartOutbound = sparePartOutboundService.getById(sparePartOutboundId); String sparesPartInventoryId = sparePartOutboundDetail.getSparesPartInventoryId(); SparesPartInventory sparesPartInventory = sparesPartInventoryService.getById(sparesPartInventoryId); //2.获取出库数量 Double outboundMainQuantity = sparePartOutboundDetail.getOutboundMainQuantity(); BigDecimal outboundMainQuantityB = new BigDecimal(outboundMainQuantity); String sparePartId = sparePartOutboundDetail.getSparePartId(); //3.获取主 辅单位转换比例 SparePart sparePart = sparePartService.getById(sparePartId); /* String conversionRatio = sparePart.getConversionRatio(); double conversionRatioD = 0; if(StringUtils.isBlank(conversionRatio)){ conversionRatioD = Double.parseDouble("0"); }else{ conversionRatioD = Double.parseDouble(conversionRatio); } BigDecimal conversionRatioB = new BigDecimal(conversionRatioD);*/ //4.计算出库数量更新库存数量(库存) Double mainQuantity = sparesPartInventory.getMainQuantity(); BigDecimal mainQuantityB = new BigDecimal(mainQuantity); /* Double auxiliaryQuantity = sparesPartInventory.getAuxiliaryQuantity();*/ /* BigDecimal auxiliaryQuantityB = new BigDecimal(auxiliaryQuantity);*/ BigDecimal mainQuantitySubtract = mainQuantityB.subtract(outboundMainQuantityB);//减完后的库存主数量 /* BigDecimal outboundAuxiliaryQuantity = outboundMainQuantityB.multiply(conversionRatioB);//出库辅数量*/ /* BigDecimal auxiliaryQuantitySubtract = auxiliaryQuantityB.subtract(outboundAuxiliaryQuantity);//减完后的库存辅数量*/ sparesPartInventory.setMainQuantity(mainQuantitySubtract.doubleValue()); /* sparesPartInventory.setAuxiliaryQuantity(auxiliaryQuantitySubtract.doubleValue());*/ boolean b = sparesPartInventoryService.updateById(sparesPartInventory); if(!b){ return b; } sparePartOutboundDetail.setStatus("1"); super.updateById(sparePartOutboundDetail); //5.更新出库单状态 List sparePartOutboundDetails = super.lambdaQuery() .eq(SparePartOutboundDetail::getSparePartOutboundId, sparePartOutboundId) .eq(SparePartOutboundDetail::getStatus, "0") .eq(SparePartOutboundDetail::getDelFlag, "0").list(); if(sparePartOutboundDetails.size()==0){ sparePartOutbound.setStatus("4"); sparePartOutboundService.updateById(sparePartOutbound); } //6.判断出库后库存是否低于安全库存或出现积压情况 List roleList = new ArrayList<>(); List dictModelList = sysDictService.getDictItems("warehouse_message"); for(DictModel dictModel : dictModelList){ roleList.add(dictModel.getText()); } QueryWrapper queryWrapper = Wrappers.query(); queryWrapper.in("role_code", roleList); List sysRoleList = sysRoleService.list(queryWrapper); List sysUsers = new ArrayList<>(); for (SysRole sysRole : sysRoleList) { sysUsers.addAll(sysUserService.getAllUsersByRoleId(sysRole.getId())); } double quantity = 0.0; List sparesPartInventoryList = sparesPartInventoryService.lambdaQuery().eq(SparesPartInventory::getSparePartId,sparesPartInventory.getSparePartId()).eq(SparesPartInventory::getDelFlag, CommonConstant.DEL_FLAG_0).list(); for(SparesPartInventory sparesPartInventory1 : sparesPartInventoryList){ quantity = quantity + sparesPartInventory1.getMainQuantity(); } if(quantity < sparePart.getInventoryLowerLimit()){ String messageTitle; String messageContent; String sysTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); messageTitle = "系统消息:备件编码为 " + sparePart.getNum() + " 的备件库存量不足,请及时处理!"; messageContent = "系统消息:备件编码:" + sparePart.getNum() + " 的备件库存量已低于安全库存!" + "\n" + "消息时间:" + sysTime + "\n"; for (SysUser sysUser : sysUsers) { sysBaseAPI.sendAnnouncement("admin", sysUser.getUsername(), messageTitle, messageContent, "备件库存量不足"); } }else if(quantity > sparePart.getInventoryUpperLimit()){ String messageTitle; String messageContent; String sysTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); messageTitle = "系统消息:备件编码为 " + sparePart.getNum() + " 的备件出现积压情况,请及时处理!"; messageContent = "系统消息:备件编码为 " + sparePart.getNum() + " 的备件库存量已高于库存上限值,出现积压情况!" + "\n" + "消息时间:" + sysTime + "\n"; for (SysUser sysUser : sysUsers) { sysBaseAPI.sendAnnouncement("admin", sysUser.getUsername(), messageTitle, messageContent, "备件积压"); } } return true; } }