package com.aucma.api.service.impl;

import com.aucma.api.domain.vo.SAPResultOrderInfoVo;
import com.aucma.api.domain.vo.SAPResultVo;
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.base.service.IBaseDeviceLedgerService;
import com.aucma.base.service.IBaseMaterialInfoService;
import com.aucma.base.service.IBaseOrderInfoService;
import com.aucma.base.service.IOrderBomInfoService;
import com.aucma.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * SAP接口存入数据业务层
 *
 * @Author YinQ
 * @create 2023-09-27 15:07
 */
@Service
public class SAPPutStorageServiceImpl implements ISAPPutStorageService {

    @Autowired
    private IBaseMaterialInfoService baseMaterialInfoService;

    @Autowired
    private IBaseDeviceLedgerService baseDeviceLedgerService;

    @Autowired
    private IBaseOrderInfoService baseOrderInfoService;

    @Autowired
    private IOrderBomInfoService orderBomInfoService;

    /**
     * 转物料实体类 保存数据
     *
     * @param resultVo
     * @return
     */
    public ArrayList<BaseMaterialInfo> insertSAPMaterialInfo(SAPResultVo resultVo) {
        ArrayList<BaseMaterialInfo> baseMaterialInfoArrayList = new ArrayList<>();
        List<HashMap<String, String>> itemList = resultVo.getO_TAB().get("item");
        for (HashMap<String, String> map : itemList) {
            BaseMaterialInfo baseMaterialInfo = new BaseMaterialInfo();
            baseMaterialInfo.setPlantCode(map.get("WERKS"));
            baseMaterialInfo.setMaterialCode(map.get("MATNR"));
            List<BaseMaterialInfo> baseMaterialInfoList = baseMaterialInfoService.selectBaseMaterialInfoList(baseMaterialInfo);
            if (baseMaterialInfoList.isEmpty()) {
                BaseMaterialInfo materialInfo = new BaseMaterialInfo();
                materialInfo.setMaterialCode(map.get("MATNR"));
                materialInfo.setMaterialName(map.get("MAKTX"));
                materialInfo.setMaterialType(map.get("MTART"));
                materialInfo.setPlantCode(map.get("WERKS"));
                materialInfo.setMaterialUnit(map.get("MEINS"));
                baseMaterialInfoService.insertBaseMaterialInfo(materialInfo);
                baseMaterialInfoArrayList.add(materialInfo);
            }
        }
        return baseMaterialInfoArrayList;
    }

    /**
     * 转换生产工单实体类 保存数据
     */
    @Override
    public ArrayList<BaseOrderInfo> insertSAPBaseOrderInfo(SAPResultOrderInfoVo resultVo) throws ParseException {
        ArrayList<BaseOrderInfo> baseOrderInfoList = new ArrayList<>();
        List<HashMap<String, String>> itemList = resultVo.getO_TAB().get("item");
        for (HashMap<String, String> map : itemList) {
            BaseOrderInfo boInfo = new BaseOrderInfo();
            boInfo.setOrderCode(map.get("AUFNR"));
            boInfo.setMaterialCode(map.get("MATNR"));
            boInfo.setFactoryCode(map.get("WERKS"));
            List<BaseOrderInfo> baseOrderInfos = baseOrderInfoService.selectBaseOrderInfoList(boInfo);
            if (baseOrderInfos.isEmpty()) {
                BaseOrderInfo baseOrderInfo = new BaseOrderInfo();
                baseOrderInfo.setOrderCode(map.get("AUFNR"));
                baseOrderInfo.setOrderStatus(map.get("STTXT"));
                baseOrderInfo.setSaleOrderCode(map.get("KDAUF"));
                baseOrderInfo.setMaterialCode(map.get("MATNR"));
                baseOrderInfo.setMaterialName(map.get("MAKTX"));
                if (!map.get("GAMNG").equals(null)) {
                    baseOrderInfo.setOrderAmount(new BigDecimal(map.get("GAMNG")));
                }
                baseOrderInfo.setFactoryCode(map.get("WERKS"));
                baseOrderInfo.setOrderType(map.get("AUART"));
                baseOrderInfo.setBeginDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get("GLTRP")));
                baseOrderInfo.setEndDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get("GSTRP")));
                baseOrderInfo.setCreatedTime(DateUtils.getNowDate());
                baseOrderInfoService.insertBaseOrderInfo(baseOrderInfo);
                baseOrderInfoList.add(baseOrderInfo);
            }

        }


        return baseOrderInfoList;
    }

    /**
     * 转设备台账实体类 保存数据
     *
     * @param resultVo
     * @return
     */
    @Override
    public ArrayList<BaseDeviceLedger> insertSAPDeviceLedger(SAPResultOrderInfoVo resultVo) throws ParseException {
        ArrayList<BaseDeviceLedger> baseDeviceLedgerArrayList = new ArrayList<>();
        List<HashMap<String, String>> itemList = resultVo.getO_TAB().get("item");
        for (HashMap<String, String> map : itemList) {
            BaseDeviceLedger device = new BaseDeviceLedger();
            device.setCostCenter(map.get("KOSTL"));;
            device.setDeviceCode(map.get("EQUNR"));
            device.setFactoryCode(map.get("IWERK"));
            List<BaseDeviceLedger> baseDeviceLedgers = baseDeviceLedgerService.selectBaseDeviceLedgerList(device);
            if (!baseDeviceLedgers.isEmpty()) {
                BaseDeviceLedger deviceLedger = new BaseDeviceLedger();
                deviceLedger.setCostCenter(map.get("KOSTL"));//
                deviceLedger.setFactoryCode(map.get("IWERK"));
                deviceLedger.setDeviceName(map.get("EQKTX"));
                deviceLedger.setEnableDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get("INBDT")));
                deviceLedger.setManufacturer(map.get("ELIEF"));
                deviceLedger.setDeviceType(map.get("EQTYP"));
                deviceLedger.setDeviceCode(map.get("EQUNR"));//
                deviceLedger.setUsedDepartment(map.get("NAME1"));
                deviceLedger.setCreatedTime(DateUtils.getNowDate());
                baseDeviceLedgerService.insertBaseDeviceLedger(deviceLedger);
                baseDeviceLedgerArrayList.add(deviceLedger);
            }
        }
        return baseDeviceLedgerArrayList;
    }

    /**
     * 转换产品bom信息实体类 保存数据
     *
     * */
    @Override
    public ArrayList<OrderBomInfo> insertSAPOrderBomInfo(SAPResultOrderInfoVo resultVo) throws ParseException {
        ArrayList<OrderBomInfo> orderBomInfoList = new ArrayList<>();
        List<HashMap<String, String>> itemList = resultVo.getO_TAB().get("item");

        for (HashMap<String, String> map : itemList) {
            OrderBomInfo bomInfo = new OrderBomInfo();
            bomInfo.setMaterialCode(map.get("IDNRK"));
            bomInfo.setParentId(map.get("MATNR"));
            bomInfo.setFactoryCode(map.get("WERKS"));
            List<OrderBomInfo> orderBomInfos = orderBomInfoService.selectOrderBomInfoList(bomInfo);
            if (orderBomInfos.isEmpty()) {
                OrderBomInfo orderBomInfo = new OrderBomInfo();
                orderBomInfo.setFactoryCode(map.get("WERKS"));
                orderBomInfo.setParentId(map.get("MATNR"));
                orderBomInfo.setMaterialCode(map.get("IDNRK"));
                if (!map.get("MENGE").isEmpty()) {
                    orderBomInfo.setStandardAmount(new BigDecimal(map.get("MENGE")));
                }
                orderBomInfo.setSort(map.get("SORTF"));
                orderBomInfo.setVbeln(map.get("VBELN"));
                orderBomInfo.setVbpos(map.get("VBPOS"));
                orderBomInfoService.insertOrderBomInfo(orderBomInfo);
                orderBomInfoList.add(orderBomInfo);
            }
        }

        return orderBomInfoList;
    }

}
