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_FI_CUSTOMER_MAINTAIN;
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-08
 * @Description: ZOA_FI_VENDOR_MAINTAIN的API调用
 */

@Slf4j
@RestController
@RequestMapping({"/papi/secondev/workflow/controller" })
public class ZOA_FI_CUSTOMER_MAINTAIN_CONTROLLER {
    @GetMapping("/ZOA_FI_CUSTOMER_MAINTAIN")
    //@PostMapping("/post")
    public WeaResult<String> run(@RequestParam Map<String,Object> params) {
        log.error("ZOA_FI_CUSTOMER_MAINTAIN_CONTROLLER execute start");
        log.error("ZOA_FI_CUSTOMER_MAINTAIN_CONTROLLER execute params : " + params);
        try {
            String edit_flag = null2String(params.get("edit_flag")); //动作类型
            String bukrs = null2String(params.get("bukrs")); //公司代码
            String kunnr = null2String(params.get("kunnr")); //客户编号
            String ktokd = null2String(params.get("ktokd")); //科目组
            String anred = null2String(params.get("anred")); //称谓
            String name1 = null2String(params.get("name1")); //名称
            String sortl = null2String(params.get("sortl")); //简称-搜索项
            String telf1 = null2String(params.get("telf1")); //电话
            String smtp_addr = null2String(params.get("smtp_addr")); //邮件
            String telf2 = null2String(params.get("telf2")); //手机
            String telfx = null2String(params.get("telfx")); //传真
            String land1 = null2String(params.get("land1")); //国家
            String regio = null2String(params.get("regio")); //地区
            String spras = null2String(params.get("spras")); //语言
            String ort01 = null2String(params.get("ort01")); //城市
            String stras = null2String(params.get("stras")); //地址
            String pstlz = null2String(params.get("pstlz"));//邮政编号
            String bankl = null2String(params.get("bankl")); //开户行行号
            String banka = null2String(params.get("banka"));//开户行名称
            String banks = null2String(params.get("banks"));//开户行国家
            String koinh = null2String(params.get("koinh")); //开户行账号
            String stcd5 = null2String(params.get("stcd5"));//纳税登记证代码

            String akont = null2String(params.get("akont")); //统驭科目
            String zterm = null2String(params.get("zterm"));//付款条件
            String zuawa = null2String(params.get("zuawa")); //排序码
            String zwels = null2String(params.get("zwels"));//付款方式
            String frgrp = null2String(params.get("frgrp"));//批准组
            String zahls = null2String(params.get("zahls")); //付款冻结



            String estatus = "E";
            String emessage = "";
            String[] prdetail = new String[38];
            prdetail[0] = edit_flag; //动作类型
            prdetail[1] = bukrs; //公司代码
            prdetail[2] = kunnr; //客户编号
            prdetail[3] = ktokd; //科目组
            prdetail[4] = anred; //称谓
            prdetail[5] = name1; //名称
            prdetail[6] = sortl; //简称-搜索项
            prdetail[7] = telf1; //电话
            prdetail[8] = smtp_addr; //邮件
            prdetail[9] = telf2; //手机
            prdetail[10] = telfx; //传真
            prdetail[11] = land1; //国家
            prdetail[12] = regio; //地区
            prdetail[13] = spras; //语言
            prdetail[14] = ort01; //城市
            prdetail[15] = stras; //地址
            prdetail[16] = pstlz;//邮政编号
            prdetail[17] = bankl; //开户行行号
            prdetail[18] = banka;//开户行名称
            prdetail[19] = banks;//开户行国家
            prdetail[20] = koinh; //开户行账号
            prdetail[21] = stcd5;//纳税登记证代码

            prdetail[22] = akont;//统驭科目
            prdetail[23] = zterm;//付款条件
            prdetail[24] = zuawa; //排序码
            prdetail[25] = zwels;//付款方式
            prdetail[26] = frgrp;//批准组

            //付款冻结
            if(zahls.equals("X")){
                prdetail[27] = zahls;
            }else{
                prdetail[27] = "";
            }

            ZOA_FI_CUSTOMER_MAINTAIN po=new ZOA_FI_CUSTOMER_MAINTAIN();
            String[][] fhz = po.Getexecute(prdetail);
            if(fhz[0][0].equals("S")){
                estatus = fhz[0][0];
            }
            emessage = fhz[0][1];
            JSONObject xmlData = new JSONObject();
            xmlData.put("estatus", estatus);
            xmlData.put("emessage", emessage);
            xmlData.put("kunnr", null2String(fhz[1][0]));
            xmlData.put("bukrs", null2String(fhz[1][1]));
            xmlData.put("ktokd", null2String(fhz[1][2]));
            xmlData.put("txt30", null2String(fhz[1][3]));
            xmlData.put("anred", null2String(fhz[1][4]));
            xmlData.put("name1", null2String(fhz[1][5]));
            xmlData.put("sortl", null2String(fhz[1][6]));
            xmlData.put("telf1", null2String(fhz[1][7]));
            xmlData.put("smtp_addr", null2String(fhz[1][8]));
            xmlData.put("telf2", null2String(fhz[1][9]));
            xmlData.put("telfx", null2String(fhz[1][10]));
            xmlData.put("land1", null2String(fhz[1][11]));
            xmlData.put("landx50", null2String(fhz[1][12]));
            xmlData.put("bland", null2String(fhz[1][13]));
            xmlData.put("bezei", null2String(fhz[1][14]));
            xmlData.put("spras", null2String(fhz[1][15]));
            xmlData.put("sptxt", null2String(fhz[1][16]));
            xmlData.put("ort01", null2String(fhz[1][17]));
            xmlData.put("stras", null2String(fhz[1][18]));
            xmlData.put("pstlz", null2String(fhz[1][19]));
            xmlData.put("bankl", null2String(fhz[1][20]));
            xmlData.put("banka", null2String(fhz[1][21]));
            xmlData.put("banks", null2String(fhz[1][22]));
            xmlData.put("landx", null2String(fhz[1][23]));
            xmlData.put("koinh", null2String(fhz[1][24]));
            xmlData.put("stcd5", null2String(fhz[1][25]));
            xmlData.put("akont", null2String(fhz[1][26]));
            xmlData.put("txt20", null2String(fhz[1][27]));
            xmlData.put("zterm", null2String(fhz[1][28]));
            xmlData.put("text1", null2String(fhz[1][29]));
            xmlData.put("zuawa", null2String(fhz[1][30]));
            xmlData.put("ttext", null2String(fhz[1][31]));
            xmlData.put("zwels", null2String(fhz[1][32]));
            xmlData.put("text2", null2String(fhz[1][33]));
            xmlData.put("frgrp", null2String(fhz[1][34]));
            xmlData.put("frgrt", null2String(fhz[1][35]));
            xmlData.put("zahls", null2String(fhz[1][36]));
            return WeaResult.success(JSONObject.toJSONString(xmlData));
        }catch (Exception e){
            log.error("ZOA_FI_VENDOR_MAINTAIN_CONTROLLER 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;
    }
}
