<?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.dgtly.system.mapper.SysUserShopExtMapper">

    <resultMap type="SysUserShopExt" id="SysUserShopExtResult">
        <result property="userId"    column="user_id"    />
        <result property="loginName"    column="login_name"    />
        <result property="shopCode"    column="shop_code"    />
        <result property="shopName"    column="shop_name"    />
        <result property="isShopType"    column="is_shop_type"    />
        <result property="subofficeCode"    column="subOffice_code"    />
        <result property="subofficeName"    column="subOffice_name"    />
        <result property="userUpdateTime"    column="user_update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="delTime"    column="del_time"    />
        <result property="constraintUserId"    column="constraint_user_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectSysUserShopExtVo">
        select user_id, login_name, shop_code, shop_name, is_shop_type, subOffice_code, subOffice_name, user_update_time, del_flag, del_time, create_time,constraint_user_id from sys_user_shop_ext
    </sql>

    <select id="selectSysUserShopExtList" parameterType="SysUserShopExt" resultMap="SysUserShopExtResult">
        <include refid="selectSysUserShopExtVo"/>
        <where>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="loginName != null  and loginName != ''"> and login_name like concat('%', #{loginName}, '%')</if>
            <if test="shopCode != null  and shopCode != ''"> and shop_code = #{shopCode}</if>
            <if test="shopName != null  and shopName != ''"> and shop_name like concat('%', #{shopName}, '%')</if>
            <if test="isShopType != null  and isShopType != ''"> and is_shop_type = #{isShopType}</if>
            <if test="subofficeCode != null  and subofficeCode != ''"> and subOffice_code = #{subofficeCode}</if>
            <if test="subofficeName != null  and subofficeName != ''"> and subOffice_name like concat('%', #{subofficeName}, '%')</if>
            <if test="userUpdateTime != null  and userUpdateTime != ''"> and user_update_time = #{userUpdateTime}</if>
            <if test="delTime != null "> and del_time = #{delTime}</if>
            <if test="constraintUserId != null and constraintUserId !='' "> and constraint_user_id = #{constraintUserId}</if>
        </where>
    </select>

    <select id="selectSysUserShopExtById" parameterType="String" resultMap="SysUserShopExtResult">
        <include refid="selectSysUserShopExtVo"/>
        where user_id = #{userId} and delFlag='0'
    </select>

    <insert id="insertSysUserShopExt" parameterType="SysUserShopExt">
        insert into sys_user_shop_ext
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null  and userId != ''">user_id,</if>
            <if test="loginName != null  and loginName != ''">login_name,</if>
            <if test="shopCode != null  and shopCode != ''">shop_code,</if>
            <if test="shopName != null  and shopName != ''">shop_name,</if>
            <if test="isShopType != null  and isShopType != ''">is_shop_type,</if>
            <if test="subofficeCode != null  and subofficeCode != ''">subOffice_code,</if>
            <if test="subofficeName != null  and subofficeName != ''">subOffice_name,</if>
            <if test="userUpdateTime != null  and userUpdateTime != ''">user_update_time,</if>
            <if test="delFlag != null  and delFlag != ''">del_flag,</if>
            <if test="delTime != null ">del_time,</if>
            <if test="constraintUserId != null ">constraint_user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null  and userId != ''">#{userId},</if>
            <if test="loginName != null  and loginName != ''">#{loginName},</if>
            <if test="shopCode != null  and shopCode != ''">#{shopCode},</if>
            <if test="shopName != null  and shopName != ''">#{shopName},</if>
            <if test="isShopType != null  and isShopType != ''">#{isShopType},</if>
            <if test="subofficeCode != null  and subofficeCode != ''">#{subofficeCode},</if>
            <if test="subofficeName != null  and subofficeName != ''">#{subofficeName},</if>
            <if test="userUpdateTime != null  and userUpdateTime != ''">#{userUpdateTime},</if>
            <if test="delFlag != null  and delFlag != ''">#{delFlag},</if>
            <if test="delTime != null ">#{delTime},</if>
            <if test="constraintUserId != null ">#{constraintUserId},</if>
         </trim>
    </insert>

    <update id="updateSysUserShopExt" parameterType="SysUserShopExt">
        update sys_user_shop_ext
        <trim prefix="SET" suffixOverrides=",">
            <if test="loginName != null  and loginName != ''">login_name = #{loginName},</if>
            <if test="shopCode != null  and shopCode != ''">shop_code = #{shopCode},</if>
            <if test="shopName != null  and shopName != ''">shop_name = #{shopName},</if>
            <if test="isShopType != null  and isShopType != ''">is_shop_type = #{isShopType},</if>
            <if test="subofficeCode != null  and subofficeCode != ''">subOffice_code = #{subofficeCode},</if>
            <if test="subofficeName != null  and subofficeName != ''">subOffice_name = #{subofficeName},</if>
            <if test="userUpdateTime != null  and userUpdateTime != ''">user_update_time = #{userUpdateTime},</if>
            <if test="delFlag != null  and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="delTime != null ">del_time = #{delTime},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteSysUserShopExtById" parameterType="String">
        delete from sys_user_shop_ext where user_id = #{userId}
    </delete>

    <delete id="deleteSysUserShopExtByIds" parameterType="String">
        delete from sys_user_shop_ext where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <select id="searchRunTime" resultType="java.lang.String">
        select max(user_update_time) from sys_user_shop_ext where is_shop_type =#{isShopType}
    </select>

    <update id="deleteUserShopExt" >
        update sys_user_shop_ext set del_flag = 2, del_time = now()
           where user_id =#{userId} and login_name = #{loginName} and shop_code = #{shopCode} and  is_shop_type = #{isShopType}
    </update>

    <delete id="deletePhysicsUserShopExt">
        delete from sys_user_shop_ext where user_id =#{userId} and login_name = #{loginName} and shop_code = #{shopCode} and  is_shop_type = #{isShopType}
    </delete>

    <select id="selectLoginNameByUserId" resultType="java.lang.String">
        select login_name from sys_user_shop_ext
          where constraint_user_id = #{dataUserId} and shop_code = #{shopCode} and  is_shop_type = #{isShopType} limit 1
    </select>

</mapper>
