<?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.ss.risk.mapper.RiskMapper">

    <resultMap type="RiskVO" id="RiskResult" autoMapping="true">
        <result column="type" property="type" typeHandler="com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler"/>
        <result column="submit_type" property="submitType" typeHandler="com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler"/>
    </resultMap>

    <sql id="isFinished">
        (sr.end_time is not null and sr.end_time &lt;= now())
    </sql>

    <sql id="selectRiskVo">
        select
            sr.risk_id,
            sr.user_id,
            sr.type,
            sr.reason,
            sr.end_time,
            sr.create_time,
            sr.unseal_self,
            sr.submit_type,
            sr.video_words,
            <include refid="isFinished"/> as is_finished,
            if(su.is_real, su.real_name, su.user_name) as user_name
        from ss_risk sr
            left join sm_user su on su.user_id = sr.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.riskId != null "> and sr.risk_id = #{query.riskId}</if>
        <if test="query.userId != null "> and sr.user_id = #{query.userId}</if>
        <if test="query.type != null  and query.type != ''"> and sr.type = #{query.type}</if>
        <if test="query.containsType != null  and query.containsType != ''">
            and find_in_set(#{query.containsType}, sr.type)
        </if>
        <if test="query.reason != null  and query.reason != ''"> and sr.reason like concat('%', #{query.reason}, '%')</if>
        <if test="query.userName != null  and query.userName != ''">
         and if(su.is_real, su.real_name, su.user_name) like concat('%', #{query.userName}, '%')
         </if>
        <if test="query.unsealSelf != null "> and unseal_self = #{query.unsealSelf}</if>
        <if test="query.videoWords != null  and query.videoWords != ''"> and video_words like concat('%', #{query.videoWords}, '%')</if>
        <if test="query.isFinished != null">
            <if test="query.isFinished">and <include refid="isFinished"/></if>
            <if test="!query.isFinished">and !<include refid="isFinished"/></if>
        </if>
    </sql>

    <select id="selectRiskList" parameterType="RiskQuery" resultMap="RiskResult">
        <include refid="selectRiskVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectRiskByRiskId" parameterType="Long" resultMap="RiskResult">
        <include refid="selectRiskVo"/>
        where risk_id = #{riskId}
    </select>

    <insert id="insertRisk" parameterType="Risk" useGeneratedKeys="true" keyProperty="riskId">
        insert into ss_risk
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="type != null and type.size() > 0">type,</if>
            <if test="reason != null">reason,</if>
            <if test="endTime != null">end_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="unsealSelf != null">unseal_self,</if>
            <if test="submitType != null">submit_type,</if>
            <if test="videoWords != null">video_words,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="type != null and type.size() > 0">#{type,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
            <if test="reason != null">#{reason},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="unsealSelf != null">#{unsealSelf},</if>
            <if test="submitType != null">#{submitType,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
            <if test="videoWords != null">#{videoWords},</if>
         </trim>
    </insert>

    <update id="updateRisk" parameterType="Risk">
        update ss_risk
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where risk_id = #{data.riskId}
    </update>

    <update id="batchSetRealName">
        update ss_risk
        set real_name_id = #{realNameId}
        where risk_id in
        <foreach item="item" collection="riskIds" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.type != null and data.type.size() > 0">type = #{data.type,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
        <if test="data.reason != null">reason = #{data.reason},</if>
        <if test="data.endTime != null">end_time = #{data.endTime},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.unsealSelf != null">unseal_self = #{data.unsealSelf},</if>
        <if test="data.submitType != null">submit_type = #{data.submitType,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
        <if test="data.videoWords != null">video_words = #{data.videoWords},</if>
    </sql>

    <delete id="deleteRiskByRiskId" parameterType="Long">
        delete from ss_risk where risk_id = #{riskId}
    </delete>

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