package com.aucma.api.task;

import com.aucma.api.domain.dto.SAPPortDto;
import com.aucma.api.domain.dto.WERKSDto;
import com.aucma.api.service.ISAPPortService;
import com.aucma.common.utils.DateUtils;
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;

    /**
     * SAP物料主数据定时任务
     *
     * @param days 开始时间（天）
     */
    public void SAPMaterialDataTask(String factoryCode, Long days) {
        SAPPortDto sapPortDto = this.SAPRequestDataAssembly(factoryCode, days);
        portService.getSAPMaterialData(sapPortDto);
    }

    /**
     * 生产工单定时任务
     *
     * @param days 开始时间（天）
     */
    public void SAPProduceWorkTicketsDataTask(String factoryCode, Long days) {
        SAPPortDto sapPortDto = this.SAPRequestDataAssembly(factoryCode, days);
        portService.getSapBaseOrderInfo(sapPortDto);
    }
    /**
     *设备台账定时任务
     *
     * @param days 开始时间（天）
     */
    public void SAPDeviceLedgerDataTask(String factoryCode, Long days) {
        SAPPortDto sapPortDto = this.SAPRequestDataAssembly(factoryCode, days);
        portService.getSapDeviceLedgers(sapPortDto);
    }

    /**
     * 产品bom信息定时任务
     *
     * @param days 开始时间（天）
     */
    public void SAPBaseBomInfoDataTask(String factoryCode, Long days) {
        SAPPortDto sapPortDto = this.SAPRequestDataAssembly(factoryCode, days);
        portService.getSapOrderBomInfo(sapPortDto);
    }


    /**
     * 定时任务：SAP请求数据组装
     * @param factoryCode
     * @param days
     * @return
     */
    public SAPPortDto SAPRequestDataAssembly(String factoryCode, Long days) {
        String startDate = DateUtils.convertDaysToDateString(days);
        String endDate = DateUtils.getDate();
        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;
    }

}
