package com.ruoyi.xq.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserVip;
import com.ruoyi.xq.dto.admin.user.UserVipAdminVo;
import com.ruoyi.xq.dto.admin.user.req.AddVipReq;
import com.ruoyi.xq.service.UserService;
import com.ruoyi.xq.service.UserVipService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * VIP用户
 *
 * @author 77
 * @date 2024-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xq/userVip")
public class UserVipController extends BaseController {

    private final UserVipService userVipService;
    private final UserService userService;

    /**
     * 查询VIP用户列表
     */
    @SaCheckPermission("xq:userVip:list")
    @GetMapping("/list")
    public TableDataInfo<UserVipAdminVo> list(UserVipAdminVo bo, PageQuery pageQuery) {
        Page<UserVipAdminVo> page = userVipService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    /**
     * 获取VIP用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xq:userVip:query")
    @GetMapping("/{id}")
    public R<UserVip> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userVipService.getById(id));
    }

    @SaCheckPermission("xq:userVip:add")
    @Log(title = "VIP用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addVip")
    public R<Void> addVip(@RequestBody AddVipReq bo) {
        User user = userService.getByUsercode(bo.getUsercode());
        if(user == null){
            return R.fail("用户不存在!");
        }
        userVipService.updateVipOrderAdmin(bo,user);
        return R.ok();
    }

    /**
     * 删除VIP用户
     *
     */
    @SaCheckPermission("xq:userVip:remove")
    @Log(title = "VIP用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        userVipService.removeVip(id);
        return R.ok();
    }
}
