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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qs.mp.channel.domain.ChannelOrderDetail;
import com.qs.mp.channel.domain.ChannelTicketTransfer;
import com.qs.mp.channel.domain.param.ChannelTicketTransferParam;
import com.qs.mp.channel.domain.vo.ChannelMyTicketVO;
import com.qs.mp.channel.domain.vo.ChannelTicketTransferDetailVO;
import com.qs.mp.channel.mapper.ChannelTicketTransferMapper;
import com.qs.mp.channel.service.IChannelOrderDetailService;
import com.qs.mp.channel.service.IChannelTicketTransferService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 经销商盲票转让信息表 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-04-21
 */
@Service
public class ChannelTicketTransferServiceImpl extends ServiceImpl<ChannelTicketTransferMapper, ChannelTicketTransfer> implements IChannelTicketTransferService {


    @Autowired
    private IChannelOrderDetailService channelOrderDetailService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean transferTicket(Long transferChannelId, ChannelTicketTransferParam channelTicketTransferParam) {

        // 转让盲票
        ChannelTicketTransfer channelTicketTransfer = new ChannelTicketTransfer();
        channelTicketTransfer.setTransferChannelId(transferChannelId);
        channelTicketTransfer.setChannelId(channelTicketTransferParam.getChannelId());
        channelTicketTransfer.setBoxId(channelTicketTransferParam.getBoxId());
        channelTicketTransfer.setPkgId(channelTicketTransferParam.getPkgId());


        // 记录盲票转让信息
        this.save(channelTicketTransfer);

        // 更新盲票拥有人
        channelOrderDetailService.update(new LambdaUpdateWrapper<ChannelOrderDetail>()
                .set(ChannelOrderDetail::getChannelId, channelTicketTransferParam.getChannelId())
                .eq(ChannelOrderDetail::getPkgId, channelTicketTransferParam.getPkgId())
                .eq(ChannelOrderDetail::getChannelId, transferChannelId));

        return true;
    }

    @Override
    public List<ChannelMyTicketVO> listTransferTicketList(Long transferChannelId) {
        QueryWrapper<ChannelMyTicketVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t.transfer_channel_id", transferChannelId);
        queryWrapper.orderByDesc("t.created_time");
        return this.baseMapper.listTransferTicketList(queryWrapper);
    }

    @Override
    public ChannelTicketTransferDetailVO getTicketTransferDetailById(Long id, Long transferChannelId) {
        QueryWrapper<ChannelTicketTransferDetailVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t1.id", id);
        queryWrapper.eq("t1.transfer_channel_id", transferChannelId);
        return this.baseMapper.getTicketTransferDetail(queryWrapper);
    }
}
