<?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.pnkx.mapper.PxMenstruationRecordMapper">

    <resultMap type="PxMenstruationRecord" id="PxMenstruationRecordResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="date" column="date"/>
        <result property="type" column="type"/>
        <result property="mood" column="mood"/>
        <result property="makeLove" column="make_love"/>
        <result property="temperature" column="temperature"/>
        <result property="weight" column="weight"/>
        <result property="version" column="version"/>
        <result property="state" column="state"/>
        <result property="items" column="items"/>
        <result property="results" column="results"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectPxMenstruationRecordVo">
        select id,
               user_id, `date`, `type`, mood, make_love, state, items, results, temperature, weight, version, create_by, create_time, update_by, update_time, remark
        from px_menstruation_record
    </sql>

    <select id="getPxMenstruationRecordList" parameterType="PxMenstruationRecord" resultMap="PxMenstruationRecordResult">
        <include refid="selectPxMenstruationRecordVo"/>
        <where>
            <if test="date != null ">
                and `date` = #{date}
            </if>
            <if test="type != null  and type != ''">
                and `type` = #{type}
            </if>
            <if test="items != null  and items != ''">
                and items like concat('%', #{items}, '%')
            </if>
            <if test="results != null  and results != ''">
                and results like concat('%', #{results}, '%')
            </if>
           <if test="state != null  and state != ''">
                and `state` = #{state}
            </if>
            <if test="mood != null  and mood != ''">
                and mood = #{mood}
            </if>
            <if test="makeLove != null ">
                and make_love = #{makeLove}
            </if>
            <if test="temperature != null ">
                and temperature = #{temperature}
            </if>
            <if test="weight != null ">
                and weight = #{weight}
            </if>
            <if test="version != null  and version != ''">
                and version = #{version}
            </if>
        </where>
        order by `date` desc
    </select>

    <select id="selectPxMenstruationRecordList" parameterType="PxMenstruationRecord"
            resultMap="PxMenstruationRecordResult">
        <include refid="selectPxMenstruationRecordVo"/>
        where date_format(date,'%Y-%m') in ( date_format(#{date},'%Y-%m'),
        date_format(DATE_SUB(#{date}, INTERVAL 1 MONTH),'%Y-%m'),
        date_format(DATE_SUB(#{date}, INTERVAL -1 MONTH),'%Y-%m')
        )
        and del_flag = 0
        order by date desc
    </select>

    <select id="selectPxMenstruationRecordById" parameterType="Long" resultMap="PxMenstruationRecordResult">
        <include refid="selectPxMenstruationRecordVo"/>
        where id = #{id}
    </select>

    <!--APP首页获取姨妈提醒列表-->
    <select id="selectMenstruationRecordList"
            resultType="com.pnkx.domain.po.PxMenstruationRecord">
        <include refid="selectPxMenstruationRecordVo"/>
         where type = 1 or type = 0 order by date desc limit 2
    </select>

    <!-- 获取最后一次姨妈开始的记录 -->
    <select id="getLastStartDate" resultType="com.pnkx.domain.po.PxMenstruationRecord">
        <include refid="selectPxMenstruationRecordVo"/>
        where type = 0 order by date desc limit 1
    </select>

    <insert id="insertPxMenstruationRecord" parameterType="PxMenstruationRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into px_menstruation_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="date != null">`date`,</if>
            <if test="type != null">`type`,</if>
            <if test="mood != null">mood,</if>
            <if test="makeLove != null">make_love,</if>
            <if test="temperature != null">temperature,</if>
            <if test="weight != null">weight,</if>
            <if test="version != null">version,</if>
            <if test="state != null">`state`,</if>
            <if test="items != null">items,</if>
            <if test="results != null">results,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="date != null">#{date},</if>
            <if test="type != null">#{type},</if>
            <if test="mood != null">#{mood},</if>
            <if test="makeLove != null">#{makeLove},</if>
            <if test="temperature != null">#{temperature},</if>
            <if test="weight != null">#{weight},</if>
            <if test="version != null">#{version},</if>
            <if test="state != null">#{state},</if>
            <if test="items != null">#{items},</if>
            <if test="results != null">#{results},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updatePxMenstruationRecord" parameterType="PxMenstruationRecord">
        update px_menstruation_record
        <trim prefix="SET" suffixOverrides=",">
            type = #{type},
            <if test="userId != null">user_id = #{userId},</if>
            <if test="date != null">`date` = #{date},</if>
            <if test="mood != null">mood = #{mood},</if>
            <if test="makeLove != null">make_love = #{makeLove},</if>
            <if test="temperature != null">temperature = #{temperature},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="version != null">version = #{version},</if>
            <if test="state != null">`state` = #{state},</if>
            <if test="items != null">items = #{items},</if>
            <if test="results != null">results = #{results},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePxMenstruationRecordById" parameterType="Long">
        update px_menstruation_record
        set del_flag = 1
        where id = #{id}
    </delete>

    <delete id="deletePxMenstruationRecordByIds" parameterType="String">
        update px_menstruation_record set del_flag = 1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
