<?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="cn.iocoder.yudao.module.inspection.mapper.InspectionNewsMapper">


    <sql id="selectInspectionNewsVo">
        select id, category, news_title, news_cover, publish_unit, news_content, type,video_url, order_num, read_num, like_num, collect_num, create_time, creator, update_time, updater from inspection_news
    </sql>

    <sql id="selectnewsByIdVo">
        select id, user_id, news_id, type1, type2 from inspection_news_association
    </sql>

    <select id="selectInspectionNewsList" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionNews" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionNews">
        <include refid="selectInspectionNewsVo"/>
        <where>
            <if test="vo.category != null  and vo.category != ''"> and category = #{vo.category}</if>
            <if test="vo.newsTitle != null  and vo.newsTitle != ''"> and news_title = #{vo.newsTitle}</if>
            <if test="vo.newsCover != null  and vo.newsCover != ''"> and news_cover = #{vo.newsCover}</if>
            <if test="vo.publishUnit != null  and vo.publishUnit != ''"> and publish_unit = #{vo.publishUnit}</if>
            <if test="vo.newsContent != null  and vo.newsContent != ''"> and news_content = #{vo.newsContent}</if>
            <if test="vo.type != null  and vo.type != ''"> and type = #{vo.type}</if>
            <if test="vo.orderNum != null "> and order_num = #{vo.orderNum}</if>
            <if test="vo.readNum != null "> and read_num = #{vo.readNum}</if>
            <if test="vo.likeNum != null "> and like_num = #{vo.likeNum}</if>
            <if test="vo.collectNum != null "> and collect_num = #{vo.collectNum}</if>
            <if test="vo.creator != null "> and creator = #{vo.creator}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectInspectionNewsById" parameterType="Long" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionNews">
        <include refid="selectInspectionNewsVo"/>
        where id = #{id}
    </select>

    <insert id="insertInspectionNews" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionNews">
        insert into inspection_news
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="category != null">category,</if>
            <if test="newsTitle != null and newsTitle != ''">news_title,</if>
            <if test="newsCover != null and newsCover != ''">news_cover,</if>
            <if test="publishUnit != null and publishUnit != ''">publish_unit,</if>
            <if test="newsContent != null and newsContent != ''">news_content,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="videoUrl != null and videoUrl != ''">video_url,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="readNum != null">read_num,</if>
            <if test="likeNum != null">like_num,</if>
            <if test="collectNum != null">collect_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="creator != null">creator,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updater != null">updater,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="category != null">#{category},</if>
            <if test="newsTitle != null and newsTitle != ''">#{newsTitle},</if>
            <if test="newsCover != null and newsCover != ''">#{newsCover},</if>
            <if test="publishUnit != null and publishUnit != ''">#{publishUnit},</if>
            <if test="newsContent != null and newsContent != ''">#{newsContent},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="videoUrl != null and videoUrl != ''">#{videoUrl},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="readNum != null">#{readNum},</if>
            <if test="likeNum != null">#{likeNum},</if>
            <if test="collectNum != null">#{collectNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="creator != null">#{creator},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updater != null">#{updater},</if>
        </trim>
    </insert>

    <update id="updateInspectionNews" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionNews">
        update inspection_news
        <trim prefix="SET" suffixOverrides=",">
            <if test="category != null">category = #{category},</if>
            <if test="newsTitle != null and newsTitle != ''">news_title = #{newsTitle},</if>
            <if test="newsCover != null and newsCover != ''">news_cover = #{newsCover},</if>
            <if test="publishUnit != null and publishUnit != ''">publish_unit = #{publishUnit},</if>
            <if test="newsContent != null and newsContent != ''">news_content = #{newsContent},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="videoUrl != null and videoUrl != ''">video_url = #{videoUrl},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="readNum != null">read_num = #{readNum},</if>
            <if test="likeNum != null">like_num = #{likeNum},</if>
            <if test="collectNum != null">collect_num = #{collectNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateInspectionNewsById" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionNews">
        update inspection_news
        <trim prefix="SET" suffixOverrides=",">
            <if test="category != null">category = #{category},</if>
            <if test="newsTitle != null and newsTitle != ''">news_title = #{newsTitle},</if>
            <if test="newsCover != null and newsCover != ''">news_cover = #{newsCover},</if>
            <if test="publishUnit != null and publishUnit != ''">publish_unit = #{publishUnit},</if>
            <if test="newsContent != null and newsContent != ''">news_content = #{newsContent},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="videoUrl != null and videoUrl != ''">video_url = #{videoUrl},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="readNum != null">read_num = read_num +1,</if>
            <if test="likeNum != null">like_num = like_num +1,</if>
            <if test="collectNum != null">collect_num = collect_num +1,</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInspectionNewsById" parameterType="Long">
        delete from inspection_news where id = #{id}
    </delete>

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

    <select id="selectnewsById" parameterType="Long" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionNews">
        <include refid="selectInspectionNewsVo"/>
        where userId = #{user_id} and id = #{news_id}
    </select>

    <update id="addReadNum">
         update inspection_news
         set read_num = read_num +1
         where id = #{id}
    </update>
    <select id="collectionNews" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionNews">
    SELECT
        news.*
    FROM
        inspection_news_association ina
        INNER JOIN inspection_news news ON ina.news_id = news.id
        WHERE ina.user_id = #{userId} and ina.type = 'collect'
        <if test="newsName != null and newsName!=''">
         and    news.news_title like concat('%',#{newsName},'%')
        </if>
        group by news.id
        order by ina.create_time desc
    </select>
    <select id="newMsgNum" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        inspection_news news
        left JOIN  inspection_news_association ina ON ina.news_id = news.id and ina.user_id = #{partnerId} and ina.type = 'read'
        WHERE (news.category = 'jcztz' or news.category = 'zflm') and  ina.id is null
    </select>
    <select id="msgList" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionNews">
        SELECT
        news.*,IF(ina.id is null,0,1) as isRead
        FROM
        inspection_news news
        left JOIN  inspection_news_association ina ON ina.news_id = news.id and ina.type = 'read'
        WHERE news.category = 'zflm'
        <if test="ifShow != null and ifShow != ''">
            or news.category = 'jcztz'
            or news.type = #{ifShow}
            or news.type = 'null'
        </if>
        group by news.id
        order by isRead,news.create_time desc
    </select>
    <select id="listGfClass" resultType="java.lang.String">
        SELECT
        publish_unit
        FROM
        inspection_news
        WHERE category = #{type}
        group by publish_unit
    </select>
</mapper>
