package com.sw.mes.yb.scheduled;

import com.sw.mes.enumvalue.EnumValuesServer;
import com.sw.mes.sap.entity.ReportErpOutputRecord;
import com.sw.mes.sap.entity.ReportErpOutputRecordDetail;
import com.sw.mes.sap.service.ReportErpOutputRecordDetailService;
import com.sw.mes.sap.service.ReportErpOutputRecordService;
import com.sw.mes.yb.pad.entity.ProductionPaintingLine;
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.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 定时组装erp数据
 * @author wujiaxin
 */
@Slf4j
@Component
public class PackageErpCrystalDataScheduling {

    @Autowired
    private LongCrystalService longCrystalService;

    @Autowired
    private ReportErpOutputRecordService reportErpOutRecordService;

    @Autowired
    private ReportErpOutputRecordDetailService reportErpOutputRecordDetailService;

    @Autowired
    private ProductionPaintingLineService productionPaintingLineService;


    /**
     * 组装长晶棒产出实体定时任务（已划线长晶棒）
     */
    @XxlJob("packageCrystalOutPutData")
    public void packageCrystalOutPutData() {
        //拉取该时间段未上报的已划线的长晶棒产出数据
        log.info("长晶棒信息定时拉取【已划线】");
        List<Map<String,Object>> listMap = longCrystalService.pullOutPutData();
        //根据工单号合并
        if(!CollectionUtils.isEmpty(listMap)){

            Map<String,List<Map>>  map1 = listMap.stream().collect(Collectors.groupingBy(map->map.get("erpOrderNo").toString()));
            Iterator iterator = map1.keySet().iterator();
            //合并后插入新建的主表、插入新建的子表
            //组装批次号
            while (iterator.hasNext()) {
                String key = iterator.next().toString();
                List<Map> maps =  map1.get(key);
                Map temp = maps.get(0);
                ReportErpOutputRecord reportErpOutputRecord = new ReportErpOutputRecord();
                //组装实体
                reportErpOutputRecord.setOrderNo(temp.get("erpOrderNo").toString());
                reportErpOutputRecord.setCost(BigDecimal.ZERO);
                reportErpOutputRecord.setCreateTime(new Date());
                reportErpOutputRecord.setPushed(0);
//            reportErpOutputRecord.setType(0);
//            reportErpSquareOutputRecord.setDepartmentCode();
//            reportErpSquareOutputRecord.setSerial();

                if(temp.get("auart").toString().equals(EnumValuesServer.ProductionOrderType.常规.getKey()) || temp.get("auart").toString().equals(EnumValuesServer.ProductionOrderType.实验.getKey())){
                    reportErpOutputRecord.setWarehouseCode("017");
                    reportErpOutputRecord.setReceiveCode("10216");
                } else if(temp.get("auart").toString().equals(EnumValuesServer.ProductionOrderType.研发.getKey())){
                    reportErpOutputRecord.setWarehouseCode("032");
                    reportErpOutputRecord.setReceiveCode("10201");
                }

                BigDecimal weight =  new BigDecimal(0);
                reportErpOutRecordService.saveOrUpdate(reportErpOutputRecord);
                for(Map map : maps){
                    ReportErpOutputRecordDetail reportErpOutputRecordDetail = new ReportErpOutputRecordDetail();
                    reportErpOutputRecordDetail.setRid(reportErpOutputRecord.getId());
                    reportErpOutputRecordDetail.setBid(Long.parseLong(map.get("id").toString()));
                    reportErpOutputRecordDetail.setOrderNo(map.get("orderNo") == null? "":map.get("orderNo").toString());
                    reportErpOutputRecordDetail.setLotNo(map.get("lotNo") == null? "":map.get("lotNo").toString());
                    reportErpOutputRecordDetail.setCrystalNo(map.get("crystalNo") == null ? "" :map.get("crystalNo").toString());
                    reportErpOutputRecordDetail.setReportWorkState(0);
                    BigDecimal weight1 = map.get("weight") == null ? BigDecimal.ZERO:new BigDecimal(map.get("weight").toString());
                    reportErpOutputRecordDetail.setWeight(weight1);
                    weight = weight.add(weight1);

                    LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(reportErpOutputRecordDetail.getCrystalNo());
                    reportErpOutputRecordDetail.setWorkShop(byCrystalNo.getWorkShop());
                    reportErpOutputRecordDetail.setProductType(byCrystalNo.getProductType());
                    reportErpOutputRecordDetail.setCrystalType(byCrystalNo.getConductiveType());
                    reportErpOutputRecordDetail.setLength(byCrystalNo.getLength());
                    reportErpOutputRecordDetail.setWeight(byCrystalNo.getWeight());
                    reportErpOutputRecordDetail.setSpecs(byCrystalNo.getSpecs());
                    reportErpOutputRecordDetail.setLotNo(byCrystalNo.getLotNo());
                    reportErpOutputRecordDetail.setStartTime(byCrystalNo.getLineStartTime());

//                    //查询划线记录
//                    ProductionPaintingLine byCrystalNo1 = productionPaintingLineService.findByCrystalNo(reportErpOutputRecordDetail.getCrystalNo());
//                    if(null != byCrystalNo1){
//                        reportErpOutputRecordDetail.setStartTime(byCrystalNo1.getStartTime());
//                    }
                    reportErpOutputRecordDetailService.saveOrUpdate(reportErpOutputRecordDetail);

                    byCrystalNo.setRodPulled(1);
                    longCrystalService.saveOrUpdate(byCrystalNo);
                }
                reportErpOutputRecord.setQuantity(weight);
                reportErpOutRecordService.saveOrUpdate(reportErpOutputRecord);
            }

            //更改原来库里是否拉取的字段
            List<Long> idList1 = listMap.stream().map(map->Long.parseLong(map.get("id").toString())).collect(Collectors.toList());
            longCrystalService.updatePulledState(idList1);
        }
    }

}
