<?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.grid.GridRegionUserMapper">
    <resultMap id="BaseResultMap" type="com.ruoyi.system.domain.grid.GridRegionUser">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="wg_id" jdbcType="VARCHAR" property="wgId"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="is_valid" jdbcType="VARCHAR" property="isValid"/>
        <result column="ylzd1" jdbcType="VARCHAR" property="ylzd1"/>
        <result column="ylzd2" jdbcType="VARCHAR" property="ylzd2"/>
        <result column="ylzd3" jdbcType="VARCHAR" property="ylzd3"/>
        <result column="ylzd4" jdbcType="VARCHAR" property="ylzd4"/>
        <result column="ylzd5" jdbcType="VARCHAR" property="ylzd5"/>
        <result column="create_time" jdbcType="VARCHAR" property="createTime"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_time" jdbcType="VARCHAR" property="updateTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
    </resultMap>
    <sql id="Example_Where_Clause">
        <where>
            <foreach collection="oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" prefixOverrides="and" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach close=")" collection="criterion.value" item="listItem" open="("
                                             separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Update_By_Example_Where_Clause">
        <where>
            <foreach collection="example.oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" prefixOverrides="and" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach close=")" collection="criterion.value" item="listItem" open="("
                                             separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Base_Column_List">
        id, wg_id, user_id, is_valid, ylzd1, ylzd2, ylzd3, ylzd4, ylzd5, create_time,
    create_by, update_time, update_by
    </sql>
    <select id="selectByExample" parameterType="com.ruoyi.system.domain.grid.GridRegionUserExample"
            resultMap="BaseResultMap">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from grid_region_user
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>

    <select id="selectOneGridUserByWgCode" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from grid_region_user
        WHERE wg_id=#{wgCode,jdbcType=VARCHAR} and is_valid = '1' AND user_id IN
        (SELECT user_id FROM sys_user_role WHERE role_id IN
        (select role_id FROM sys_role where role_key='centerman' or role_key='onegrid'))
        order by create_time
    </select>
    <select id="selectTwoGridUserByWgCode" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from grid_region_user
        WHERE wg_id like concat(#{wgCode},'%') and is_valid = '1' AND user_id IN
        (SELECT user_id FROM sys_user_role WHERE role_id IN
        (select role_id FROM sys_role where role_key='twogrid'))
    </select>
    <select id="selectThreeGirdUserByWgCode" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from grid_region_user
        WHERE wg_id like concat(#{wgCode},'%') and is_valid = '1' AND user_id IN
        (SELECT user_id FROM sys_user_role WHERE role_id IN
        (select role_id FROM sys_role where role_key='threegird'))
    </select>

    <select id="selectParentGirdUserByWgCode" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from grid_region_user
        WHERE is_valid = '1' and
        wg_id = (SELECT wg_parent_id from grid_region where wg_code=#{wgCode} and is_valid = '1');
    </select>

    <select id="selectChildGirdUserByWgCode" resultType="com.ruoyi.system.domain.grid.GridRegionUser">
        select
        <include refid="Base_Column_List"/>
        from grid_region_user
        WHERE is_valid = '1' and
        wg_id in (SELECT wg_code from grid_region where wg_code like concat(#{wgCode},'%') and is_valid = '1' and (level = '2' or level = '3'));
    </select>

    <select id="selectRandom" resultType="com.ruoyi.system.domain.grid.GridRegionUser" parameterType="int">
        select
        <include refid="Base_Column_List"/>
        from grid_region_user where is_valid = 1  ORDER BY RAND() LIMIT #{userNum,jdbcType=INTEGER}
    </select>

    <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from grid_region_user
        where id = #{id,jdbcType=VARCHAR}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
        delete
        from grid_region_user
        where id = #{id,jdbcType=VARCHAR}
    </delete>
    <delete id="deleteByExample" parameterType="com.ruoyi.system.domain.grid.GridRegionUserExample">
        delete from grid_region_user
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </delete>
    <insert id="insert" parameterType="com.ruoyi.system.domain.grid.GridRegionUser">
        insert into grid_region_user (id, wg_id,
                                      user_id, is_valid, ylzd1,
                                      ylzd2, ylzd3, ylzd4,
                                      ylzd5, create_time, create_by,
                                      update_time, update_by)
        values (#{id,jdbcType=VARCHAR},  #{wgId,jdbcType=VARCHAR},
                #{userId,jdbcType=VARCHAR}, #{isValid,jdbcType=VARCHAR}, #{ylzd1,jdbcType=VARCHAR},
                #{ylzd2,jdbcType=VARCHAR}, #{ylzd3,jdbcType=VARCHAR}, #{ylzd4,jdbcType=VARCHAR},
                #{ylzd5,jdbcType=VARCHAR}, #{createTime,jdbcType=VARCHAR}, #{createBy,jdbcType=VARCHAR},
                #{updateTime,jdbcType=VARCHAR}, #{updateBy,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" parameterType="com.ruoyi.system.domain.grid.GridRegionUser">
        insert into grid_region_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="wgId != null">
                wg_id,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="isValid != null">
                is_valid,
            </if>
            <if test="ylzd1 != null">
                ylzd1,
            </if>
            <if test="ylzd2 != null">
                ylzd2,
            </if>
            <if test="ylzd3 != null">
                ylzd3,
            </if>
            <if test="ylzd4 != null">
                ylzd4,
            </if>
            <if test="ylzd5 != null">
                ylzd5,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=VARCHAR},
            </if>
            <if test="wgId != null">
                #{wgId,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=VARCHAR},
            </if>
            <if test="isValid != null">
                #{isValid,jdbcType=VARCHAR},
            </if>
            <if test="ylzd1 != null">
                #{ylzd1,jdbcType=VARCHAR},
            </if>
            <if test="ylzd2 != null">
                #{ylzd2,jdbcType=VARCHAR},
            </if>
            <if test="ylzd3 != null">
                #{ylzd3,jdbcType=VARCHAR},
            </if>
            <if test="ylzd4 != null">
                #{ylzd4,jdbcType=VARCHAR},
            </if>
            <if test="ylzd5 != null">
                #{ylzd5,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=VARCHAR},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <select id="countByExample" parameterType="com.ruoyi.system.domain.grid.GridRegionUserExample"
            resultType="java.lang.Long">
        select count(*) from grid_region_user
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        update grid_region_user
        <set>
            <if test="record.id != null">
                id = #{record.id,jdbcType=VARCHAR},
            </if>
            <if test="record.wgId != null">
                wg_id = #{record.wgId,jdbcType=VARCHAR},
            </if>
            <if test="record.userId != null">
                user_id = #{record.userId,jdbcType=VARCHAR},
            </if>
            <if test="record.isValid != null">
                is_valid = #{record.isValid,jdbcType=VARCHAR},
            </if>
            <if test="record.ylzd1 != null">
                ylzd1 = #{record.ylzd1,jdbcType=VARCHAR},
            </if>
            <if test="record.ylzd2 != null">
                ylzd2 = #{record.ylzd2,jdbcType=VARCHAR},
            </if>
            <if test="record.ylzd3 != null">
                ylzd3 = #{record.ylzd3,jdbcType=VARCHAR},
            </if>
            <if test="record.ylzd4 != null">
                ylzd4 = #{record.ylzd4,jdbcType=VARCHAR},
            </if>
            <if test="record.ylzd5 != null">
                ylzd5 = #{record.ylzd5,jdbcType=VARCHAR},
            </if>
            <if test="record.createTime != null">
                create_time = #{record.createTime,jdbcType=VARCHAR},
            </if>
            <if test="record.createBy != null">
                create_by = #{record.createBy,jdbcType=VARCHAR},
            </if>
            <if test="record.updateTime != null">
                update_time = #{record.updateTime,jdbcType=VARCHAR},
            </if>
            <if test="record.updateBy != null">
                update_by = #{record.updateBy,jdbcType=VARCHAR},
            </if>
        </set>
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByExample" parameterType="map">
        update grid_region_user
        set id = #{record.id,jdbcType=VARCHAR},
        wg_id = #{record.wgId,jdbcType=VARCHAR},
        user_id = #{record.userId,jdbcType=VARCHAR},
        is_valid = #{record.isValid,jdbcType=VARCHAR},
        ylzd1 = #{record.ylzd1,jdbcType=VARCHAR},
        ylzd2 = #{record.ylzd2,jdbcType=VARCHAR},
        ylzd3 = #{record.ylzd3,jdbcType=VARCHAR},
        ylzd4 = #{record.ylzd4,jdbcType=VARCHAR},
        ylzd5 = #{record.ylzd5,jdbcType=VARCHAR},
        create_time = #{record.createTime,jdbcType=VARCHAR},
        create_by = #{record.createBy,jdbcType=VARCHAR},
        update_time = #{record.updateTime,jdbcType=VARCHAR},
        update_by = #{record.updateBy,jdbcType=VARCHAR}
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByPrimaryKeySelective" parameterType="com.ruoyi.system.domain.grid.GridRegionUser">
        update grid_region_user
        <set>
            <if test="wgId != null">
                wg_id = #{wgId,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                user_id = #{userId,jdbcType=VARCHAR},
            </if>
            <if test="isValid != null">
                is_valid = #{isValid,jdbcType=VARCHAR},
            </if>
            <if test="ylzd1 != null">
                ylzd1 = #{ylzd1,jdbcType=VARCHAR},
            </if>
            <if test="ylzd2 != null">
                ylzd2 = #{ylzd2,jdbcType=VARCHAR},
            </if>
            <if test="ylzd3 != null">
                ylzd3 = #{ylzd3,jdbcType=VARCHAR},
            </if>
            <if test="ylzd4 != null">
                ylzd4 = #{ylzd4,jdbcType=VARCHAR},
            </if>
            <if test="ylzd5 != null">
                ylzd5 = #{ylzd5,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=VARCHAR},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=VARCHAR}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ruoyi.system.domain.grid.GridRegionUser">
        update grid_region_user
        set
            wg_id       = #{wgId,jdbcType=VARCHAR},
            user_id     = #{userId,jdbcType=VARCHAR},
            is_valid    = #{isValid,jdbcType=VARCHAR},
            ylzd1       = #{ylzd1,jdbcType=VARCHAR},
            ylzd2       = #{ylzd2,jdbcType=VARCHAR},
            ylzd3       = #{ylzd3,jdbcType=VARCHAR},
            ylzd4       = #{ylzd4,jdbcType=VARCHAR},
            ylzd5       = #{ylzd5,jdbcType=VARCHAR},
            create_time = #{createTime,jdbcType=VARCHAR},
            create_by   = #{createBy,jdbcType=VARCHAR},
            update_time = #{updateTime,jdbcType=VARCHAR},
            update_by   = #{updateBy,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>
    <update id="batchUpdateStatus">
        update grid_region_user set is_valid = #{status} where user_id = #{userId}
    </update>
</mapper>
