package com.gkhy.hazmat.framework.listener; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.gkhy.hazmat.common.config.IdTableNameHandler; import com.gkhy.hazmat.common.enums.CodePrexEnum; import com.gkhy.hazmat.common.enums.OperateStatusEnum; import com.gkhy.hazmat.common.listener.EntryEvent; import com.gkhy.hazmat.system.domain.HzHazmat; import com.gkhy.hazmat.system.domain.HzHazmatFlow; import com.gkhy.hazmat.system.domain.HzProduct; import com.gkhy.hazmat.system.domain.HzProductFlow; import com.gkhy.hazmat.system.service.HzHazmatFlowService; import com.gkhy.hazmat.system.service.HzHazmatService; import com.gkhy.hazmat.system.service.HzProductFlowService; import com.gkhy.hazmat.system.service.HzProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Component public class CustomListener { @Autowired private HzHazmatService hazmatService; @Autowired private HzProductService productService; @Autowired private HzHazmatFlowService hazmatFlowService; @Autowired private HzProductFlowService productFlowService; @Async("threadPoolTaskExecutor") @EventListener public void addFlow(EntryEvent hazmatEntryEvent){ String codePrex=hazmatEntryEvent.getCodePrex(); if(codePrex.equals(CodePrexEnum.MATERIAL.getCode())){ generateHazmatFlow(hazmatEntryEvent); }else{ generateProductFlow(hazmatEntryEvent); } } private void generateHazmatFlow(EntryEvent hazmatEntryEvent) { Integer pageSize=100; Integer pageIndex=1; List hazmatFlowList=new ArrayList<>(); //设置分表id IdTableNameHandler.setCurrentId(hazmatEntryEvent.getCompanyId()); while (true){ LambdaQueryWrapper eq = Wrappers.lambdaQuery().eq(true, HzHazmat::getEntryId, hazmatEntryEvent.getEntryRecordId()); eq.last(" limit "+((pageIndex-1)*pageSize)+","+pageSize); List hazmatList=hazmatService.list(eq); if(hazmatList.isEmpty()){ break; } hazmatFlowList=hazmatList.stream().map(item -> { //生成流向 HzHazmatFlow hazmatFlow=new HzHazmatFlow(); hazmatFlow.setHazmatId(item.getId()); hazmatFlow.setBasicId(item.getBasicId()); hazmatFlow.setState(OperateStatusEnum.ENTRY.getCode()); hazmatFlow.setCompanyId(hazmatEntryEvent.getCompanyId()); hazmatFlow.setNum(item.getRemaining()); hazmatFlow.setCreateId(hazmatEntryEvent.getUserId()); return hazmatFlow; }).collect(Collectors.toList()); hazmatFlowService.saveBatch(hazmatFlowList); hazmatFlowList.clear(); pageIndex++; } IdTableNameHandler.removeCurrentId(); } private void generateProductFlow(EntryEvent hazmatEntryEvent) { Integer pageSize=100; Integer pageIndex=1; List productFlowList=new ArrayList<>(); IdTableNameHandler.setCurrentId(hazmatEntryEvent.getCompanyId()); while (true){ LambdaQueryWrapper eq = Wrappers.lambdaQuery().eq(true, HzProduct::getEntryId, hazmatEntryEvent.getEntryRecordId()); eq.last(" limit "+((pageIndex-1)*pageSize)+","+pageSize); List productList=productService.list(eq); if(productList.isEmpty()){ break; } productFlowList=productList.stream().map(item -> { //生成流向 HzProductFlow productFlow=new HzProductFlow(); productFlow.setProductId(item.getId()); productFlow.setBasicId(item.getBasicId()); productFlow.setState(OperateStatusEnum.ENTRY.getCode()); productFlow.setCompanyId(hazmatEntryEvent.getCompanyId()); productFlow.setNum(item.getRemaining()); productFlow.setCreateId(hazmatEntryEvent.getUserId()); return productFlow; }).collect(Collectors.toList()); productFlowService.saveBatch(productFlowList); productFlowList.clear(); pageIndex++; } IdTableNameHandler.removeCurrentId(); } }