package com.qs.mp.web.controller.api.channel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.mp.admin.domain.TicketPackage;
import com.qs.mp.admin.service.ITicketPackageService;
import com.qs.mp.channel.domain.Channel;
import com.qs.mp.channel.domain.ChannelOrderDetail;
import com.qs.mp.channel.domain.ChannelTicketTransfer;
import com.qs.mp.channel.domain.param.ChannelMyTicketQueryParam;
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.service.IChannelOrderDetailService;
import com.qs.mp.channel.service.IChannelService;
import com.qs.mp.channel.service.IChannelTicketTransferService;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.common.enums.ChannelCertifyStatusEnum;
import com.qs.mp.common.enums.TicketPkgSaleStatusEnum;
import com.qs.mp.common.utils.StringUtils;
import com.qs.mp.utils.SecurityUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 我的盲票相关接口
 * @author Cup
 * @date 2022/4/21
 */
@RestController
@RequestMapping("/api/v1/mp/channel/my/ticket")
@Api(tags = "我的盲票相关接口")
public class ChannelMyTicketController extends BaseApiController {

    @Autowired
    private IChannelOrderDetailService channelOrderDetailService;

    @Autowired
    private IChannelService channelService;

    @Autowired
    private IChannelTicketTransferService channelTicketTransferService;

    @Autowired
    private ITicketPackageService ticketPackageService;

    @PostMapping("list")
    @ApiOperation("我的盲票列表")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = ChannelMyTicketVO.class)
    )
    public TableDataInfo list(@RequestBody ChannelMyTicketQueryParam channelMyTicketQueryParam) {
        Long channelId = SecurityUtils.getLoginUser().getChannelId();
        if (channelId == null) {
            throw new RuntimeException("非法调用");
        }

        startPage();
        // 获取我的盲票列表
        List<ChannelMyTicketVO> list =  channelOrderDetailService.listMyTicket(channelId, channelMyTicketQueryParam);
        return getDataTable(list);
    }

    @PostMapping("/channel/mobile/{mobile}")
    @ApiOperation("根据手机号获取经销商信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String", paramType = "path")
    )
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = Channel.class)
    )
    public AjaxResult channelInfoByMobile(@PathVariable("mobile") String mobile) {
        Long channelId = SecurityUtils.getLoginUser().getChannelId();
        if (channelId == null) {
            return AjaxResult.error("非法调用");
        }
        if (StringUtils.isBlank(mobile)) {
            return AjaxResult.success();
        }
        Channel channel = channelService.getOne(new LambdaQueryWrapper<Channel>()
                .eq(Channel::getMobile, mobile)
                .eq(Channel::getLevel, 0));

        return AjaxResult.success(channel);
    }




    @PostMapping("/transfer/save")
    @ApiOperation("盲票转让")
    public AjaxResult transfer(@RequestBody ChannelTicketTransferParam channelTicketTransferParam) {
        // 自己为转让人
        Long transferChannelId = SecurityUtils.getLoginUser().getChannelId();
        if (transferChannelId == null) {
            return AjaxResult.error("非法调用");
        }
        if (StringUtils.isBlank(channelTicketTransferParam.getBoxId()) ||
                StringUtils.isBlank(channelTicketTransferParam.getPkgId()) ||
                Objects.isNull(channelTicketTransferParam.getChannelId()) ||
                StringUtils.isBlank(channelTicketTransferParam.getMobile())) {
            return AjaxResult.error("转让参数不能为空");
        }

        // 校验受让人信息
        Channel channel = channelService.getOne(new LambdaQueryWrapper<Channel>()
                .eq(Channel::getChannelId, channelTicketTransferParam.getChannelId())
                .eq(Channel::getLevel, 0));
        if (Objects.isNull(channel)) {
            return AjaxResult.error("受让人信息不存在");
        }
        if (ChannelCertifyStatusEnum.NOT_CERTIFIED.equals(channel.getCertifyStatus())) {
            return AjaxResult.error("受让门店未认证");
        }
        if (transferChannelId.equals(channel.getChannelId())) {
            return AjaxResult.error("不能转让给自己");
        }

        // 校验当前操作的盲票是否为自己的盲票
        int count = channelOrderDetailService.count(new LambdaQueryWrapper<ChannelOrderDetail>()
                .eq(ChannelOrderDetail::getChannelId, transferChannelId)
                .eq(ChannelOrderDetail::getPkgId, channelTicketTransferParam.getPkgId()));
        if (count == 0) {
            return AjaxResult.error("非法操作他人盲票");
        }
        // 校验票包是否为待售状态
        count = ticketPackageService.count(new LambdaQueryWrapper<TicketPackage>().eq(TicketPackage::getPkgId, channelTicketTransferParam.getPkgId())
                .eq(TicketPackage::getSaleStatus, TicketPkgSaleStatusEnum.WAIT_SALE));
        if (count == 0) {
            return AjaxResult.error("票包不为待售状态");
        }

        // 转让盲票
        boolean flag  = channelTicketTransferService.transferTicket(transferChannelId, channelTicketTransferParam);

        return AjaxResult.success(flag);

    }



    @PostMapping("/transfer/list")
    @ApiOperation("已转让盲票列表")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = ChannelMyTicketVO.class)
    )
    public TableDataInfo transferList(){
        Long transferChannelId = SecurityUtils.getLoginUser().getChannelId();
        if (transferChannelId == null) {
            throw new RuntimeException("非法调用");
        }

        // 开启分页
        startPage();

        // 获取我的已转让的盲票列表
        List<ChannelMyTicketVO> list = channelTicketTransferService.listTransferTicketList(transferChannelId);
        return getDataTable(list);
    }


    @PostMapping("/transfer/detail/{id}")
    @ApiOperation("转让盲票详情")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "转让详情id", required = true, dataType = "Long", paramType = "query")
    )
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = ChannelTicketTransferDetailVO.class)
    )
    public AjaxResult detail(@PathVariable("id") Long id) {
        Long transferChannelId = SecurityUtils.getLoginUser().getChannelId();

        if (transferChannelId == null) {
            return AjaxResult.error("非法调用");
        }

        ChannelTicketTransferDetailVO channelTicketTransferDetailVO =  channelTicketTransferService.getTicketTransferDetailById(id, transferChannelId);

        return AjaxResult.success(channelTicketTransferDetailVO);
    }

}
