package com.kxs.product.biz.controller.kxsapp;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxs.common.core.util.R;
import com.kxs.common.security.annotation.Inner;
import com.kxs.product.api.dto.kxsapp.ticket.TicketTransferDTO;
import com.kxs.product.api.dto.kxsapp.ticket.TicketTransferQuery;
import com.kxs.product.api.model.KxsTicket;
import com.kxs.product.api.vo.kxsapp.ticket.TicketListVO;
import com.kxs.product.api.vo.kxsapp.ticket.TicketTransferListVO;
import com.kxs.product.biz.service.KxsTicketService;
import com.kxs.product.biz.service.KxsTicketTransferService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

/**
 * 兑换券(Ticket)控制层
 *
 * @author 系统
 * @since 2024-01-04 16:20:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("ticket")
public class TicketController {

    /**
     * 服务对象
     */
    private final KxsTicketService kxsTicketService;
    private final KxsTicketTransferService kxsTicketTransferService;


    /**
     * 检查用户是否满足预设职级条件
     *
     * @param userId 用户 ID
     * @param level  职级
     * @return {@link R}
     */
    @GetMapping("checkUserPreLevelTicket")
    public R checkUserPreLevelTicket(@RequestParam("userId") Long userId, @RequestParam("level") Long level){

        return R.ok(kxsTicketService.checkUserPreLevelTicket(userId, level));
    }

    /**
     * 校验用户名下劵大于等于3或下单过指定商品
     */
    @Inner
    @GetMapping("/verifyUserTicketCountAndShopAndAct")
    public Boolean verifyUserTicketCountAndShopAndAct(@RequestParam("userId") Long userId) {

        return kxsTicketService.verifyUserTicketCountAndShopAndAct(userId);
    }

    /**
     * 兑换券列表
     *
     * @param page 分页对象
     * @return 兑换券列表
     */
    @GetMapping("/list")
    public R list(@ParameterObject Page<TicketListVO> page, @RequestParam("ticketType") Integer ticketType) {
        return R.ok(kxsTicketService.getByPage(page, ticketType));
    }


    /**
     * 兑换券划拨记录
     *
     * @param page 分页对象
     * @return 兑换券划拨记录
     */
    @GetMapping("/ticketTransferList")
    public R ticketTransferList(@ParameterObject Page<TicketTransferListVO> page, @ParameterObject TicketTransferQuery ticketTransferQuery) {

        return R.ok(kxsTicketTransferService.getByPage(page, ticketTransferQuery));
    }


    /**
     * 兑换券划拨详情
     *
     * @param id 主键ID
     * @return 兑换券划拨详情
     */
    @GetMapping("/ticketTransferDetail")
    public R ticketTransferDetail(@RequestParam("id") Integer id) {

        return R.ok(kxsTicketTransferService.getByData(id));
    }


    /**
     * 兑换券划拨
     *
     * @param param 实体对象
     * @return 兑换券划拨
     */
    @PostMapping("/ticketTransfer")
    public R ticketTransfer(@Valid @RequestBody TicketTransferDTO param) {

        return kxsTicketTransferService.ticketTransfer(param);
    }


    /**
     * 通过sn查询券码信息
     *
     * @param sn 券码
     * @return 兑换券划拨详情
     */
    @Inner
    @GetMapping("/loadTicketBySn")
    public R loadTicketBySn(@RequestParam("sn") Integer sn) {

        return R.ok(kxsTicketService.getOne(Wrappers.<KxsTicket>lambdaQuery().eq(KxsTicket::getTicketCode, sn)));
    }


    /**
     * 更新券
     *
     * @param ticket 券码
     * @return {@link R}<{@link KxsTicket}>
     */
    @Inner
    @PostMapping("/updateByTicket")
    public R updateByTicket(@RequestBody KxsTicket ticket) {

        return R.ok(kxsTicketService.updateById(ticket));
    }




}

