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

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.interfaces.k3.CallK3CustomerService;
import com.weaver.seconddev.interfaces.k3.CustomerServiceStub;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * @Author: calyrex
 * @CreateTime: 2025-01-17
 * @Description: K3_Check_CustomerInfo动作流调用
 */
@org.springframework.stereotype.Service("workflow_action_K3_Check_CustomerInfo_Action")
@Slf4j
public class K3_Check_CustomerInfo_Action implements EsbServerlessRpcRemoteInterface {

    @Autowired
    private RequestInfo requestInfoTemp;
    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        log.error("K3_Check_CustomerInfo_Action execute start");
        log.error("K3_Check_CustomerInfo_Action execute params : " + params);
        try {
            String datacenterid = null2String(params.get("datacenterid")); //数据中心
            String custname= null2String(params.get("custname")); //客户名称
            String custnumber = null2String(params.get("custnumber")); //客户编号
            String code = "0";
            String message = "";
            String issuccess = "";
            String result = "";
            CustomerServiceStub.CustomerSearchReq  req = new CustomerServiceStub.CustomerSearchReq();
            req.setUser("oa");
            req.setPwd("k3cloud_htgd");
            req.setFName(custname);
            req.setFNumber(custnumber);
            req.setDataCenterID(datacenterid);
            CallK3CustomerService services = new CallK3CustomerService();
            CustomerServiceStub.CustomerSearchRes res = services.getCustMsg(req);
            code = res.getCode();
            message = res.getMessage();
            issuccess = res.getIsSuccess();
            result = res.getResult();
            Map<String, Object> resultJson=new HashMap<>();
            resultJson.put("code",code);
            resultJson.put("message",message);
            resultJson.put("IsSuccess",issuccess);
            resultJson.put("result",result);
            List<Map<String,Object>> customers = new ArrayList<>();
            if(!result.equals("0")) {
                CustomerServiceStub.ArrayOfCustSample supsample = res.getCustSamples();
                CustomerServiceStub.CustSample[] arrs = supsample.getCustSample();
                if (arrs != null && arrs.length > 0) {
                    for (int i = 0; i < arrs.length; i++) {
                        Map<String,Object> item = new HashMap<>();
                        item.put("custname", arrs[i].getFCustName());
                        item.put("custnumber", arrs[i].getFCustNumber());
                        customers.add(item);
                    }
                }
            }
            resultJson.put("customers",customers);
            return WeaResult.success(resultJson);
        }catch (Exception e){
            log.error("K3_Check_CustomerInfo_Action execute error", e);
            return WeaResult.fail(500, "K3_Check_CustomerInfo_Action execute error", e);
        }
    }

    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;
    }
}
