package com.iformall.service.msg.impl;

import com.iformall.common.ErrorCode;
import com.iformall.domain.po.*;
import com.iformall.domain.po.msg.BaseMsg;
import com.iformall.domain.po.msg.FmInsideCouponVerifyMsg;
import com.iformall.domain.po.msg.FmInsideOrderSuccessMsg;
import com.iformall.enums.EnumCouponVerifyType;
import com.iformall.exception.MallinkException;
import com.iformall.service.*;
import com.iformall.service.msg.MsgSendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Stormeye Wu
 * @date 2019/5/10
 */
@Service
public class FmInsideCouponVerifyMsgServiceImpl implements MsgSendService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxCouponOrderService couponOrderService;

    @Autowired
    private WxMerchantBUserService merchantBUserService;

    @Override
    public void send(BaseMsg baseMsg) throws Exception{
        FmInsideCouponVerifyMsg msg = (FmInsideCouponVerifyMsg)baseMsg;
        WxCouponOrder couponOrder = couponOrderService.getById(msg.getCouponOrderId());
        if(couponOrder == null) {
            logger.error("券包ID未找到: " + msg.getCouponOrderId());
            throw new MallinkException(ErrorCode.COUPON_ORDER_IS_NULL.getCode(), "券包ID未找到" + msg.getCouponOrderId());
        }

        WxMerchantBUser user = merchantBUserService.getById(msg.getBUserId());
        if(user == null) {
            logger.error("用户未找到: " + msg.getBUserId());
            throw new MallinkException(ErrorCode.USER_IS_EMPTY.getCode(), "用户未找到" + msg.getBUserId());
        }

        couponOrderService.actionAfterVerify(couponOrder, user);
    }
}
