package com.yida.data.common.core.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yida.data.common.core.entity.constant.ConsumeConstant;
import com.yida.data.common.core.entity.consume.EduConsumeConfig;
import com.yida.data.common.core.entity.consume.Recharge;
import com.yida.data.common.core.exception.FebsException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 消费机工具类
 *
 * @author ZYJ
 * @date 2023/4/4
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ConsumeUtil {

    public static final String DEFAULT_CLIENT_TYPE = "4";

    /**
     * 获取人员信息
     *
     * @param config    学校消费机服务器信息
     * @param accountNo 校园卡号
     * @return cn.hutool.json.JSONObject
     * @author ZYJ
     * @date 2023/4/6 11:16
     */
    public static JSONObject getPersonInfo(EduConsumeConfig config, String accountNo) {
        // 封装请求参数
        JSONObject params = JSONUtil.createObj()
                // 人员编号
                .set("accountno", accountNo);
        HttpResponse httpResponse = HttpRequest.get(config.getServiceIp() + ConsumeConstant.GET_PERSON_INFO)
                .form(params)
                .execute();
        // 返回数据
        log.info("获取消费机人员信息接口返回数据: {}", httpResponse.body());
        JSONObject jsonObject = JSONUtil.parseObj(httpResponse.body());
        if (jsonObject.containsKey(ConsumeConstant.ERROR_CONTENT_NAME)) {
            JSONObject errorContent = jsonObject.getJSONObject(ConsumeConstant.ERROR_CONTENT_NAME);
            log.error("获取人员信息失败, param: {}, code: {}, msg: {}", params, errorContent.getStr(ConsumeConstant.ERROR_CODE_NAME),
                    errorContent.getStr(ConsumeConstant.ERROR_MESSAGE_NAME));
            throw new FebsException("获取人员信息失败");
        }
        return jsonObject;
    }

    /**
     * 挂失卡片
     *
     * @param config 学校消费机服务器信息
     * @param cardId 卡片唯一标识
     * @return cn.hutool.json.JSONObject
     * @author ZYJ
     * @date 2023/4/6 14:11
     */
    public static JSONObject lockCard(EduConsumeConfig config, String cardId) {
        // 封装请求参数
        JSONObject params = JSONUtil.createObj()
                // 卡片唯一标识
                .set("cardID", cardId);
        HttpResponse httpResponse = HttpRequest.post(config.getServiceIp() + ConsumeConstant.LOCK_CARD + "?client-type=" + DEFAULT_CLIENT_TYPE)
                .body(params.toString())
                .execute();
        // 返回数据
        log.info("挂失卡片接口返回数据: {}", httpResponse.body());
        JSONObject jsonObject = JSONUtil.parseObj(httpResponse.body());
        if (jsonObject.containsKey(ConsumeConstant.ERROR_CONTENT_NAME)) {
            JSONObject errorContent = jsonObject.getJSONObject(ConsumeConstant.ERROR_CONTENT_NAME);
            log.error("挂失卡片失败, param: {}, code: {}, msg: {}", params, errorContent.getStr(ConsumeConstant.ERROR_CODE_NAME),
                    errorContent.getStr(ConsumeConstant.ERROR_MESSAGE_NAME));
            throw new FebsException("挂失卡片失败");
        }
        return jsonObject;
    }

    /**
     * 解挂卡片
     *
     * @param config 学校消费机服务器信息
     * @param cardId 卡片唯一标识
     * @return cn.hutool.json.JSONObject
     * @author ZYJ
     * @date 2023/4/6 14:26
     */
    public static JSONObject unLockCard(EduConsumeConfig config, String cardId) {
        // 封装请求参数
        JSONObject params = JSONUtil.createObj()
                // 卡片唯一标识
                .set("cardID", cardId);
        HttpResponse httpResponse = HttpRequest.post(config.getServiceIp() + ConsumeConstant.UN_LOCK_CARD + "?client-type=" + DEFAULT_CLIENT_TYPE)
                .body(params.toString())
                .execute();
        // 返回数据
        log.info("解挂卡片接口返回数据: {}", httpResponse.body());
        JSONObject jsonObject = JSONUtil.parseObj(httpResponse.body());
        if (jsonObject.containsKey(ConsumeConstant.ERROR_CONTENT_NAME)) {
            JSONObject errorContent = jsonObject.getJSONObject(ConsumeConstant.ERROR_CONTENT_NAME);
            log.error("解挂卡片失败, param: {}, code: {}, msg: {}", params, errorContent.getStr(ConsumeConstant.ERROR_CODE_NAME),
                    errorContent.getStr(ConsumeConstant.ERROR_MESSAGE_NAME));
            throw new FebsException("解挂卡片失败");
        }
        return jsonObject;
    }

    /**
     * 充值
     *
     * @param config   学校消费机服务器信息
     * @param recharge 消费机充值类
     * @return cn.hutool.json.JSONObject
     * @author ZYJ
     * @date 2023/4/6 16:56
     */
    public static JSONObject recharge(EduConsumeConfig config, Recharge recharge) {
        // 封装请求参数
        JSONObject params = JSONUtil.createObj()
                .set("amount", recharge.getAmount())
                .set("accountNo", recharge.getAccountNo())
                .set("increase", recharge.isIncrease())
                .set("walletType", recharge.getWalletType())
                .set("transactionNumber", recharge.getTransactionNumber())
                .set("payment", JSONUtil.createObj()
                        .set("payType", recharge.getPaymentModel().getPayType())
                        .set("payTime", recharge.getPaymentModel().getPayTime())
                        .set("orderNumber", recharge.getPaymentModel().getOrderNumber())
                        .set("innerOrderNumber", recharge.getPaymentModel().getInnerOrderNumber())
                );
        HttpResponse httpResponse = HttpRequest.post(config.getServiceIp() + ConsumeConstant.RECHARGE + "?client-type=" + DEFAULT_CLIENT_TYPE)
                .body(params.toString())
                .execute();
        // 返回数据
        log.info("充值接口返回数据: {}", httpResponse.body());
        JSONObject jsonObject = JSONUtil.parseObj(httpResponse.body());
        if (jsonObject.containsKey(ConsumeConstant.ERROR_CONTENT_NAME)) {
            JSONObject errorContent = jsonObject.getJSONObject(ConsumeConstant.ERROR_CONTENT_NAME);
            log.error("充值失败, param: {}, code: {}, msg: {}", params, errorContent.getStr(ConsumeConstant.ERROR_CODE_NAME),
                    errorContent.getStr(ConsumeConstant.ERROR_MESSAGE_NAME));
            throw new FebsException("充值失败");
        }
        return jsonObject;
    }

    /**
     * 充值撤销
     *
     * @param config            学校消费机服务器信息
     * @param accountNo         校园卡号
     * @param walletType        充值钱包类型. 1:现金, 2:补贴
     * @param transactionNumber 流水号
     * @return cn.hutool.json.JSONObject
     * @author ZYJ
     * @date 2023/4/6 17:15
     */
    public static JSONObject undoRecharge(EduConsumeConfig config, String accountNo, Integer walletType, String transactionNumber) {
        // 封装请求参数
        JSONObject params = JSONUtil.createObj()
                .set("accountNo", accountNo)
                .set("walletType", walletType)
                .set("transactionNumber", transactionNumber);
        HttpResponse httpResponse = HttpRequest.post(config.getServiceIp() + ConsumeConstant.UNDO_RECHARGE + "?client-type=" + DEFAULT_CLIENT_TYPE)
                .body(params.toString())
                .execute();
        // 返回数据
        log.info("充充值撤销接口返回数据: {}", httpResponse.body());
        JSONObject jsonObject = JSONUtil.parseObj(httpResponse.body());
        if (jsonObject.containsKey(ConsumeConstant.ERROR_CONTENT_NAME)) {
            JSONObject errorContent = jsonObject.getJSONObject(ConsumeConstant.ERROR_CONTENT_NAME);
            log.error("充值撤销失败, param: {}, code: {}, msg: {}", params, errorContent.getStr(ConsumeConstant.ERROR_CODE_NAME),
                    errorContent.getStr(ConsumeConstant.ERROR_MESSAGE_NAME));
            throw new FebsException("充值撤销失败");
        }
        return jsonObject;
    }
}
