package com.jeequan.jeepay.thirdparty.channel.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.AlipayCommerceIotDeviceBindModel;
import com.alipay.api.domain.AlipayCommerceIotDeviceBindQueryModel;
import com.alipay.api.domain.AlipayCommerceIotDeviceUnbindModel;
import com.alipay.api.request.AlipayCommerceIotDeviceBindQueryRequest;
import com.alipay.api.request.AlipayCommerceIotDeviceBindRequest;
import com.alipay.api.request.AlipayCommerceIotDeviceUnbindRequest;
import com.alipay.api.response.AlipayCommerceIotDeviceBindQueryResponse;
import com.alipay.api.response.AlipayCommerceIotDeviceBindResponse;
import com.alipay.api.response.AlipayCommerceIotDeviceUnbindResponse;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.paychannel.alipaybiz.IAlipayIotDeviceBindService;
import com.jeequan.jeepay.core.model.params.alipay.AlipayIsvParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/*
* 支付宝Iot设备接口
*
* @author zx
* 
* @date 2023/3/8 15:13
*/
@Slf4j
@Service
public class AlipayIotDeviceBindService implements IAlipayIotDeviceBindService {

    @Override
    public void bind(AlipayIsvParams alipayIsvParams, String mchNo, String alipayMerchantNo, String alipayShopId, String supplierId, JSONObject deviceJSON) {
        AlipayCommerceIotDeviceBindRequest request = new AlipayCommerceIotDeviceBindRequest();
        AlipayCommerceIotDeviceBindModel model = new AlipayCommerceIotDeviceBindModel();
        model.setAppType("MINI_APP");
        model.setMiniAppId("RUYI_LITE");
        model.setDeviceIdType("SN");
        model.setDeviceSn(deviceJSON.getString("deviceNo"));
        model.setSupplierId(supplierId); // 设备供应商ID：
        model.setExternalId(mchNo); // jeepay商户号
        model.setMerchantIdType("direct");
        model.setMerchantId(alipayMerchantNo);
        model.setShopId(alipayShopId);
        model.setSource(alipayIsvParams.getPid()); // 受理商户的ISV在支付宝的pid
        model.setSpiAppId(alipayIsvParams.getAppId()); // 表示ISV在开放平台注册的SPI服务应用的app_id
        model.setTerminalBindInfo(deviceJSON.toJSONString()); // 由ISV自定义的扩展字段，在支付宝侧向SPI服务发起请求时透传给ISV

        request.setBizModel(model);
        AlipayCommerceIotDeviceBindResponse response = AlipayKit.getAlipayClientWrapper(alipayIsvParams).getAlipayClient().execute(request);
        log.info("alipayIot设备绑定响应，deviceNo={}，response={}", deviceJSON.getString("deviceNo"), JSONObject.toJSONString(response));
        if (!response.isSuccess()) {
            throw new BizException(AlipayKit.appendErrMsg(response.getMsg(), response.getSubMsg()));
        }
    }

    @Override
    public void unbind(AlipayIsvParams alipayIsvParams, String mchNo, String alipayMerchantNo, String alipayShopId, String supplierId, String deviceSn){
        AlipayCommerceIotDeviceUnbindRequest request = new AlipayCommerceIotDeviceUnbindRequest();
        AlipayCommerceIotDeviceUnbindModel model = new AlipayCommerceIotDeviceUnbindModel();
        model.setAppType("MINI_APP");
        model.setMiniAppId("RUYI_LITE");
        model.setDeviceIdType("SN");
        model.setDeviceSn(deviceSn);
        model.setSupplierId(supplierId);
        model.setSource(alipayIsvParams.getPid());
        model.setExternalId(mchNo); // jeepay商户号 1678169795992
        model.setMerchantIdType("direct");
        model.setMerchantId(alipayMerchantNo);
        model.setShopId(alipayShopId);

        request.setBizModel(model);
        AlipayCommerceIotDeviceUnbindResponse response = AlipayKit.getAlipayClientWrapper(alipayIsvParams).getAlipayClient().execute(request);
        log.info("alipayIot设备解绑响应，deviceNo：{}，response={}", deviceSn, JSONObject.toJSONString(response));
        if ("NOT_BIND".equals(response.getSubCode())) {
            return;
        }
        if (!response.isSuccess()) {
            throw new BizException(AlipayKit.appendErrMsg(response.getMsg(), response.getSubMsg()));
        }
    }

    @Override
    public JSONObject query(AlipayIsvParams alipayIsvParams, String deviceSn){
        AlipayCommerceIotDeviceBindQueryRequest request = new AlipayCommerceIotDeviceBindQueryRequest();
        AlipayCommerceIotDeviceBindQueryModel model = new AlipayCommerceIotDeviceBindQueryModel();
        model.setAppType("MINI_APP");
        model.setMiniAppId("RUYI_LITE");
        model.setDeviceIdType("SN");
        model.setDeviceSn(deviceSn);
        request.setBizModel(model);

        AlipayCommerceIotDeviceBindQueryResponse response = AlipayKit.getAlipayClientWrapper(alipayIsvParams).getAlipayClient().execute(request);
        log.info("alipayIot设备查询响应，deviceNo：{}，response：{}", deviceSn, JSONObject.toJSONString(response));
        if (!response.isSuccess()) {
            throw new BizException(AlipayKit.appendErrMsg(response.getMsg(), response.getSubMsg()));
        }

        return (JSONObject) JSONObject.toJSON(response);
    }

}
