package common.service.impl;

import common.model.AppInfo;
import common.repository.AppCategoryRepository;
import common.repository.AppInfoRepository;
import common.repository.CityRepository;
import common.service.AppService;
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 util.StringUtil;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zxys on 2017/12/27.
 */
@Service
public class AppServiceImpl implements AppService
{

    @Autowired AppInfoRepository appInfoRepository;
    @Autowired AppCategoryRepository appCategoryRepository;
    @Autowired CityRepository cityRepository;

    @Override
    public Page<AppInfo> findAppList(AppInfo app, int pageNum, int pageSize)
    {
        Specification<AppInfo> specification = getWhereClause(app);
        List<Sort.Order> orders = new ArrayList<Sort.Order>();
        orders.add(new Sort.Order(Sort.Direction.ASC,"id"));
        Sort sort = new Sort(orders);
        Pageable pageable = new PageRequest(pageNum, pageSize, sort);
        Page<AppInfo> appInfoPage = appInfoRepository.findAll(specification, pageable);
        return appInfoPage;
    }

    @Override
    public List<String> findAllCategoryByLevel(int level)
    {
        return appCategoryRepository.findCategoryByLevel(level);
    }

    @Override
    public List<String> findCity()
    {
        return cityRepository.findCitys();
    }

    /**
     * 动态生成where语句
     * @param appInfo
     * @return
     */
    private Specification<AppInfo> getWhereClause(final AppInfo appInfo){
        return new Specification<AppInfo>() {
            @Override
            public Predicate toPredicate(Root<AppInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicate = new ArrayList<>();
                if (appInfo.getReyun() != null) {
                    predicate.add(cb.equal(root.get("reyun").as(Integer.class), appInfo.getReyun()));
                }
                if (!StringUtil.isEmpty(appInfo.getLocation())) {
                    predicate.add(cb.equal(root.get("location").as(String.class), appInfo.getLocation()));
                }
                if (!StringUtil.isEmpty(appInfo.getFirstCate())) {
                    predicate.add(cb.equal(root.get("firstCate").as(String.class), appInfo.getFirstCate()));
                }
                if (!StringUtil.isEmpty(appInfo.getSecondCate())) {
                    predicate.add(cb.equal(root.get("secondCate").as(String.class), appInfo.getSecondCate()));
                }
                Predicate[] pre = new Predicate[predicate.size()];
                query.where(predicate.toArray(pre));
                return query.getRestriction();
            }
        };
    }

}
