package com.weaver.seconddev.interfaces.workflow.controller;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.seconddev.interfaces.workflow.action.ZOA_FICO_COC_CHECK;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: calyrex
 * @CreateTime: 2025-01-10
 * @Description:ZOA_FICO_COC_CHECK的API调用
 */

@Slf4j
@RestController
@RequestMapping({"/papi/secondev/workflow/controller" })
public class ZOA_FICO_COC_CHECK_CONTROLLER {
    @GetMapping("/ZOA_FICO_COC_CHECK")
    public WeaResult<String> run(@RequestParam Map<String,Object> params) {
        log.error("ZOA_FICO_COC_CHECK_CONTROLLER execute start");
        log.error("ZOA_FICO_COC_CHECK_CONTROLLER execute params : " + params);
        try {
            String cbzxdm = null2String(params.get("cbzxdm")); //成本中心代码

            String estatus = "E";
            String emessage = "";

            ZOA_FICO_COC_CHECK po=new ZOA_FICO_COC_CHECK();
            Map<String,String> map = po.Getexecute(cbzxdm);
            if(map != null && map.size() > 0){
                estatus = map.get("estatus");
                emessage = map.get("emessage");
                JSONObject xmlData = new JSONObject();
                xmlData.put("estatus", estatus);
                xmlData.put("emessage", emessage);

                // 依次将各个元素添加到子 Map 中
                xmlData.put("DATAB", null2String(map.get("DATAB")));
                xmlData.put("KTEXT", null2String(map.get("KTEXT")));
                xmlData.put("LTEXT", null2String(map.get("LTEXT")));
                xmlData.put("VERAK", null2String(map.get("VERAK")));
                xmlData.put("KOSAR", null2String(map.get("KOSAR")));
                xmlData.put("KHINR", null2String(map.get("KHINR")));
                xmlData.put("BUKRS", null2String(map.get("BUKRS")));
                xmlData.put("FKBER", null2String(map.get("FKBER")));
                xmlData.put("WAERS", null2String(map.get("WAERS")));
                xmlData.put("PRCTR", null2String(map.get("PRCTR")));
                xmlData.put("ZZWERKS", null2String(map.get("ZZWERKS")));
                xmlData.put("KOKRS", null2String(map.get("KOKRS")));
                xmlData.put("KOSTL", null2String(map.get("KOSTL")));
                xmlData.put("CCNAME", null2String(map.get("CCNAME")));
                xmlData.put("CBNAME", null2String(map.get("CBNAME")));
                xmlData.put("LRNAME", null2String(map.get("LRNAME")));
                xmlData.put("GCNAME", null2String(map.get("GCNAME")));
                xmlData.put("HBNAME", null2String(map.get("HBNAME")));
                xmlData.put("GSNAME", null2String(map.get("GSNAME")));
                return WeaResult.success(JSONObject.toJSONString(xmlData));
            }else {
                estatus = map.get("estatus");
                emessage = map.get("emessage");
                JSONObject xmlData = new JSONObject();
                xmlData.put("estatus", estatus);
                xmlData.put("emessage", emessage);
                return WeaResult.success(JSONObject.toJSONString(xmlData));
            }
        }catch (Exception e){
            log.error("ZOA_FI_CUSTOMER_MAINTAIN_CONTROLLER execute error : " , e);
            return WeaResult.fail(500, "执行异常", e.getMessage());
        }
    }

    public static String null2String(String s){
        return s == null ? "" : s;
    }

    public static String null2String(Object o){
        return o == null ? "" : o.toString();
    }

    public static String null2String(String s1,String s2){
        return s1 == null ? (s2 == null ? "" : s2) : s1;
    }
}
