|  |  |  | 
|---|
|  |  |  | import com.core.common.Cools; | 
|---|
|  |  |  | import com.core.common.SpringUtils; | 
|---|
|  |  |  | import com.core.exception.CoolException; | 
|---|
|  |  |  | import com.zy.asrs.entity.Mat; | 
|---|
|  |  |  | import com.zy.asrs.entity.Node; | 
|---|
|  |  |  | import com.zy.asrs.entity.Pla; | 
|---|
|  |  |  | import com.zy.asrs.entity.param.GlobleParameter; | 
|---|
|  |  |  | import com.zy.asrs.service.MatService; | 
|---|
|  |  |  | import com.zy.asrs.service.NodeService; | 
|---|
|  |  |  | import com.zy.asrs.service.PlaService; | 
|---|
|  |  |  | import com.zy.common.entity.MatExcel; | 
|---|
|  |  |  | import com.zy.common.entity.PlaExcel; | 
|---|
|  |  |  | import lombok.Data; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.BeanUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 2019-11-25 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Data | 
|---|
|  |  |  | public class PlaExcelListener extends AnalysisEventListener<PlaExcel> { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final String OWNER_HZ = "海正生物"; | 
|---|
|  |  |  | private static final String OWNER_HNE = "海诺尔"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private int total = 0; | 
|---|
|  |  |  | private int index = 1; | 
|---|
|  |  |  | private String recordLog = ""; | 
|---|
|  |  |  | private Long userId; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public PlaExcelListener() { | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void invoke(PlaExcel excel, AnalysisContext ctx) { | 
|---|
|  |  |  | index ++; | 
|---|
|  |  |  | PlaService plaService = SpringUtils.getBean(PlaService.class); | 
|---|
|  |  |  | NodeService nodeService = SpringUtils.getBean(NodeService.class); | 
|---|
|  |  |  | MatService matService = SpringUtils.getBean(MatService.class); | 
|---|
|  |  |  | Date now = new Date(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 商品 | 
|---|
|  |  |  | if(Cools.isEmpty(excel.getBatch()) || Cools.isEmpty(excel.getPackageNo())){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | throw new CoolException("第" +  index++ + "数据错误,包号或者批号为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Pla pla = plaService.selectByBatchAndPackageNo(excel.getBatch(),excel.getPackageNo()); | 
|---|
|  |  |  | Pla pla = plaService.selectByBatchAndPackageNo(excel.getBatch(),excel.getPackageNo(),excel.getBrand()); | 
|---|
|  |  |  | if (pla == null) { | 
|---|
|  |  |  | pla = excel; | 
|---|
|  |  |  | pla.setStep(2); | 
|---|
|  |  |  | pla = plaCreate(pla,excel,matService,nodeService); | 
|---|
|  |  |  | pla.setCreateTime(new Date()); | 
|---|
|  |  |  | pla.setModifyTime(new Date()); | 
|---|
|  |  |  | pla.setStatus(GlobleParameter.PLA_STATUS_00); | 
|---|
|  |  |  | pla.setStockFreeze(1); | 
|---|
|  |  |  | if(!Cools.isEmpty(pla.getLocNo())){ | 
|---|
|  |  |  | Node node = nodeService.selectByUuid(pla.getLocNo()); | 
|---|
|  |  |  | if(Cools.isEmpty(node)){ | 
|---|
|  |  |  | throw new CoolException("未能找到相应的库位信息"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | pla.setStash(node.getParentName()); | 
|---|
|  |  |  | pla.setLocNo(node.getUuid()); | 
|---|
|  |  |  | pla.setStatus(GlobleParameter.PLA_STATUS_1); | 
|---|
|  |  |  | pla.setPakinTime(new Date()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | plaService.insert(pla); | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | log.error("保存商品信息失败,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage()); | 
|---|
|  |  |  | throw new CoolException("保存商品信息失败,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!plaService.insert(pla)) { | 
|---|
|  |  |  | throw new CoolException("保存商品信息失败,商品编码:" + excel.getMatnr()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //            if (!plaService.insert(pla)) { | 
|---|
|  |  |  | //                throw new CoolException("保存商品信息失败,商品编码:" + excel.getMatnr()); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | total++; | 
|---|
|  |  |  | }else if (pla.getStatus().equals(GlobleParameter.PLA_STATUS_00)){ | 
|---|
|  |  |  | }else if(Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_0) || Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_00) || Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_1) ){ | 
|---|
|  |  |  | excel.setId(pla.getId()); | 
|---|
|  |  |  | excel.setCreateTime(pla.getCreateTime()); | 
|---|
|  |  |  | excel.setModifyTime(new Date()); | 
|---|
|  |  |  | excel.setStep(2); | 
|---|
|  |  |  | excel.setStatus(GlobleParameter.PLA_STATUS_00); | 
|---|
|  |  |  | excel.setStatus(pla.getStatus()); | 
|---|
|  |  |  | excel.setPakinTime(pla.getPakinTime()); | 
|---|
|  |  |  | excel.setStash(pla.getStash()); | 
|---|
|  |  |  | excel.setStockFreeze(pla.getStockFreeze()); | 
|---|
|  |  |  | if(Cools.isEmpty(excel.getLocNo())){ | 
|---|
|  |  |  | excel.setLocNo(pla.getLocNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BeanUtils.copyProperties(excel,pla); | 
|---|
|  |  |  | plaService.updateById(pla); | 
|---|
|  |  |  | pla = plaCreate(pla,excel,matService,nodeService); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | plaService.updateById(pla); | 
|---|
|  |  |  | recordLog = recordLog + index + "," + pla.getBatch() + "," + pla.getPackageNo() + ";" ; | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | log.error("保存商品信息失败,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage()); | 
|---|
|  |  |  | throw new CoolException("保存商品信息失败,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | total++; | 
|---|
|  |  |  | } else if (Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_2) || Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_3) || Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_4)) { | 
|---|
|  |  |  | // 补传质量指标 | 
|---|
|  |  |  | pla.setProDate(excel.getProDate()); | 
|---|
|  |  |  | pla.setWeight(excel.getWeight()); | 
|---|
|  |  |  | pla.setPackageType(excel.getPackageType()); | 
|---|
|  |  |  | pla.setZpalletType(excel.getZpalletType()); | 
|---|
|  |  |  | pla.setFingerMelting(excel.getFingerMelting()); | 
|---|
|  |  |  | pla.setWater(excel.getWater()); | 
|---|
|  |  |  | pla.setFusingPoint(excel.getFusingPoint()); | 
|---|
|  |  |  | pla.setVadf1(excel.getVadf1()); | 
|---|
|  |  |  | pla.setVadf2(excel.getVadf2()); | 
|---|
|  |  |  | pla.setYellowness(excel.getYellowness()); | 
|---|
|  |  |  | pla.setOpacity(excel.getOpacity()); | 
|---|
|  |  |  | pla.setL(excel.getL()); | 
|---|
|  |  |  | pla.setA(excel.getA()); | 
|---|
|  |  |  | pla.setB(excel.getB()); | 
|---|
|  |  |  | pla.setFillIn(excel.getFillIn()); | 
|---|
|  |  |  | pla.setStr1(excel.getStr1()); | 
|---|
|  |  |  | pla.setStr2(excel.getStr2()); | 
|---|
|  |  |  | pla.setStr3(excel.getStr3()); | 
|---|
|  |  |  | pla.setStr4(excel.getStr4()); | 
|---|
|  |  |  | pla.setStr5(excel.getStr5()); | 
|---|
|  |  |  | pla.setStr6(excel.getStr6()); | 
|---|
|  |  |  | pla.setMassState(excel.getMassState()); | 
|---|
|  |  |  | pla.setProblem(excel.getProblem()); | 
|---|
|  |  |  | pla.setModifyTime(now); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | plaService.updateById(pla); | 
|---|
|  |  |  | recordLog = recordLog + index + "," + pla.getBatch() + "," + pla.getPackageNo() + ";" ; | 
|---|
|  |  |  | } catch (Exception e){ | 
|---|
|  |  |  | log.error("补传质量指标异常,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage()); | 
|---|
|  |  |  | throw new CoolException("补传质量指标异常,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | total++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private String getType(String matnr){ | 
|---|
|  |  |  | String type = matnr.substring(0,3); | 
|---|
|  |  |  | if(Cools.eq(type,"301")){ | 
|---|
|  |  |  | return "纯树脂"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Cools.eq(type,"302")){ | 
|---|
|  |  |  | return "改性树脂"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Cools.eq(type,"303")){ | 
|---|
|  |  |  | return "副产物"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Cools.eq(type,"304")){ | 
|---|
|  |  |  | return "委托加工产品"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Cools.eq(type,"305")){ | 
|---|
|  |  |  | return "丙交酯"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return ""; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void setPla(Pla pla){ | 
|---|
|  |  |  | String batch = pla.getBatch(); | 
|---|
|  |  |  | String line = batch.substring(8,10); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(Cools.isEmpty(pla.getLine())){ | 
|---|
|  |  |  | pla.setLine(line); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(Cools.isEmpty(pla.getOwner()) && Cools.isEmpty(pla.getWorkshop())){ | 
|---|
|  |  |  | if("纯树脂".equals(pla.getType())){ | 
|---|
|  |  |  | if("01".equals(line) || "02".equals(line)){ | 
|---|
|  |  |  | pla.setOwner(OWNER_HZ); | 
|---|
|  |  |  | pla.setWorkshop("一车间"); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | pla.setOwner(OWNER_HNE); | 
|---|
|  |  |  | pla.setWorkshop("三车间"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else if ("改性树脂".equals(pla.getType())){ | 
|---|
|  |  |  | if (pla.getBrand() != null && (pla.getBrand().equals("161") || pla.getBrand().equals("171") || pla.getBrand().equals("181"))) { | 
|---|
|  |  |  | pla.setOwner(OWNER_HZ); | 
|---|
|  |  |  | pla.setWorkshop("一车间"); | 
|---|
|  |  |  | } else if ("01".equals(line) || "02".equals(line) || "03".equals(line) || "04".equals(line)){ | 
|---|
|  |  |  | pla.setOwner(OWNER_HNE); | 
|---|
|  |  |  | pla.setWorkshop("二车间"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | public int getTotal() { | 
|---|
|  |  |  | return total; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Pla plaCreate(Pla pla, PlaExcel excel, MatService matService, NodeService nodeService){ | 
|---|
|  |  |  | pla = excel; | 
|---|
|  |  |  | pla.setStep(2); | 
|---|
|  |  |  | pla.setModifyTime(new Date()); | 
|---|
|  |  |  | pla.setStatus(GlobleParameter.PLA_STATUS_00); | 
|---|
|  |  |  | pla.setStockFreeze(Cools.isEmpty(excel.getStockFreeze()) ? 1 : excel.getStockFreeze()); | 
|---|
|  |  |  | pla.setWeightAnfme(pla.getWeight()); | 
|---|
|  |  |  | Mat mat = matService.selectByMaktx(pla.getBrand()); | 
|---|
|  |  |  | if(Cools.isEmpty(mat)){ | 
|---|
|  |  |  | throw new CoolException("无法找到对应的牌号信息,牌号=" + pla.getBrand()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(excel.getProblem()) && excel.getProblem().contains("实验料")) { | 
|---|
|  |  |  | excel.setMassState("实验料"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | pla.setMatnr(mat.getMatnr()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(Cools.isEmpty(pla.getType())){ | 
|---|
|  |  |  | pla.setType(getType(mat.getMatnr())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //车间、产线、主体 | 
|---|
|  |  |  | setPla(pla); | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | log.info(e.getMessage() + "Pla: " + pla.getBatch()); | 
|---|
|  |  |  | throw new CoolException(e.getMessage() + ",批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(!Cools.isEmpty(pla.getLocNo())){ | 
|---|
|  |  |  | Node node = nodeService.selectByUuid(pla.getLocNo()); | 
|---|
|  |  |  | if(Cools.isEmpty(node)){ | 
|---|
|  |  |  | throw new CoolException("未能找到相应的库位信息,库位编号=" +pla.getLocNo()); | 
|---|
|  |  |  | //log.info("未能找到相应的库位信息,库位编号={}",pla.getLocNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | pla.setStash(node.getParentName()); | 
|---|
|  |  |  | pla.setLocNo(node.getUuid()); | 
|---|
|  |  |  | pla.setStatus(GlobleParameter.PLA_STATUS_1); | 
|---|
|  |  |  | pla.setPakinTime(new Date()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return pla; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|