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.pojo.params.*;
import com.zanxiang.game.module.manage.pojo.vo.*;
import com.zanxiang.game.module.manage.service.*;
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 java.util.List;

/**
 * @author : lingfeng
 * @time : 2022-06-29
 * @description : 玩家管理接口
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IIpDataAssayService ipDataAssayService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserAddressService userAddressService;

    @Autowired
    private IUserLoginLogService userLoginLogService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IGameUserService gameUserService;

    @Autowired
    private IGameUserRoleService gameUserRoleService;

    @Autowired
    private IUserCardService userCardService;

    @PostMapping(value = "/list")
    @PreAuthorize(permissionKey = "manage:user:list")
    public ResultVO<IPage<UserListVO>> list(@Validated @RequestBody UserListParam param) {
        return ResultVO.ok(userService.list(param));
    }

    @PostMapping(value = "/update")
    @PreAuthorize(permissionKey = "manage:user:update")
    public ResultVO<Boolean> userUpdate(@Validated @RequestBody UserUpdateParam param) {
        return ResultVO.ok(userService.userUpdate(param));
    }

    @GetMapping(value = "/info")
    @PreAuthorize(permissionKey = "manage:user:info")
    public ResultVO<UserVO> getUserInfo(@RequestParam Long id) {
        return ResultVO.ok(userService.getUserInfo(id));
    }

    @PostMapping(value = "/login/list")
    @PreAuthorize(permissionKey = "manage:user:loginList")
    public ResultVO<IPage<UserRoleLoginVO>> loginList(@RequestBody UserLoginLogParam param) {
        return ResultVO.ok(userLoginLogService.loginList(param));
    }

    @PostMapping(value = "/order/list")
    @PreAuthorize(permissionKey = "manage:user:orderList")
    public ResultVO<IPage<UserOrderListVO>> orderList(@Validated @RequestBody UserOrderListParam param) {
        return ResultVO.ok(orderService.orderList(param));
    }

    @GetMapping(value = "/address/list")
    @PreAuthorize(permissionKey = "manage:user:addressList")
    public ResultVO<List<UserAddressVO>> list(@RequestParam Long userId) {
        return ResultVO.ok(userAddressService.list(userId));
    }

    @PostMapping(value = "/game/list")
    @PreAuthorize(permissionKey = "manage:user:gameList")
    public ResultVO<IPage<GameUserListVO>> gameUserList(@Validated @RequestBody GameUserListParam param) {
        return ResultVO.ok(gameUserService.gameUserList(param));
    }

    @GetMapping(value = "/game/role/list")
    @PreAuthorize(permissionKey = "manage:user:gameRoleList")
    public ResultVO<List<GameUserRoleVO>> roleList(@RequestParam Long userId) {
        return ResultVO.ok(gameUserRoleService.roleList(userId));
    }

    @PostMapping(value = "/role/list")
    @PreAuthorize(permissionKey = "manage:user:roleList")
    public ResultVO<IPage<GameUserRoleListVO>> roleList(@Validated @RequestBody GameUserRoleListParam param) {
        return ResultVO.ok(gameUserRoleService.list(param));
    }

    @PostMapping(value = "/role/list/excel")
    @PreAuthorize(permissionKey = "manage:user:roleListExcel")
    public void getGameUserRoleExcel(@Validated @RequestBody GameUserRoleListParam param, HttpServletResponse response) {
        gameUserRoleService.getGameUserRoleExcel(param, response);
    }

    @PostMapping(value = "/card/list")
    @PreAuthorize(permissionKey = "manage:userCard:list")
    public ResultVO<IPage<UserCardVO>> authList(@Validated @RequestBody UserNameAuthListParam param) {
        return ResultVO.ok(userCardService.getUserCardList(param));
    }

    @GetMapping(value = "/weChat/info")
    @PreAuthorize(permissionKey = "manage:user:weChatInfo")
    public ResultVO<UserWeChatVO> getUserWeChat(@RequestParam Long userId) {
        return ResultVO.ok(userService.getUserWeChat(userId));
    }

    @PatchMapping(value = "/applet/to/h5")
    @PreAuthorize(permissionKey = "manage:user:appletToH5")
    public ResultVO<Boolean> appletToH5(@RequestParam Long userId, @RequestParam String mobile, @RequestParam Integer type) {
        return ResultVO.ok(userService.appletToH5(userId, mobile, type));
    }

    @PatchMapping(value = "/ip/assay")
    @PreAuthorize(permissionKey = "manage:user:ipAssay")
    public ResultVO<Void> userIpAssay(@RequestParam Long supperGameId) {
        ipDataAssayService.userHandle(supperGameId);
        return ResultVO.ok();
    }

    @PatchMapping(value = "/ip/assay/by/ids")
    @PreAuthorize(permissionKey = "manage:user:ipAssayByIds")
    public ResultVO<Void> userIpAssayById(@RequestParam String ids) {
        ipDataAssayService.userHandleByIds(ids);
        return ResultVO.ok();
    }

    @PatchMapping(value = "/login/log/ip/assay")
    @PreAuthorize(permissionKey = "manage:user:loginLogIpAssay")
    public ResultVO<Void> userLoginLogIpAssay() {
        ipDataAssayService.userLoginLogHandle();
        return ResultVO.ok();
    }

    @PatchMapping(value = "/login/log/ip/assay/by/ids")
    @PreAuthorize(permissionKey = "manage:user:loginLogIpAssayByIds")
    public ResultVO<Void> userLoginLogHandleByIds(@RequestParam String ids) {
        ipDataAssayService.userLoginLogHandleByIds(ids);
        return ResultVO.ok();
    }
}
