package com.sw.mes.grade.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.grade.entity.RuleTestData;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.Map;

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
public interface RuleTestDataDao extends BaseDao<RuleTestData, Integer> {
    @Query(value = "SELECT grtd.id,igv.name,grtd.section_no,grtd.`status`,grtd.grade,grtd.grade_data,igv.version,grtd.gmt_update,grtd.user_update,grtd.grade_old,grt.remarks FROM grade_fb_rule_test_data grtd " +
            " LEFT JOIN grade_fb_rule_test grt ON  grtd.rule_test_id = grt.id\n" +
            " LEFT JOIN inspect_grade_version igv ON grt.grade_version_id = igv.id  WHERE (igv.version  = ?1 OR ?1 IS NULL) AND (grtd.gmt_update >= ?2 OR ?2 IS NULL) AND (grtd.gmt_update < ?3 OR ?3 IS NULL)" +
            " AND grtd.grade LIKE ?4 AND grtd.section_no LIKE ?5 AND (grtd.`status` = ?6 OR ?6 IS NULL) ORDER BY grtd.id DESC",
            countQuery = "SELECT COUNT(1) FROM grade_fb_rule_test_data grtd \n" +
                    " LEFT JOIN grade_fb_rule_test grt ON  grtd.rule_test_id = grt.id\n" +
                    " LEFT JOIN inspect_grade_version igv ON grt.grade_version_id = igv.id  WHERE (igv.version  = ?1 OR ?1 IS NULL) AND (grtd.gmt_update >= ?2 OR ?2 IS NULL) AND (grtd.gmt_update < ?3 OR ?3 IS NULL)" +
                    " AND grtd.grade LIKE ?4 AND grtd.section_no LIKE ?5 AND (grtd.`status` = ?6 OR ?6 IS NULL) ", nativeQuery = true)
    Page<Map<String, Object>> listByPageV2(String gradeVersion, String gradeVersionName, String sectionNo, String grade, String no, Integer status, Pageable pageable);

    @Query(value = "select * from grade_fb_rule_test_data where section_no = ?1 order by id desc limit 1",nativeQuery = true)
    RuleTestData findBysectionNo(String sectionNo);
}
