<?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.PxDiaryMapper">
    <resultMap type="PxDiary" id="PxDiaryResult">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="mood" column="mood"/>
        <result property="weather" column="weather"/>
        <result property="content" column="content"/>
        <result property="date" column="date"/>
        <result property="richText" column="rich_text"/>
        <result property="delFlag" column="del_flag"/>
        <result property="version" column="version"/>
        <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="selectPxDiaryVo">
        select id,
               title,
               mood,
               weather, `date`, content, rich_text, del_flag, version, create_by, create_time, update_by, update_time, remark
        from px_diary
    </sql>

    <select id="selectPxDiaryList" parameterType="PxDiary" resultMap="PxDiaryResult">
        <include refid="selectPxDiaryVo"/>
        <where>
            <if test="title != null and title != ''">
                and title = #{title}
            </if>
            <if test="mood != null and mood != ''">
                and mood = #{mood}
            </if>
            <if test="weather != null and weather != ''">
                and weather = #{weather}
            </if>
            <if test="content != null and content != ''">
                and content = #{content}
            </if>
            <if test="richText != null and richText != ''">
                and rich_text = #{richText}
            </if>
            <if test="version != null and version != ''">
                and version = #{version}
            </if>
            <if test="date != null">
                and 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')
                )
            </if>
            and del_flag = 0
        </where>
        order by `date` desc
    </select>

    <select id="selectPxDiaryById" parameterType="Long"
            resultMap="PxDiaryResult">
        <include refid="selectPxDiaryVo"/>
        where id = #{id}
    </select>

    <!--全局检索日记-->
    <select id="retrieval" parameterType="string"
            resultMap="PxDiaryResult">
        <include refid="selectPxDiaryVo"/>
        where title like concat('%', #{searchCode}, '%')
        or content like concat('%', #{searchCode}, '%')
        or rich_text like concat('%', #{searchCode}, '%')
    </select>

    <insert id="insertPxDiary" parameterType="PxDiary" useGeneratedKeys="true"
            keyProperty="id">
        insert into px_diary
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,
            </if>
            <if test="mood != null">mood,
            </if>
            <if test="weather != null">weather,
            </if>
            <if test="content != null">content,
            </if>
            <if test="date != null">date,
            </if>
            <if test="richText != null">rich_text,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="version != null">version,
            </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="title != null">#{title},
            </if>
            <if test="mood != null">#{mood},
            </if>
            <if test="weather != null">#{weather},
            </if>
            <if test="content != null">#{content},
            </if>
            <if test="date != null">#{date},
            </if>
            <if test="richText != null">#{richText},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="version != null">#{version},
            </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="updatePxDiary" parameterType="PxDiary">
        update px_diary
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title =
                #{title},
            </if>
            <if test="mood != null">mood =
                #{mood},
            </if>
            <if test="weather != null">weather =
                #{weather},
            </if>
            <if test="content != null">content =
                #{content},
            </if>
            <if test="date != null">date =
                #{date},
            </if>
            <if test="richText != null">rich_text =
                #{richText},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="version != null">version =
                #{version},
            </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="deletePxDiaryById" parameterType="Long">
        delete
        from px_diary
        where id = #{id}
    </delete>

    <delete id="deletePxDiaryByIds" parameterType="String">
        delete from px_diary where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
