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

import com.tmerclub.cloud.api.platform.vo.DistributionRecruitConfigApiVO;
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.common.util.BeanUtil;
import com.tmerclub.cloud.distribution.service.DistributionConfigService;
import com.tmerclub.cloud.distribution.service.DistributionUserIncomeService;
import com.tmerclub.cloud.distribution.service.DistributionUserService;
import com.tmerclub.cloud.distribution.vo.*;
import com.tmerclub.cloud.distribution.vo.*;
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:07
 */
@RestController("appDistributionUserController")
@RequestMapping("/distribution_user")
@Tag(name = "app-分销员信息")
public class DistributionUserController {

    @Autowired
    private DistributionUserService distributionUserService;
    @Autowired
    private DistributionConfigService distributionConfigService;
    @Autowired
    private DistributionUserIncomeService distributionUserIncomeService;



    @GetMapping("/info")
    @Operation(summary = "查看分销中心信息", description = "查看分销中心信息")
    public ServerResponseEntity<AchievementDataVO> getByDistributionUserId() {
        Long userId = AuthUserContext.get().getUserId();
        DistributionUserVO distributionUser = distributionUserService.getByUserId(userId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是销售员
            throw new LuckException("您还不是销售员");
        }
        AchievementDataVO achievementDataVO = distributionUserService.getAchievementDataById(distributionUser.getDistributionUserId());
        return ServerResponseEntity.success(achievementDataVO);
    }

    @GetMapping("/distributionUserInfo")
    @Operation(summary = "分销员信息", description = "分销员信息")
    public ServerResponseEntity<DistributionUserInfoVO> getDistributionUserInfo() {
        Long userId = AuthUserContext.get().getUserId();
        // 推广开关
        DistributionRecruitConfigApiVO recruitConfig = distributionConfigService.getDistributionRecruitConfig();
        DistributionUserVO distributionUser = distributionUserService.getByUserId(userId);
        DistributionUserInfoVO distributionUserInfoVO = BeanUtil.map(distributionUser, DistributionUserInfoVO.class);
        if (distributionUserInfoVO == null) {
            distributionUserInfoVO = new DistributionUserInfoVO();
        }
        distributionUserInfoVO.setRecruitState(recruitConfig.getState());
        return ServerResponseEntity.success(distributionUserInfoVO);
    }


    @GetMapping("/subordinate")
    @Operation(summary = "查看下级", description = "查看下级")
    public ServerResponseEntity<PageVO<DistributionUserSimpleInfoVO>> subordinatePage(@Valid PageDTO pageDTO) {
        Long userId = AuthUserContext.get().getUserId();
        DistributionUserVO distributionUser = distributionUserService.getByUserId(userId);
        PageVO<DistributionUserSimpleInfoVO> pageVO = distributionUserService.getPageDistributionUserSimpleInfoByParentUserId(pageDTO, distributionUser.getDistributionUserId());
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/distributionOrderPage")
    @Operation(summary = "分页查看分销员佣金明细", description = "分页查看分销员佣金明细")
    public ServerResponseEntity<PageVO<DistributionOrderVO>> distributionOrderPage(@Valid PageDTO pageDTO) {
        Long userId = AuthUserContext.get().getUserId();
        DistributionUserVO distributionUser = distributionUserService.getByUserId(userId);
        PageVO<DistributionOrderVO> pageVO = distributionUserIncomeService.getDistributionOrderByDistributionUserId(pageDTO, distributionUser.getDistributionUserId());
        return ServerResponseEntity.success(pageVO);
    }


}
