package com.ssj.dao.weixin.library.dao;

import com.ssj.bean.weixin.libmy.domain.TbLibJoin;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface ILibJoinDao extends JpaRepository<TbLibJoin, String> {

    public Page<TbLibJoin> findAll(Specification<TbLibJoin> specification, Pageable page);


    @Query(nativeQuery = true, value = " update   tb_lib_manager set state=2  where id = ?1 ")
    @Modifying
    public void libManagerDelete(String id);

    @Query(nativeQuery = true, value = " update   tb_lib_manager set state=2  where lib_id = ?1 and user_id = ?2 and  type = ?3")
    @Modifying
    public void libManagerDeleteIn(String libId, String userId, Integer type);


    @Query(nativeQuery = true, value = " SELECT  * from   tb_lib_join  where type >= 1 ")
    public List<TbLibJoin> getValidLibJoin();

    @Query(nativeQuery = true, value = " SELECT  * from   tb_lib_join  where org_code in (?1) AND type >= 1 ")
    public List<TbLibJoin> getLibJoinByCode(List<String> orgCode);

    @Query(nativeQuery = true, value = " SELECT a.*  from  ( SELECT\n" +
            "	*,\n" +
            "	ROUND(\n" +
            "		6378.138 * 2 * ASIN(\n" +
            "			SQRT(\n" +
            "				POW(\n" +
            "					SIN(\n" +
            "						(\n" +
            "							?1 * PI() / 180 - league_lat * PI() / 180\n" +
            "						) / 2\n" +
            "					),\n" +
            "					2\n" +
            "				) + COS(?1 * PI() / 180) * COS(league_lat * PI() / 180) * POW(\n" +
            "					SIN(\n" +
            "						(\n" +
            "							?2 * PI() / 180 - league_lng * PI() / 180\n" +
            "						) / 2\n" +
            "					),\n" +
            "					2\n" +
            "				)\n" +
            "			)\n" +
            "		),\n" +
            "		2\n" +
            "	) AS juli\n" +
            "FROM\n" +
            "	tb_lib_join  where  type >=?3  \n" +
            "ORDER BY\n" +
            "	juli ASC LIMIT 1 ) a")
    public TbLibJoin getAnnexLib(String locationLat, String locationLng, int type);

    public TbLibJoin getById(String libId);

    @Query(nativeQuery = true, value = "select * from tb_lib_join where id in (select DISTINCT t.lib_id from tb_lib_manager t where t.user_id = ?1 and t.type = 6 and t.state = 1)")
    public List<TbLibJoin> findByMasterId(String masterId);

    @Query(nativeQuery = true, value = "select tb_lib_join.* from kmt_course, tb_lesson_service, tb_lib_join " +
            "where kmt_course.lesson_service_id = tb_lesson_service.id " +
            "and tb_lesson_service.lib_id = tb_lib_join.id " +
            "and kmt_course.id = ?1")
    TbLibJoin findOneByCourseId(String courseId);

    TbLibJoin findByLeagueCode(String leagueCode);

    @Query(nativeQuery = true, value = "select * from tb_lib_join where type>0 and cancel_stat > 0 ")
	public TbLibJoin findOneByCancelStat();

    @Query(nativeQuery = true,value = "select distinct league_city from tb_lib_join where league_city!='' and league_city is not null ")
    public List<Map<String,Object>> findAllCity();

    @Query(nativeQuery = true,value = "select * from tb_lib_join where league_city regexp ?1")
    public List<TbLibJoin>findAllLibList(String city);

    List<TbLibJoin> findByLeagueName(String leagueName);


    /**
     *删除audit 记录
     */
    @Query(nativeQuery = true, value = " delete from   tb_lib_manager_audit   where lib_manager_id = ?1 ")
    @Modifying
    void libAuditDelete(String id);
}
