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 dmp.model.AppData;
import org.apache.commons.collections.map.HashedMap;
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.Constant;
import util.HttpClientUtil;
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;
import java.util.Map;

/**
 * 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, String sortString, Integer isASC)
    {
        Specification<AppInfo> specification = getWhereClause(app);
        List<Sort.Order> orders = new ArrayList<Sort.Order>();
        if (StringUtil.isEmpty(sortString)) {
            sortString = "pkgName";
        }
        orders.add(new Sort.Order((isASC!=null && isASC==0)?Sort.Direction.DESC:Sort.Direction.ASC,sortString));
        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();
    }

    @Override
    public String findAppDataList(String startDate, String endDate, List<String> pkgNameList, Integer isASC)
    {
        Map<String, String> conditions = new HashedMap();
        String url = Constant.reportUrl + "/api/dmp/newdevicebypkgname/test";
        conditions.put("datatype", "list");
        conditions.put("startdate", "2017-12-20");
        conditions.put("enddate", "2017-12-26");
        if (pkgNameList != null && pkgNameList.size()>0) {
            conditions.put("pkgname", String.join(",", pkgNameList));
        }
        if (isASC != null) {
            conditions.put("isasc", isASC.toString());
        }
        String responseJson = HttpClientUtil.doHttpPostRequest(url, "manager", conditions);
        return responseJson;
    }

    /**
     * 动态生成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()));
                }
                if (!StringUtil.isEmpty(searchString)) {

                }
                Predicate[] pre = new Predicate[predicate.size()];
                query.where(predicate.toArray(pre));
                return query.getRestriction();
            }
        };
    }

}
