package com.sysu.admin.controller.lz.tree;

import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.QBean;
import com.querydsl.jpa.impl.JPAQuery;
import com.sysu.admin.controller.lz.area.LzArea;
import com.sysu.admin.controller.lz.area.LzAreaRepository;
import com.sysu.admin.site.CommonVo;
import com.sysu.admin.support.base.BaseService;
import com.sysu.admin.support.system.user.User;
import com.xiesx.fastboot.core.jpa.JpaPlusRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Query;
import javax.persistence.criteria.*;
import java.util.List;

@Service
public class LzTreeService extends BaseService<LzTree, Integer> {

    @Autowired
    LzTreeRepository lzTreeRepository;

    public List<LzTree> findAll(CommonVo vo){
        String filter = " where 1=1 ";
        if(vo != null){
            if(vo.getClazzId() != null){
                filter += " and c.id = " + vo.getClazzId();
            }
            if(vo.getPeriodId() != null){
                filter += " and b.lzPeriod.id = " + vo.getPeriodId();
            }
        }
        String sql = "SELECT a, b, c FROM LzTree a JOIN FETCH a.lzEveryday b JOIN FETCH a.clazz c";
        Query query = mEntityManager.createQuery(sql + filter);
        return query.getResultList();
    }

    public LzTree findByCode(String code){
        return lzTreeRepository.findByCode(code);
    }

    public LzTree findByPoint(Double[] point, Integer m){
        double degree = m / (2 * Math.PI * 6371004) * 360;
        return lzTreeRepository.findByPoint("Point("+point[0]+" "+point[1]+")", degree);
    }
    @Override
    public JpaPlusRepository<LzTree, Integer> r() {
        return lzTreeRepository;
    }
}
