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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.MchStoreTerminal;
import com.jeequan.jeepay.core.interfaces.paychannel.IIsvmchTerminalService;
import com.jeequan.jeepay.core.model.params.yspay.YspayIsvParams;
import com.jeequan.jeepay.core.model.params.yspay.YspayIsvsubMchParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.channel.yspay.utils.YspayKit;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class YspayIsvmchTerminalService implements IIsvmchTerminalService {

    @Override
    public ChannelRetMsg sendup(MchStoreTerminal mchStoreTerminal, String isvNo, String mchNo, String mchAppId, String ifCode, boolean isSendup) {

        // 获取到 服务商配置信息
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        YspayIsvParams isvParams = (YspayIsvParams)configContextQueryService.queryIsvParams(isvNo, ifCode);
        YspayIsvsubMchParams mchParams = (YspayIsvsubMchParams)configContextQueryService.queryIsvsubMchParams(mchNo, mchAppId, ifCode);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        try {
            JSONObject bizContent = new JSONObject();

            JSONArray infoList = new JSONArray();
            JSONObject info = new JSONObject();
            // 商户号
            info.put("mercId", mchParams.getMercId());
            //CUPS_WECHAT : 银联微信支付
            //CUPS_ALIPAY : 银联支付宝
            //CUPS_POS_SCANPAY : 银联云闪付
            //NUCC_WECHAT : 网联终端采集
            info.put("channelCode", "CUPS_POS_SCANPAY");
            // 00-新增 01-修改 02-注销
            info.put("operationType", isSendup?"00":"02");
            info.put("terminalType", "11");
            info.put("terminalNo", mchStoreTerminal.getTrmNo());
            info.put("terminalAddr", mchStoreTerminal.getDetailAddress());
            infoList.add(info);

            bizContent.put("terminalInfoList", infoList.toJSONString());
            // 发送请求
            String resultStr = YspayKit.applymentRequest("/openapi/report/scan/channel/terminalReport", "bindTerm", isvParams, bizContent);
            JSONObject response = JSONObject.parseObject(resultStr);
            JSONObject resData = response.getJSONObject("data");
            String code = resData.getString("code");
            if ("00".equals(code)) {
                String trmNo = resData.getString("trmNo");
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
                channelRetMsg.setChannelOrderId(trmNo);
                return channelRetMsg;
            } else {
                log.error("【银盛终端报备】 请求失败：code={}, msg={}", code, resData.getString("note"));
                channelRetMsg.setChannelErrMsg(resData.getString("note"));
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                return channelRetMsg;
            }
        }  catch (Exception e) {
            log.error("【银盛终端报备】绑定终端设备失败", e);
            channelRetMsg.setChannelErrMsg(e.getMessage());
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
            return channelRetMsg;
        }

    }
}
