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

import com.tmerclub.cloud.api.payment.constant.PaySysType;
import com.tmerclub.cloud.api.payment.dto.AllinpayBalanceDTO;
import com.tmerclub.cloud.api.payment.feign.AllinpayFeignClient;
import com.tmerclub.cloud.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.common.exception.LuckException;
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.distribution.model.DistributionUserWallet;
import com.tmerclub.cloud.distribution.service.DistributionUserService;
import com.tmerclub.cloud.distribution.service.DistributionUserWalletBillService;
import com.tmerclub.cloud.distribution.service.DistributionUserWalletService;
import com.tmerclub.cloud.distribution.vo.DistributionUserVO;
import com.tmerclub.cloud.distribution.vo.DistributionUserWalletInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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("appDistributionUserWalletController")
@RequestMapping("/distributionUserWallet")
@Tag(name = "app-分销员钱包信息")
public class DistributionUserWalletController {

    @Autowired
    private DistributionUserWalletService distributionUserWalletService;

    @Autowired
    private DistributionUserService distributionUserService;


    @Autowired
    private DistributionUserWalletBillService distributionUserWalletBillService;

    @DubboReference
    private ConfigFeignClient configFeignClient;

    @DubboReference
    private AllinpayFeignClient allinpayFeignClient;

    @GetMapping
    @Operation(summary = "获取分销员钱包信息")
    public ServerResponseEntity<DistributionUserWalletInfoVO> getByWalletId() {
        Long userId = AuthUserContext.get().getUserId();
        //查看当前用户是否为分销员
        DistributionUserVO distributionUser = distributionUserService.getByUserId(userId);
        if (Objects.isNull(distributionUser)) {
            throw new LuckException("您还不是分销员");
        }
        DistributionUserWallet distributionUserWallet = distributionUserWalletService.getByDistributionUserId(distributionUser.getDistributionUserId());
        //计算积累收益
        Long haveWithdrawalSum = distributionUserWalletBillService.getHaveWithdrawalSum(distributionUserWallet.getWalletId());
        distributionUserWallet.setAccumulateAmount(haveWithdrawalSum + distributionUserWallet.getSettledAmount());
        DistributionUserWalletInfoVO userWalletInfoVO = BeanUtil.map(distributionUserWallet, DistributionUserWalletInfoVO.class);
        Integer paySysType = configFeignClient.getPaySysType().getData();
        // 如果是通联支付，分销钱包和用户余额打通，可提现金额为用户的余额
        if (Objects.equals(paySysType, PaySysType.ALLINPAY.value())) {
            AllinpayBalanceDTO allinpayBalanceDTO = allinpayFeignClient.getAllinpayBalance(userId, PaySysType.ALLINPAY.value()).getData();
            userWalletInfoVO.setSettledAmount(Objects.isNull(allinpayBalanceDTO) ? 0L : allinpayBalanceDTO.getAllAmount() - allinpayBalanceDTO.getFreezenAmount());
        }
        return ServerResponseEntity.success(userWalletInfoVO);
    }
}
