package com.zt.plat.module.erp.service.erp;

import com.zt.plat.module.erp.api.ErpExternalApi;
import com.zt.plat.module.erp.api.dto.BillMainSubmitErpReqDTO;
import com.zt.plat.module.erp.api.dto.ErpSubmitReqDTO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBillMainSaveReqVO;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

@Service
@Validated
public class ErpBillMainServiceImpl implements ErpBillMainService {

    @Resource
    public ErpExternalApi erpExternalApi;

    @Override
    public String submitBillMainToErp(ErpBillMainSaveReqVO createVo) {
        ErpSubmitReqDTO reqDTO = buildBaseReqDTO(createVo, "071");

        Map<String, Object> req = new HashMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("pstng_date", createVo.getPostingDate());
        head.put("doc_date", createVo.getPostingDate());
        head.put("header_txt", createVo.getRemark());
        head.put("bill_of_lading", "");
        req.put("head", head);

        List<Map<String, Object>> items = getMaps(createVo);
        req.put("item", items);
        reqDTO.setReq(req);

        return submitToErp(reqDTO);
    }

    @Override
    public String submitBillMainReverseToErp(ErpBillMainSaveReqVO createVo) {
        ErpSubmitReqDTO reqDTO = buildBaseReqDTO(createVo, "079");

        Map<String, Object> req = new HashMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("pstng_date", createVo.getPostingDate());
        head.put("mat_doc", createVo.getMaterialCoding());
        head.put("doc_year", "");
        head.put("header_txt", "");
        req.put("head", head);

        List<Map<String, Object>> items = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        item.put("matdoc_item", createVo.getPoItem());
        item.put("line_id", "");
        items.add(item);
        req.put("item", items);
        reqDTO.setReq(req);

        return submitToErp(reqDTO);
    }

    @Override
    public String submitErp(BillMainSubmitErpReqDTO reqDTO) {
        String res = null;
        switch (reqDTO.getFuncnr()) {
            case "095":
                // TODO 暂时返回成功
                /*ErpSubmitReqDTO erpSubmitReqDTO = buildBaseReqDTO(createVo, reqDTO.getFuncnr());
                Map<String, Object> req = new HashMap<>();
                Map<String, Object> head = new HashMap<>();
                head.put("pstng_date", createVo.getPostingDate());
                head.put("doc_date", createVo.getPostingDate());
                head.put("header_txt", createVo.getRemark());
                head.put("bill_of_lading", "");
                req.put("head", head);
                List<Map<String, Object>> items = getMaps(createVo);
                req.put("item", items);
                erpSubmitReqDTO.setReq(req);
                res = submitToErp(erpSubmitReqDTO);*/

                res = "095";
                break;
            case "071":
                // TODO 暂时返回成功
                /*ErpSubmitReqDTO erpSubmitReqDTO = buildBaseReqDTO(createVo, reqDTO.getFuncnr());
                Map<String, Object> req = new HashMap<>();
                Map<String, Object> head = new HashMap<>();
                head.put("pstng_date", createVo.getPostingDate());
                head.put("doc_date", createVo.getPostingDate());
                head.put("header_txt", createVo.getRemark());
                head.put("bill_of_lading", "");
                req.put("head", head);
                List<Map<String, Object>> items = getMaps(createVo);
                req.put("item", items);
                erpSubmitReqDTO.setReq(req);
                res = submitToErp(erpSubmitReqDTO);*/

                res = "071";
                break;
            case "093":
                // TODO 暂时返回成功
                /*ErpSubmitReqDTO erpSubmitReqDTO = buildBaseReqDTO(createVo, reqDTO.getFuncnr());
                Map<String, Object> req = new HashMap<>();
                Map<String, Object> head = new HashMap<>();
                head.put("pstng_date", createVo.getPostingDate());
                head.put("doc_date", createVo.getPostingDate());
                head.put("header_txt", createVo.getRemark());
                head.put("bill_of_lading", "");
                req.put("head", head);
                List<Map<String, Object>> items = getMaps(createVo);
                req.put("item", items);
                erpSubmitReqDTO.setReq(req);
                res = submitToErp(erpSubmitReqDTO);*/

                res = "093";
                break;
            default:
                break;
        }
        return res;
    }

    // 抽取重复代码：构建基础 ErpSubmitReqDTO
    private ErpSubmitReqDTO buildBaseReqDTO(ErpBillMainSaveReqVO vo, String funcnr) {
        ErpSubmitReqDTO reqDTO = new ErpSubmitReqDTO();
        reqDTO.setFuncnr(funcnr);
        reqDTO.setBskey(vo.getBillNumber());
        reqDTO.setUsrid(vo.getCreatorId());
        reqDTO.setUsrnm(vo.getCreatorName());
        return reqDTO;
    }

    // 抽取重复代码：提交 ERP 并记录日志
    private String submitToErp(ErpSubmitReqDTO reqDTO) {
        Map<String, String> response = erpExternalApi.submitDataToErp(reqDTO);
        return response.get("resStr");
    }

    @NotNull
    private static List<Map<String, Object>> getMaps(ErpBillMainSaveReqVO createVo) {
        List<Map<String, Object>> items = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        item.put("material", createVo.getMaterialCoding());
        item.put("plant", createVo.getFactoryNumber());
        item.put("stge_loc", createVo.getWarehouseNumber());
        item.put("batch", createVo.getBatch());
        item.put("entry_qnt", createVo.getMaterialQuantity());
        item.put("entry_uom", createVo.getMaterialUom());
        item.put("po_number", createVo.getOrderNo());
        item.put("po_item", createVo.getPoItem());
        item.put("customer", createVo.getCustomerNumber());
        item.put("sales_ord", "");
        item.put("s_ord_item", "");
        item.put("consume_ant", "");
        item.put("item_text", createVo.getRemark());
        item.put("ref_doc_yr", "");
        item.put("ref_doc", "");
        item.put("ref_doc_it", "");
        item.put("unload_pt", "");
        items.add(item);
        return items;
    }
}
