package com.iformall.service.msg.impl;

import com.iformall.common.ErrorCode;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.po.WxCouponChannel;
import com.iformall.domain.po.WxOrder;
import com.iformall.domain.po.msg.BaseMsg;
import com.iformall.domain.po.msg.FmInsideOrderSuccessMsg;
import com.iformall.exception.MallinkException;
import com.iformall.service.WxCUserService;
import com.iformall.service.WxCouponChannelService;
import com.iformall.service.WxCouponService;
import com.iformall.service.WxOrderService;
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 FmInsideOrderSuccessMsgServiceImpl implements MsgSendService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxOrderService orderService;

    @Autowired
    private WxCUserService userService;

    @Autowired
    private WxCouponService couponService;

    @Autowired
    private WxCouponChannelService couponChannelService;



    @Override
    public void send(BaseMsg baseMsg) throws Exception{
        FmInsideOrderSuccessMsg msg = (FmInsideOrderSuccessMsg)baseMsg;
        WxOrder record = orderService.getById(msg.getOrderId());
        if(record == null) {
            logger.error("订单未找到: " + msg.getOrderId());
            throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND.getCode(), "订单未找到" + msg.getOrderId());
        }
        if(!record.getCUserId().equals(msg.getCUserId())) {
            logger.error("订单用户不一致: " + msg.getOrderId());
            throw new MallinkException(ErrorCode.ORDER_USER_NOT_MATCH.getCode(), "订单用户不一致" + msg.getOrderId());
        }
        WxCouponChannel couponChannel = couponChannelService.getById(record.getCouponChannelId());
        if(couponChannel == null) {
            logger.error("投放渠道未找到: " + record.getCouponChannelId());
            throw new MallinkException(ErrorCode.ORDER_COUPON_CHANNEL_NOT_MATCH.getCode(), "投放渠道未找到" + record.getCouponChannelId());
        }
        if(!couponChannel.getCouponId().equals(msg.getCouponId())) {
            logger.error("券ID不一致: " + msg.getCouponId());
            throw new MallinkException(ErrorCode.ORDER_COUPON_NOT_MATCH.getCode(), "订单用户不一致" + msg.getOrderId());
        }

        WxCUser user = userService.getById(msg.getCUserId());
        if(user == null) {
            logger.error("用户未找到: " + msg.getCUserId());
            throw new MallinkException(ErrorCode.USER_IS_EMPTY.getCode(), "用户未找到" + msg.getCUserId());
        }
        WxCoupon coupon = couponService.getById(msg.getCouponId());
        if(coupon == null) {
            logger.error("券未找到" + msg.getCouponId());
            throw new MallinkException(ErrorCode.COUPON_ORDER_IS_NULL.getCode(), "券未找到" + msg.getCouponId());
        }

        // 下订单后处理
        orderService.actionAfterCouponOrderSuccess(record, coupon, user);
    }
}
