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.GameOceanengineOrderLogDTO;
import com.zanxiang.game.back.serve.pojo.dto.GameOceanengineUserLogDTO;
import com.zanxiang.game.back.serve.pojo.dto.OrderReportDTO;
import com.zanxiang.game.back.serve.pojo.vo.GameOceanengineOrderLogVO;
import com.zanxiang.game.back.serve.pojo.vo.GameOceanengineOrderSplitLogVO;
import com.zanxiang.game.back.serve.pojo.vo.GameOceanengineUserLogVO;
import com.zanxiang.game.back.serve.service.IGameOceanengineOrderLogService;
import com.zanxiang.game.back.serve.service.IGameOceanengineOrderSplitLogService;
import com.zanxiang.game.back.serve.service.IGameOceanengineUserLogService;
import com.zanxiang.module.util.exception.BaseException;
import com.zanxiang.module.util.pojo.ResultVO;
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.*;

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

@RestController
@RequestMapping("/oceanengine")
public class OceanengineLogController {
    @Autowired
    private IGameOceanengineOrderLogService oceanengineOrderLogService;
    @Autowired
    private IGameOceanengineUserLogService oceanengineUserLogService;
    @Autowired
    private IGameOceanengineOrderSplitLogService gameOceanengineOrderSplitLogService;

    @PreAuthorize(permissionKey = "gameBack:oceanengine:orderLogs")
    @PostMapping("/orderLogs")
    public ResultVO<IPage<GameOceanengineOrderLogVO>> oceanengineOrderLogList(@RequestBody GameOceanengineOrderLogDTO dto) {
        return ResultVO.ok(oceanengineOrderLogService.oceanengineOrderLogList(dto));
    }

    @PreAuthorize(permissionKey = "gameBack:oceanengine:orderLogs")
    @GetMapping("/orderSplitList/{orderId}")
    public ResultVO<List<GameOceanengineOrderSplitLogVO>> orderSplitList(@PathVariable("orderId") String orderId) {
        return ResultVO.ok(gameOceanengineOrderSplitLogService.listByOrderNo(Collections.singletonList(orderId)));
    }

    @PreAuthorize(permissionKey = "gameBack:oceanengine:orderReport")
    @PostMapping("/orderReport/{ids}/{backMoney}")
    public ResultVO<Boolean> oceanengineOrderReport(@PathVariable("ids") List<Long> ids, @PathVariable("backMoney") BigDecimal backMoney) {
        OrderReportDTO dto = new OrderReportDTO();
        dto.setOrderIds(ids);
        dto.setSplitOrder(false);
        dto.setBackMoney(backMoney);
        return oceanengineOrderReport(dto);
    }

    @PreAuthorize(permissionKey = "gameBack:oceanengine:orderReport")
    @PostMapping("/orderReport")
    public ResultVO<Boolean> oceanengineOrderReport(@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(oceanengineOrderLogService.oceanengineOrderReport(dto));
    }

    @PreAuthorize(permissionKey = "gameBack:oceanengine:userLogs")
    @PostMapping("/userLogs")
    public ResultVO<IPage<GameOceanengineUserLogVO>> oceanengineUserLogList(@RequestBody GameOceanengineUserLogDTO dto) {
        return ResultVO.ok(oceanengineUserLogService.oceanengineUserLogList(dto));
    }

    @PreAuthorize(permissionKey = "gameBack:oceanengine:userReport")
    @PostMapping("/userReport/{ids}")
    public ResultVO<Boolean> oceanengineUserReport(@PathVariable("ids") List<Long> ids) {
        return ResultVO.ok(oceanengineUserLogService.oceanengineUserReport(ids));
    }
}
