package com.izouma.nineth.service;

import com.anji.captcha.util.StringUtils;
import com.izouma.nineth.domain.UserFundAccount;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.UserFundAccountDTO;
import com.izouma.nineth.enums.AccountStatus;
import com.izouma.nineth.enums.AccountType;
import com.izouma.nineth.enums.FundChangeType;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.*;
import com.izouma.nineth.utils.JpaUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@AllArgsConstructor
public class UserFundAccountService {

    private UserFundAccountRepo userFundAccountRepo;


    public Page<UserFundAccount> all(PageQuery pageQuery) {
        return userFundAccountRepo.findAll(JpaUtils.toSpecification(pageQuery, UserFundAccount.class), JpaUtils.toPageRequest(pageQuery));
    }



    /**
     * 获取首选账户
     * @param userId
     * @return
     */
    public UserFundAccount getPrimaryAlipayAccount(Long userId){
        List<UserFundAccount> accountList  = userFundAccountRepo.findByUserId(userId);
        accountList  = accountList.stream().filter(account -> {
            return account.getAccountType() == AccountType.ALIPAY_LOGON_ID || account.getAccountType() == AccountType.ALIPAY_USER_ID;
        }).collect(Collectors.toList());

        UserFundAccount primaryAccount = null;
        for(UserFundAccount account : accountList){
            if(account.isPrimaryFlag()){
                primaryAccount = account;
                break;
            }

        }
        if(primaryAccount == null && accountList.size() > 0){
            primaryAccount = accountList.get(0);
        }

        return primaryAccount;
    }

    /**
     * 添加支付宝账户
     * @param userFundAccountDTO
     * @return
     */
    public UserFundAccount addAlipayAccount(Long userId, UserFundAccountDTO userFundAccountDTO){
        if(StringUtils.isBlank(userFundAccountDTO.getAccountNo())){
            throw new BusinessException("账户号不能为空");
        }

        if(StringUtils.isBlank(userFundAccountDTO.getAccountName())){
            throw new BusinessException("账户真实名称不能为空");
        }

        if(userFundAccountDTO.getAccountType() == null){
            //默认情况
            userFundAccountDTO.setAccountType(AccountType.ALIPAY_LOGON_ID);
        }else{
            if(userFundAccountDTO.getAccountType() != AccountType.ALIPAY_LOGON_ID
                ||userFundAccountDTO.getAccountType() != AccountType.ALIPAY_USER_ID){
                throw new BusinessException("仅支持添加支付宝账户");
            }
        }

        UserFundAccount userFundAccount = UserFundAccount.builder()
                .accountType(userFundAccountDTO.getAccountType())
                .accountNo(userFundAccountDTO.getAccountNo())
                .accountName(userFundAccountDTO.getAccountName())
                .accountStatus(AccountStatus.NORMAL)
                .userId(userId).primaryFlag(userFundAccountDTO.isPrimaryFlag()).build();

        userFundAccountRepo.save(userFundAccount);

        return userFundAccount;

    }


    /**
     * 移除账户
     * @param userId
     * @param accountId
     */
    public void removeAccount(Long userId, Long accountId){
        UserFundAccount account = userFundAccountRepo.findById(accountId).orElseThrow(new BusinessException("账户不存在"));
        if(account.getUserId() != userId){
            throw new BusinessException("没有移除权限");
        }

        userFundAccountRepo.delete(account);

    }


    /**
     * 昨日开通钱包数
     * @return 昨日开通钱包数
     */
    public Integer lastDayOpenWalletNum() {
        return userFundAccountRepo.lastDayOpenWalletNum();
    }

    /**
     * 累计开通钱包数
     * @return 累计开通钱包数
     */
    public Integer openWalletNum() {
        return userFundAccountRepo.openWalletNum();
    }

    /**
     * 当天累计销户钱包数
     * @return 当天累计销户钱包数
     */
    public Integer lastDayClosingWalletNum() {
        return userFundAccountRepo.lastDayClosingWalletNum();
    }
}
