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.PlatChannelBo;
import org.dromara.game.domain.vo.PlatChannelVo;
import org.dromara.game.service.IPlatChannelService;
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/channel")
public class PlatChannelController extends BaseController {

    private final IPlatChannelService platChannelService;

    /**
     * 查询渠道列表
     */
    @SaCheckPermission("game:channel:list")
    @GetMapping("/list")
    public TableDataInfo<PlatChannelVo> list(PlatChannelBo bo, PageQuery pageQuery) {
        return platChannelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出渠道列表
     */
    @SaCheckPermission("game:channel:export")
    @Log(title = "渠道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PlatChannelBo bo, HttpServletResponse response) {
        List<PlatChannelVo> list = platChannelService.queryList(bo);
        ExcelUtil.exportExcel(list, "渠道", PlatChannelVo.class, response);
    }

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

    /**
     * 新增渠道
     */
    @SaCheckPermission("game:channel:add")
    @Log(title = "渠道", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PlatChannelBo bo) {
        return toAjax(platChannelService.insertByBo(bo));
    }

    /**
     * 修改渠道
     */
    @SaCheckPermission("game:channel:edit")
    @Log(title = "渠道", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PlatChannelBo bo) {
        return toAjax(platChannelService.updateByBo(bo));
    }

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