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

import com.ssj.bean.weixin.libmy.domain.TbLibManagerAudit;
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;

/**
 * @author Mount Liu
 * @date 2019/2/28
 */
@Repository
public interface LibManagerAuditDao extends JpaRepository<TbLibManagerAudit, String> {

    /**
     * 分页查询
     *
     * @param specification
     * @param initPage
     * @return
     */
    Page<TbLibManagerAudit> findAll(Specification<TbLibManagerAudit> specification, Pageable initPage);

    /**
     * 查询机构老师对应审批记录
     *
     * @param libManagerId
     * @return
     */
    List<TbLibManagerAudit> findByLibManagerIdOrderByCreateTimeDesc(String libManagerId);

    /**
     * 查询对应审批记录
     *
     * @param libManagerId
     * @param status
     * @return
     */
    List<TbLibManagerAudit> findByLibManagerIdAndStatusOrderByCreateTimeDesc(String libManagerId,int status);

    @Query(nativeQuery = true, value = "select * from tb_lib_manager_audit where user_id = ?1 and type = ?2 order by create_time desc limit 1 ")
    TbLibManagerAudit findNewestOneByUserIdAndType(String userId, int type);

    TbLibManagerAudit findByUserIdAndTypeAndStatus(String teacherId, int type, int status);

    TbLibManagerAudit findByUserIdAndType(String userId, int type);

    /**
     * 查询最新的一条审批记录
     * @param userId
     * @param type
     * @return
     */
    TbLibManagerAudit findTopByUserIdAndTypeOrderByCreateTimeDesc(String userId, int type);

    /**
     * 查询最新的一条审批记录
     * @param userId
     * @param status
     * @param type
     * @return
     */
    TbLibManagerAudit findTopByUserIdAndStatusAndTypeInOrderByCreateTimeDesc(String userId, int status, int[] type);

    /**
     * 根据管理员id查询最新的一条申请记录
     */
    TbLibManagerAudit findTopByLibManagerIdOrderByCreateTimeDesc(String libManagerId);

    /**
     * 统计审批数
     * @param teacherId
     * @param type
     * @param status
     * @return
     */
    long countByUserIdAndTypeAndStatus(String teacherId, int type, int status);

    /**
     * 统计审批数
     * @param type
     * @param status
     * @return
     */
    long countByTypeInAndStatus(int[] type, int status);

    @Query(nativeQuery = true,value = "select * from tb_lib_manager_audit where user_id=?1  and type in(8,11) and status=3")
    List<TbLibManagerAudit> findByUserId(String userId);

    @Modifying
    @Query(nativeQuery = true,value = "delete from tb_lib_manager_audit where user_id=?1 and type in (2,10,15,8,11)")
    void deleteByUserId(String userId);

    @Query(nativeQuery = true,value = "select count(distinct user_id) from tb_lib_manager_audit where type=17 and status=3")
    int findUnOperateNum();

    @Query(nativeQuery = true,value = "select * from tb_lib_manager_audit where type=?1 and status=?2")
    List<TbLibManagerAudit> findAllByTypeAndStatus(int type,int status);
}
