package org.dromara.system.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.system.domain.constant.GameEventConstant;
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.GameEventVo;
import org.dromara.system.domain.bo.GameEventBo;
import org.dromara.system.service.IGameEventService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 赛事基本信息
 *
 * @author zlt
 * @date 2025-07-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/gameEvent")
public class GameEventController extends BaseController {

    private final IGameEventService gameEventService;

    /**
     * 查询赛事基本信息列表
     */
    @SaCheckPermission("system:gameEvent:list")
    @GetMapping("/list")
    public TableDataInfo<GameEventVo> list(GameEventBo bo, PageQuery pageQuery) {
        return gameEventService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出赛事基本信息列表
     */
    @SaCheckPermission("system:gameEvent:export")
    @Log(title = "赛事基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GameEventBo bo, HttpServletResponse response) {
        List<GameEventVo> list = gameEventService.queryList(bo);
        ExcelUtil.exportExcel(list, "赛事基本信息", GameEventVo.class, response);
    }

    /**
     * 获取赛事基本信息详细信息
     *
     * @param eventId 主键
     */
    @SaCheckPermission("system:gameEvent:query")
    @GetMapping("/{eventId}")
    public R<GameEventVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long eventId) {
        return R.ok(gameEventService.queryById(eventId));
    }

    /**
     * 新增赛事基本信息
     */
    @SaCheckPermission("system:gameEvent:add")
    @Log(title = "赛事基本信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody GameEventBo bo) {
        Long eventId = gameEventService.insertByBo(bo);
        return eventId != null && eventId > 0 ? R.ok(eventId) : R.fail("新增赛事失败");
    }

    /**
     * 修改赛事基本信息
     */
    @SaCheckPermission("system:gameEvent:edit")
    @Log(title = "赛事基本信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GameEventBo bo) {
        return toAjax(gameEventService.updateByBo(bo));
    }

    /**
     * 删除赛事基本信息
     *
     * @param eventIds 主键串
     */
    @SaCheckPermission("system:gameEvent:remove")
    @Log(title = "赛事基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{eventIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] eventIds) {
        return toAjax(gameEventService.deleteWithValidByIds(List.of(eventIds), true));
    }

    /**
     * 查询赛事名称和id的map
     */
    @GetMapping("/map")
    public R<Map<String, Long>> getEventIdNameMap() {
        return R.ok(gameEventService.getEventIdNameMap());
    }

    /**
     * 获取默认赛事信息
     */
    @GetMapping("/default")
    public R<GameEventVo> getDefaultEvent() {
        return R.ok(gameEventService.getDefaultEvent());
    }

    /**
     * 修改赛事默认状态
     */
    @SaCheckPermission("system:gameEvent:edit")
    @Log(title = "赛事默认状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeEventDefault")
    public R<Void> changeEventDefault(@RequestBody GameEventBo bo) {
        // 如果修改的对象 原本是默认并且准备取消则禁止
        Object cacheObject = RedisUtils.getCacheObject(GameEventConstant.DEFAULT_EVENT_ID);
        Long defaultEventId = Long.valueOf(cacheObject.toString());
        if (defaultEventId.equals(bo.getEventId()) && bo.getIsDefault().equals("1")) {
            throw new ServiceException("默认赛事不能取消");
        }
        int result = gameEventService.updateEventDefault(bo);
        return toAjax(result);
    }

    /**
     * 查询赛事数量
     *
     * @param type 0:所有 1:未开始 2:进行中 3:已结束
     * @return
     */
    @GetMapping("/count/{type}")
    public R<Long> count(@PathVariable Long type) {
        return R.ok(gameEventService.countGameEvent(type));
    }
}
