<?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.willalp.control.mapper.HsControlStatMapper">

    <resultMap type="com.willalp.control.domain.HsControlStat" id="HsControlStatResult">
        <result property="statId" column="stat_id"/>
        <result property="useNoId" column="use_no_id"/>
        <result property="dosage" column="dosage"/>
        <result property="isStat" column="is_stat"/>
        <result property="isDelete" column="is_delete"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="createTimeVo" column="createTimeVo"/>
    </resultMap>

    <sql id="selectHsControlStatVo">
        select stat_id, use_no_id, dosage, is_delete, create_by, create_time
        from hs_control_stat
    </sql>

    <select id="selectHsControlStatList" parameterType="HsControlStat" resultMap="HsControlStatResult">
        <include refid="selectHsControlStatVo"/>
        <where>
            is_delete = 0
            <if test="useNoId != null  and useNoId != ''">
                  and use_no_id = #{useNoId}
            </if>
            <if test="dosage != null  and dosage != ''">
                  and dosage = #{dosage}
            </if>
            <if test="isDelete != null ">
                  and is_delete = #{isDelete}
                        </if>
        </where>
    </select>
    <select id="getStatYearMonthDateByUseNoId" resultMap="HsControlStatResult">
        SELECT use_no_id,
               SUM(dosage)               dosage,
               is_stat,
               substr(create_time, 1, 7) createTimeVo
        FROM hs_control_stat
        WHERE use_no_id = #{useNoId}
          AND is_delete = 0
        GROUP BY substr(
                         create_time,
                         1,
                         7
                     )
        ORDER BY create_time desc
    </select>
    <select id="getStatYearMonthDateByUseNoIdAndYearMonth" resultMap="HsControlStatResult">
        SELECT use_no_id,
               dosage                    dosage,
               substr(create_time, 9, 2) createTimeVo,
               is_stat,
               create_time
        FROM hs_control_stat
        WHERE use_no_id = #{useNoId}
          AND create_time like CONCAT(#{yearMonth}, '%')
          AND is_delete = 0
        ORDER BY create_time desc
    </select>
</mapper>
