package com.izouma.nineth.service;

import com.alipay.api.AlipayApiException;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.config.Constants;
import com.izouma.nineth.domain.IdentityAuth;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.dto.BankValidate;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.UserBankCard;
import com.izouma.nineth.dto.UserRegister;
import com.izouma.nineth.enums.AuthStatus;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.IdentityAuthRepo;
import com.izouma.nineth.repo.UserBankCardRepo;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.service.storage.StorageService;
import com.izouma.nineth.utils.BankUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Slf4j
public class UserServiceTest extends ApplicationTests {

    @Autowired
    private UserService           userService;
    @Autowired
    private UserRepo              userRepo;
    @Autowired
    private StorageService        storageService;
    @Autowired
    private UserBankCardRepo      userBankCardRepo;
    @Autowired
    private AdapayMerchantService adapayMerchantService;
    @Autowired
    private IdentityAuthRepo      identityAuthRepo;

    @Test
    public void findByUsernameAndDelFalse1() {
    }

    @Test
    public void searchByPhone() {
        System.out.println(userService.searchByPhone("15077886171"));
    }

    @Test
    public void register() throws IOException {
        try {
            User user = userService.create(UserRegister.builder()
                    .username("1111fff1g1rertuiohejldknhgurphoj")
                    .nickname("1111fff1g1rertuiohejldknhgurphoj")
                    .avatar(Constants.DEFAULT_AVATAR)
                    .phone("15077886171")
                    .build());
            System.out.println(user.getId());
        } catch (Exception e) {
            if (Pattern.matches(".*SQL.*constraint.*phone.*", e.getMessage())) {
                System.out.println("手机号已注册");
            }
            if (Pattern.matches(".*SQL.*constraint.*username.*", e.getMessage())) {
                System.out.println("用户名已存在");
            }
            e.printStackTrace();
        }

    }

    @Test
    public void batchRegister() {
        userService.batchRegister("13226246698\n" +
                "13793310531\n" +
                "13358006613\n" +
                "18898406893\n" +
                "17615876370\n" +
                "13267002685\n" +
                "15888548080\n" +
                "13678662069\n" +
                "15659009359\n" +
                "13011776866\n" +
                "18810668532\n" +
                "13543322894\n" +
                "13589360750\n" +
                "18582589484\n" +
                "15194185209\n" +
                "15063692500\n" +
                "13605489618\n" +
                "18069783568\n" +
                "15801815684\n" +
                "13285413859\n" +
                "13688419164\n" +
                "15979115671\n" +
                "13799940755\n" +
                "18301015323", "123456");
    }

    @Test
    public void test() {
        PageQuery pageQuery = new PageQuery();
        Map<String, Object> query = pageQuery.getQuery();
        query.put("inviteCode", "JL21FF");
        pageQuery.setSize(5000);
        List<User> phone = userService.all(pageQuery)
                .getContent();
        phone.forEach(user -> System.out.println(user.getPhone() + ":" + user.getCreatedAt()));
        // 2022-01-14T15:53:58 504 第一次查询
        phone.forEach(user -> System.out.print(user.getPhone() + " "));
    }

    @Test
    public void switchAccount() {
        userService.checkSettleAccount();
    }

    @Test
    public void phoneRegister() {
        userService.phoneRegister("18100004444", "1234", "123456", null, 9972L,
                206925L, null);
    }

    @Test
    public void addBankCard() throws BaseAdaPayException {
        Long userId = 134613L;
        String bankNo = "6222024301070380165";
        String phone = "15077886171";
        User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
        IdentityAuth identityAuth = identityAuthRepo.findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(userId, AuthStatus.SUCCESS)
                .orElseThrow(new BusinessException("用户未认证"));
        if (identityAuth.isOrg()) {
            //throw new BusinessException("企业认证用户请绑定对公账户");
        }
        if (!StringUtils.isBlank(user.getSettleAccountId())) {
            throw new BusinessException("此账号已绑定");
        }
        BankValidate bankValidate = BankUtils.validate(bankNo);
        if (!bankValidate.isValidated()) {
            throw new BusinessException("暂不支持此卡");
        }

        adapayMerchantService.createMemberForAll(userId.toString(), user.getPhone(), identityAuth.getRealName(), identityAuth.getIdNo());
        user.setMemberId(user.getId().toString());
        userRepo.save(user);

        String accountId = adapayMerchantService.createSettleAccountForAll
                (user.getMemberId(), identityAuth.getRealName(),
                        identityAuth.getIdNo(), phone, bankNo);
        user.setSettleAccountId(accountId);
        userRepo.save(user);

        userBankCardRepo.save(UserBankCard.builder()
                .bank(bankValidate.getBank())
                .bankName(bankValidate.getBankName())
                .bankNo(bankNo)
                .cardType(bankValidate.getCardType())
                .cardTypeDesc(bankValidate.getCardTypeDesc())
                .userId(userId)
                .phone(phone)
                .realName(identityAuth.getRealName())
                .idNo(identityAuth.getIdNo())
                .build());
    }

    @Test
    public void removeBank() throws BaseAdaPayException {
        Long[] arr = {2577842L, 1440523L, 2775597L, 2766468L, 2686690L, 2747709L,
                2804878L, 1737822L, 1692606L, 1690986L, 12895L, 2768872L,
                2758319L, 1722623L, 2767026L, 2742354L, 2772909L, 2813187L,
                2592513L, 1245862L, 2581606L, 2775941L, 1649287L, 2764550L,
                2660763L, 2609032L, 2777065L, 2589915L, 2810643L, 3114342L,
                2897490L, 2579233L, 2728905L, 3064530L, 3261900L, 1634473L,
                2798840L, 3146142L, 2872925L, 3309420L, 3618828L, 2786593L,
                3337840L, 3253588L, 2745257L, 3454559L, 3474642L, 3468716L,
                3363882L, 2685228L, 3088476L, 2900428L, 2705763L, 2687527L,
                2787733L, 2737842L, 3324792L, 3147077L, 3145577L, 3142936L,
                3385528L, 2888036L, 3366975L, 2842334L, 2864480L, 3277214L,
                3260809L, 2867983L, 3246561L, 3472246L, 3302523L, 2882982L,
                3150301L, 2782692L, 3082410L, 3043665L, 3448907L, 3421038L,
                3371851L, 3225869L, 3087358L, 3463444L, 3478718L, 3434422L,
                2784575L, 2622956L, 2700225L, 1635188L, 2715660L, 620172L,
                3418309L, 3476261L, 2597211L, 3293018L, 3261495L, 3443258L
        };
        for (Long aLong : arr) {
            userService.removeBankCard(aLong);
        }
    }

    @Test
    public void scanWeakPassword() {
        userService.scanWeakPassword();
    }

    @Test
    public void aliAuth() throws AlipayApiException {
        String certifyId = userService.prepareAliAuth("IDENTITY_CARD", 1L, "熊竹", "321002199408304614");
        log.info(certifyId);
        String url = userService.getAliAuthUrl(certifyId);
        log.info(url);
        userService.checkFaceAuth(certifyId);
    }

    @Test
    public void checkauth() throws AlipayApiException {
        userService.checkFaceAuth("7160e6a875cb67648bc7a3cce6e5397e");
    }
}