
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.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.common.constant.DistributionAudit;
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.util.PrincipalUtil;
import com.tmerclub.cloud.distribution.constant.EnterprisePayStatus;
import com.tmerclub.cloud.distribution.dto.DistributionWithdrawCashDTO;
import com.tmerclub.cloud.distribution.model.DistributionUser;
import com.tmerclub.cloud.distribution.model.DistributionUserWallet;
import com.tmerclub.cloud.distribution.model.DistributionWithdrawCash;
import com.tmerclub.cloud.distribution.service.DistributionUserService;
import com.tmerclub.cloud.distribution.service.DistributionUserWalletService;
import com.tmerclub.cloud.distribution.service.DistributionWithdrawCashService;
import com.tmerclub.cloud.distribution.vo.DistributionUserVO;
import com.tmerclub.cloud.distribution.vo.DistributionWithdrawCashVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
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.PutMapping;
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:10
 */
@RestController("platformDistributionWithdrawCashController")
@RequestMapping("/p/distributionWithdrawCash")
@Tag(name = "分销员提现记录")
public class DistributionWithdrawCashController {

    @Autowired
    private DistributionWithdrawCashService distributionWithdrawCashService;
    @Autowired
    private DistributionUserWalletService distributionUserWalletService;
    @Autowired
    private DistributionUserService distributionUserService;
    @DubboReference
    private ConfigFeignClient configFeignClient;

    @GetMapping("/page")
    @Operation(summary = "获取分销员提现记录列表", description = "分页获取分销员提现记录列表")
    public ServerResponseEntity<PageVO<DistributionWithdrawCashVO>> page(@Valid PageDTO pageDTO, String userMobile, DistributionWithdrawCashDTO distributionWithdrawCashDTO) {
        PageVO<DistributionWithdrawCashVO> distributionWithdrawCashPage = distributionWithdrawCashService.page(pageDTO, userMobile, distributionWithdrawCashDTO);
        for (DistributionWithdrawCashVO record : distributionWithdrawCashPage.getList()) {
            DistributionUserVO distributionUser = record.getDistributionUserVO();
            if (Objects.nonNull(distributionUser)) {
                if (StrUtil.isNotBlank(distributionUser.getNickName()) && PrincipalUtil.isMobile(distributionUser.getNickName())) {
                    distributionUser.setNickName(PhoneUtil.hideBetween(distributionUser.getNickName()).toString());
                }
                if (StrUtil.isNotBlank(distributionUser.getUserMobile())) {
                    distributionUser.setUserMobile(PhoneUtil.hideBetween(distributionUser.getUserMobile()).toString());
                }
            }
        }
        return ServerResponseEntity.success(distributionWithdrawCashPage);
    }


    @PutMapping("/updateSuccess")
    @Operation(summary = "更新分销员提现记录", description = "更新分销员提现记录")
    public ServerResponseEntity<Void> update(Long withdrawCashId) {
        DistributionWithdrawCash distributionWithdrawCash = distributionWithdrawCashService.getByWithdrawCashId(withdrawCashId);
        if (distributionWithdrawCash == null) {
            // 无法获取提现信息
            throw new LuckException("无法获取提现信息");
        }
        Long walletId = distributionWithdrawCash.getWalletId();
        DistributionUserWallet wallet = distributionUserWalletService.getByWalletId(walletId);
        if (wallet == null) {
            // 无法获取用户钱包信息
            throw new LuckException("无法获取用户钱包信息");
        }
        DistributionUser distributionUser = distributionUserService.getByDistributionUserId(wallet.getDistributionUserId());
        if (distributionUser == null) {
            // 无法获取用户信息
            throw new LuckException("无法获取用户信息");
        }
        distributionWithdrawCash.setState(1);
        distributionWithdrawCashService.update(distributionWithdrawCash);

        // 判断打款状态 1审核通过企业打款 2审核通过人工打款

        if (!distributionWithdrawCash.getMoneyFlow().equals(DistributionAudit.Withdrawals_ONE.value())) {
            return ServerResponseEntity.success();
        }
        // 获取用户的openId
        DistributionUserWallet one = distributionUserWalletService.getByWalletId(distributionWithdrawCash.getWalletId());

        Long userId = distributionUserService.getByDistributionUserId(one.getDistributionUserId()).getUserId();
        // 添加企业支付记录
        distributionWithdrawCashService.enterprisePay(distributionWithdrawCash, userId, distributionWithdrawCash.getBizUserId(), EnterprisePayStatus.APPLY.getValue());
        return ServerResponseEntity.success();
    }

}
