package com.prolog.cs.biz.inbound.utils;

import com.prolog.cs.basic.common.model.SysRegistry;
import com.prolog.cs.biz.inbound.remote.CommonCaller;
//import com.prolog.cs.biz.inbound.vo.taobao.AlibabaAlihealthDrugKytGetentinfoRequestV2;
import com.prolog.framework.common.message.RestMessage;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAlihealthDrugMscGetentinfoRequest;
import com.taobao.api.response.AlibabaAlihealthDrugMscGetentinfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class TaobaoUtil {

    @Resource
    private CommonCaller commonCaller;

    /**
     * 根据企业名称查询企业唯一标识【ref_ent_id】和企业ID【ent_id】
     * @param ent_name
     * @return
     */
    public String getentinfo(String ent_name,String enterpriseid){
        //查询系统配置的参数
        RestMessage<SysRegistry> restMessage = commonCaller.getRegistryInfo("refEntId",enterpriseid);
        if (!restMessage.isSuccess()) {
            throw new RuntimeException("未查询到 refEntId 的系统配置参数:" + restMessage.getMessage());
        }
        SysRegistry data1 = restMessage.getData();
        String refEntId = data1.getRegValue();

        RestMessage<SysRegistry> restMessage3 = commonCaller.getRegistryInfo("serverUrl",enterpriseid);
        if (!restMessage3.isSuccess()) {
            throw new RuntimeException("未查询到 serverUrl 的系统配置参数:" + restMessage.getMessage());
        }
        SysRegistry data3 = restMessage3.getData();
        String serverUrl = data3.getRegValue();

        RestMessage<SysRegistry> restMessage4 = commonCaller.getRegistryInfo("appKey",enterpriseid);
        if (!restMessage4.isSuccess()) {
            throw new RuntimeException("未查询到 appKey 的系统配置参数:" + restMessage.getMessage());
        }
        SysRegistry data4 = restMessage4.getData();
        String appKey = data4.getRegValue();

        RestMessage<SysRegistry> restMessage5 = commonCaller.getRegistryInfo("appSecret",enterpriseid);
        if (!restMessage5.isSuccess()) {
            throw new RuntimeException("未查询到 appSecret 的系统配置参数:" + restMessage.getMessage());
        }
        SysRegistry data5 = restMessage5.getData();
        String appSecret = data5.getRegValue();

        TaobaoClient client = new DefaultTaobaoClient(serverUrl, appKey, appSecret);
//        AlibabaAlihealthDrugKytGetentinfoRequestV2 req = new AlibabaAlihealthDrugKytGetentinfoRequestV2();
//        req.setRef_ent_id(refEntId);
//        req.setEntName(ent_name);
//        AlibabaAlihealthDrugKytGetentinfoResponse rsp = null;

        AlibabaAlihealthDrugMscGetentinfoRequest req = new AlibabaAlihealthDrugMscGetentinfoRequest();
        req.setRefEntId(refEntId);
        req.setEntName(ent_name);
        AlibabaAlihealthDrugMscGetentinfoResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        log.info("获取企业唯一标识返回结果:{}",rsp.getBody());
        if("SUCCESS".equals(rsp.getResult().getMsgCode())){
            return  rsp.getResult().getModel().getEntId();
        }else {
            return  "";
        }
    }

    public String getentinfoV2(String ent_name,String refEntId,String serverUrl,String appKey,String appSecret){

        TaobaoClient client = new DefaultTaobaoClient(serverUrl, appKey, appSecret);
        AlibabaAlihealthDrugMscGetentinfoRequest req = new AlibabaAlihealthDrugMscGetentinfoRequest();
        req.setRefEntId(refEntId);
        req.setEntName(ent_name);
        AlibabaAlihealthDrugMscGetentinfoResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        log.info("获取企业唯一标识返回结果:{}",rsp.getBody());
        if("SUCCESS".equals(rsp.getResult().getMsgCode())){
            return  rsp.getResult().getModel().getEntId();
        }else {
            return  "";
        }
    }
}
