package com.jeequan.jeepay.pay.service.payorderpush;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.jeequan.jeepay.components.mq.model.PushSpeakerMQ;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import com.jeequan.jeepay.db.entity.MchStore;
import com.jeequan.jeepay.db.entity.MemberAccountHistory;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.service.impl.MchStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/*
* 支付成功 通知处理 service
*
* @author zx
*
* @date 2021/6/8 17:43
*/
@Slf4j
public abstract class AbstractPushService {

    @Autowired MchStoreService mchStoreService;

    // 校验订单
    protected Boolean preCheck(PayOrder payOrder){

        if(payOrder == null) {
            log.error("查询支付订单为空");
            return false;
        }
        if(payOrder.getState() != PayOrder.STATE_SUCCESS) {
            log.error("订单状态不是支付成功，payOrderId={}，state={}", payOrder.getPayOrderId(), payOrder.getState());
            return false;
        }

        // 超出播报时间的订单  不再播报
        if(DateUtil.compare(DateUtil.date(), DateUtil.offsetMinute(payOrder.getSuccessTime(), PayOrderInfo4Device.EXPIRED_TIME)) > 0) {
            log.error("订单超出{}分钟，不再播报，payOrderId={}，订单成功时间={}", PayOrderInfo4Device.EXPIRED_TIME, payOrder.getPayOrderId(), payOrder.getSuccessTime());
            return false;
        }

        return true;
    }

    // 播报订单类型
    protected String getOrderType(Byte orderType) {
        if (orderType == PushSpeakerMQ.MsgPayload.ORDER_TYPE_PAY) {
            return "支付订单号";
        }else  if (orderType == PushSpeakerMQ.MsgPayload.ORDER_TYPE_MEMBER) {
            return "会员余额支付流水";
        }else {
            return "";
        }
    }

    // 播报订单信息
    protected PayOrderInfo4Device getSpeakInfo(PayOrder payOrder) {

        PayOrderInfo4Device speakPayOrderInfo = new PayOrderInfo4Device();
        BeanUtil.copyProperties(payOrder, speakPayOrderInfo);

        return speakPayOrderInfo;
    }

    // 打印订单信息
    protected PayOrderInfo4Device getPrintInfo(PayOrder payOrder) {

        PayOrderInfo4Device printPayOrderInfo = new PayOrderInfo4Device();
        BeanUtil.copyProperties(payOrder, printPayOrderInfo);

        return printPayOrderInfo;
    }

    // 播报订单信息
    protected PayOrderInfo4Device getOrderInfo(MemberAccountHistory accountHistory) {

        PayOrderInfo4Device speakPayOrderInfo = new PayOrderInfo4Device();
        BeanUtil.copyProperties(accountHistory, speakPayOrderInfo);

        speakPayOrderInfo.setPayOrderId(String.valueOf(accountHistory.getHid()));
        speakPayOrderInfo.setAmount(Math.abs(accountHistory.getChangeAmount()));
        speakPayOrderInfo.setWayCodeType(PayOrderInfo4Device.BOARD_TYPE_MEMBER); // 会员余额支付

        if (accountHistory.getStoreId() != null) {
            MchStore mchStore = mchStoreService.getById(accountHistory.getStoreId());
            speakPayOrderInfo.setStoreName(mchStore.getStoreName());
        }

        speakPayOrderInfo.setWayCodeType(PayOrderInfo4Device.BOARD_TYPE_MEMBER);

        return speakPayOrderInfo;
    }

}
