
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.DistributionUserWalletBillDTO;
import com.tmerclub.cloud.distribution.model.DistributionUserWalletBill;
import com.tmerclub.cloud.distribution.service.DistributionUserWalletBillService;
import com.tmerclub.cloud.distribution.vo.DistributionUserVO;
import com.tmerclub.cloud.distribution.vo.DistributionUserWalletBillVO;
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.Objects;

/**
 * 分销员钱包流水记录
 *
 * @author cl
 * @date 2021-08-09 14:14:10
 */
@RestController("platformDistributionUserWalletBillController")
@RequestMapping("/p/distributionUserWalletBill")
@Tag(name = "分销员钱包流水记录")
public class DistributionUserWalletBillController {

    @Autowired
    private DistributionUserWalletBillService distributionUserWalletBillService;

    @GetMapping("/page")
    @Operation(summary = "获取分销员钱包流水记录列表", description = "分页获取分销员钱包流水记录列表")
    public ServerResponseEntity<PageVO<DistributionUserWalletBill>> page(@Valid PageDTO pageDTO) {
        PageVO<DistributionUserWalletBill> distributionUserWalletBillPage = distributionUserWalletBillService.page(pageDTO);
        return ServerResponseEntity.success(distributionUserWalletBillPage);
    }

    @GetMapping("/walletBillPage")
    @Operation(summary = "获取分销员钱包流水记录列表", description = "分页获取分销员钱包流水记录列表")
    public ServerResponseEntity<PageVO<DistributionUserWalletBillVO>> walletBillPage(@Valid PageDTO pageDTO, String userMobile, String nickName) {
        PageVO<DistributionUserWalletBillVO> pageVO = distributionUserWalletBillService.walletBillPage(pageDTO, userMobile, nickName);
        for (DistributionUserWalletBillVO record : pageVO.getList()) {
            DistributionUserVO distributionUser = record.getDistributionUserVO();
            if (Objects.nonNull(distributionUser)) {
                if (StrUtil.isNotBlank(distributionUser.getUserMobile()) && PrincipalUtil.isMobile(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(pageVO);
    }

    @GetMapping
    @Operation(summary = "获取分销员钱包流水记录", description = "根据id获取分销员钱包流水记录")
    public ServerResponseEntity<DistributionUserWalletBill> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(distributionUserWalletBillService.getById(id));
    }

    @PutMapping
    @Operation(summary = "更新分销员钱包流水记录", description = "更新分销员钱包流水记录")
    public ServerResponseEntity<Void> update(@Valid @RequestBody DistributionUserWalletBillDTO distributionUserWalletBillDTO) {
        DistributionUserWalletBill distributionUserWalletBill = BeanUtil.map(distributionUserWalletBillDTO, DistributionUserWalletBill.class);
        distributionUserWalletBillService.update(distributionUserWalletBill);
        return ServerResponseEntity.success();
    }
}
