package com.izouma.nineth.repo;

import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.domain.Collection;
import com.izouma.nineth.enums.OrderStatus;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.service.AssetService;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class CollectionRepoTest extends ApplicationTests {
    @Autowired
    private CollectionRepo   collectionRepo;
    @Autowired
    private AssetService     assetService;
    @Autowired
    private TagRepo          tagRepo;
    @Autowired
    private UserPropertyRepo userPropertyRepo;
    @Autowired
    private OrderRepo        orderRepo;

    @Test
    public void updateCDN() {
        List<List<String>> list = collectionRepo.selectResource(83346L);
        for (int i = 0; i < list.get(0).size(); i++) {
            list.get(0).set(i, assetService.replaceCDN(list.get(0).get(i)));
        }
        collectionRepo.updateCDN(Long.parseLong(list.get(0).get(0)),
                list.get(0).get(1),
                list.get(0).get(2),
                list.get(0).get(3),
                list.get(0).get(4),
                list.get(0).get(5));
    }

    @Test
    public void testMaxCount() {
        Collection collection = collectionRepo.findById(8017401L).orElse(null);
        AtomicInteger userMax = new AtomicInteger();
        userPropertyRepo.findById(9972L).ifPresent(userProperty -> userMax.set(userProperty.getMaxCount()));
        if (collection.getMaxCount() > 0) {
            int count;
            if (StringUtils.isNotBlank(collection.getCountId())) {
                count = orderRepo.countByUserIdAndCountIdAndStatusIn(9972L, collection.getCountId(), Arrays
                        .asList(OrderStatus.FINISH, OrderStatus.NOT_PAID, OrderStatus.PROCESSING));
            } else {
                count = orderRepo.countByUserIdAndCollectionIdAndStatusIn(9972L, 8017401L, Arrays
                        .asList(OrderStatus.FINISH, OrderStatus.NOT_PAID, OrderStatus.PROCESSING));
            }
            if (userMax.get() > 0) {
                if (count >= userMax.get()) {
                    throw new BusinessException("限购" + userMax.get() + "件");
                }
            } else {
                if (count >= collection.getMaxCount()) {
                    throw new BusinessException("限购" + collection.getMaxCount() + "件");
                }
            }

        }
    }

    @Test
    public void byTag() {
        collectionRepo.findAll(new Specification<Collection>() {
            @Override
            public Predicate toPredicate(Root<Collection> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Join join = root.join("tags");
                return criteriaBuilder.and(criteriaBuilder.equal(join.get("id"), 1L));
            }
        });
    }

    @Test
    public void test() {
        System.out.println(collectionRepo.countAllByNameLike('%' + "123%"));
    }
}