<?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.iformall.mapper.MallUserInfoMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.MallUserInfo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="is_admin" jdbcType="INTEGER" property="isAdmin"/>
        <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="bopen_id" jdbcType="VARCHAR" property="bopenId"/>
        <result column="web_open_id" jdbcType="VARCHAR" property="webOpenId"/>
        <result column="flow_permission" property="flowPermission"/>
    </resultMap>

    <sql id="allSafeColumns">
	    `id`,`tenant_id`,`username`,`name`,`password`,`create_time`,`last_login_time`,`status`,`is_admin`,`nick_name`,`phone`,`flow_permission`,`web_open_id`
	</sql>

    <sql id="allColumns">
	    `id`,`tenant_id`,`username`,`name`,`password`,`create_time`,`last_login_time`,`status`,`is_admin`,`nick_name`,`phone`,`flow_permission`,
	    `bopen_id`,`web_open_id`
	</sql>

    <sql id="dynamicWhereConditions">
        where 1 = 1
        <if test=" null != id ">
            and `id` = #{id}
        </if>

        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>

        <if test=" null != username ">
            and `username` like concat('%', #{username},'%')
        </if>

        <if test=" null != nickName ">
            and `nick_name` like concat('%', #{nickName},'%')
        </if>

        <if test=" null != name ">
            and `name` like concat('%', #{name},'%')
        </if>

        <if test=" null != password ">
            and `password` like concat('%', #{password},'%')
        </if>

        <if test=" null != createTime ">
            and `create_time` = #{createTime}
        </if>

        <if test=" null != lastLoginTime ">
            and `last_login_time` = #{lastLoginTime}
        </if>

        <if test=" null != status ">
            and `status` = #{status}
        </if>

        <if test=" null != isAdmin ">
            and `is_admin` = #{isAdmin}
        </if>
        <if test=" null != phone ">
            and `phone` = #{phone}
        </if>
        <if test=" null != bopenId ">
            and `bopen_id` = #{bopenId}
        </if>
        <if test=" null != webOpenId ">
            and `web_open_id` = #{webOpenId}
        </if>
        <if test=" null != flowPermission ">
            and JSON_CONTAINS(flow_permission->'$[*]', #{flowPermission}, '$')
        </if>

        <if test=" null != ids ">
            and id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findList" parameterType="com.iformall.domain.po.MallUserInfo" resultMap="BaseResultMap">
        select
        <include refid="allSafeColumns"/>
        from mall_user_info
        <include refid="dynamicWhereConditions"/>
    </select>

    <select id="selectByUserName" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from mall_user_info
        where 1=1
        <if test=" null != username ">
            and username=#{username}
        </if>
    </select>

    <select id="selectById" resultMap="BaseResultMap">
        select
        <include refid="allSafeColumns"/>
        from mall_user_info
        where 1=1
        <if test=" null != id ">
            and id=#{id}
        </if>
    </select>

    <select id="selectByUserNameWebOpen" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from mall_user_info
        where 1=1
        <if test=" null != username ">
            and username=#{username}
        </if>
        <if test=" null != webOpenId ">
            and `web_open_id`=#{webOpenId}
        </if>
    </select>

    <select id="selectByBOpenId" resultMap="BaseResultMap">
        select
        <include refid="allSafeColumns"/>
        from mall_user_info
        where 1=1
        <if test=" null != tenantId ">
            and `tenant_id`=#{tenantId}
        </if>
        <if test=" null != bopenId ">
            and `bopen_id`=#{bopenId}
        </if>
    </select>

    <select id="selectByWebOpenId" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from mall_user_info
        where 1=1
        <if test=" null != webOpenId ">
            and `web_open_id`=#{webOpenId}
        </if>
    </select>

    <update id="removeWebOpenId" >
        update mall_user_info
        set `web_open_id` = null
        where 1=1
        <if test=" null != tenantId ">
            and `tenant_id`=#{tenantId}
        </if>
        <if test=" null != username ">
            and username=#{username}
        </if>
        <if test=" null != id ">
            and `id` = #{id}
        </if>
    </update>

    <select id="selectByPhone" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from mall_user_info
        where 1=1
        <if test=" null != tenantId ">
            and `tenant_id`=#{tenantId}
        </if>
        <if test=" null != phone ">
            and `phone`=#{phone}
        </if>
    </select>

    <select id="hasButtonPermission" resultType="java.lang.Integer">
	    SELECT 1
	    FROM mall_user_role ur, mall_user_role_permission rp, mall_permission p
	    WHERE rp.role_id = ur.role_id
	    AND rp.permission_id = p.id
	    AND p.resource_type = 1
	    AND ur.uid = #{userId}
	    AND p.permission = #{permission}
  	</select>

    <select id="cntByUserName" resultType="java.lang.Long">
        select count(*) from mall_user_info where 1=1
        <if test=" null != username ">
            and username=#{username}
        </if>
        <if test=" null != phone ">
            and `phone` = #{phone}
        </if>
        <if test=" null != id ">
            and `id` != #{id}
        </if>
    </select>


    <resultMap id="VBaseResultMap" type="com.iformall.domain.vo.MallUserInfoVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="is_admin" jdbcType="INTEGER" property="isAdmin"/>
        <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="bopen_id" jdbcType="VARCHAR" property="bopenId"/>
        <result column="web_open_id" jdbcType="VARCHAR" property="webOpenId"/>
        <result column="flow_permission" property="flowPermission"/>

        <result column="mall_name" jdbcType="VARCHAR" property="mallName"/>
        <result column="mall_group" jdbcType="VARCHAR" property="mallGroup"/>
        <result column="img_url" jdbcType="VARCHAR" property="imgUrl"/>
        <result column="img_url_h" jdbcType="VARCHAR" property="imgUrlH"/>
    </resultMap>

    <sql id="allSafeVColumns">
        u.`id`,u.`tenant_id`,u.`username`,u.`name`,u.`password`,u.`create_time`,u.`last_login_time`,u.`status`,u.`is_admin`,u.`nick_name`,u.`phone`,u.`flow_permission`,
        m.`name` as mall_name, m.`group` as mall_group, m.`img_url`, m.`img_url_h`
    </sql>

    <sql id="allVColumns">
        u.`id`,u.`tenant_id`,u.`username`,u.`name`,u.`password`,u.`create_time`,u.`last_login_time`,u.`status`,u.`is_admin`,u.`nick_name`,u.`phone`,u.`flow_permission`,
        u.`bopen_id`,u.`web_open_id`,
        m.`name` as mall_name, m.`group` as mall_group, m.`img_url`, m.`img_url_h`
    </sql>


    <select id="selectUsersByPhone" resultMap="VBaseResultMap">
        select
        <include refid="allSafeVColumns"/>
        from mall_user_info u
        left join wx_mall m on m.`tenant_id` = u.`tenant_id`
        where 1=1
        <if test=" null != phone ">
            and u.`phone` = #{phone}
        </if>
    </select>

    <select id="selectUsersByWebOpenId" resultMap="VBaseResultMap">
        select
        <include refid="allSafeVColumns"/>
        from mall_user_info u
        left join wx_mall m on m.`tenant_id` = u.`tenant_id`
        where 1=1
        <if test=" null != webOpenId ">
            and u.`web_open_id` = #{webOpenId}
        </if>
    </select>

</mapper>
