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

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.PrincipalUtil;
import com.tmerclub.cloud.distribution.dto.DistributionUserIncomeDTO;
import com.tmerclub.cloud.distribution.model.DistributionUserIncome;
import com.tmerclub.cloud.distribution.service.DistributionUserIncomeService;
import com.tmerclub.cloud.distribution.vo.DistributionUserIncomeVO;
import com.tmerclub.cloud.distribution.vo.DistributionUserVO;
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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

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

    @Autowired
    private DistributionUserIncomeService distributionUserIncomeService;

    @GetMapping("/page")
    @Operation(summary = "获取分销收入记录信息列表", description = "分页获取分销收入记录信息列表")
    public ServerResponseEntity<PageVO<DistributionUserIncome>> page(@Valid PageDTO pageDTO) {
        PageVO<DistributionUserIncome> distributionUserIncomePage = distributionUserIncomeService.page(pageDTO);
        return ServerResponseEntity.success(distributionUserIncomePage);
    }

    @GetMapping("/effectPage")
    @Operation(summary = "获取分销推广效果列表", description = "分页获取分销推广效果列表")
    public ServerResponseEntity<PageVO<DistributionUserIncomeVO>> effectPage(@Valid PageDTO pageDTO, DistributionUserIncomeDTO distributionUserIncomeDTO, String userMobile) {
        PageVO<DistributionUserIncomeVO> distributionUserIncomePage = distributionUserIncomeService.effectPage(pageDTO, distributionUserIncomeDTO, userMobile, Constant.PLATFORM_SHOP_ID);
        for (DistributionUserIncomeVO record : distributionUserIncomePage.getList()) {
            DistributionUserVO distributionUser = record.getDistributionUser();
            if (Objects.nonNull(distributionUser)) {
                if (StrUtil.isNotBlank(distributionUser.getUserMobile())) {
                    distributionUser.setUserMobile(PhoneUtil.hideBetween(distributionUser.getUserMobile()).toString());
                }
                if (StrUtil.isNotBlank(distributionUser.getNickName()) && PrincipalUtil.isMobile(distributionUser.getNickName())) {
                    distributionUser.setNickName(PhoneUtil.hideBetween(distributionUser.getNickName()).toString());
                }
            }
        }
        return ServerResponseEntity.success(distributionUserIncomePage);
    }

    @GetMapping
    @Operation(summary = "获取分销收入记录信息", description = "根据incomeId获取分销收入记录信息")
    public ServerResponseEntity<DistributionUserIncome> getByIncomeId(@RequestParam Long incomeId) {
        return ServerResponseEntity.success(distributionUserIncomeService.getByIncomeId(incomeId));
    }


}
