package org.dromara.game.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.game.domain.bo.PlatProductBo;
import org.dromara.game.domain.vo.PlatProductVo;
import org.dromara.game.service.IPlatProductService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品
 *
 * @author Hixon
 * @date 2024-08-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/game/product")
public class PlatProductController extends BaseController {

    private final IPlatProductService platProductService;

    /**
     * 查询产品列表
     */
    @SaCheckPermission("game:product:list")
    @GetMapping("/list")
    public TableDataInfo<PlatProductVo> list(PlatProductBo bo, PageQuery pageQuery) {
        return platProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询产品列表
     */
    @SaCheckPermission("game:product:list")
    @GetMapping("/all")
    public List<PlatProductVo> all() {
        PlatProductBo bo = new PlatProductBo();
        return platProductService.queryList(bo);
    }

    /**
     * 导出产品列表
     */
    @SaCheckPermission("game:product:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PlatProductBo bo, HttpServletResponse response) {
        List<PlatProductVo> list = platProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品", PlatProductVo.class, response);
    }

    /**
     * 获取产品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("game:product:query")
    @GetMapping("/{id}")
    public R<PlatProductVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(platProductService.queryById(id));
    }

    /**
     * 新增产品
     */
    @SaCheckPermission("game:product:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PlatProductBo bo) {
        return toAjax(platProductService.insertByBo(bo));
    }

    /**
     * 修改产品
     */
    @SaCheckPermission("game:product:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PlatProductBo bo) {
        return toAjax(platProductService.updateByBo(bo));
    }

    /**
     * 删除产品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("game:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(platProductService.deleteWithValidByIds(List.of(ids), true));
    }
}
