<?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.xhpc.card.mapper.IXhpcCardClientUserMapper">
    <resultMap id="BaseResultMap" type="com.xhpc.card.domain.CardClientUser">
        <id column="usersID" jdbcType="INTEGER" property="usersID"/>
        <result column="usersName" jdbcType="VARCHAR" property="usersName"/>
        <result column="usersPwd" jdbcType="VARCHAR" property="usersPwd"/>
        <result column="usersPhone" jdbcType="VARCHAR" property="usersPhone"/>
        <result column="usersEmail" jdbcType="VARCHAR" property="usersEmail"/>
        <result column="usersAdress" jdbcType="VARCHAR" property="usersAdress"/>
        <result column="usersLevel" jdbcType="INTEGER" property="usersLevel"/>
        <result column="usersCorp" jdbcType="VARCHAR" property="usersCorp"/>
        <result column="corpNo" jdbcType="VARCHAR" property="corpNo"/>
        <result column="corpName" jdbcType="VARCHAR" property="corpName"/>
        <result column="usersTime" jdbcType="DATE" property="usersTime"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="del_flag" jdbcType="INTEGER" property="delFlag"/>
    </resultMap>


    <select id="selectXhpcCardClientUserList" resultType="map">
        select
             ccu.usersID as usersID,
             ccu.usersName as usersName,
             ccu.usersLevel as usersLevel,
             ope.name as operatorName,
             ope.corp_no as corpNo,
             ccu.tenant_id as tenantId,
             ccu.usersTime as usersTime,
               ccu.status as status
        from t_iccard_client_users ccu
        join xhpc_operator ope on ope.corp_no = ccu.corpNo and ccu.usersCorp =ope.operator_id
        where ccu.del_flag=0
        <if test="userName !=null and userName !=''">
            and ccu.usersName like concat('%', #{userName}, '%')
        </if>
        <if test="usersLevel !=null">
            and ccu.usersLevel =#{usersLevel}
        </if>
        <if test="operatorId !=null">
            and ope.operator_id =#{operatorId}
        </if>
        <if test="tenantId !=null and tenantId !=''">
            and ccu.tenant_id =#{tenantId}
        </if>
        <if test="status!=0">
            and ope.operator_id =#{logOperatorId}
        </if>
        order by ccu.usersTime desc
    </select>

    <insert id="insertXhpcCardClientUser">
        insert into t_iccard_client_users
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != usersName and ''!=usersName">
                usersName,
            </if>
            <if test="null != usersPwd and ''!=usersPwd">
                usersPwd,
            </if>
            <if test="null != usersPhone and ''!=usersPhone">
                usersPhone,
            </if>
            <if test="usersEmail!= null and usersEmail != ''">
                usersEmail,
            </if>
            <if test="null != usersAdress and ''!=usersAdress">
                usersAdress,
            </if>
            <if test="null != usersLevel">
                usersLevel,
            </if>
            <if test="null != usersCorp ">
                usersCorp,
            </if>
            <if test="null != corpNo and '' != corpNo">
                corpNo,
            </if>
            <if test="null != corpName and ''!=corpName ">
                corpName,
            </if>
                usersTime, status,
            <if test="null != tenantId and '' != tenantId">
                tenant_id
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != usersName and ''!=usersName">
                #{usersName},
            </if>
            <if test="null != usersPwd and ''!=usersPwd">
                #{usersPwd},
            </if>
            <if test="null != usersPhone and ''!=usersPhone">
                #{usersPhone},
            </if>
            <if test="usersEmail!= null and usersEmail != ''">
                #{usersEmail},
            </if>
            <if test="null != usersAdress and ''!=usersAdress">
                #{usersAdress},
            </if>
            <if test="null != usersLevel">
                #{usersLevel},
            </if>
            <if test="null != usersCorp ">
                #{usersCorp},
            </if>
            <if test="null != corpNo and '' != corpNo">
                #{corpNo},
            </if>
            <if test="null != corpName and ''!=corpName ">
                #{corpNameand},
            </if>
                sysdate(), 0,
            <if test="null != tenantId and '' != tenantId">
                #{tenantId}
            </if>
        </trim>
    </insert>

    <select id="getoperatorList" resultType="map">
        select
            corp_no as corpNo,
            operator_id usersCorp,
            name as name
        from xhpc_operator where tenant_id=#{tenantId} and del_flag =0
    </select>

    <select id="getXhpcCardClientUserById" resultType="map">
        select
            ccu.usersID as usersID,
            ccu.usersName as usersName,
            ccu.usersPwd as usersPwd,
            ccu.usersPhone as usersPhone,
            ccu.usersEmail as usersEmail,
            ccu.usersAdress as usersAdress,
            ccu.usersLevel as usersLevel,
            ope.name as operatorName,
            ope.corp_no as corpNo,
            ccu.tenant_id as tenantId,
            concat(ccu.del_flag, "") as delFlag,
            ccu.usersTime as usersTime
        from t_iccard_client_users ccu
        join xhpc_operator ope on ope.corp_no = ccu.corpNo
        where ccu.status=0
        <if test="type==0">
            <if test="usersID !=null">
                and ccu.usersID=#{usersID}
            </if>
        </if>
        <if test="type==1">
            <if test="usersID !=null">
                and ccu.usersID !=#{usersID}
            </if>
        </if>
        <if test="null != usersName and ''!=usersName">
            and ccu.usersName=#{usersName}
        </if>
    </select>

    <update id="updateXhpcCardClientUser" >
        update t_iccard_client_users
        <trim prefix="SET" suffixOverrides=",">
            <if test="null != usersName and ''!=usersName">
                usersName= #{usersName},
            </if>
            <if test="null != usersPwd and ''!=usersPwd">
                usersPwd= #{usersPwd},
            </if>
            <if test="null != usersPhone and ''!=usersPhone">
                usersPhone= #{usersPhone},
            </if>
            <if test="usersEmail!= null and usersEmail != ''">
                usersEmail = #{usersEmail},
            </if>
            <if test="null != usersAdress and ''!=usersAdress">
                usersAdress=#{usersAdress},
            </if>
            <if test="null != usersLevel">
                usersLevel=#{usersLevel},
            </if>
            <if test="null != usersCorp ">
                usersCorp=#{usersCorp},
            </if>
            <if test="null != corpNo and '' != corpNo">
                corpNo= #{corpNo},
            </if>
            <if test="null != corpName and ''!=corpName ">
                corpName= #{corpNameand},
            </if>
            <if test="null != usersTime">
                usersTime=#{usersTime},
            </if>
            <if test="null != status">
                status=#{status},
            </if>
            <if test="null != tenantId and '' != tenantId">
                tenant_id= #{tenantId}
            </if>
        </trim>
        where usersID = #{usersID}
    </update>
</mapper>