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.dto.OrderReportDTO;
import com.zanxiang.game.back.serve.pojo.vo.GameOceanengineOrderSplitLogVO;
import com.zanxiang.game.back.serve.pojo.vo.GameTencentMiniGameOrderVO;
import com.zanxiang.game.back.serve.pojo.vo.GameTencentMiniGameUserVO;
import com.zanxiang.game.back.serve.pojo.vo.GameTencentMiniOrderSplitLogVO;
import com.zanxiang.game.back.serve.service.IGameTencentMiniGameOrderService;
import com.zanxiang.game.back.serve.service.IGameTencentMiniGameOrderSplitLogService;
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.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
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.Collections;
import java.util.List;

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

    @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:orderLogs")
    @GetMapping("/orderSplitList/{orderId}")
    public ResultVO<List<GameTencentMiniOrderSplitLogVO>> orderSplitList(@PathVariable("orderId") String orderId) {
        return ResultVO.ok(gameTencentMiniGameOrderSplitLogService.listByOrderNo(Collections.singletonList(orderId)));
    }

    @PreAuthorize(permissionKey = "gameBack:tencentMiniGame:orderReport")
    @PostMapping("/orderReport/{ids}/{backMoney}")
    @ApiOperation(value = "腾讯订单手动上报")
    public ResultVO<Boolean> tencentOrderReport(@PathVariable("ids") List<Long> ids, @PathVariable("backMoney") BigDecimal backMoney) {
        OrderReportDTO dto = new OrderReportDTO();
        dto.setOrderIds(ids);
        dto.setSplitOrder(false);
        dto.setBackMoney(backMoney);
        return tencentOrderReport(dto);
    }

    @PreAuthorize(permissionKey = "gameBack:tencentMiniGame:orderReport")
    @PostMapping("/orderReport")
    @ApiOperation(value = "腾讯订单手动上报")
    public ResultVO<Boolean> tencentOrderReport(@Validated @RequestBody OrderReportDTO dto) {
        if (CollectionUtils.isEmpty(dto.getOrderIds())) {
            throw new BaseException("参数错误！");
        }
        if (dto.getSplitOrder()) {
            if (CollectionUtils.isEmpty(dto.getSplitMoney())) {
                throw new BaseException("回传金额错误！");
            }
            dto.getSplitMoney().forEach(backMoney -> {
                if (backMoney.compareTo(BigDecimal.ZERO) <= 0) {
                    throw new BaseException("回传金额错误！");
                }
            });
            if (dto.getBetweenMinuteMin() == null || dto.getBetweenMinuteMax() == null
                    || dto.getBetweenMinuteMin() < 1 || dto.getBetweenMinuteMax() < 1
                    || dto.getBetweenMinuteMin().compareTo(dto.getBetweenMinuteMax()) >= 0) {
                throw new BaseException("回传间隔时间错误！");
            }
        } else {
            if (dto.getBackMoney().compareTo(BigDecimal.ZERO) <= 0) {
                throw new BaseException("回传金额错误！");
            }
        }
        return ResultVO.ok(gameTencentMiniGameOrderService.doReport(dto));
    }

    @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));
    }
}
