package com.aucma.api.task;

import com.aucma.api.domain.dto.SAPBOMPortDto;
import com.aucma.api.domain.dto.SAPPortDto;
import com.aucma.api.domain.dto.WERKSDto;
import com.aucma.api.service.ISAPPortService;
import com.aucma.base.domain.BaseOrderInfo;
import com.aucma.base.mapper.PublicMapper;
import com.aucma.base.service.IBaseOrderInfoService;
import com.aucma.base.service.IRecordDnbInstantService;
import com.aucma.common.utils.DateUtils;
import com.aucma.production.domain.BaseBomInfo;
import com.aucma.production.service.IBaseBomInfoService;
import com.aucma.production.service.IProductPlanInfoService;
import com.aucma.report.service.IDeviceAlarmRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author YinQ
 * @create 2023-10-08 9:11
 */
@Component("SAPTask")
public class SAPTask {

    @Autowired
    private ISAPPortService portService;

    @Autowired
    private IBaseBomInfoService baseBomInfoService;

    @Autowired
    private IProductPlanInfoService productPlanInfoService;

    @Autowired
    private IBaseOrderInfoService orderInfoService;

    @Autowired
    private IDeviceAlarmRecordService deviceAlarmRecordService;

    @Autowired
    private IRecordDnbInstantService recordDnbInstantService;

    @Autowired
    private PublicMapper publicMapper;

    /**
     * SAP物料主数据定时任务
     * @param factoryCode
     * @param startDays
     * @param endDays
     */
    public void SAPMaterialDataTask(String factoryCode, Long startDays, Long endDays) {
        SAPPortDto sapPortDto = this.SAPRequestDataAssembly(factoryCode, startDays, endDays);
        portService.getSAPMaterialData(sapPortDto);
    }

    /**
     * 生产工单定时任务
     *
     * @param factoryCode
     * @param startDays
     * @param endDays
     */
    public void SAPProduceWorkTicketsDataTask(String factoryCode, Long startDays, Long endDays) {
        SAPPortDto sapPortDto = this.SAPRequestDataAssembly(factoryCode, startDays, endDays);
        portService.getSapBaseOrderInfo(sapPortDto);
    }

    /**
     *设备台账定时任务
     *
     * @param factoryCode
     * @param startDays
     * @param endDays
     */
    public void SAPDeviceLedgerDataTask(String factoryCode, Long startDays, Long endDays) {
        SAPPortDto sapPortDto = this.SAPRequestDataAssembly(factoryCode, startDays, endDays);
        portService.getSapDeviceLedgers(sapPortDto);
    }

    /**
     * 产品bom信息定时任务
     *
     * @param factoryCode
     * @param startDays
     * @param endDays
     */
    public void SAPBaseBomInfoDataTask(String factoryCode, Long startDays, Long endDays) {
        SAPPortDto sapPortDto = this.SAPRequestDataAssembly(factoryCode, startDays, endDays);
        SAPBOMPortDto sapbomPortDto = new SAPBOMPortDto();
        sapbomPortDto.setFactoryCodes(sapPortDto.getFactoryCodes());
        String startDate = sapPortDto.getStartDate();
        String endDate = sapPortDto.getEndDate();
        List<String> dateList = DateUtils.getDifferenceDateList(startDate, endDate);
        for (String date : dateList) {
            sapbomPortDto.setStartDate(date);
            sapbomPortDto.setEndDate(date);
            try {
                portService.getSapOrderBomInfo(sapbomPortDto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


//    /**
//     * 自动同步生产BOM定时任务
//     *
//     * 当前业务已经改为全部手动维护树形BOM，这里注释掉定时任务入口，避免定时器再次写入生产BOM。
//     *
//     */
//    public void automaticSynchronizationBOM() {
//        baseBomInfoService.addAutomaticSynchronizationBOM(new BaseBomInfo());
//    }

    /**
     * 自动下达MES工单
     *
     */
    public void automaticReleaseOrderPlan() {
        //查询未下达MES工单的SAP计划
        BaseOrderInfo orderInfo = new BaseOrderInfo();
        orderInfo.setIsRelease(1L);
        List<BaseOrderInfo> baseOrderInfos = orderInfoService.selectBaseOrderInfoList(orderInfo);
        for (BaseOrderInfo baseOrderInfo : baseOrderInfos) {
            try {
                productPlanInfoService.releaseOrderPlan(baseOrderInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }


    /**
     * 定时任务：SAP请求数据组装
     * @param factoryCode
     * @param startDays
     * @param endDays
     * @return
     */
    public SAPPortDto SAPRequestDataAssembly(String factoryCode, Long startDays, Long endDays) {
        String startDate = DateUtils.convertDaysToDateString(startDays,0);
        String endDate = DateUtils.convertDaysToDateString(endDays,1);
        SAPPortDto sapPortDto = new SAPPortDto();
        WERKSDto werksDto = new WERKSDto();
        List<HashMap<String, String>> factoryList = new ArrayList<>();
        HashMap<String, String> hashMap = new HashMap<>();
        sapPortDto.setStartDate(startDate);
        sapPortDto.setEndDate(endDate);
        hashMap.put("WERKS", factoryCode);
        factoryList.add(hashMap);
        werksDto.setItem(factoryList);
        sapPortDto.setFactoryCodes(werksDto);
        return sapPortDto;
    }

    /***
     *  定时任务：定期删除设备报警记录信息
     */

    public void DeleteDeviceAlarmRecordTable(Long dates){
        deviceAlarmRecordService.deleteDeviceAlarmRecordByDates(dates);
        System.out.println("删除设备报警记录数据库中数据");
    }

    /**
     * 定时任务：定期删除电实时数据信息
     * */
    public void DeleteRecordDnbInstant(Long dates){
        recordDnbInstantService.deleteRecordDnbInstantByDates(dates);
        System.out.println("删除电实时数据");
    }
    /**
     *
     * 定时任务：定期删除门体发泡设备数据
     * */
    public void DeleteDOORFOAMREMACHINE(Long dates){
        publicMapper.DeleteDOORFOAMREMACHINE(dates);
        System.out.println("删除门体发泡数据");
    }


}
