package com.rf.psychological.dao.repository;

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.opLog.dao.SysLogEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author lpf
 * @description: 操作日志表
 * @date 2022/1/12 12:42
 */
public interface SysLogRepository extends BaseRepository<SysLogEntity, String> {

    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteById(String id);

    @Query(value = "select id, create_time, ip, method, operation, user_id, user_name, institution_no,result,type,url,response_time from t_sys_log\n" +
            "where institution_no=?1 and if(?2 is not null and ?2 !='',t_sys_log.operation like CONCAT('%',?2,'%') ,1=1)\n" +
            "and  if(?3 is not null and ?3 !='',t_sys_log.create_time>?3 ,1=1)  and if(?4 is not null and ?4 !='', t_sys_log.create_time<?4 ,1=1) " +
            "and  if(?5 is not null and ?5 !='',user_name like CONCAT('%',?5,'%') ,1=1) " +
            "and  if(?6 is not null and ?6 !='',type = ?6 ,1=1) order by create_time desc",
            countQuery = "select count(*) from t_sys_log\n" +
                    "where institution_no=?1 and if(?2 is not null and ?2 !='',t_sys_log.operation like CONCAT('%',?2,'%') ,1=1)\n" +
                    "and  if(?3 is not null and ?3 !='',t_sys_log.create_time>?3 ,1=1)  and if(?4 is not null and ?4 !='', t_sys_log.create_time<?4 ,1=1) " +
                    "and  if(?5 is not null and ?5 !='',user_name like CONCAT('%',?5,'%') ,1=1) and if(?6 is not null and ?6 !='',type = ?6 ,1=1) ",
            nativeQuery = true)
    Page<SysLogEntity> findByInstitutionNo(String institutionNo, String searchKey, Date beginTime, Date endTime, String searchUserName, String type, Pageable pageable);
}
