package com.sw.mes.sap.scheduled;

import com.alibaba.fastjson.JSONObject;
import com.sw.mes.base.global.GlobalSwitch;
import com.sw.mes.base.global.SwitchConfig;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.outside.service.EapService;
import com.sw.mes.yb.plan.entity.ProductionSchedule;
import com.sw.mes.yb.plan.service.ProductionScheduleService;
import com.sw.mes.yb.production.entity.LoadingTask;
import com.sw.mes.yb.production.service.LoadingTaskService;
import com.sw.mes.yb.report.entity.CrystalPullingInfoLog;
import com.sw.mes.yb.report.service.CrystalPullingInfoLogService;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.sw.mes.base.global.GlobalSwitchNamespace.CODE_SWITCH;

/**
 * mes对接erp接口
 * @author xushu
 */
@Slf4j
@Component
public class MesToSapScheduling {

    @Autowired
    private EapService eapService;

    @Autowired
    private LoadingTaskService loadingTaskService;

    @Autowired
    private CrystalPullingInfoLogService crystalPullingInfoLogService;

    @Autowired
    private ProductionScheduleService productionScheduleService;

    @Autowired
    private DictionaryItemService dictionaryItemService;

    /**
     * 拉取erp订单job
     */
    @XxlJob("reportArgonToSap")
    public void reportArgonToSap() {
        log.info("氩气消耗-月结");
        //查询本月所有投产的没停炉的设备报消耗
        Map<String, List<CrystalPullingInfoLog>> collect1 = new HashMap<>();
        SwitchConfig config = GlobalSwitch.switchConfig(CODE_SWITCH, "scheduleReportArgonToSap", false);
        if(config.isEnable()){
            List<LoadingTask> byConfirmTime;
            String time = config.getValue();
//            if(null != time && !time.equals("")){
//                byConfirmTime = loadingTaskService.findByConfirmTime(time,
//                        DateUtil.longSdf.format(DateUtil.getCurrentMonthLastDayEndTime8H()));
//            } else {
            byConfirmTime = loadingTaskService.findByConfirmTime(DateUtil.longSdf.format(DateUtil.addMonth(DateUtil.getCurrentMonthLastDayEndTime8H(), -1)),
                    DateUtil.longSdf.format(DateUtil.getCurrentMonthLastDayEndTime8H()));
//            }
            if(null != byConfirmTime && byConfirmTime.size() > 0){
                List<String> collect = byConfirmTime.stream().map(loadingTask -> loadingTask.getLotNo()).collect(Collectors.toList());
                List<CrystalPullingInfoLog> crystalPullingInfoLogByLotNos = crystalPullingInfoLogService.getCrystalPullingInfoLogByLotNos(collect);
                if(null != crystalPullingInfoLogByLotNos && crystalPullingInfoLogByLotNos.size() > 0){
                    collect1 = crystalPullingInfoLogByLotNos.stream().collect(Collectors.groupingBy(CrystalPullingInfoLog::getLotNo));
                }
                for (LoadingTask loadingTask : byConfirmTime) {
                    if(null != collect1 && collect1.containsKey(loadingTask.getLotNo())){
                        continue;
                    }
                    ProductionSchedule byLotNo = productionScheduleService.findByLotNo(loadingTask.getLotNo());
                    log.info("氩气：月结上报炉台信息:" + JSONObject.toJSONString(byLotNo));
                    eapService.reportArgonToSap(loadingTask.getEquipmentCode(), byLotNo.getProductionOrder().getAufnr(),loadingTask.getLotNo(), null);
                }
            }
        }
    }
}
