<?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.manage.mapper.BaseAreaUserMapper">

    <resultMap type="com.ruoyi.manage.domain.BaseAreaUser" id="BaseAreaUserResult">
        <result property="objid" column="objid"/>
        <result property="userId" column="user_id"/>
        <result property="areaId" column="area_id"/>
        <association property="sysUser" javaType="com.ruoyi.common.core.domain.entity.SysUser">
            <result property="userName" column="user_name"/>
        </association>
        <association property="baseStore" resultMap="com.ruoyi.manage.mapper.BaseStoreMapper.BaseStoreResult"/>
    </resultMap>

    <sql id="selectBaseAreaUserVo">
        select objid, bau.user_id, area_id, su.user_name, bs.store_name
        from base_area_user bau
                 left join sys_user su on bau.user_id = su.login_name
                 left join base_store bs on bs.store_id = area_id
    </sql>

    <select id="selectBaseAreaUserList" parameterType="BaseAreaUser" resultMap="BaseAreaUserResult">
        <include refid="selectBaseAreaUserVo"/>
        <where>
            <if test="userId != null  and userId != ''">and bau.user_id = #{userId}</if>
            <if test="areaId != null ">and area_id = #{areaId}</if>
        </where>
    </select>

    <select id="selectBaseAreaUserByObjid" parameterType="Long" resultMap="BaseAreaUserResult">
        <include refid="selectBaseAreaUserVo"/>
        where objid = #{objid}
    </select>

    <insert id="insertBaseAreaUser" parameterType="BaseAreaUser" useGeneratedKeys="true" keyProperty="objid">
        insert into base_area_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="areaId != null">area_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="areaId != null">#{areaId},</if>
        </trim>
    </insert>

    <update id="updateBaseAreaUser" parameterType="BaseAreaUser">
        update base_area_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
        </trim>
        where objid = #{objid}
    </update>

    <delete id="deleteBaseAreaUserByObjid" parameterType="Long">
        delete
        from base_area_user
        where objid = #{objid}
    </delete>

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

    <select id="selectSysUser" resultMap="com.ruoyi.system.mapper.SysUserMapper.SysUserResult">
        select login_name,
               user_name
        from sys_user
        where status = 0;
    </select>

</mapper>