package com.jeequan.jeepay.member.ctrl.member;

import com.jeequan.jeepay.components.mq.model.PushPrinterMQ;
import com.jeequan.jeepay.components.mq.model.PushSpeakerMQ;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.QRCodeParams;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.db.entity.MchQrcodeCard;
import com.jeequan.jeepay.db.entity.MemberAccountHistory;
import com.jeequan.jeepay.db.entity.MemberInfo;
import com.jeequan.jeepay.member.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MemberAccountHistoryService;
import com.jeequan.jeepay.service.impl.MemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 会员账户支付  controller
*
* @author zx
* @date 2023/4/12 17:27
*/
@RestController
@RequestMapping("/api/member/pay")
public class MemberPayController extends CommonCtrl {

    @Autowired private MemberInfoService memberInfoService;
    @Autowired private MemberAccountHistoryService memberAccountHistoryService;
    @Autowired private IMQSender mqSender;

    /**
     * 会员账户支付
     **/
    @PostMapping
    public ApiRes pay() {

        // token转换对象
        QRCodeParams qrCodeParams = tokenConvert();

        MchQrcodeCard mchQrCode = mchQrcodeCardService.getById(qrCodeParams.getId());
        if(mchQrCode == null || mchQrCode.getQrcState() != CS.YES || mchQrCode.getBindState() != CS.YES){
            throw new BizException("当前静态码不存在或不可用或未绑定");
        }

        // 获取商户配置信息
        MchAppConfigContext mchAppConfigContext = getQrParamsMchAppConfigContext(qrCodeParams, false);
        if(mchAppConfigContext == null){
            throw new BizException("商户或商户应用不存在");
        }
        if(mchAppConfigContext.getMchInfo() == null || mchAppConfigContext.getMchInfo().getState() != CS.YES){
            throw new BizException("商户信息不存在或商户状态不可用");
        }

        Long amount = getRequiredAmountL("amount");
        if(amount <= 0) {
            throw new BizException("金额错误");
        }

        MemberInfo memberInfo = memberInfoService.selectOne(mchAppConfigContext.getMchNo(), getCurrentMbrId());
        if (memberInfo == null || memberInfo.getState() != CS.YES) {
            throw new BizException("会员不存在或已禁用");
        }

        MemberAccountHistory accountHistory = memberAccountHistoryService.updateMbrAccountAndInsertHistory(memberInfo.getMchNo(), mchQrCode.getStoreId(),
                mchQrCode.getQrcId(), memberInfo.getMbrId(), -amount, MemberAccountHistory.BIZ_CONSUME, null, getValString("remark"));

        // 语音播报
        this.speak(String.valueOf(accountHistory.getHid()));

        return ApiRes.ok();
    }

    public void speak(String hid) {

        // 发送订单支付成功  云喇叭通知
        mqSender.send(PushSpeakerMQ.build(hid, PushSpeakerMQ.MsgPayload.ORDER_TYPE_MEMBER));

        // 发送订单支付成功  云打印通知
        mqSender.send(PushPrinterMQ.build(hid, PushPrinterMQ.MsgPayload.ORDER_TYPE_MEMBER));
    }


}
