<?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.bonus.canteen.core.nutrition.mapper.HealthPopularScienceMapper">
    <resultMap type="com.bonus.canteen.core.nutrition.domain.HealthPopularScience" id="HealthPopularScienceResult">
        <result property="articleId"    column="article_id"    />
        <result property="articleTitle"    column="article_title"    />
        <result property="coverPhoto"    column="cover_photo"    />
        <result property="articleType"    column="article_type"    />
        <result property="articleContent"    column="article_content"    />
        <result property="articleUrl"    column="article_url"    />
        <result property="ifVisible"    column="if_visible"    />
        <result property="hits"    column="hits"    />
        <result property="ifPushed"    column="if_pushed"    />
        <result property="pushTime"    column="push_time"    />
        <result property="summary"    column="summary"    />
        <result property="delFlag"    column="del_flag"    />
        <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="selectHealthPopularScienceVo">
        select hps.article_id, hps.article_title, hps.cover_photo, hps.article_type, hps.article_content,
               hps.article_url, hps.if_visible, hps.hits, hps.if_pushed, hps.push_time, hps.summary, hps.del_flag,
               hps.create_by, hps.create_time, hps.update_by, hps.update_time, hps.remark,
               GROUP_CONCAT(hac.chronic_name ORDER BY hac.chronic_id desc ) AS chronic_names,
               GROUP_CONCAT(hac.chronic_id ORDER BY hac.chronic_id desc ) AS chronic_ids
        from health_popular_science hps
        left join health_article_chronic hac on hps.article_id = hac.article_id
    </sql>

    <select id="selectHealthPopularScienceList" parameterType="com.bonus.canteen.core.nutrition.domain.HealthPopularScience" resultMap="HealthPopularScienceResult">
        <include refid="selectHealthPopularScienceVo"/>
        <where>
            hps.del_flag = 0
            <if test="articleId != null "> and hps.article_id = #{articleId}</if>
            <if test="articleTitle != null  and articleTitle != ''"> and hps.article_title like concat('%', #{articleTitle}, '%')</if>
            <if test="coverPhoto != null  and coverPhoto != ''"> and hps.cover_photo = #{coverPhoto}</if>
            <if test="articleType != null "> and hps.article_type = #{articleType}</if>
            <if test="articleContent != null  and articleContent != ''"> and hps.article_content = #{articleContent}</if>
            <if test="articleUrl != null  and articleUrl != ''"> and hps.article_url = #{articleUrl}</if>
            <if test="ifVisible != null "> and hps.if_visible = #{ifVisible}</if>
            <if test="hits != null "> and hps.hits = #{hits}</if>
            <if test="ifPushed != null "> and hps.if_pushed = #{ifPushed}</if>
            <if test="pushTime != null "> and hps.push_time = #{pushTime}</if>
            <if test="summary != null  and summary != ''"> and hps.summary = #{summary}</if>
            <if test="chronicIdArray != null  and chronicIdArray != ''">
                and hac.chronic_id in
                <foreach item="chronicId" collection="chronicIdArray" open="(" separator="," close=")">
                    #{chronicId}
                </foreach>
            </if>
        </where>
        GROUP BY
            hps.article_id,
            hps.article_title,
            hps.cover_photo,
            hps.article_type,
            hps.article_url,
            hps.summary,
            hps.if_visible,
            hps.if_pushed,
            hps.push_time,
            hps.hits,
            hps.create_time,
            hps.update_time
    </select>
    
    <select id="selectHealthPopularScienceById" parameterType="Long" resultMap="HealthPopularScienceResult">
        <include refid="selectHealthPopularScienceVo"/>
        where hps.article_id = #{articleId}
    </select>
        
    <insert id="insertHealthPopularScience" parameterType="com.bonus.canteen.core.nutrition.domain.HealthPopularScience" useGeneratedKeys="true" keyProperty="articleId">
        insert into health_popular_science
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="articleTitle != null and articleTitle != ''">article_title,</if>
            <if test="coverPhoto != null">cover_photo,</if>
            <if test="articleType != null">article_type,</if>
            <if test="articleContent != null">article_content,</if>
            <if test="articleUrl != null">article_url,</if>
            <if test="ifVisible != null">if_visible,</if>
            <if test="hits != null">hits,</if>
            <if test="ifPushed != null">if_pushed,</if>
            <if test="pushTime != null">push_time,</if>
            <if test="summary != null">summary,</if>
            <if test="delFlag != null">del_flag,</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="articleTitle != null and articleTitle != ''">#{articleTitle},</if>
            <if test="coverPhoto != null">#{coverPhoto},</if>
            <if test="articleType != null">#{articleType},</if>
            <if test="articleContent != null">#{articleContent},</if>
            <if test="articleUrl != null">#{articleUrl},</if>
            <if test="ifVisible != null">#{ifVisible},</if>
            <if test="hits != null">#{hits},</if>
            <if test="ifPushed != null">#{ifPushed},</if>
            <if test="pushTime != null">#{pushTime},</if>
            <if test="summary != null">#{summary},</if>
            <if test="delFlag != null">#{delFlag},</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="updateHealthPopularScience" parameterType="com.bonus.canteen.core.nutrition.domain.HealthPopularScience">
        update health_popular_science
        <trim prefix="SET" suffixOverrides=",">
            <if test="articleTitle != null and articleTitle != ''">article_title = #{articleTitle},</if>
            <if test="coverPhoto != null">cover_photo = #{coverPhoto},</if>
            <if test="articleType != null">article_type = #{articleType},</if>
            <if test="articleContent != null">article_content = #{articleContent},</if>
            <if test="articleUrl != null">article_url = #{articleUrl},</if>
            <if test="ifVisible != null">if_visible = #{ifVisible},</if>
            <if test="hits != null">hits = #{hits},</if>
            <if test="ifPushed != null">if_pushed = #{ifPushed},</if>
            <if test="pushTime != null">push_time = #{pushTime},</if>
            <if test="summary != null">summary = #{summary},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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 article_id = #{articleId}
    </update>

    <delete id="deleteHealthPopularScienceById" parameterType="Long">
        delete from health_popular_science where article_id = #{articleId}
    </delete>

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