package cn.com.qmth.stmms.ms.core.repository;

import cn.com.qmth.stmms.ms.core.domain.MarkLog;
import cn.com.qmth.stmms.ms.core.domain.MarkLogOperType;
import cn.com.qmth.stmms.ms.core.domain.Paper;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

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

/**
 * @author ting.yin
 * @date 2016年10月21日
 */
@Repository
public interface MarkLogRepo extends JpaRepository<MarkLog, Long>, JpaSpecificationExecutor<MarkLog> {

    /**
     * 根据条件查询最近一条日志
     *
     * @param createUserId
     * @param operType
     * @param subject
     * @param examNumber
     * @return
     */
    public MarkLog findTopByCreateUserIdAndOperTypeAndSubjectAndExamNumberOrderByCreateTimeDesc(Long createUserId, Integer operType, String subject, String examNumber);

    List<MarkLog> findByCreateUserId(Long userId);

    void deleteByPaperIdIn(List<Long> paperIds);

    @Query("select s.createUserName as createUserName, max(s.id) as id from MarkLog s where s.createRole = '采集员' and s.workId = ?1 group by s.createUserName")
    List<Map> findByWorkId(@Param("workId") Long workId);

    List<MarkLog> findByWorkIdAndSubjectAndOperTypeAndPaperIdIn(Long workId, String subject, Integer operType, List<Long> paperIds);

    Page<Map> findByIdIn(List<Long> longs, Pageable pageable);

    @Query("select count(s) from MarkLog s where s.workId = ?1 and s.paperId = ?2 and (s.operType = ?3 or s.operType = ?4)")
    int countByWorkIdAndPaperIdAndOperType(Long workId, Long id, int id1, int id2);
}
