package com.aucma.api.controller;

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.api.service.ISAPPutStorageService;
import com.aucma.base.domain.BaseDeviceLedger;
import com.aucma.base.domain.BaseMaterialInfo;
import com.aucma.base.domain.BaseOrderInfo;
import com.aucma.base.domain.OrderBomInfo;
import com.aucma.common.annotation.Log;
import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.enums.BusinessType;
import com.aucma.production.domain.CalendarInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author YinQ
 * @create 2023-09-26 14:29
 */
@RestController
@RequestMapping("/sap/port")
public class SAPPortController extends BaseController {

    @Autowired
    private ISAPPortService sapPortService;

    @Autowired
    private ISAPPutStorageService storageService;


    /**
     * 获取SAP物料主数据
     * @param paramMap
     * @return
     */
    @PostMapping("/getSAPMaterialData")
    public AjaxResult getSAPMaterialData(@RequestBody Map paramMap) {
        SAPPortDto sapPortDto = new SAPPortDto();
        sapPortDto.setStartDate(String.valueOf(paramMap.get("NDATE")));
        sapPortDto.setEndDate(String.valueOf(paramMap.get("EDATE")));
        HashMap<String,List<HashMap<String,String>>> werksMap = (HashMap<String,List<HashMap<String,String>>>)paramMap.get("WERKS");
        WERKSDto werksDto = new WERKSDto();
        werksDto.setItem(werksMap.get("item"));
        sapPortDto.setFactoryCodes(werksDto);
        List<BaseMaterialInfo> materialVoList = sapPortService.getSAPMaterialData(sapPortDto);
        return AjaxResult.success(materialVoList);
    }

    /**
     * 获取生产工单
     * @param paramMap
     * @return
     */
    @PostMapping ("/getProduceWorkTickets")
    public AjaxResult getProduceWorkTickets(@RequestBody Map paramMap){
        SAPPortDto sapPortDto = new SAPPortDto();
        sapPortDto.setStartDate(String.valueOf(paramMap.get("NDATE")));
        sapPortDto.setEndDate(String.valueOf(paramMap.get("EDATE")));
        HashMap<String,List<HashMap<String,String>>> werksMap = (HashMap<String,List<HashMap<String,String>>>)paramMap.get("WERKS");
        WERKSDto werksDto = new WERKSDto();
        werksDto.setItem(werksMap.get("item"));
        sapPortDto.setFactoryCodes(werksDto);
        List<BaseOrderInfo> BaseOrderInfoVoList = sapPortService.getSapBaseOrderInfo(sapPortDto);
        return AjaxResult.success(BaseOrderInfoVoList);
    }

    /**
     * 获取生产工单测试
     * @param params
     * @return
     */
    @PostMapping ("/getProduceWorkTest")
    public AjaxResult getProduceWorkTest(@RequestBody String params){
        List<BaseOrderInfo> BaseOrderInfoVoList = sapPortService.getProduceWorkTest(params);
        return AjaxResult.success(BaseOrderInfoVoList);
    }

    /**
     * 获取设备台账
     * @param paramMap
     * @return
     */
    @PostMapping("/getDeviceLedger")
    public AjaxResult getdeviceLedger(@RequestBody Map paramMap) {
        SAPPortDto sapPortDto = new SAPPortDto();
        sapPortDto.setStartDate(String.valueOf(paramMap.get("NDATE")));
        sapPortDto.setEndDate(String.valueOf(paramMap.get("EDATE")));
        HashMap<String,List<HashMap<String,String>>> werksMap = (HashMap<String,List<HashMap<String,String>>>)paramMap.get("WERKS");
        WERKSDto werksDto = new WERKSDto();
        werksDto.setItem(werksMap.get("item"));
        sapPortDto.setFactoryCodes(werksDto);
        List<BaseDeviceLedger> BaseDeviceLedgerVoList = sapPortService.getSapDeviceLedgers(sapPortDto);
        return AjaxResult.success(BaseDeviceLedgerVoList);
    }

    /**
     * 获取产品bom信息
     * @param paramMap
     * @return
     */
    @PostMapping("/getBaseBomInfo")
    public AjaxResult getBaseBomInfo(@RequestBody Map paramMap) {
        SAPBOMPortDto sapPortDto = new SAPBOMPortDto();
        sapPortDto.setStartDate(String.valueOf(paramMap.get("NDATE")));
        sapPortDto.setEndDate(String.valueOf(paramMap.get("EDATE")));
        HashMap<String,List<HashMap<String,String>>> werksMap = (HashMap<String,List<HashMap<String,String>>>)paramMap.get("WERKS");
        WERKSDto werksDto = new WERKSDto();
        werksDto.setItem(werksMap.get("item"));
        sapPortDto.setFactoryCodes(werksDto);
        List<OrderBomInfo> BaeeBomVoList = sapPortService.getSapOrderBomInfo(sapPortDto);
        return AjaxResult.success(BaeeBomVoList);
    }

    /**
     * 手动同步生产工单
     * @param calendarInfo
     * @return
     */
    @Log(title = "生产工单" , businessType = BusinessType.INSERT)
    @PostMapping("/addSAPCalendar")
    public AjaxResult addSAPCalendar(@RequestBody CalendarInfo calendarInfo) {
        return toAjax(sapPortService.addSAPCalendar(calendarInfo));
    }

    /**
     * 手动更换订单产线
     * @param baseOrderInfo
     * @return
     */
    @PostMapping("/replaceProductionLine")
    public AjaxResult replaceProductionLine(@RequestBody BaseOrderInfo baseOrderInfo) {
        return toAjax(storageService.replaceProductionLine(baseOrderInfo));
    }
}
