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

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.interfaces.k3.CallK3SupplierServices;
import com.weaver.seconddev.interfaces.k3.SupplierServiceStub;
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_SupplierInfo动作流
 */

@org.springframework.stereotype.Service("workflow_action_K3_Check_SupplierInfo_Action")
@Slf4j
public class K3_Check_SupplierInfo_Action implements EsbServerlessRpcRemoteInterface {

    @Autowired
    private RequestInfo requestInfoTemp;
    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        log.error("K3_Check_SupplierInfo_Action execute start");
        log.error("K3_Check_SupplierInfo_Action execute params : " + params);
        try {
            String datacenterid = null2String(params.get("datacenterid")); //数据中心
            String  supname= null2String(params.get("supname")); //供应商名称
            String supnumber = null2String(params.get("supnumber")); //供应商编号
            String code = "0";
            String message = "";
            String issuccess = "";
            String result = "";
            SupplierServiceStub.SupplierSearchReq req = new SupplierServiceStub.SupplierSearchReq();
            req.setUser("oa");
            req.setPwd("k3cloud_htgd");
            req.setFName(supname);
            req.setFNumber(supnumber);
            req.setDataCenterID(datacenterid);
            CallK3SupplierServices services = new CallK3SupplierServices();
            SupplierServiceStub.SupplierSearchRes res = services.getSupMsg(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>> suppliers = new ArrayList<>();

            if(!result.equals("0")) {
                SupplierServiceStub.ArrayOfSupSample supsample = res.getSupSamples();
                SupplierServiceStub.SupSample[] arrs = supsample.getSupSample();
                if (arrs != null && arrs.length > 0) {
                    for (int i = 0; i < arrs.length; i++) {
                        Map<String,Object> item = new HashMap<>();
                        item.put("supname", arrs[i].getFSupName());
                        item.put("supnumber", arrs[i].getFSupNumber());
                        suppliers.add(item);
                    }
                }
            }
            resultJson.put("suppliers",suppliers);
            return WeaResult.success(resultJson);
        }catch (Exception e){
            log.error("K3_Check_SupplierInfo_Action execute error", e);
            return WeaResult.fail(500, "K3_Check_SupplierInfo_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;
    }
}
