package com.zanxiang.game.back.serve.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zanxiang.erp.security.annotation.PreAuthorize;
import com.zanxiang.game.back.serve.pojo.dto.GameTencentMiniGameOrderDTO;
import com.zanxiang.game.back.serve.pojo.dto.GameTencentMiniGameUserDTO;
import com.zanxiang.game.back.serve.pojo.vo.GameTencentMiniGameOrderVO;
import com.zanxiang.game.back.serve.pojo.vo.GameTencentMiniGameUserVO;
import com.zanxiang.game.back.serve.service.IGameTencentMiniGameOrderService;
import com.zanxiang.game.back.serve.service.IGameTencentMiniGameUserService;
import com.zanxiang.module.util.NumberUtil;
import com.zanxiang.module.util.exception.BaseException;
import com.zanxiang.module.util.pojo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/tencentMiniGame")
@Api("腾讯小游戏回传")
public class TencentMiniGameLogController {
    @Autowired
    private IGameTencentMiniGameUserService gameTencentMiniGameUserService;
    @Autowired
    private IGameTencentMiniGameOrderService gameTencentMiniGameOrderService;

    @PreAuthorize(permissionKey = "gameBack:tencentMiniGame:orderLogs")
    @PostMapping("/orderLogs")
    @ApiOperation(value = "腾讯订单回传日志列表")
    public ResultVO<IPage<GameTencentMiniGameOrderVO>> tencentOrderLogList(@RequestBody GameTencentMiniGameOrderDTO dto) {
        return ResultVO.ok(gameTencentMiniGameOrderService.listOfPage(dto));
    }

    @PreAuthorize(permissionKey = "gameBack:tencentMiniGame:orderReport")
    @PostMapping("/orderReport/{ids}/{backMoney}")
    @ApiOperation(value = "腾讯订单手动上报")
    public ResultVO<Boolean> tencentOrderReport(@PathVariable("ids") List<Long> ids, @PathVariable("backMoney") BigDecimal backMoney) {
        if (backMoney.compareTo(BigDecimal.ZERO) <= 0) {
            throw new BaseException("回传金额错误！");
        }
        return ResultVO.ok(gameTencentMiniGameOrderService.doReport(ids, NumberUtil.multiply100(backMoney).longValue()));
    }

    @PreAuthorize(permissionKey = "gameBack:tencentMiniGame:userLogs")
    @PostMapping("/userLogs")
    @ApiOperation(value = "腾讯用户回传日志列表")
    public ResultVO<IPage<GameTencentMiniGameUserVO>> tencentUserLogList(@RequestBody GameTencentMiniGameUserDTO dto) {
        return ResultVO.ok(gameTencentMiniGameUserService.listOfPage(dto));
    }

    @PreAuthorize(permissionKey = "gameBack:tencentMiniGame:userReport")
    @PostMapping("/userReport/{ids}")
    @ApiOperation(value = "腾讯用户手动上报")
    public ResultVO<Boolean> tencentUserReport(@PathVariable("ids") List<Long> ids) {
        return ResultVO.ok(gameTencentMiniGameUserService.doReport(ids));
    }
}
