<?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.system.mapper.UserRelateMapper">
    
    <resultMap type="UserRelate" id="UserRelateResult">
        <result property="relateId"    column="relate_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="loginName"    column="login_name"    />
        <result property="danganId"    column="dangan_id"    />
        <result property="danganName"    column="dangan_name"    />
        <result property="danganLoginName"    column="dangan_login_name"    />
        <result property="status"    column="status"    />
        <result property="relateTime"    column="relate_time"    />
        <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"    />

        <result property="danganNums"    column="dangan_nums"    />


        <result property="danganUrl"    column="dangan_url"    />
        <result property="danganStatus"    column="dangan_status"    />

    </resultMap>

    <sql id="selectUserRelateVo">
        select ur.relate_id, ur.user_id,
         u.user_name, ur.login_name,
          d.page_address as dangan_url ,
       d.dangan_no  as  dangan_nums,
         ur.dangan_id, d.dangan_name, ur.dangan_login_name,
         d.status  as dangan_status  ,
         ur.status,
         ur.relate_time, ur.create_by, ur.create_time, ur.update_by, ur.update_time, ur.remark
        from sys_user_relate ur
        left join sys_user u on u.user_id=ur.user_id
        left join sys_dangan d on d.dangan_id = ur.dangan_id
    </sql>

    <select id="selectUserRelateList" parameterType="UserRelate" resultMap="UserRelateResult">
        <include refid="selectUserRelateVo"/>
        <where>  
            <if test="userId != null "> and ur.user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and u.user_name like concat('%', #{userName}, '%')</if>
            <if test="loginName != null  and loginName != ''"> and ur.login_name like concat('%', #{loginName}, '%')</if>
            <if test="danganId != null "> and ur.dangan_id = #{danganId}</if>
            <if test="danganNums != null  and danganNums != ''"> and d.dangan_no = #{danganNums}</if>

            <if test="danganName != null  and danganName != ''"> and d.dangan_name like concat('%', #{danganName}, '%')</if>
            <if test="danganLoginName != null  and danganLoginName != ''"> and ur.dangan_login_name like concat('%', #{danganLoginName}, '%')</if>
            <if test="status != null  and status != ''"> and ur.status = #{status}</if>
            <if test="relateTime != null "> and ur.relate_time = #{relateTime}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND to_date(ur.relate_time,'YY-MM-DD') &gt;= to_date(#{params.beginTime},'YY-MM-DD')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND to_date(ur.relate_time,'YY-MM-DD') &lt;= to_date(#{params.endTime},'YY-MM-DD')
            </if>
        </where>
    </select>
    
    <select id="selectUserRelateById" parameterType="Long" resultMap="UserRelateResult">
        <include refid="selectUserRelateVo"/>
        where ur.relate_id = #{relateId}
    </select>
        
    <insert id="insertUserRelate" parameterType="UserRelate" useGeneratedKeys="true" keyProperty="relateId">
        insert into sys_user_relate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="loginName != null">login_name,</if>
            <if test="danganId != null">dangan_id,</if>
            <if test="danganName != null">dangan_name,</if>
            <if test="danganLoginName != null">dangan_login_name,</if>
            <if test="status != null">status,</if>
            <if test="relateTime != null">relate_time,</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="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="loginName != null">#{loginName},</if>
            <if test="danganId != null">#{danganId},</if>
            <if test="danganName != null">#{danganName},</if>
            <if test="danganLoginName != null">#{danganLoginName},</if>
            <if test="status != null">#{status},</if>
            <if test="relateTime != null">#{relateTime},</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="updateUserRelate" parameterType="UserRelate">
        update sys_user_relate
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="loginName != null">login_name = #{loginName},</if>
            <if test="danganId != null">dangan_id = #{danganId},</if>
            <if test="danganName != null">dangan_name = #{danganName},</if>
            <if test="danganLoginName != null">dangan_login_name = #{danganLoginName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="relateTime != null">relate_time = #{relateTime},</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 relate_id = #{relateId}
    </update>

    <delete id="deleteUserRelateById" parameterType="Long">
        delete from sys_user_relate where relate_id = #{relateId}
    </delete>

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

    <insert id="batchUserRelate">
        insert into sys_user_relate (user_id,user_name,login_name,dangan_id,dangan_name,dangan_login_name,status,relate_time,create_by,create_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId},#{item.userName},#{item.loginName},#{item.danganId},#{item.danganName},#{item.danganLoginName},#{item.status},#{item.relateTime},#{item.createBy},#{item.createTime})
        </foreach>
    </insert>
</mapper>