package com.bizzan.bitrade.service;

import com.bizzan.bitrade.dao.ExchangeCoinRepository;
import com.bizzan.bitrade.entity.ExchangeCoin;
import com.bizzan.bitrade.pagination.Criteria;
import com.bizzan.bitrade.service.Base.BaseService;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Path;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExchangeCoinService  extends BaseService {
    @Autowired
    private ExchangeCoinRepository coinRepository;

    public List<ExchangeCoin> findAllEnabled() {
        Specification<ExchangeCoin> spec = (root, criteriaQuery, criteriaBuilder) -> {
            Path<String> enable = root.get("enable");
            criteriaQuery.where(criteriaBuilder.equal(enable, 1));
            return null;
        };
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "sort");
        Sort sort = new Sort(order);
        return coinRepository.findAll(spec, sort);
    }
    
    public List<ExchangeCoin> findAllEnabledWithoutPlatformCoins() {
        Specification<ExchangeCoin> spec = (root, criteriaQuery, criteriaBuilder) -> {
            Path<String> enable = root.get("enable");
            Path<String> robotType = root.get("robotType");
            criteriaQuery.where(criteriaBuilder.equal(enable, 1), criteriaBuilder.equal(robotType, 0));
            return null;
        };
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "sort");
        Sort sort = new Sort(order);
        return coinRepository.findAll(spec, sort);
    }
    
    public ExchangeCoin findPlatformCoin() {
        Specification<ExchangeCoin> spec = (root, criteriaQuery, criteriaBuilder) -> {
            Path<String> robotType = root.get("robotType");
            criteriaQuery.where(criteriaBuilder.equal(robotType, 1));
            return null;
        };
        List<ExchangeCoin> list = coinRepository.findAll(spec);
        return list.get(0);
    }

    //获取所有可显示币种
    public List<ExchangeCoin> findAllVisible() {
        Specification<ExchangeCoin> spec = (root, criteriaQuery, criteriaBuilder) -> {
            Path<String> visible = root.get("visible");
            Path<String> enable = root.get("enable");
            criteriaQuery.where(criteriaBuilder.equal(enable, 1), criteriaBuilder.equal(visible, 1));
            return null;
        };
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "sort");
        Sort sort = new Sort(order);
        return coinRepository.findAll(spec, sort);
    }

    public ExchangeCoin findOne(Long id) {
        return coinRepository.findOne(id);
    }


    public ExchangeCoin save(ExchangeCoin contractCoin) {
        return coinRepository.save(contractCoin);
    }

    public Page<ExchangeCoin> pageQuery(int pageNo, Integer pageSize) {
        Sort orders = Criteria.sortStatic("sort");
        PageRequest pageRequest = new PageRequest(pageNo - 1, pageSize, orders);
        return coinRepository.findAll(pageRequest);
    }

    /**
     * 获取合约交易对，如果引入多类型合约，使用该函数的地方都需要更改为：findBySymbolAndCategory
     * @param symbol
     * @return
     */
    public ExchangeCoin findBySymbol(String symbol) {
        return coinRepository.findBySymbol(symbol);
    }

    public List<ExchangeCoin> findAll() {
        return coinRepository.findAll();
    }


    public Page<ExchangeCoin> findAll(Predicate predicate, Pageable pageable) {
        return coinRepository.findAll(predicate, pageable);
    }

}
