<?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.njcn.gather.icd.mapper.PqIcdPathMapper">


    <select id="selectIcdByDevType" resultType="com.njcn.gather.icd.pojo.po.PqIcdPath">
        select path.*
        from pq_icd_path path
                 inner join pq_dev_type type on path.id = type.icd
        where type.id = #{devTypeId}
    </select>

    <select id="selectPageList" resultType="com.njcn.gather.icd.pojo.po.PqIcdPath">
        select p.id,
               p.name,
               p.state,
               p.angle,
               p.use_phase_index as usePhaseIndex,
               p.Json_Str as jsonStr,
               p.Xml_Str as xmlStr,
               p.Result as result,
               p.Msg as msg,
               p.Type as type,
               p.Reference_Icd_Id as referenceIcdId,
               ref.name as referenceIcdName,
               case when p.Icd is null then false else true end as hasIcdFile,
               p.create_by as createBy,
               p.create_time as createTime,
               p.update_by as updateBy,
               p.update_time as updateTime
        from pq_icd_path p
                 left join pq_icd_path ref on p.Reference_Icd_Id = ref.id
            and ref.state = 1
            and ref.Type in (1, 3)
        where p.state = 1
        <if test="name != null and name != ''">
            and p.name like concat('%', #{name}, '%')
        </if>
        order by p.create_time desc
    </select>

    <select id="selectStandardList" resultType="com.njcn.gather.icd.pojo.po.PqIcdPath">
        select id, name, Type as type
        from pq_icd_path
        where state = 1
          and Type in (1, 3)
        order by create_time desc
    </select>

    <select id="selectStandardByName" resultType="com.njcn.gather.icd.pojo.po.PqIcdPath">
        select id, name, Type as type, state
        from pq_icd_path
        where state = 1
          and Type in (1, 3)
          and name = #{name}
        limit 1
    </select>

    <select id="selectByIdsWithoutBlob" resultType="com.njcn.gather.icd.pojo.po.PqIcdPath">
        select id, name, Type as type, state
        from pq_icd_path
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
          and state = 1
    </select>

    <select id="countActiveReferencesToStandards" resultType="java.lang.Long">
        select count(1)
        from pq_icd_path
        where state = 1
          and Type in (2, 4)
          and Reference_Icd_Id in
          <foreach collection="standardIds" item="id" open="(" separator="," close=")">
              #{id}
          </foreach>
    </select>

    <select id="selectDetailById" resultType="com.njcn.gather.icd.pojo.po.PqIcdPath">
        select p.id,
               p.name,
               p.state,
               p.angle,
               p.use_phase_index as usePhaseIndex,
               p.Json_Str as jsonStr,
               p.Xml_Str as xmlStr,
               p.Result as result,
               p.Msg as msg,
               p.Type as type,
               p.Reference_Icd_Id as referenceIcdId,
               ref.name as referenceIcdName,
               case when p.Icd is null then false else true end as hasIcdFile,
               p.create_by as createBy,
               p.create_time as createTime,
               p.update_by as updateBy,
               p.update_time as updateTime
        from pq_icd_path p
                 left join pq_icd_path ref on p.Reference_Icd_Id = ref.id
            and ref.state = 1
            and ref.Type in (1, 3)
        where p.id = #{id}
          and p.state = 1
    </select>

    <select id="selectExportById" resultType="com.njcn.gather.icd.pojo.po.PqIcdPath">
        select id,
               name,
               state,
               Json_Str as jsonStr,
               Xml_Str as xmlStr,
               Icd as icd
        from pq_icd_path
        where id = #{id}
          and state = 1
    </select>

    <select id="selectExportJsonByIds" resultType="com.njcn.gather.icd.pojo.vo.PqIcdExportJsonVO">
        select id,
        name,
        Icd as rawIcd,
        angle,
        use_phase_index as usePhaseIndex,
        Json_Str as jsonStr,
        Xml_Str as xmlStr,
        Result as result,
        Msg as msg,
        Type as type,
        Reference_Icd_Id as referenceIcdId
        from pq_icd_path
        where state = 1
        and id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>

