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.PlatProductCategoryBo;
import org.dromara.game.domain.vo.PlatProductCategoryVo;
import org.dromara.game.service.IPlatProductCategoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品分类
 *
 * @author Lion Li
 * @date 2024-08-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/game/productCategory")
public class PlatProductCategoryController extends BaseController {

    private final IPlatProductCategoryService platProductCategoryService;

    /**
     * 查询产品分类列表
     */
    @SaCheckPermission("game:productCategory:list")
    @GetMapping("/list")
    public TableDataInfo<PlatProductCategoryVo> list(PlatProductCategoryBo bo, PageQuery pageQuery) {
        return platProductCategoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询产品分类列表
     */
    @SaCheckPermission("game:productCategory:list")
    @GetMapping("/all")
    public List<PlatProductCategoryVo> all() {
        PlatProductCategoryBo bo = new PlatProductCategoryBo();
        return platProductCategoryService.queryList(bo);
    }

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

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

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

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

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