package com.game.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.game.common.core.domain.R;
import com.game.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.enums.BusinessType;
import com.game.common.annotation.DataSource;
import com.game.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.game.business.domain.AppGame;
import com.game.business.service.IAppGameService;
import com.game.common.utils.poi.ExcelUtil;
import com.game.common.core.page.TableDataInfo;

/**
 * 游戏配置Controller
 * 
 * @author game
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/business/game")
@Api(value = "AppGameController", description = "游戏配置接口", tags = {"游戏配置"})
public class AppGameController extends BaseController
{
    @Autowired
    private IAppGameService appGameService;

    /**
     * 查询游戏配置列表
     */
    @PreAuthorize("@ss.hasPermi('business:game:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询游戏配置列表", notes = "获取游戏配置列表")
    public TableDataInfo list(AppGame appGame)
    {
        startPage();
        List<AppGame> list = appGameService.selectAppGameList(appGame);
        return getDataTable(list);
    }

    /**
     * 查询游戏配置列表
     */
//    @PreAuthorize("@ss.hasPermi('business:game:list')")
    @GetMapping("/allList")
    @ApiOperation(value = "查询游戏配置列表", notes = "获取游戏配置列表")
    public R<List<AppGame>> allList(AppGame appGame)
    {
//        startPage();
        appGame.setStatus(1L);
        List<AppGame> list = appGameService.selectAppGameList(appGame);
        return R.ok(list);
    }

    /**
     * 导出游戏配置列表
     */
    @PreAuthorize("@ss.hasPermi('business:game:export')")
    @Log(title = "游戏配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出游戏配置列表", notes = "导出游戏配置列表")
    public void export(HttpServletResponse response, AppGame appGame)
    {
        List<AppGame> list = appGameService.selectAppGameList(appGame);
        ExcelUtil<AppGame> util = new ExcelUtil<AppGame>(AppGame.class);
        util.exportExcel(response, list, "游戏配置数据");
    }

    /**
     * 获取游戏配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:game:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取游戏配置详细信息", notes = "获取游戏配置详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appGameService.selectAppGameById(id));
    }

    /**
     * 新增游戏配置
     */
    @PreAuthorize("@ss.hasPermi('business:game:add')")
    @Log(title = "游戏配置", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增游戏配置", notes = "新增游戏配置")
    @PostMapping
    public AjaxResult add(@RequestBody AppGame appGame)
    {
        return toAjax(appGameService.insertAppGame(appGame));
    }

    /**
     * 修改游戏配置
     */
    @PreAuthorize("@ss.hasPermi('business:game:edit')")
    @Log(title = "游戏配置", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改游戏配置", notes = "修改游戏配置")
    @PutMapping
    public AjaxResult edit(@RequestBody AppGame appGame)
    {
        return toAjax(appGameService.updateAppGame(appGame));
    }

    /**
     * 删除游戏配置
     */
    @PreAuthorize("@ss.hasPermi('business:game:remove')")
    @Log(title = "游戏配置", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除游戏配置", notes = "删除游戏配置")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appGameService.deleteAppGameByIds(ids));
    }


    @GetMapping("/gameTime")
    @ApiOperation(value = "记录游戏打开", notes = "记录游戏打开")
    public R gameTime(@RequestParam(name = "type") Integer type, // 类型 0：打开  1：关闭
                      @RequestParam(name = "osType") String osType, //系统类型 ios或安卓
                      @RequestParam(name = "gameId") String gameId,
                      @RequestParam(name = "gameName") String gameName,
                      @RequestParam(name = "date") String date)
    {
        if(null != type && type.intValue() == 1){
            logger.info("用户：{}，关闭游戏{}（{}），时间{}，系统：{}", SecurityUtils.getUserId(), gameId, gameName, date);
        }else{
            logger.info("用户：{}，打开游戏{}（{}），时间{},系统：{}",SecurityUtils.getUserId(), gameId, gameName, date);
        }

        return R.ok("记录成功","记录成功");
    }
}
