
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.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.PrincipalUtil;
import com.tmerclub.cloud.distribution.dto.DistributionUserBanDTO;
import com.tmerclub.cloud.distribution.dto.DistributionUserDTO;
import com.tmerclub.cloud.distribution.model.DistributionUser;
import com.tmerclub.cloud.distribution.model.DistributionUserBan;
import com.tmerclub.cloud.distribution.service.DistributionUserService;
import com.tmerclub.cloud.distribution.service.DistributionUserWalletBillService;
import com.tmerclub.cloud.distribution.vo.DistributionUserAchievementVO;
import com.tmerclub.cloud.distribution.vo.DistributionUserBanVO;
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.*;

/**
 * 分销员信息
 *
 * @author cl
 * @date 2021-08-09 14:14:07
 */
@RestController("platformDistributionUserController")
@RequestMapping("/p/distributionUser")
@Tag(name = "platform-分销员信息")
public class DistributionUserController {

    @Autowired
    private DistributionUserService distributionUserService;

    @Autowired
    private DistributionUserWalletBillService distributionUserWalletBillService;

    @GetMapping("/page")
    @Operation(summary = "获取分销员管理列表", description = "分页获取分销员管理列表")
    public ServerResponseEntity<PageVO<DistributionUserVO>> page(@Valid PageDTO pageDTO, DistributionUserDTO distributionUserDTO) {
        PageVO<DistributionUserVO> distributionUserPage = distributionUserService.distributionUserPage(pageDTO, distributionUserDTO);
        setPhoneHide(distributionUserPage);
        return ServerResponseEntity.success(distributionUserPage);
    }

    @GetMapping("/achievementPage")
    @Operation(summary = "获取分销业绩统计列表", description = "分页获取分销业绩统计")
    public ServerResponseEntity<PageVO<DistributionUserAchievementVO>> achievementPage(@Valid PageDTO pageDTO, DistributionUserDTO distributionUserDTO, String userMobile) {
        PageVO<DistributionUserAchievementVO> page = distributionUserService.achievementPage(pageDTO, distributionUserDTO, userMobile);
        //计算累计收益
        for (DistributionUserAchievementVO distributionUserAchievementVO : page.getList()) {
            Long haveWithdrawalSum = distributionUserWalletBillService.getHaveWithdrawalSum(distributionUserAchievementVO.getWalletId());
            distributionUserAchievementVO.setAccumulateAmount(haveWithdrawalSum + distributionUserAchievementVO.getSettledAmount());
        }
        for (DistributionUserAchievementVO record : page.getList()) {
            if (StrUtil.isNotBlank(record.getNickName()) && PrincipalUtil.isMobile(record.getNickName())) {
                record.setNickName(PhoneUtil.hideBetween(record.getNickName()).toString());
            }
            if (StrUtil.isNotBlank(record.getUserMobile())) {
                record.setUserMobile(PhoneUtil.hideBetween(record.getUserMobile()).toString());
            }
        }
        return ServerResponseEntity.success(page);
    }

    @Operation(summary = "修改分销员的状态")
    @PutMapping("/updateState")
    public ServerResponseEntity<Void> updateState(@RequestBody DistributionUserBanDTO distributionUserBanDTO) {
        DistributionUserBan distributionUserBan = BeanUtil.map(distributionUserBanDTO, DistributionUserBan.class);
        distributionUserService.updateDistributionStateAndBan(distributionUserBan);
        // 清除缓存
        DistributionUser dbDistributionUser = distributionUserService.getByDistributionUserId(distributionUserBanDTO.getDistributionUserId());
        distributionUserService.removeCacheByUserId(dbDistributionUser.getUserId());
        return ServerResponseEntity.success();
    }

    @Operation(summary = "获取分销员最新的封禁信息")
    @GetMapping("/getLatestBanInfo")
    public ServerResponseEntity<DistributionUserBanVO> getLatestBanInfo(@RequestParam("distributionUserId") Long distributionUserId) {
        DistributionUserBanVO distributionUserBanVO = distributionUserService.getLatestBanInfoByDistributionUserId(distributionUserId);
        return ServerResponseEntity.success(distributionUserBanVO);
    }

    private void setPhoneHide(PageVO<DistributionUserVO> distributionUserPage) {
        for (DistributionUserVO distributionUserVO : distributionUserPage.getList()) {
            if (StrUtil.isNotBlank(distributionUserVO.getInviteeMobile())) {
                distributionUserVO.setInviteeMobile(PhoneUtil.hideBetween(distributionUserVO.getInviteeMobile()).toString());
            }
            if (StrUtil.isNotBlank(distributionUserVO.getUserMobile())) {
                distributionUserVO.setUserMobile(PhoneUtil.hideBetween(distributionUserVO.getUserMobile()).toString());
            }
            if (StrUtil.isNotBlank(distributionUserVO.getInvitee()) && PrincipalUtil.isMobile(distributionUserVO.getInvitee())) {
                distributionUserVO.setInvitee(PhoneUtil.hideBetween(distributionUserVO.getInvitee()).toString());
            }
            if (StrUtil.isNotBlank(distributionUserVO.getNickName()) && PrincipalUtil.isMobile(distributionUserVO.getNickName())) {
                distributionUserVO.setNickName(PhoneUtil.hideBetween(distributionUserVO.getNickName()).toString());
            }
        }
    }
}
