package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.vo.AthleteScoreVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.GameAthleteVo;
import org.dromara.system.domain.bo.GameAthleteBo;
import org.dromara.system.service.IGameAthleteService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 参赛队员
 *
 * @author zlt
 * @date 2025-07-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/gameAthlete")
public class GameAthleteController extends BaseController {

    private final IGameAthleteService gameAthleteService;

    /**
     * 查询参赛队员列表
     */
    @SaCheckPermission("system:gameAthlete:list")
    @GetMapping("/list")
    public TableDataInfo<GameAthleteVo> list(GameAthleteBo bo, PageQuery pageQuery) {
        return gameAthleteService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出参赛队员列表
     */
    @SaCheckPermission("system:gameAthlete:export")
    @Log(title = "参赛队员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GameAthleteBo bo, HttpServletResponse response) {
        List<GameAthleteVo> list = gameAthleteService.queryList(bo);
        ExcelUtil.exportExcel(list, "参赛队员", GameAthleteVo.class, response);
    }

    /**
     * 获取参赛队员详细信息
     *
     * @param athleteId 主键
     */
    @SaCheckPermission("system:gameAthlete:query")
    @GetMapping("/{athleteId}")
    public R<GameAthleteVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long athleteId) {
        return R.ok(gameAthleteService.queryById(athleteId));
    }

    /**
     * 根据赛事ID获取参赛者积分信息列表
     *
     * @param eventId 赛事ID
     */
    @SaCheckPermission("system:gameAthlete:list")
    @GetMapping("/listByEventId")
    public R<List<AthleteScoreVo>> getAthleteScoreList(@NotNull(message = "赛事ID不能为空") @RequestParam Long eventId){
        List<AthleteScoreVo> list = gameAthleteService.queryAthleteScoreList(eventId);
        return R.ok(list);
    }

    /**
     * 新增参赛队员
     */
    @SaCheckPermission("system:gameAthlete:add")
    @Log(title = "参赛队员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GameAthleteBo bo) {
        return toAjax(gameAthleteService.insertByBo(bo));
    }

    /**
     * 修改参赛队员
     */
    @SaCheckPermission("system:gameAthlete:edit")
    @Log(title = "参赛队员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GameAthleteBo bo) {
        return toAjax(gameAthleteService.updateByBo(bo));
    }

    /**
     * 删除参赛队员
     *
     * @param athleteIds 主键串
     */
    @SaCheckPermission("system:gameAthlete:remove")
    @Log(title = "参赛队员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{athleteIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] athleteIds) {
        return toAjax(gameAthleteService.deleteWithValidByIds(List.of(athleteIds), true));
    }
}
