
package com.tmerclub.cloud.distribution.controller.app;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.distribution.constant.DistributionUserStateEnum;
import com.tmerclub.cloud.distribution.service.DistributionUserIncomeService;
import com.tmerclub.cloud.distribution.service.DistributionUserService;
import com.tmerclub.cloud.distribution.vo.DistributionUserIncomeOrderVO;
import com.tmerclub.cloud.distribution.vo.DistributionUserIncomeVO;
import com.tmerclub.cloud.distribution.vo.DistributionUserVO;
import com.tmerclub.cloud.distribution.vo.StatisticsDisUserIncomeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 分销收入记录信息
 *
 * @author cl
 * @date 2021-08-09 14:14:09
 */
@RestController("appDistributionUserIncomeController")
@RequestMapping("/distributionUserIncome")
@Tag(name = "app-分销收入记录信息")
public class DistributionUserIncomeController {

    @Autowired
    private DistributionUserIncomeService distributionUserIncomeService;

    @Autowired
    private DistributionUserService distributionUserService;

    @GetMapping("/page")
    @Operation(summary = "获取分销收入记录信息列表")
    public ServerResponseEntity<PageVO<DistributionUserIncomeVO>> page(@Valid PageDTO pageDTO) {
        DistributionUserVO distributionUser = distributionUserService.getByUserId(AuthUserContext.get().getUserId());
        if (Objects.isNull(distributionUser)) {
            throw new LuckException("您还不是分销员");
        }
        PageVO<DistributionUserIncomeVO> incomePageVO = distributionUserIncomeService.getDistributionUserIncomePage(pageDTO, distributionUser.getDistributionUserId());
        return ServerResponseEntity.success(incomePageVO);
    }

    @GetMapping("/statistics")
    @Operation(summary = "统计分销员今日、本月的收益")
    public ServerResponseEntity<StatisticsDisUserIncomeVO> statisticsDistributionUserIncome() {
        DistributionUserVO distributionUser = distributionUserService.getByUserId(AuthUserContext.get().getUserId());
        if (Objects.isNull(distributionUser)) {
            throw new LuckException("您还不是分销员");
        }
        return ServerResponseEntity.success(distributionUserIncomeService.statisticsDistributionUserIncome(distributionUser.getDistributionUserId()));
    }

    @GetMapping("/getMyPromotionOrderByState")
    @Operation(summary = "我的推广订单", description = "通过状态获取我的推广订单(0:待支付 1:待结算 2:已结算 -1:订单失效)")
    public ServerResponseEntity<PageVO<DistributionUserIncomeOrderVO>> getMyPromotionOrderByState(@Valid PageDTO pageDTO, Integer state) {
        DistributionUserVO distributionUser = distributionUserService.getByUserId(AuthUserContext.get().getUserId());
        if (Objects.isNull(distributionUser)) {
            throw new LuckException("您还不是分销员");
        }
        if (!Objects.equals(distributionUser.getState(), DistributionUserStateEnum.NORMAL.value())) {
            throw new LuckException("您的分销身份异常，请联系管理员");
        }
        return ServerResponseEntity.success(distributionUserIncomeService.getMyPromotionOrderByState(pageDTO, distributionUser.getDistributionUserId(), state));
    }
}
