<?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.user.mapper.XhpcInternetUserMapper">

    <resultMap type="com.xhpc.user.domain.XhpcInternetUser" id="XhpcInternetUserResult">
        <result column="internet_user_id" property="internetUserId"/>
        <result column="name" property="name"/>
        <result column="phone" property="phone"/>
        <result column="contact_name" property="contactName"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="open_bank" property="openBank"/>
        <result column="card_number" property="cardNumber"/>
        <result column="cooperation_start_time" property="cooperationStartTime" jdbcType="TIMESTAMP"/>
        <result column="cooperation_end_time" property="cooperationEndTime" jdbcType="TIMESTAMP"/>
        <result column="balance" property="balance"/>
        <result column="area_code" property="areaCode"/>
        <result column="address" property="address"/>
        <result column="detailed_address" property="detailedAddress"/>
        <result column="commission_type" property="commissionType"/>
        <result column="commission_rate" property="commissionRate"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="status" property="status"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="remark" property="remark"/>
        <result column="operator_id_evcs" property="operatorIdEvcs"/>
        <result column="role_id" property="roleId"/>
    </resultMap>


    <insert id="insert" parameterType="com.xhpc.user.domain.XhpcInternetUser" useGeneratedKeys="true"
            keyProperty="internetUserId">
        INSERT INTO xhpc_internet_user
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="null != name and '' != name">
                name,
            </if>
            <if test="null != phone and '' != phone">
                phone,
            </if>
            <if test="null != contactName and '' != contactName">
                contact_name,
            </if>
            <if test="null != contactPhone and '' != contactPhone">
                contact_phone,
            </if>
            <if test="null != openBank and '' != openBank">
                open_bank,
            </if>
            <if test="null != cardNumber and '' != cardNumber">
                card_number,
            </if>
            <if test="null != cooperationStartTime ">
                cooperation_start_time,
            </if>
            <if test="null != cooperationEndTime ">
                cooperation_end_time,
            </if>
            <if test="null != balance and '' != balance">
                balance,
            </if>
            <if test="null != areaCode and '' != areaCode">
                area_code,
            </if>
            <if test="null != address and '' != address">
                address,
            </if>
            <if test="null != detailedAddress and '' != detailedAddress">
                detailed_address,
            </if>
            <if test="null != commissionType and '' != commissionType">
                commission_type,
            </if>
            <if test="null != commissionRate and '' != commissionRate">
                commission_rate,
            </if>
            <if test="null != longitude and '' != longitude">
                longitude,
            </if>
            <if test="null != latitude and '' != latitude">
                latitude,
            </if>
            <if test="null != status and '' != status">
                status,
            </if>
            <if test="null != delFlag and '' != delFlag">
                del_flag,
            </if>
            <if test="null != createBy and '' != createBy">
                create_by,
            </if>
            <if test="null != createTime ">
                create_time,
            </if>
            <if test="null != updateBy and '' != updateBy">
                update_by,
            </if>
            <if test="null != updateTime ">
                update_time,
            </if>
            <if test="null != remark and '' != remark">
                remark,
            </if>
            <if test="null != operatorIdEvcs and '' != operatorIdEvcs">
                operator_id_evcs,
            </if>
            <if test="null != tenantId and '' != tenantId">
                tenant_id,
            </if>
            <if test="null != roleId">
                role_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != name and '' != name">
                #{name},
            </if>
            <if test="null != phone and '' != phone">
                #{phone},
            </if>
            <if test="null != contactName and '' != contactName">
                #{contactName},
            </if>
            <if test="null != contactPhone and '' != contactPhone">
                #{contactPhone},
            </if>
            <if test="null != openBank and '' != openBank">
                #{openBank},
            </if>
            <if test="null != cardNumber and '' != cardNumber">
                #{cardNumber},
            </if>
            <if test="null != cooperationStartTime ">
                #{cooperationStartTime},
            </if>
            <if test="null != cooperationEndTime ">
                #{cooperationEndTime},
            </if>
            <if test="null != balance and '' != balance">
                #{balance},
            </if>
            <if test="null != areaCode and '' != areaCode">
                #{areaCode},
            </if>
            <if test="null != address and '' != address">
                #{address},
            </if>
            <if test="null != detailedAddress and '' != detailedAddress">
                #{detailedAddress},
            </if>
            <if test="null != commissionType and '' != commissionType">
                #{commissionType},
            </if>
            <if test="null != commissionRate and '' != commissionRate">
                #{commissionRate},
            </if>
            <if test="null != longitude and '' != longitude">
                #{longitude},
            </if>
            <if test="null != latitude and '' != latitude">
                #{latitude},
            </if>
            <if test="null != status and '' != status">
                #{status},
            </if>
            <if test="null != delFlag and '' != delFlag">
                #{delFlag},
            </if>
            <if test="null != createBy and '' != createBy">
                #{createBy},
            </if>
            <if test="null != createTime ">
                #{createTime},
            </if>
            <if test="null != updateBy and '' != updateBy">
                #{updateBy},
            </if>
            <if test="null != updateTime ">
                #{updateTime},
            </if>
            <if test="null != remark and '' != remark">
                #{remark},
            </if>
            <if test="null != operatorIdEvcs and '' != operatorIdEvcs">
                #{operatorIdEvcs},
            </if>
            <if test="null != tenantId and '' != tenantId">
                #{tenantId},
            </if>
            <if test="null != roleId">
                #{roleId},
            </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.xhpc.user.domain.XhpcInternetUser">
        UPDATE xhpc_internet_user
        <set>
            <if test="null != name and '' != name">name = #{name},</if>
            <if test="null != phone and '' != phone">phone = #{phone},</if>
            <if test="null != contactName and '' != contactName">contact_name = #{contactName},</if>
            <if test="null != contactPhone and '' != contactPhone">contact_phone = #{contactPhone},</if>
            <if test="null != openBank and '' != openBank">open_bank = #{openBank},</if>
            <if test="null != cardNumber and '' != cardNumber">card_number = #{cardNumber},</if>
            <if test="null != cooperationStartTime">cooperation_start_time = #{cooperationStartTime},</if>
            <if test="null != cooperationEndTime">cooperation_end_time = #{cooperationEndTime}, </if>
            <if test="null != balance ">balance = #{balance},</if>
            <if test="null != areaCode and '' != areaCode">area_code = #{areaCode},</if>
            <if test="null != address and '' != address">address = #{address},</if>
            <if test="null != detailedAddress and '' != detailedAddress">detailed_address = #{detailedAddress},</if>
            <if test="null != commissionType ">commission_type = #{commissionType},</if>
            <if test="null != commissionRate and '' != commissionRate">commission_rate = #{commissionRate},</if>
            <if test="null != longitude and '' != longitude">longitude = #{longitude},</if>
            <if test="null != latitude and '' != latitude">latitude = #{latitude},</if>
            <if test="null != status ">status = #{status},</if>
            <if test="null != delFlag and '' != delFlag">del_flag = #{delFlag},</if>
            <if test="null != createBy and '' != createBy">create_by = #{createBy},</if>
            <if test="null != createTime ">create_time = #{createTime},</if>
            <if test="null != updateBy and '' != updateBy">update_by = #{updateBy},</if>
            <if test="null != updateTime">update_time = #{updateTime},</if>
            <if test="null != remark and '' != remark">remark = #{remark},</if>
            <if test="null != operatorIdEvcs and '' != operatorIdEvcs">operator_id_evcs = #{operatorIdEvcs}</if>
            <if test="null != roleId">role_id = #{roleId}</if>
        </set>
        WHERE internet_user_id = #{internetUserId}
    </update>

    <delete id="deleteByIds" parameterType="java.lang.String">
        delete from xhpc_internet_user where internet_user_id in
        <foreach collection="array" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </delete>

    <select id="info" parameterType="java.lang.Long" resultType="java.util.Map">
      select
            xiu.internet_user_id internetUserId, xiu.`name`,  xiu.phone,  xiu.contact_name contactName,
            xiu.contact_phone contactPhone, xiu.open_bank openBank, xiu.card_number cardNumber,
            xiu.cooperation_start_time cooperationStartTime, xiu.cooperation_end_time cooperationEndTime,
            xiu.balance, xiu.area_code areaCode, xiu.address, xiu.detailed_address detailedAddress,
            xiu.commission_type commissionType, xiu.commission_rate commissionRate,
            xiu.longitude,  xiu.latitude, xiu.`status`, xiu.`role_id` roleId,
            xiu.create_by createBy ,xiu.create_time createTime,
            xiu.update_time updateTime,  xiu.update_by updateBy,
            xiu.del_flag delflag, xiu.remark ,
            xiu.operator_id_evcs operatorIdEvcs,
            CASE WHEN xiu.`status` = 0 THEN '正常' else  '禁用'  end statusName,
            CASE WHEN xiu.commission_type = 0 THEN '总金额提成' else  '服务费提成'  end commissionTypeName
        from  xhpc_internet_user xiu
        LEFT JOIN sys_user su on su.internet_user_id = xiu.internet_user_id
        WHERE xiu.del_flag = 0 and xiu.internet_user_id = #{internetUserId}
    </select>

    <select id="selectInternetList" parameterType="java.lang.Long" resultType="java.util.Map">
        select
        xiu.internet_user_id internetUserId, xiu.`name`, xiu.phone, xiu.contact_name contactName,
        xiu.contact_phone contactPhone,xiu.create_time createTime,
        xiu.commission_type commissionType, xiu.commission_rate commissionRate,
        xiu.cooperation_start_time  startTime , xiu.cooperation_end_time endTime,
        xiu.operator_id_evcs operatorIdEvcs,
        xiu.commission_type commissioType,
        xiu.latitude, xiu.`status`,su.user_id userId,
        CASE WHEN xiu.`status` = 0 THEN '正常' else '禁用' end statusName,
        CASE WHEN xiu.commission_type = 0 THEN '总金额提成' WHEN xiu.commission_type = 1 THEN '服务费提成' else '电量提成' end commissionTypeName
        from xhpc_internet_user xiu
        LEFT JOIN sys_user su on su.internet_user_id = xiu.internet_user_id
        WHERE xiu.del_flag = 0
        <if test="name != null and name != ''">
            and xiu.name like concat('%', #{name}, '%')
        </if>
          <if test="tenantId !=null and ''!=tenantId">
              and xiu.tenant_id =#{tenantId}
          </if>
        <if test="contactName != null and contactName != ''">
            and xiu.contact_name like concat('%', #{contactName}, '%')
        </if>
        <if test="contactPhone != null and contactPhone != ''">
            and xiu.contact_phone like concat('%', #{contactPhone}, '%')
        </if>
        <if test="createTimeStart != null and createTimeStart != ''"><!-- 开始时间检索 -->
            AND xiu.create_time &gt;= #{createTimeStart}
        </if>
        <if test="createTimeEnd != null and createTimeEnd != ''"><!-- 结束时间检索 -->
            AND xiu.create_time &lt;= #{createTimeEnd}
        </if>
        ORDER BY xiu.create_time DESC
    </select>

    <select id="getUserByInternetUserId" parameterType="java.lang.Long"
            resultType="com.xhpc.system.api.domain.SysUser">
         select user_id          userId,
               dept_id          deptid,
               user_name        userName,
               nick_name        nickName,
               user_type        userType,
               email,
               phonenumber,
               operator_id      operatorId,
               internet_user_id internetUserId,
               sex,
               avatar,
               password,
               data_power_type  dataPowerType,
               status,
               del_flag         delFlag,
               login_ip         loginIp,
               login_date       loginDate,
               create_by        createBy,
               create_time      createTime,
               update_by        updateBy,
               update_time      updateTime,
               remark
        from sys_user
        where del_flag = 0
          and internet_user_id = #{internetUserId}
    </select>

    <select id="list" parameterType="java.lang.Long" resultType="java.util.Map">
        select
        xiu.internet_user_id internetUserId, xiu.`name`, xiu.phone, xiu.contact_name contactName,
        xiu.contact_phone contactPhone,
        xiu.commission_type commissionType, xiu.commission_rate commissionRate,
        xiu.latitude, xiu.`status`,
        CASE WHEN xiu.`status` = 0 THEN '正常' else '禁用' end statusName,
        CASE WHEN xiu.commission_type = 0 THEN '总金额提成' else '服务费提成' end commissionTypeName
        from xhpc_internet_user xiu
        LEFT JOIN sys_user su on su.internet_user_id = xiu.internet_user_id
        WHERE xiu.del_flag = 0
        <if test="name != null and name != ''">
            and xiu.name like concat('%', #{name}, '%')
        </if>
        ORDER BY xiu.update_time DESC
    </select>

    <select id="getOperatorIdEvcs" resultMap="XhpcInternetUserResult">
        select * from xhpc_internet_user where del_flag =0 and operator_id_evcs=#{operatorIdEvcs} limit 1
    </select>

    <select id="getCountOperatorIdEvcs" resultType="int">
        select
        count(internet_user_id)
        from xhpc_internet_user
        where del_flag =0
        and operator_id_evcs=#{operatorIdEvcs}
        <if test="internetUserId !=null">
            and internet_user_id !=#{internetUserId}
        </if>
    </select>
</mapper>
