<?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.ruoyi.project.mapper.TrsSiteUpdateMapper">
    
    <resultMap type="TrsSiteUpdate" id="TrsSiteUpdateResult">
        <result property="id"    column="id"    />
        <result property="record"    column="record"    />
        <result property="sitename"    column="sitename"    />
        <result property="siteurl"    column="siteurl"    />
        <result property="status"    column="status"    />
        <result property="time"    column="time"    />
        <result property="desc"    column="desc"    />
        <result property="outday"    column="outday"    />
        <result property="scantime"    column="scantime"    />
        <result property="other1"    column="other1"    />
        <result property="other2"    column="other2"    />
    </resultMap>

    <sql id="selectTrsSiteUpdateVo">
        select id, record, sitename, siteurl, status, time, `desc`, outday, scantime, other1, other2 from trs_site_update
    </sql>

    <select id="selectTrsSiteUpdateList" parameterType="TrsSiteUpdate" resultMap="TrsSiteUpdateResult">
        <include refid="selectTrsSiteUpdateVo"/>
        <where>  
            <if test="record != null "> and record = #{record}</if>
            <if test="sitename != null  and sitename != ''"> and sitename like concat('%', #{sitename}, '%')</if>
            <if test="siteurl != null  and siteurl != ''"> and siteurl = #{siteurl}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''"> and time between #{params.beginTime} and #{params.endTime}</if>
            <if test="desc != null  and desc != ''"> and `desc` = #{desc}</if>
            <if test="outday != null "> and outday = #{outday}</if>
            <if test="params.beginScantime != null and params.beginScantime != '' and params.endScantime != null and params.endScantime != ''"> and scantime between #{params.beginScantime} and #{params.endScantime}</if>
            <if test="other1 != null  and other1 != ''"> and other1 = #{other1}</if>
            <if test="other2 != null  and other2 != ''"> and other2 = #{other2}</if>
        </where>
    </select>
    
    <select id="selectTrsSiteUpdateById" parameterType="Long" resultMap="TrsSiteUpdateResult">
        <include refid="selectTrsSiteUpdateVo"/>
        where id = #{id}
    </select>

    <insert id="insertTrsSiteUpdate" parameterType="TrsSiteUpdate" useGeneratedKeys="true" keyProperty="id">
        insert into trs_site_update
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="record != null">record,</if>
            <if test="sitename != null">sitename,</if>
            <if test="siteurl != null">siteurl,</if>
            <if test="status != null">status,</if>
            <if test="time != null">time,</if>
            <if test="desc != null">`desc`,</if>
            <if test="outday != null">outday,</if>
            <if test="scantime != null">scantime,</if>
            <if test="other1 != null">other1,</if>
            <if test="other2 != null">other2,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="record != null">#{record},</if>
            <if test="sitename != null">#{sitename},</if>
            <if test="siteurl != null">#{siteurl},</if>
            <if test="status != null">#{status},</if>
            <if test="time != null">#{time},</if>
            <if test="desc != null">#{desc},</if>
            <if test="outday != null">#{outday},</if>
            <if test="scantime != null">#{scantime},</if>
            <if test="other1 != null">#{other1},</if>
            <if test="other2 != null">#{other2},</if>
         </trim>
    </insert>

    <update id="updateTrsSiteUpdate" parameterType="TrsSiteUpdate">
        update trs_site_update
        <trim prefix="SET" suffixOverrides=",">
            <if test="record != null">record = #{record},</if>
            <if test="sitename != null">sitename = #{sitename},</if>
            <if test="siteurl != null">siteurl = #{siteurl},</if>
            <if test="status != null">status = #{status},</if>
            <if test="time != null">time = #{time},</if>
            <if test="desc != null">`desc` = #{desc},</if>
            <if test="outday != null">outday = #{outday},</if>
            <if test="scantime != null">scantime = #{scantime},</if>
            <if test="other1 != null">other1 = #{other1},</if>
            <if test="other2 != null">other2 = #{other2},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTrsSiteUpdateById" parameterType="Long">
        delete from trs_site_update where id = #{id}
    </delete>

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

    <delete id="clearSiteUpdate">
        DELETE FROM trs_site_update
    </delete>

    <insert id="doMonitorInsert" >
        INSERT INTO trs_site_update (record, other1, sitename, `desc`, time, scantime, outday)
        SELECT
        record as record,
        CASE
            WHEN record = 95 THEN '今日头条'
            WHEN record = 96 THEN '短视频'
            WHEN record = 97 THEN '微博'
            WHEN record = 98 THEN '微信'
            WHEN record = 99 THEN '网站'
            ELSE record  -- 如果 msg 不在映射表中，则使用原 record 值
            END as other1,
        sitename,
        '两周1次' as `desc`,
        MAX(time) as time,
        NOW() as scantime,
        DATEDIFF(NOW(), MAX(time)) as outday
        FROM
        trs_article_info
        where sitename not in ('稀土团团', '石拐区社保中心', '石拐区劳动人事争议调处服务中心', '包头市公安局稀土高新区分局')
        GROUP BY
        sitename, record
        HAVING
        MAX(time) &lt; DATE_SUB(NOW(), INTERVAL 14 DAY)
        OR MAX(time) IS NULL
    </insert>

</mapper>