<?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.riskInfo.mapper.RiskInfoMapper">

    <resultMap type="RiskInfoVO" id="RiskInfoResult" autoMapping="true">
        <result property="riskSubmitType" column="risk_submit_type" typeHandler="com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler"/>
        <result property="riskType" column="risk_type" typeHandler="com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler"/>
    </resultMap>

    <sql id="selectRiskInfoVo">
        select
            sri.info_id,
            sri.risk_id,
            sri.status,
            sri.real_name_id,
            sri.id_card_front,
            sri.id_card_back,
            sri.id_card_hand,
            sri.video,
            sri.duty_video,
            sri.business_licence,
            sri.verify_id,
            sri.verify_by,
            sri.verify_remark,
            sri.verify_time,
            sri.create_time,
            sri.submit_time,
            sr.user_id as user_id,
            sr.submit_type as risk_submit_type,
            sr.unseal_self as risk_unseal_self,
            sr.type as risk_type,
            sr.reason as risk_reason,
            sr.video_words as risk_video_words,
            <include refid="com.ruoyi.ss.risk.mapper.RiskMapper.isFinished"/> as risk_is_finished,
            if(su.is_real, su.real_name, su.user_name) as user_name,
            srn.name as real_name,
            srn.id_card as real_id_card,
            srn.face_image as real_face
        from ss_risk_info sri
            left join ss_risk sr on sr.risk_id = sri.risk_id
            left join sm_user su on su.user_id = sr.user_id
            left join ss_real_name srn on srn.id = sri.real_name_id
    </sql>

    <sql id="searchCondition">
        <if test="query.infoId != null "> and sri.info_id = #{query.infoId}</if>
        <if test="query.riskId != null "> and sri.risk_id = #{query.riskId}</if>
        <if test="query.status != null  and query.status != ''"> and sri.status = #{query.status}</if>
        <if test="query.realNameId != null "> and sri.real_name_id = #{query.realNameId}</if>
        <if test="query.verifyId != null "> and sri.verify_id = #{query.verifyId}</if>
        <if test="query.verifyBy != null  and query.verifyBy != ''"> and sri.verify_by like concat('%', #{query.verifyBy}, '%')</if>
        <if test="query.verifyRemark != null  and query.verifyRemark != ''"> and sri.verify_remark like concat('%', #{query.verifyRemark}, '%')</if>
        <if test="query.userId != null "> and sr.user_id = #{query.userId}</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.excludeInfoId != null">
            and sri.info_id != #{query.excludeInfoId}
        </if>
        <if test="query.statusList != null and query.statusList.size() > 0">
            and sri.status in
            <foreach collection="query.statusList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectRiskInfoList" parameterType="RiskInfoQuery" resultMap="RiskInfoResult">
        <include refid="selectRiskInfoVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectRiskInfoByInfoId" parameterType="Long" resultMap="RiskInfoResult">
        <include refid="selectRiskInfoVo"/>
        where sri.info_id = #{infoId}
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(sri.info_id)
        from ss_risk_info sri
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertRiskInfo" parameterType="RiskInfo" useGeneratedKeys="true" keyProperty="infoId">
        insert into ss_risk_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="riskId != null">risk_id,</if>
            <if test="status != null and status != ''">`status`,</if>
            <if test="realNameId != null">real_name_id,</if>
            <if test="idCardFront != null">id_card_front,</if>
            <if test="idCardBack != null">id_card_back,</if>
            <if test="idCardHand != null">id_card_hand,</if>
            <if test="video != null">video,</if>
            <if test="dutyVideo != null">duty_video,</if>
            <if test="businessLicence != null">business_licence,</if>
            <if test="verifyId != null">verify_id,</if>
            <if test="verifyBy != null">verify_by,</if>
            <if test="verifyRemark != null">verify_remark,</if>
            <if test="verifyTime != null">verify_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="submitTime != null">submit_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="riskId != null">#{riskId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="realNameId != null">#{realNameId},</if>
            <if test="idCardFront != null">#{idCardFront},</if>
            <if test="idCardBack != null">#{idCardBack},</if>
            <if test="idCardHand != null">#{idCardHand},</if>
            <if test="video != null">#{video},</if>
            <if test="dutyVideo != null">#{dutyVideo},</if>
            <if test="businessLicence != null">#{businessLicence},</if>
            <if test="verifyId != null">#{verifyId},</if>
            <if test="verifyBy != null">#{verifyBy},</if>
            <if test="verifyRemark != null">#{verifyRemark},</if>
            <if test="verifyTime != null">#{verifyTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="submitTime != null">#{submitTime},</if>
         </trim>
    </insert>

    <update id="updateRiskInfo" parameterType="RiskInfo">
        update ss_risk_info
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where info_id = #{data.infoId}
    </update>

    <update id="updateByQuery">
        update ss_risk_info sri
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <sql id="updateColumns">
        <if test="data.riskId != null">risk_id = #{data.riskId},</if>
        <if test="data.status != null and data.status != ''">`status` = #{data.status},</if>
        <if test="data.realNameId != null">real_name_id = #{data.realNameId},</if>
        <if test="data.idCardFront != null">id_card_front = #{data.idCardFront},</if>
        <if test="data.idCardBack != null">id_card_back = #{data.idCardBack},</if>
        <if test="data.idCardHand != null">id_card_hand = #{data.idCardHand},</if>
        <if test="data.video != null">video = #{data.video},</if>
        <if test="data.dutyVideo != null">duty_video = #{data.dutyVideo},</if>
        <if test="data.businessLicence != null">business_licence = #{data.businessLicence},</if>
        <if test="data.verifyId != null">verify_id = #{data.verifyId},</if>
        <if test="data.verifyBy != null">verify_by = #{data.verifyBy},</if>
        <if test="data.verifyRemark != null">verify_remark = #{data.verifyRemark},</if>
        <if test="data.verifyTime != null">verify_time = #{data.verifyTime},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.submitTime != null">submit_time = #{data.submitTime},</if>
    </sql>

    <delete id="deleteRiskInfoByInfoId" parameterType="Long">
        delete from ss_risk_info where info_id = #{infoId}
    </delete>

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