package com.zhongshu.card.server.core.service.hxz;

import cn.hutool.json.JSONUtil;
import com.github.microservice.models.hxz.*;
import com.github.microservice.models.type.OrderFromType;
import com.github.microservice.net.ResultContent;
import com.github.microservice.types.payment.PaymentType;
import com.zhongshu.card.server.core.dao.devices.DeviceInfoDao;
import com.zhongshu.card.server.core.dao.org.UserCountDao;
import com.zhongshu.card.server.core.dao.payment.ExpenseFlowDao;
import com.zhongshu.card.server.core.dao.school.CardInfoDao;
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
import com.zhongshu.card.server.core.domain.payment.ExpenseFlow;
import com.zhongshu.card.server.core.httpRequest.ApiRequestService;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.payment.ExpenseFlowServiceImpl;
import com.zhongshu.card.server.core.service.payment.RequestInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author TRX
 * @date 2024/6/25
 */
@Slf4j
@Service
public class HxzService extends SuperService {

    @Autowired
    DeviceInfoDao deviceInfoDao;

    @Autowired
    RequestInfoService requestInfoService;

    @Autowired
    ApiRequestService apiRequestService;

    @Autowired
    CardInfoDao cardInfoDao;

    @Autowired
    UserCountDao userCountDao;

    @Autowired
    ExpenseFlowServiceImpl expenseFlowService;

    @Autowired
    ExpenseFlowDao expenseFlowDao;

    /**
     * 得到系统时间
     *
     * @param param
     * @return
     */
    public ResultContent serverTime(ServerTimeModel param) {
        String deviceId = param.getDeviceID().toString();
        ServerTimeResult result = new ServerTimeResult();
        DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
        if (ObjectUtils.isEmpty(deviceInfo)) {
            result.setFailed("业务设备未注册");
            requestInfoService.addRequestInfo(param, result);
            return ResultContent.buildFail("业务设备未注册");
        } else {
            result.setSuccess();
        }
        String str = JSONUtil.toJsonStr(result);
        requestInfoService.addRequestInfo(param, result);
        return ResultContent.buildSuccess(str);
    }

    /**
     * 云板消费机 刷卡消费
     *
     * @param param
     * @return
     */
    public ResultContent consumTransactions(ConsumTransactionsModel param) {
        // 通过
        param.setOrderFromType(OrderFromType.Swipe);
        // 返回参数
        ConsumTransactionsResult resultModel = new ConsumTransactionsResult();

        org.springframework.util.StopWatch stopWatch = new org.springframework.util.StopWatch();
        stopWatch.start("t1");

        String mqttDataId = param.getMqttDataId();
        ExpenseFlow expenseFlow = expenseFlowService.init(mqttDataId);
        stopWatch.stop();

        if (ObjectUtils.isEmpty(expenseFlow)) {
            resultModel.setFailed("重复订单");
            return ResultContent.buildSuccess(JSONUtil.toJsonStr(resultModel));
        }
        stopWatch.start("t2 创建订单");
        // 创建订单
        expenseFlowService.createExpenseFlowByHxz(param, expenseFlow);
        stopWatch.stop();

        stopWatch.start("t3 支付");
        // 订单支付
        ResultContent<ExpenseFlow> resultContent = expenseFlowService.orderPay(expenseFlow);
        if (resultContent.isFailed()) {
            resultModel.setFailed(resultContent.getMsg());
            requestInfoService.addRequestInfo(param, resultModel);
            return ResultContent.buildSuccess(JSONUtil.toJsonStr(resultModel));
        }
        stopWatch.stop();
        stopWatch.start("t4 创建支付结果");
        // 构建返回结果
        resultModel = expenseFlowService.buildPayResult(expenseFlow.getOrderNo());
        stopWatch.stop();
        stopWatch.start("t5");

        requestInfoService.addRequestInfo(param, resultModel);
        stopWatch.stop();
        log.info("time; {}", stopWatch.prettyPrint());
        return ResultContent.buildSuccess(expenseFlowService.buildPayResultStr(resultModel));
    }

    /**
     * 用户刷卡消费结果查询接口
     *
     * @param param
     * @return
     */
    public ResultContent orderQuery(OrderQueryModel param) {
        // 构建返回结果
        ConsumTransactionsResult resultModel = expenseFlowService.buildPayResult(param.getOrder());
        requestInfoService.addRequestInfo(param, resultModel);
        return ResultContent.buildSuccess(expenseFlowService.buildPayResultStr(resultModel));
    }

    /**
     * 付款码（二维码）支付接口
     *
     * @param param
     * @return
     */
    public ResultContent qRCodeTransaction(QRCodeTransactionModel param) {
        //  返回参数
        QRCodeTransactionResult model = new QRCodeTransactionResult();
        String mqttDataId = param.getMqttDataId();
        ExpenseFlow expenseFlow = expenseFlowService.init(mqttDataId);
        if (ObjectUtils.isEmpty(expenseFlow)) {
            model.setFailed("重复订单");
            return ResultContent.buildSuccess(JSONUtil.toJsonStr(model));
        }

        // 创建订单
        ResultContent<ExpenseFlow> createResult = expenseFlowService.createExpenseFlowByHxz(param, expenseFlow);
        expenseFlow = createResult.getContent();

        if (expenseFlow.getPaymentType() == PaymentType.UserWallet) {
            // 订单支付
            ResultContent<ExpenseFlow> resultContent = expenseFlowService.orderPay(expenseFlow);
            if (resultContent.isSuccess()) {
                model.setSuccess();
                model.setQrorder(expenseFlow.getOrderNo());
            } else {
                model.setFailed(resultContent.getMsg());
            }
        } else {
            // 扣款支付订单
            ResultContent<ExpenseFlow> resultContent = expenseFlowService.expendPayQrCode(expenseFlow);
            if (resultContent.isFailed()) {
                model.setFailed(resultContent.getMsg());
            } else {
                model.setSuccess();
                model.setQrorder(expenseFlow.getOrderNo());
            }
        }
        // 记录日志
        requestInfoService.addRequestInfo(param, model);
        return ResultContent.buildSuccess(JSONUtil.toJsonStr(model));
    }

    /**
     * 用户刷卡消费结果查询接口
     *
     * @param param
     * @return
     */
    public ResultContent transactionInquiry(TransactionInquiryModel param) {
        // 构建返回结果
        ConsumTransactionsResult resultModel = expenseFlowService.buildPayResult(param.getQROrder());
        requestInfoService.addRequestInfo(param, resultModel);
        return ResultContent.buildSuccess(expenseFlowService.buildPayResultStr(resultModel));
    }

}
