package com.qs.mp.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.mp.channel.domain.ChannelOrder;
import com.qs.mp.channel.service.IChannelOrderService;
import com.qs.mp.channel.service.IChannelUserRelService;
import com.qs.mp.common.enums.ChannelCommissionResourceEnum;
import com.qs.mp.common.enums.ChannelOrderTypeEnum;
import com.qs.mp.common.exception.ServiceException;
import com.qs.mp.framework.domain.AsyncTask;
import com.qs.mp.framework.mapper.AsyncTaskMapper;
import com.qs.mp.framework.service.IAsyncTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.mp.user.service.IUserTicketOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-06-07
 */
@Service
public class AsyncTaskServiceImpl extends ServiceImpl<AsyncTaskMapper, AsyncTask> implements IAsyncTaskService {

    @Autowired
    private IChannelOrderService channelOrderService;

    @Autowired
    private IUserTicketOrderService userTicketService;;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void channelConfirmReceipt(AsyncTask asyncTask) {
        // 获取供应商订单编号
        String orderId = asyncTask.getBizId();

        // 查询订单
        ChannelOrder channelOrder = channelOrderService.getById(orderId);
        if (Objects.isNull(channelOrder)) {
            throw new ServiceException("经销商订单不存在,OrderId:" + orderId);
        }

        if (ChannelOrderTypeEnum.RETAIL.equals(channelOrder.getType())) {
            return;
        }

        // 分佣
        userTicketService.commToChannel(orderId, ChannelCommissionResourceEnum.CHANNEL.getValue());
    }
}
