<?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.lymsh.yimiao.main.data.dao.MedKidsMapper">

<resultMap id="MedKidsResultMap" type="com.lymsh.yimiao.main.data.model.MedKids">
<result column="ki_id" property="id" jdbcType="VARCHAR" />
<result column="ki_name" property="name" jdbcType="VARCHAR" />
<result column="ki_sex" property="sex" jdbcType="VARCHAR" />
<result column="ki_birthday" property="birthday" jdbcType="VARCHAR" />
<result column="ki_guardianid" property="guardianId" jdbcType="VARCHAR" />
<result column="ki_currentOrganizationid" property="currentOrganizationId" jdbcType="VARCHAR" />
<result column="ki_barCode" property="barCode" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="ki_buildoper" property="buildOPer" jdbcType="VARCHAR" />
<result column="ki_buildtime" property="buildTime" jdbcType="VARCHAR" />
<result column="isdelete" property="isDelete" jdbcType="VARCHAR" />
</resultMap>

<select id="getMedKids" resultMap="MedKidsResultMap" parameterType="java.lang.String">
select ki_id,ki_name,ki_sex,ki_birthday,ki_guardianid,ki_currentOrganizationid,ki_barCode,phone,ki_buildoper, ki_buildtime,isdelete,fathername,mothername
from med_kids
WHERE ki_id = #{id,jdbcType=VARCHAR}
</select>


<sql id="orderAndLimit">
<if test="sort != null and sort != '' ">
order by ${sort} 
<if test="need != null">
limit #{offset, jdbcType=INTEGER} , #{limit, jdbcType=INTEGER}
</if>
</if>
</sql>



<sql id="MedKidsCondition">
<where>
 1 = 1
<if test="id != null and id != ''">
 and ki_id in (#{id,jdbcType=VARCHAR})
</if>
<if test="name != null and name != ''">
and ki_name = #{name,jdbcType=VARCHAR}
</if>
<if test="sex != null and sex != ''">
and ki_sex = #{sex,jdbcType=VARCHAR}
</if>
<if test="birthday != null and birthday != ''">
and ki_birthday = #{birthday,jdbcType=VARCHAR}
</if>
<if test="guardianId != null and guardianId != ''">
and ki_guardianid = #{guardianId,jdbcType=VARCHAR}
</if>
<if test="currentOrganizationId != null and currentOrganizationId != ''">
and ki_currentOrganizationid = #{currentOrganizationId,jdbcType=VARCHAR}
</if>
 <if test="barCode != null and barCode != ''">
  and ki_barCode = #{barCode,jdbcType=VARCHAR}
 </if>
 <if test="phone != null and barCode != ''">
  and phone = #{phone,jdbcType=VARCHAR}
 </if>
 <if test="buildOPer != null and buildOPer != ''">
  and ki_buildoper = #{buildOPer,jdbcType=VARCHAR}
 </if>
 <if test="buildTime != null and buildTime != ''">
  and ki_buildtime = #{buildTime,jdbcType=VARCHAR}
 </if>
 <if test="isDelete != null and isDelete != ''">
  and isdelete = #{isDelete,jdbcType=VARCHAR}
 </if>
 <if test="ids != null ">
  and ki_id in
  <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
    #{item,jdbcType=VARCHAR}
  </foreach>
 </if>
</where>
</sql>



<select id="queryMedKids" resultMap="MedKidsResultMap" parameterType="com.lymsh.yimiao.main.data.model.MedKidsQuery">
select ki_name,ki_id,ki_sex,ki_birthday,ki_guardianid,ki_currentOrganizationid,ki_barCode,phone,ki_buildoper, ki_buildtime,isdelete,fathername,mothername
 from med_kids 
<include refid="MedKidsCondition" />
<include refid="orderAndLimit" />
</select>



<select id="queryMedKidsCount" resultType="int" parameterType="com.lymsh.yimiao.main.data.model.MedKidsQuery">
select count(1) from med_kids
<include refid="MedKidsCondition" />
</select>


 <resultMap id="KidInoculationRecordMap" type="com.lymsh.yimiao.main.data.model.KidInoculationRecords">
   <result column="KI_BIRTHDAY" property="birthday" jdbcType="VARCHAR" />
   <result column="IC_KIDID" property="recordKidId" jdbcType="VARCHAR" />
   <result column="IC_ID" property="recordId" jdbcType="VARCHAR" />
   <result column="IC_VACCINEID" property="vaccineInfoId" jdbcType="VARCHAR" />
   <result column="IC_INOCULATETIME" property="inoculateTime" jdbcType="VARCHAR" />
   <result column="MONTH_AGE" property="monthAge" jdbcType="VARCHAR" />
   <result column="IC_JICI" property="jiCi" jdbcType="VARCHAR" />
   <result column="VI_NAME" property="vaccineInfoName" jdbcType="VARCHAR" />
   <result column="VN_NAME" property="vaccineNameTitle" jdbcType="VARCHAR" />
   <result column="VN_VACCINETYPE" property="vaccineType" jdbcType="VARCHAR" />
   <result column="CNT" property="cnt" jdbcType="INTEGER" />
   <result column="VN_ID" property="vaccineNameId" jdbcType="VARCHAR" />
 </resultMap>

 <select id="queryInoculationRecord" resultMap="KidInoculationRecordMap" parameterType="java.lang.String">
   SELECT D.KI_NAME,
       TO_DATE(D.KI_BIRTHDAY,'YYYY/MM/DD') AS KI_BIRTHDAY,
       B.IC_KIDID,
       B.IC_ID,
       B.IC_VACCINEID,
       TO_DATE(B.IC_INOCULATETIME,'YYYY/MM/DD') AS IC_INOCULATETIME,
       TRUNC(MONTHS_BETWEEN(TO_DATE(B.IC_INOCULATETIME,'YYYY/MM/DD'),TO_DATE(D.KI_BIRTHDAY,'YYYY/MM/DD'))) AS MONTH_AGE,
       B.IC_JICI,
       C.VI_NAME,
       A.VN_NAME,
       A.VN_ID,
       A.VN_VACCINETYPE,
       CCC.CNT
  FROM med_InoculateRecord B
    INNER JOIN med_Vaccineinfo C ON B.IC_VACCINEID=C.VI_ID
    INNER JOIN med_VaccineName A ON C.VI_VACCINENAMEID=A.VN_ID AND A.ISDELETE=1
    INNER JOIN MED_KIDS D ON B.IC_KIDID=D.KI_ID
    INNER JOIN (SELECT AA.VN_NAME,COUNT(DISTINCT b.column_value) AS CNT
                FROM med_VaccineName AA, table(splitstr(AA.VN_MONTHAGE, ',')) b
               WHERE AA.VN_MONTHAGE IS NOT NULL
                 AND AA.ISDELETE = 1
               GROUP BY AA.VN_NAME) CCC ON A.VN_NAME=CCC.VN_NAME
  WHERE B.IC_DELETE = 1
   AND B.IC_KIDID= #{recordKidId,jdbcType=VARCHAR}
   ORDER BY IC_INOCULATETIME DESC
</select>


    <select id="queryInoculationRecords" resultMap="KidInoculationRecordMap" parameterType="com.lymsh.yimiao.main.data.model.MedInoculateRecordQuery">
        SELECT D.KI_NAME,
        TO_DATE(D.KI_BIRTHDAY,'YYYY/MM/DD') AS KI_BIRTHDAY,
        B.IC_KIDID,
        B.IC_ID,
        B.IC_VACCINEID,
        TO_DATE(B.IC_INOCULATETIME,'YYYY/MM/DD') AS IC_INOCULATETIME,
        TRUNC(MONTHS_BETWEEN(TO_DATE(B.IC_INOCULATETIME,'YYYY/MM/DD'),TO_DATE(D.KI_BIRTHDAY,'YYYY/MM/DD'))) AS MONTH_AGE,
        B.IC_JICI,
        C.VI_NAME,
        A.VN_NAME,
        A.VN_ID,
        A.VN_VACCINETYPE,
        CCC.CNT
        FROM med_InoculateRecord B
        INNER JOIN med_Vaccineinfo C ON B.IC_VACCINEID=C.VI_ID AND C.ISDELETE=1
        INNER JOIN med_VaccineName A ON C.VI_VACCINENAMEID=A.VN_ID AND A.ISDELETE=1 AND A.VN_ISINPLAN = 1
        INNER JOIN MED_KIDS D ON B.IC_KIDID=D.KI_ID
        INNER JOIN (SELECT AA.VN_NAME,COUNT(DISTINCT b.column_value) AS CNT
                    FROM med_VaccineName AA, table(splitstr(AA.VN_MONTHAGE, ',')) b
                    WHERE AA.VN_MONTHAGE IS NOT NULL
                    AND AA.ISDELETE = 1
                    GROUP BY AA.VN_NAME) CCC ON A.VN_NAME=CCC.VN_NAME
        WHERE B.IC_DELETE = 1
        AND B.IC_KIDID= #{recordKidId,jdbcType=VARCHAR}
        <include refid="orderAndLimit" />
    </select>

</mapper>