package com.sw.mes.yb.scheduled;

import com.sw.mes.enumvalue.EnumValuesServer;
import com.sw.mes.sap.entity.ReportErpSquareOutputRecord;
import com.sw.mes.sap.entity.ReportErpSquareOutputRecordDetail;
import com.sw.mes.sap.service.ReportErpSquareOutputRecordDetailService;
import com.sw.mes.sap.service.ReportErpSquareOutputRecordService;
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 PackageErpSquareCrystalDataScheduling {

    @Autowired
    private LongCrystalService longCrystalService;

    @Autowired
    private ReportErpSquareOutputRecordService reportErpSquareOutputRecordService;

    @Autowired
    private ReportErpSquareOutputRecordDetailService reportErpSquareOutputRecordDetailService;


    /**
     * 组装长晶棒产出实体定时任务（已切断长晶棒）
     */
    @XxlJob("packageSquareCrystalOutPutData")
    public void packageSquareCrystalOutPutData() {
        log.info("长晶棒信息定时拉取【已切断】");
        //拉取该时间段未上报的已切断的长晶棒产出数据
        List<Map<String,Object>> listMap = longCrystalService.pullOutPutDataCut();
        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);
                ReportErpSquareOutputRecord reportErpSquareOutputRecord = new ReportErpSquareOutputRecord();
                //组装实体
                reportErpSquareOutputRecord.setOrderNo(temp.get("erpOrderNo").toString());
                if(temp.get("auart").toString().equals(EnumValuesServer.ProductionOrderType.常规.getKey())){
                    reportErpSquareOutputRecord.setWarehouseCode("018");
                    reportErpSquareOutputRecord.setReceiveCode("10206");

                } else if(temp.get("auart").toString().equals(EnumValuesServer.ProductionOrderType.研发.getKey())){
                    reportErpSquareOutputRecord.setWarehouseCode("033");
                    reportErpSquareOutputRecord.setReceiveCode("10218");
                }
//            reportErpSquareOutputRecord.setDepartmentCode();
//            reportErpSquareOutputRecord.setSerial();
//            reportErpSquareOutputRecord.setType(0);
                reportErpSquareOutputRecord.setCost(BigDecimal.ZERO);
                reportErpSquareOutputRecord.setCreateTime(new Date());
                reportErpSquareOutputRecord.setPushed(0);

                BigDecimal weight =  new BigDecimal(0);
                reportErpSquareOutputRecordService.saveOrUpdate(reportErpSquareOutputRecord);
                for(Map map : maps){
                    ReportErpSquareOutputRecordDetail reportErpSquareOutputRecordDetail = new ReportErpSquareOutputRecordDetail();
                    LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(map.get("crystalNo") == null ? "" :map.get("crystalNo").toString());
                    reportErpSquareOutputRecordDetail.setWorkShop(byCrystalNo.getWorkShop());
                    reportErpSquareOutputRecordDetail.setProductType(byCrystalNo.getProductType());
                    reportErpSquareOutputRecordDetail.setCrystalType(byCrystalNo.getConductiveType());
                    reportErpSquareOutputRecordDetail.setLength(byCrystalNo.getLength());
                    reportErpSquareOutputRecordDetail.setWeight(byCrystalNo.getWeight());
                    reportErpSquareOutputRecordDetail.setSpecs(byCrystalNo.getSpecs());
                    reportErpSquareOutputRecordDetail.setLotNo(byCrystalNo.getLotNo());
                    reportErpSquareOutputRecordDetail.setCrystalNo(byCrystalNo.getCrystalNo());
                    reportErpSquareOutputRecordDetail.setRid(reportErpSquareOutputRecord.getId());
                    reportErpSquareOutputRecordDetail.setBid(Long.parseLong(map.get("id").toString()));
                    reportErpSquareOutputRecordDetail.setOrderNo(map.get("orderNo") == null? "":map.get("orderNo").toString());
                    reportErpSquareOutputRecordDetail.setReportWorkState(0);
                    BigDecimal weight1 = map.get("weight") == null ? BigDecimal.ZERO:new BigDecimal(map.get("weight").toString());
                    reportErpSquareOutputRecordDetail.setWeight(weight1);
                    weight = weight.add(weight1);
                    reportErpSquareOutputRecordDetailService.saveOrUpdate(reportErpSquareOutputRecordDetail);

                    byCrystalNo.setSquarePulled(1);
                    longCrystalService.saveOrUpdate(byCrystalNo);
                }
                reportErpSquareOutputRecord.setQuantity(weight);
                reportErpSquareOutputRecordService.saveOrUpdate(reportErpSquareOutputRecord);
            }

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

    }

}
