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_rule_test_data grtd " +
            " LEFT JOIN grade_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 (?1 IS NULL OR igv.version  = ?1) AND (?2 IS NULL OR grtd.gmt_update >= ?2) AND (?3 IS NULL OR grtd.gmt_update < ?3)" +
            " AND (?4 IS NULL OR grtd.grade LIKE ?4) AND grtd.section_no LIKE ?5 AND (?6 IS NULL OR grtd.`status` = ?6) ORDER BY grtd.id DESC",
            countQuery = "SELECT COUNT(1) FROM grade_rule_test_data grtd \n" +
                    " LEFT JOIN grade_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 (?1 IS NULL OR igv.version  = ?1) AND (?2 IS NULL OR grtd.gmt_update >= ?2) AND (?3 IS NULL OR grtd.gmt_update < ?3)" +
                    " AND (?4 IS NULL OR grtd.grade LIKE ?4) AND grtd.section_no LIKE ?5 AND (?6 IS NULL OR grtd.`status` = ?6)", nativeQuery = true)
    Page<Map<String, Object>> listByPageV2(String gradeVersion, String gradeVersionName, String sectionNo, String grade, String no, Integer status, Pageable pageable);
}