<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.supervision.police.mapper.ModelCaseMapper">
    <select id="selectAll" resultType="com.supervision.police.domain.ModelCase">
        select * from model_case
        where 1=1
        <if test="modelCase.id != null and modelCase.id != ''">
            and id = #{modelCase.id}
        </if>
        <if test="modelCase.isDelete">
            and data_status != '1'
        </if>
        <if test="!modelCase.isDelete">
            and data_status = '1'
        </if>
        <if test="modelCase.caseNo != null and modelCase.caseNo != ''">
            and case_no like concat('%', #{modelCase.caseNo}, '%')
        </if>
        <if test="modelCase.caseName != null and modelCase.caseName != ''">
            and case_name like concat('%', #{modelCase.caseName}, '%')
        </if>
        <if test="modelCase.caseType != null and modelCase.caseType != null">
            and FIND_IN_SET(#{modelCase.caseType}, case_type) > 0
        </if>
        <if test="modelCase.identifyResult != null and modelCase.identifyResult != ''">
            and identify_result IN
            <foreach item="item" collection="modelCase.identifyResult" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="modelCase.involvedPerson != null and modelCase.involvedPerson != ''">
            and ( law_actor like concat('%', #{modelCase.involvedPerson}, '%') or law_party like concat('%',
            #{modelCase.involvedPerson}, '%'))
        </if>
        <if test="modelCase.lawActor != null and modelCase.lawActor != ''">
            and law_actor like concat('%', #{modelCase.lawActor}, '%')
        </if>
        <if test="modelCase.lawParty != null and modelCase.lawParty != ''">
            and law_party like concat('%', #{modelCase.lawParty}, '%')
        </if>
        <if test="modelCase.updateStartTime != null">
            and DATE_FORMAT(update_time, '%Y-%m-%d') >= DATE_FORMAT(#{modelCase.updateStartTime}, '%Y-%m-%d')
        </if>
        <if test="modelCase.updateEndTime != null">
            and DATE_FORMAT(update_time, '%Y-%m-%d') &lt;= DATE_FORMAT(#{modelCase.updateEndTime}, '%Y-%m-%d')
        </if>
        order by index_num,update_time desc
    </select>
    <select id="selectMaxIndex" resultType="java.lang.Integer">
        select ifnull(max(index_num), 0)
        from model_case
        where data_status = '1'
    </select>
    <select id="getCaseIndexDetail" resultType="com.supervision.police.dto.IndexDetail">
        select mi.name as indexName, mi.index_score as score, mir.index_result, mir.atomic_ids
        from model_index mi
                 left join model_index_result mir on mi.id = mir.index_id
        WHERE mi.data_status = '1'
          and mir.case_id = #{caseId}
          and mi.index_type = #{indexType}
    </select>
    <select id="getAtomicDetail" resultType="com.supervision.police.dto.AtomicIndexDTO">
        select mar.index_id as indexId, mai.id as atomicIndexId,mai.name as indexName,mai.index_source as indexSource,
        mar.atomic_result as atomicResult,
        concat(nrs.question, nrs.answer) as record
        from model_atomic_result mar
        left join model_atomic_index mai on mar.atomic_id = mai.id
        left join note_record_split nrs on mar.record_split_id = nrs.id
        where mar.case_id = #{caseId} and mar.index_id = #{indexId} and mar.atomic_id in
        <foreach collection="atomicIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
    <select id="getIndexDetail" resultType="com.supervision.police.dto.IndexDetail">
        select mi.id          as indexId,
               mi.name        as indexName,
               mi.index_score as score,
               mir.index_result,
               mir.pre_result as preResult,
               mir.atomic_ids,
               mi.judge_logic as judgeLogic
        from model_index mi
                 left join model_index_result mir on (mi.id = mir.index_id and mir.case_id = #{caseId})
        WHERE mi.data_status = '1'
          and mi.index_type = #{indexType}
        order by CASE mir.index_result WHEN 'true' THEN 1 WHEN 'false' THEN 2 else 0 END, mi.id desc
    </select>
</mapper>