package com.zanxiang.game.module.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zanxiang.erp.security.annotation.PreAuthorize;
import com.zanxiang.game.module.manage.enums.GameServerExcelEnum;
import com.zanxiang.game.module.manage.pojo.params.GameMergeServerParam;
import com.zanxiang.game.module.manage.pojo.params.GameServerAddUpdateParam;
import com.zanxiang.game.module.manage.pojo.params.GameServerAssignParam;
import com.zanxiang.game.module.manage.pojo.params.GameServerListParam;
import com.zanxiang.game.module.manage.pojo.vo.GameServerListVO;
import com.zanxiang.game.module.manage.pojo.vo.GameServerVO;
import com.zanxiang.game.module.manage.pojo.vo.GameSupperVO;
import com.zanxiang.game.module.manage.service.IGameServerService;
import com.zanxiang.game.module.manage.service.IGameSupperService;
import com.zanxiang.module.util.pojo.ResultVO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author : lingfeng
 * @time : 2023-08-07
 * @description : 游戏区服接口
 */
@RestController
@RequestMapping("/gameServer")
@Slf4j
public class GameServerController {

    @Autowired
    private IGameServerService gameServerService;

    @Autowired
    private IGameSupperService gameSupperService;

    @PostMapping(value = "/assign")
    @PreAuthorize(permissionKey = "manage:gameServer:assign")
    public ResultVO<Boolean> gameServerAssign(@Validated @RequestBody GameServerAssignParam param) {
        return ResultVO.ok(gameServerService.gameServerAssignBatch(param));
    }

    @PostMapping(value = "/add/excel")
    @PreAuthorize(permissionKey = "manage:gameServer:addExcel")
    public ResultVO<Boolean> addOrUpdate(MultipartFile multipartFile, @RequestParam Long gameId,
                                         @RequestParam GameServerExcelEnum gameServerExcelEnum) {
        return ResultVO.ok(gameServerService.gameServerExcelImport(multipartFile, gameServerExcelEnum, gameId));
    }

    @PostMapping(value = "/add/or/update")
    @PreAuthorize(permissionKey = "manage:gameServer:addOrUpdate")
    public ResultVO<Boolean> addOrUpdate(@Validated @RequestBody GameServerAddUpdateParam param) {
        return ResultVO.ok(gameServerService.addOrUpdate(param));
    }

    @PostMapping(value = "/list")
    @PreAuthorize(permissionKey = "manage:gameServer:list")
    public ResultVO<IPage<GameServerListVO>> listOfPage(@Validated @RequestBody GameServerListParam param) {
        return ResultVO.ok(gameServerService.listOfPage(param));
    }

    @PostMapping(value = "/list/excel")
    @PreAuthorize(permissionKey = "manage:gameServer:gameServerExcel")
    public void getOrderExcel(@Validated @RequestBody GameServerListParam param, HttpServletResponse response) {
        gameServerService.getGameServerExcel(param, response);
    }

    @DeleteMapping(value = "/delete")
    @PreAuthorize(permissionKey = "manage:gameServer:delete")
    public ResultVO<Boolean> deleteById(@RequestParam Long id) {
        return ResultVO.ok(gameServerService.deleteById(id));
    }

    @PostMapping(value = "/merge/add/or/update")
    @PreAuthorize(permissionKey = "manage:gameServer:mergeAddOrUpdate")
    public ResultVO<Boolean> mergeServerAddUpdate(@Validated @RequestBody GameMergeServerParam param) {
        return ResultVO.ok(gameServerService.mergeServerAddUpdate(param));
    }

    @GetMapping(value = "/game/supper/list")
    @PreAuthorize(permissionKey = "manage:gameServer:gameSupperList")
    public ResultVO<List<GameSupperVO>> getGameSupperList() {
        return ResultVO.ok(gameSupperService.getGameSupperList());
    }

    @GetMapping(value = "/unMerge/server/list")
    @PreAuthorize(permissionKey = "manage:gameServer:unMergeServer")
    public ResultVO<List<GameServerVO>> unMergeServerList(@RequestParam Long gameId) {
        return ResultVO.ok(gameServerService.getServerList(gameId, Boolean.FALSE));
    }

    @GetMapping(value = "/merge/server/list")
    @PreAuthorize(permissionKey = "manage:gameServer:mergeServerList")
    public ResultVO<List<GameServerVO>> mergeServerList(@RequestParam Long gameId) {
        return ResultVO.ok(gameServerService.getServerList(gameId, Boolean.TRUE));
    }

    @GetMapping(value = "/all/server/list")
    @PreAuthorize(permissionKey = "manage:gameServer:allServer")
    public ResultVO<List<GameServerVO>> allServerList(@RequestParam Long gameId) {
        return ResultVO.ok(gameServerService.getServerList(gameId, null));
    }
}
