package com.ssj.dao.weixin.problem.dao;

import com.ssj.bean.weixin.problem.domain.CnArea;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface CnAreaDao extends JpaRepository<CnArea, String>{
	
	@Query(nativeQuery = true, value = "SELECT a.id FROM sys_cn_area t \n" +
      "LEFT JOIN sys_cn_area a ON t.id=a.up_id\n" +
      "WHERE t.name= ?1 AND a.name= ?2 ")
	public String getName(String city ,String name);
	
	@Query(nativeQuery = true, value = "SELECT * FROM sys_cn_area t WHERE t.type= ?1 LIMIT 5000 ")
	public List<CnArea> findByTypeList(String type);
	
	@Query(nativeQuery = true, value = "SELECT * FROM sys_cn_area t WHERE t.type= ?1 and t.up_id= ?2 LIMIT 5000 ")
	public List<CnArea> findByTypeList(String type,String upId);
	
	@Query(nativeQuery = true, value = "SELECT count(1) FROM sys_cn_area t WHERE t.type= ?1 and t.up_id= ?2 and name Like CONCAT(CONCAT('%',?3,'%'))  ")
	public Long countByTypeList(String type,String upId,String name);
	
	@Query(nativeQuery = true, value = "SELECT count(1) FROM sys_cn_area t WHERE t.type= ?1 and t.up_id= ?2  ")
	public Long countByTypeList(String type,String upId);
	
	@Query(nativeQuery = true, value = "SELECT * FROM sys_cn_area t WHERE t.type= ?1 and t.up_id= ?2 ORDER BY t.create_time asc limit ?3,?4 ")
	public List<CnArea> findByTypeList(String type,String upId,Integer pageNo,Integer pageSize);
	
	@Query(nativeQuery = true, value = "SELECT * FROM sys_cn_area t WHERE t.type= ?1 and t.up_id= ?2 and name Like CONCAT(CONCAT('%',?3,'%')) ORDER BY t.create_time asc limit ?4,?5  ")
	public List<CnArea> findByTypeList(String type,String upId,String name,Integer pageNo,Integer pageSize);
	
	
	@Query(nativeQuery = true, value = " SELECT a.name  from  ( SELECT\n" +
			"	*,\n" +
			"	ROUND(\n" +
			"		6378.138 * 2 * ASIN(\n" +
			"			SQRT(\n" +
			"				POW(\n" +
			"					SIN(\n" +
			"						(\n" +
			"							?1 * PI() / 180 - school_lat * PI() / 180\n" +
			"						) / 2\n" +
			"					),\n" +
			"					2\n" +
			"				) + COS(?1 * PI() / 180) * COS(school_lat * PI() / 180) * POW(\n" +
			"					SIN(\n" +
			"						(\n" +
			"							?2 * PI() / 180 - school_lng * PI() / 180\n" +
			"						) / 2\n" +
			"					),\n" +
			"					2\n" +
			"				)\n" +
			"			)\n" +
			"		),\n" +
			"		2\n" +
			"	) AS juli\n" +
			"FROM\n" +
			"	sys_cn_area\n" +
			"WHERE\n" +
			"	type =?3\n" +
			"ORDER BY\n" +
			"	juli ASC LIMIT 1 ) a")
	public String getLocationArea(String league_lat, String league_lng,Integer type);

	@Query(nativeQuery = true, value = "SELECT * FROM sys_cn_area t WHERE t.create_time= ?1 and (school_lat is null or school_lng is null) ")
	List<CnArea> findListByCreateTime(String createTime);

	@Query(nativeQuery = true, value = "SELECT * FROM sys_cn_area WHERE school_lat is null or school_lng is null ")
	List<CnArea> findLatLngIsNullList();
	
	public CnArea findFirstByName(String name);

	@Query(nativeQuery = true, value = "select name from sys_cn_area s where s.name like CONCAT('%',?1,'%') ")
	public List<String> findByName(String name);

	@Query(nativeQuery = true, value = "select t.school from tb_lib_vip t where t.id in(" +
			"select s.vip_id from scon_homework_picture s where s.create_time >= ?1 and s.create_time <= ?2 and s.corrected_picture_url is not null and s.corrected_picture_url != '' GROUP BY s.vip_id) " +
			"GROUP BY t.school")
	public List<String> findBySchoolName(String startTime,String endTime);
}
