package com.sw.mes.yb.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sw.mes.base.workinprocess.entity.PlanProcessingOrder;
import com.sw.mes.base.workinprocess.service.PlanProcessingOrderService;
import com.sw.mes.inspect.entity.YbInspectData;
import com.sw.mes.inspect.service.YbInspectDataService;
import com.sw.mes.yb.pad.entity.ProductionPaintingLine;
import com.sw.mes.yb.pad.entity.ProductionPaintingLineDetail;
import com.sw.mes.yb.pad.service.ProductionPaintingLineDetailService;
import com.sw.mes.yb.pad.service.ProductionPaintingLineService;
import com.sw.mes.yb.production.entity.LongCrystal;
import com.sw.mes.yb.production.service.LongCrystalService;
import com.sw.mes.yb.report.entity.WipStorageReport;
import com.sw.mes.yb.report.service.WipStorageReportService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Component
public class WipStorageReportScheduling {

    @Autowired
    private WipStorageReportService wipStorageReportService;
    @Autowired
    private YbInspectDataService ybInspectDataService;
    @Autowired
    private PlanProcessingOrderService planProcessingOrderService;
    @Autowired
    private ProductionPaintingLineService productionPaintingLineService;
    @Autowired
    private ProductionPaintingLineDetailService productionPaintingLineDetailService;
    @Autowired
    private LongCrystalService longCrystalService;

    @XxlJob("wipStorageReportScheduling")
    public void wipStorageReportScheduling() {
        CompletableFuture.runAsync(()->{
            //获取在制品数据
            List<Map<String,Object>> list= wipStorageReportService.findAllData();
            List<WipStorageReport> accOutCaseHelpExamList= JSON.parseObject(JSON.toJSONString(list), new TypeReference<List<WipStorageReport>>() {});
            for (WipStorageReport wipStorageReport : accOutCaseHelpExamList) {
                wipStorageReport.setCreateTime(new Date());
                if(StringUtils.isNotBlank(wipStorageReport.getSectionNo())){
                    //长晶棒产出查询长度、尺寸、掺杂类型
                    LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(wipStorageReport.getSectionNo());
                    if(null!=byCrystalNo){
                        if(StringUtils.isEmpty(String.valueOf(wipStorageReport.getLength()))){
                            wipStorageReport.setLength(byCrystalNo.getLength());
                        }
                        if(StringUtils.isEmpty(wipStorageReport.getSize())){
                            wipStorageReport.setSize(byCrystalNo.getSpecs());
                        }
                        if(StringUtils.isEmpty(wipStorageReport.getDopant())){
                            wipStorageReport.setDopant(byCrystalNo.getDopant());
                        }
                    }
                    //等级
                    YbInspectData bySectionNo = ybInspectDataService.findBySectionNo(wipStorageReport.getSectionNo());
                    if(null!=bySectionNo)
                    wipStorageReport.setLevel(bySectionNo.getProductLevel());
                    //物料状态
                    PlanProcessingOrder byCode = planProcessingOrderService.findByCode(wipStorageReport.getSectionNo());
                    if(null!=byCode){
                        if(byCode.getStatus()==5){
                            wipStorageReport.setMaterialState("成品");
                        }
                        wipStorageReport.setMaterialState("半成品");
                    }
                    //长度,尺寸,掺杂类型
                    ProductionPaintingLineDetail bySegmentNo = productionPaintingLineDetailService.findBySegmentNo(wipStorageReport.getSectionNo());
                    if(null!=bySegmentNo) {
                        wipStorageReport.setLength(bySegmentNo.getLength());
                        wipStorageReport.setSize(bySegmentNo.getDiameter());
                        ProductionPaintingLine productionPaintingLine = productionPaintingLineService.get(bySegmentNo.getOperateDrawId());
                        if(null!=productionPaintingLine)
                            wipStorageReport.setDopant(productionPaintingLine.getDopant());
                    }

                }

            }
            wipStorageReportService.saveAll(accOutCaseHelpExamList);
        });

    }
}
