package com.thhy.materials.modules.biz.mixingstationstorage.service.impl; import com.alibaba.nacos.common.utils.UuidUtils; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.thhy.general.common.BasicResult; import com.thhy.general.common.BasicStatus; import com.thhy.general.config.SysUserInfo; import com.thhy.general.exception.BasicException; import com.thhy.general.utils.ExcelUtils; import com.thhy.general.utils.UserInfoUtils; import com.thhy.materials.modules.biz.materialsplan.entity.RsbStatDto; import com.thhy.materials.modules.biz.materialsplan.entity.StatResultVo; import com.thhy.materials.modules.biz.materialsplan.entity.StatVo; import com.thhy.materials.modules.biz.mixingstationstorage.dto.ConsumptionRecordDto; import com.thhy.materials.modules.biz.mixingstationstorage.dto.MinXingPlantDto; import com.thhy.materials.modules.biz.mixingstationstorage.dto.NumberOfMaterialsDto; import com.thhy.materials.modules.biz.mixingstationstorage.dto.TMaterialStockRecordDto; import com.thhy.materials.modules.biz.mixingstationstorage.entity.MXConsumptionRecordsVo; import com.thhy.materials.modules.biz.mixingstationstorage.entity.MinXingPlantVo; import com.thhy.materials.modules.biz.mixingstationstorage.entity.TMaterialStockRecordVo; import com.thhy.materials.modules.biz.mixingstationstorage.mapper.MinXingPlantMapper; import com.thhy.materials.modules.biz.mixingstationstorage.service.MinXingPlantService; import com.thhy.materials.modules.biz.rsbmaterials.mapper.RsbMaterialsMapper; import com.thhy.materials.modules.biz.utils.ExcelExtendUtils; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.util.*; import java.util.concurrent.TimeUnit; /** * @Author QiuYuHao * @CreateDate 2023-09-04 14:31:59 * 搅拌站实现 */ @Service public class MinXingPlantServiceImpl implements MinXingPlantService{ @Autowired private MinXingPlantMapper minXingPlantMapper; @Autowired private RedissonClient redissonClient; @Autowired private RsbMaterialsMapper rsbMaterialsMapper; @Override @Transactional(rollbackFor = Exception.class) public BasicResult insert(MinXingPlantVo minXingPlantVo) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); minXingPlantVo.setCompanyId(companyId); minXingPlantVo.setCreateUser(sysUserInfo.getUserId()); minXingPlantMapper.insert(minXingPlantVo); RLock lock = redissonClient.getLock("materlock"); try { if (lock.tryLock(5, 10, TimeUnit.SECONDS)) { //增加库存 minXingPlantMapper.updateInventory(minXingPlantVo.getChangeStock(),null,minXingPlantVo.getDictId()); } else { throw new BasicException(BasicStatus.OP_IS_TIMEOUT); } } catch (InterruptedException e) { throw new BasicException(BasicStatus.OP_IS_REPEAT); } finally { if (lock.isLocked()) { lock.unlock(); } } return BasicResult.success(); } @Override public BasicResult selectAllList(MinXingPlantDto minXingPlantDto) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); minXingPlantDto.setCompanyId(companyId); PageHelper.startPage(minXingPlantDto.getPageNum(),minXingPlantDto.getPageSize()); List minXingPlantVos = minXingPlantMapper.selectAllList(minXingPlantDto); PageInfo pageInfo =new PageInfo<>(minXingPlantVos); return BasicResult.success(pageInfo); } /** * 入库记录导出 * @param minXingPlantDto * @return */ @Override public void export(MinXingPlantDto minXingPlantDto, HttpServletResponse rsp) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); minXingPlantDto.setCompanyId(companyId); List minXingPlantVos = minXingPlantMapper.selectAllList(minXingPlantDto); ExcelUtils.downExcel(minXingPlantVos,MinXingPlantVo.class,rsp,"搅拌机入库记录"); } /** * 搅拌站库存管理列表 */ @Override public BasicResult selectInventoryList(Map map) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); map.put("companyId",companyId); PageHelper.startPage(Integer.valueOf(map.get("pageNum")),Integer.valueOf(map.get("pageSize"))); List> list = minXingPlantMapper.selectInventoryList(map); PageInfo> info = new PageInfo<>(list); return BasicResult.success(info); } /** * 校正记录列表 * @param tMaterialStockRecordDto * @return */ @Override public BasicResult selectInventoryRecordList(TMaterialStockRecordDto tMaterialStockRecordDto) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); tMaterialStockRecordDto.setCompanyId(companyId); PageHelper.startPage(tMaterialStockRecordDto.getPageNum(),tMaterialStockRecordDto.getPageSize()); List tMaterialStockRecordVos = minXingPlantMapper.selectInventoryRecordList(tMaterialStockRecordDto); PageInfo pageInfo =new PageInfo<>(tMaterialStockRecordVos); return BasicResult.success(pageInfo); } /** * 校正记录列表导出 * @param tMaterialStockRecordDto * @return */ @Override public void exportInventoryRecord(TMaterialStockRecordDto tMaterialStockRecordDto, HttpServletResponse rsp) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); tMaterialStockRecordDto.setCompanyId(companyId); List tMaterialStockRecordVos = minXingPlantMapper.selectInventoryRecordList(tMaterialStockRecordDto); ExcelUtils.downExcel(tMaterialStockRecordVos,TMaterialStockRecordVo.class,rsp,"校正记录"); } /** * 库存管理-校正接口 * @param tMaterialStockRecordVo */ @Override @Transactional(rollbackFor = Exception.class) public void inventoryCorrection(TMaterialStockRecordVo tMaterialStockRecordVo) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); RLock lock = redissonClient.getLock("materlock"); try { if (lock.tryLock(5, 10, TimeUnit.SECONDS)) { //此时参数实体获得的id是库存id 更新库存 minXingPlantMapper.updateInventory(tMaterialStockRecordVo.getAfterStock(),tMaterialStockRecordVo.getId(),null); } else { throw new BasicException(BasicStatus.OP_IS_TIMEOUT); } } catch (InterruptedException e) { throw new BasicException(BasicStatus.OP_IS_REPEAT); } finally { if (lock.isLocked()) { lock.unlock(); } } Map param = new HashMap<>(); param.put("companyId",companyId); param.put("id",tMaterialStockRecordVo.getId()); List> list = minXingPlantMapper.selectInventoryList(param); String dictId = (String) list.get(0).get("dictId"); Double beforeStock = tMaterialStockRecordVo.getBeforeStock(); Double afterStock = tMaterialStockRecordVo.getAfterStock(); //添加校正记录 if(beforeStock > afterStock){ tMaterialStockRecordVo.setOpType(2); }else { tMaterialStockRecordVo.setOpType(1); } tMaterialStockRecordVo.setCreateUser(sysUserInfo.getRealName()); tMaterialStockRecordVo.setCompanyId(companyId); tMaterialStockRecordVo.setMaterialName(dictId); tMaterialStockRecordVo.setCreateDate(new Date()); tMaterialStockRecordVo.setType(3);//校正状态 tMaterialStockRecordVo.setId(UuidUtils.generateUuid()); minXingPlantMapper.insertInventoryRecord(tMaterialStockRecordVo); } /** * 消耗记录列表 * @param consumptionRecordDto * @return */ @Override public BasicResult selectAllConsumptionList(ConsumptionRecordDto consumptionRecordDto) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); consumptionRecordDto.setCompanyId(companyId); PageHelper.startPage(consumptionRecordDto.getPageNum(),consumptionRecordDto.getPageSize()); PageInfo mxConsumptionRecordsVoPageInfo = new PageInfo<>(minXingPlantMapper.selectAllConsumptionList(consumptionRecordDto)); return BasicResult.success(mxConsumptionRecordsVoPageInfo); } /** * 消耗记录导出 * @param consumptionRecordDto * @return */ @Override public void exportAllConsumptionList(ConsumptionRecordDto consumptionRecordDto, HttpServletResponse rsp) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); String companyId = sysUserInfo.getCompanyId(); consumptionRecordDto.setCompanyId(companyId); List mxConsumptionRecordsVos = minXingPlantMapper.selectAllConsumptionList(consumptionRecordDto); ExcelUtils.downExcel(mxConsumptionRecordsVos,MXConsumptionRecordsVo.class,rsp,"消耗记录"); } /** * 消耗记录 * @param numberOfMaterialsDto * @return */ @Override public BasicResult RsbStat(NumberOfMaterialsDto numberOfMaterialsDto) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); numberOfMaterialsDto.setCompanyId(sysUserInfo.getCompanyId()); Map map = new HashMap<>(); /** *个数 */ if(numberOfMaterialsDto.getDateType() == 2){ String endDay =getEndDay(numberOfMaterialsDto.getEndDay()) ; numberOfMaterialsDto.setEndDay(endDay); } List> list = minXingPlantMapper.numberOfMaterials(numberOfMaterialsDto); List statResultVos = new ArrayList<>(); String dictId = numberOfMaterialsDto.getDictId(); if("both".equals(dictId)){ //获取全部 statResultVos = rsbMaterialsMapper.queryAllMaterial(null); }else{ statResultVos = rsbMaterialsMapper.queryAllMaterial(dictId); } statResultVos.forEach(s->{ numberOfMaterialsDto.setDictId(s.getMaterialName()); List statVos = null; if(numberOfMaterialsDto.getDateType() == 1){ statVos = minXingPlantMapper.queryValueDay(numberOfMaterialsDto); }else { statVos = minXingPlantMapper.queryValueMonth(numberOfMaterialsDto); } s.setStatVoList(statVos); s.setMaterialCName(s.getMaterialCName()); }); map.put("statResult",statResultVos); map.put("materialTotal",list); return BasicResult.success(map); } public String getEndDay(String endDay) { int year = Integer.valueOf(endDay.substring(0, 4)); int month = Integer.valueOf(endDay.substring(5, 7)); // 设置月份,注意月份从1开始,即1表示1月,12表示12月 LocalDate lastDayOfMonth = LocalDate.of(year, month, 1).withDayOfMonth(LocalDate.of(year, month, 1).lengthOfMonth()); System.out.println("最后一天是:" + lastDayOfMonth.getDayOfMonth()); return endDay.substring(0,7) + "-"+lastDayOfMonth.getDayOfMonth(); } @Override public void statExport(NumberOfMaterialsDto numberOfMaterialsDto, String token, HttpServletResponse response) { SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo(); numberOfMaterialsDto.setCompanyId(sysUserInfo.getCompanyId()); List statResultVos = new ArrayList<>(); String dictId = numberOfMaterialsDto.getDictId(); numberOfMaterialsDto.setMoth(numberOfMaterialsDto.getStartDay().substring(0,7)); numberOfMaterialsDto.setStartMoth(numberOfMaterialsDto.getStartDay().substring(0,7)); numberOfMaterialsDto.setEndMoth(numberOfMaterialsDto.getEndDay().substring(0,7)); numberOfMaterialsDto.setYear(numberOfMaterialsDto.getStartDay().substring(0, 4)); if("".equals(dictId)){ //获取全部 statResultVos = rsbMaterialsMapper.queryAllMaterial(null); }else{ statResultVos = rsbMaterialsMapper.queryAllMaterial(dictId); } statResultVos.forEach(s->{ numberOfMaterialsDto.setDictId(s.getMaterialName()); List statVos = null; if(numberOfMaterialsDto.getDateType() == 1){ statVos = minXingPlantMapper.queryValueDay(numberOfMaterialsDto); }else { statVos = minXingPlantMapper.queryValueMonth(numberOfMaterialsDto); } s.setStatVoList(statVos); s.setMaterialCName(s.getMaterialCName()); }); ExcelExtendUtils.create(statResultVos,"原材料消耗",response); } }