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


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.erp.api.ErpExternalApi;
import com.zt.plat.module.erp.api.dto.ErpSubmitReqDTO;
import com.zt.plat.module.erp.controller.admin.erp.vo.*;
import com.zt.plat.module.system.api.user.AdminUserApi;
import com.zt.plat.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

@Service
public class ErpOrderServiceImpl implements ErpOrderService {
    @Resource
    public ErpExternalApi erpExternalApi;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public String submitOrderToErp061(ErpOrderSaveReqVO createVo) {

        ErpSubmitReqDTO reqDTO = buildBaseReqDTO(createVo, "061");

        Map<String, Object> req = new HashMap<>();
        req.put("head", createVo.getHead());
        req.put("item", createVo.getItems());
        req.put("comp", createVo.getComps());
        req.put("exte", createVo.getExte());
        req.put("acct", createVo.getAccts());
        req.put("itex", createVo.getItex());
        reqDTO.setReq(req);
        String reqJson = com.alibaba.fastjson2.JSON.toJSONString(reqDTO);
        return submitToErp(reqDTO);
    }

    private ErpSubmitReqDTO buildBaseReqDTO(ErpOrderSaveReqVO vo, String funcnr) {
        ErpSubmitReqDTO reqDTO = new ErpSubmitReqDTO();
//        reqDTO.setFuncnr(funcnr);
//        reqDTO.setBskey(vo.getOrderNo());
//        reqDTO.setUsrid(String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
//        reqDTO.setUsrnm((SecurityFrameworkUtils.getLoginUserNickname()));
        AdminUserRespDTO userRespDTO = getWorkId();
        reqDTO.setFuncnr(funcnr);
        reqDTO.setBskey(vo.getOrderNo());
        reqDTO.setUsrid(userRespDTO.getWorkcode());
        reqDTO.setUsrnm(userRespDTO.getUsername());
        return reqDTO;
    }


    private String submitToErp(ErpSubmitReqDTO reqDTO) {
        Map<String, String> response = erpExternalApi.submitDataToErp(reqDTO);
        return JSONObject.toJSONString(response);
    }


    @Override
    public String submitOrderToErp062(ErpOrderUpdateReqVO updateVo) {
        ErpSubmitReqDTO reqDTO = buildBaseReqDTO062(updateVo, "062");
        Map<String, Object> req = new HashMap<>();
        Map<String, Object> head = new HashMap<>();
        Map<String, Object> exte= new HashMap<>();
        head.put("po_number", updateVo.getPoNumber());
        head.put("delete_ind", updateVo.getDeleteInd());
        head.put("currency", updateVo.getCurrency());
        head.put("exch_rate", updateVo.getExchRate());
        req.put("head", head);
        exte.put("zzhth", updateVo.getZzhth());
        exte.put("zzxyh", updateVo.getZzhth());
        exte.put("zznote", updateVo.getZnote());
        exte.put("zzlifnr", updateVo.getZlifnr());
        req.put("exte", exte);
        req.put("itex", updateVo.getItexs());
        req.put("item", updateVo.getItems());
        req.put("acct", updateVo.getAccts());
        req.put("comp", updateVo.getComps());
        reqDTO.setReq(req);

        String reqJson = com.alibaba.fastjson2.JSON.toJSONString(reqDTO);
        return submitToErp(reqDTO);
    }

    @Override
    public String submitOrderToErp091(ErpSalesOrderSaveReqVO createVo) {
        ErpSubmitReqDTO reqDTO = buildBaseReqDTO091(createVo, "091");

        Map<String, Object> req = new HashMap<>();
        req.put("head", createVo.getHead());
        req.put("item", createVo.getItems());
        req.put("conds", JSONArray.parseArray(createVo.getConds().toString()));
        req.put("partn_es", createVo.getPartn_es());
        req.put("partn_numb", createVo.getPartn_numb());
        reqDTO.setReq(req);
        String reqJson = com.alibaba.fastjson2.JSON.toJSONString(reqDTO);

        return submitToErp(reqDTO);
    }

    @Override
    public String submitOrderToErp092(SalesDocumentRequestVO salesDocumentRequest) {
        ErpSubmitReqDTO reqDTO = buildBaseReqDTO092(salesDocumentRequest,"092");
        Map<String, Object> req = new HashMap<>();
        req.put("head", salesDocumentRequest.getHead());
        req.put("item", salesDocumentRequest.getItemList());
        req.put("cond", JSONArray.parseArray(salesDocumentRequest.getCondList().toString()));
        req.put("salesdocument", salesDocumentRequest.getSalesdocument());
        req.put("updateflag", salesDocumentRequest.getUpdateflag());
        req.put("partn_es", salesDocumentRequest.getPartn_es());
        reqDTO.setReq(req);
        String reqJson = com.alibaba.fastjson2.JSON.toJSONString(reqDTO);
        return submitToErp(reqDTO);
    }
    private ErpSubmitReqDTO buildBaseReqDTO092(SalesDocumentRequestVO salesDocumentRequest,String funcnr){
        AdminUserRespDTO userRespDTO = getWorkId();
        ErpSubmitReqDTO reqDTO = new ErpSubmitReqDTO();
        reqDTO.setFuncnr(funcnr);
        reqDTO.setBskey(salesDocumentRequest.getOrderNo());
        reqDTO.setUsrid(userRespDTO.getWorkcode());
        reqDTO.setUsrnm(userRespDTO.getUsername());
        return reqDTO;
    }

    private ErpSubmitReqDTO buildBaseReqDTO062(ErpOrderUpdateReqVO vo, String funcnr) {
        ErpSubmitReqDTO reqDTO = new ErpSubmitReqDTO();
//        reqDTO.setFuncnr(funcnr);
//        reqDTO.setBskey(vo.getPoNumber());
//        reqDTO.setUsrid(String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
//        reqDTO.setUsrnm((SecurityFrameworkUtils.getLoginUserNickname()));
        AdminUserRespDTO userRespDTO = getWorkId();
        reqDTO.setFuncnr(funcnr);
        reqDTO.setBskey(vo.getPoNumber());
        reqDTO.setUsrid(userRespDTO.getWorkcode());
        reqDTO.setUsrnm(userRespDTO.getUsername());
        return reqDTO;
    }

    ErpSubmitReqDTO buildBaseReqDTO091(ErpSalesOrderSaveReqVO vo, String funcnr){
        AdminUserRespDTO userRespDTO = getWorkId();
        ErpSubmitReqDTO reqDTO = new ErpSubmitReqDTO();
        reqDTO.setFuncnr(funcnr);
        reqDTO.setBskey(vo.getOrderNo());
        reqDTO.setUsrid(userRespDTO.getWorkcode());
        reqDTO.setUsrnm(userRespDTO.getUsername());
//        reqDTO.setBskey(vo.getOrderNo());
//        reqDTO.setUsrid(String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
//        reqDTO.setUsrnm((SecurityFrameworkUtils.getLoginUserNickname()));
     return reqDTO;
    }

    private  AdminUserRespDTO getWorkId(){
        CommonResult<AdminUserRespDTO> user = adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId());
        return user.getData();
    }
}
