
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.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.PrincipalUtil;
import com.tmerclub.cloud.distribution.dto.DistributionAuditingDTO;
import com.tmerclub.cloud.distribution.model.DistributionAuditing;
import com.tmerclub.cloud.distribution.model.DistributionUser;
import com.tmerclub.cloud.distribution.service.DistributionAuditingService;
import com.tmerclub.cloud.distribution.service.DistributionUserService;
import com.tmerclub.cloud.distribution.vo.DistributionAuditingVO;
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.*;

import java.util.Date;

/**
 * 分销员申请信息
 *
 * @author cl
 * @date 2021-08-09 14:14:05
 */
@RestController("platformDistributionAuditingController")
@RequestMapping("/p/distributionAuditing")
@Tag(name = "platform-分销员申请信息")
public class DistributionAuditingController {

    @Autowired
    private DistributionAuditingService distributionAuditingService;

    @Autowired
    private DistributionUserService distributionUserService;

    @GetMapping("/page")
    @Operation(summary = "获取分销员申请信息列表", description = "分页获取分销员申请信息列表")
    public ServerResponseEntity<PageVO<DistributionAuditingVO>> page(@Valid PageDTO pageDTO, DistributionAuditingDTO distributionAuditingDTO) {
        PageVO<DistributionAuditingVO> distributionAuditingPage = distributionAuditingService.pageDistributionAuditing(pageDTO, distributionAuditingDTO);
        setPhoneHide(distributionAuditingPage);
        return ServerResponseEntity.success(distributionAuditingPage);
    }

    @GetMapping
    @Operation(summary = "获取分销员申请信息", description = "根据auditingId获取分销员申请信息")
    public ServerResponseEntity<DistributionAuditing> getByAuditingId(@RequestParam Long auditingId) {
        return ServerResponseEntity.success(distributionAuditingService.getByAuditingId(auditingId));
    }

    @PutMapping
    @Operation(summary = "审核分销员")
    public ServerResponseEntity<Void> update(@Valid @RequestBody DistributionAuditingDTO distributionAuditingDTO) {
        DistributionAuditing distributionAuditing = BeanUtil.map(distributionAuditingDTO, DistributionAuditing.class);
        distributionAuditing.setUpdateTime(new Date());
        distributionAuditing.setModifier(AuthUserContext.get().getUserId());
        distributionAuditingService.examine(distributionAuditing);
        // 清除缓存
        DistributionUser dbDistributionUser = distributionUserService.getByDistributionUserId(distributionAuditing.getDistributionUserId());
        distributionUserService.removeCacheByUserId(dbDistributionUser.getUserId());
        return ServerResponseEntity.success();
    }

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