<?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.XhpcUserMapper">

    <resultMap type="com.xhpc.system.api.domain.SysUser" id="XhpcUserResult">
        <result column="user_id" property="userId"/>
        <result column="dept_id" property="deptId"/>
        <result column="user_name" property="userName"/>
        <result column="user_type" property="userType"/>
        <result column="nick_name" property="nickName"/>
        <result column="email" property="email"/>
        <result column="phonenumber" property="phonenumber"/>
        <result column="operator_id" property="operatorId"/>
        <result column="internet_user_id" property="internetUserId"/>
        <result column="sex" property="sex"/>
        <result column="avatar" property="avatar"/>
        <result column="password" property="password"/>
        <result column="data_power_type" property="dataPowerType"/>
        <result column="status" property="status"/>
        <result column="del_flag" property="delFlag"/>
        <result column="login_ip" property="loginIp"/>
        <result column="login_date" property="loginDate"/>
        <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"/>
    </resultMap>

    <update id="update" parameterType="com.xhpc.system.api.domain.SysUser">
        UPDATE sys_user
        <set>
            <if test="null != deptId and '' != deptId">dept_id = #{deptId},</if>
            <if test="null != userName and '' != userName">user_name = #{userName},</if>
            <if test="null != nickName and '' != nickName">nick_name = #{nickName},</if>
            <if test="null != userType and '' != userType">user_type = #{userType},</if>
            <if test="null != email and '' != email">email = #{email},</if>
            <if test="null != phonenumber and '' != phonenumber">phonenumber = #{phonenumber},</if>
            <if test="null != operatorId and '' != operatorId">operator_id = #{operatorId},</if>
            <if test="null != sex and '' != sex">sex = #{sex},</if>
            <if test="null != avatar and '' != avatar">avatar = #{avatar},</if>
            <if test="null != password and '' != password">password = #{password},</if>
            <if test="null != dataPowerType ">data_power_type = #{dataPowerType},</if>
            <if test="null != status">status = #{status},</if>
            <if test="null != delFlag and '' != delFlag">del_flag = #{delFlag},</if>
            <if test="null != loginIp and '' != loginIp">login_ip = #{loginIp},</if>
            <if test="null != loginDate and '' != loginDate">login_date = #{loginDate},</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>
        </set>
        WHERE user_id = #{userId}
    </update>

    <delete id="deleteByIds" parameterType="java.lang.Long">
        delete from sys_user where user_id in
        <foreach collection="array" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>


    <select id="selectUserByUserId" parameterType="java.lang.Long" resultMap="XhpcUserResult">
         select *
            from sys_user
        where user_id = #{userId}
    </select>

    <select id="selectPcUserList" parameterType="java.lang.String" resultType="java.util.Map">
        select user_id userId,user_name userName,
        nick_name nickName, email,status,
        create_by createBy ,create_time createTime,
        CASE WHEN `status` = 0 THEN '正常' else '禁用' end statusName
        from sys_user
        WHERE del_flag = 0 and user_type = '00'
        <if test="userName != null and userName != ''">
            and user_name like concat(concat('%', #{userName}), '%')
        </if>
        <if test="nickName != null and nickName != ''">
            and nick_name like concat(concat('%', #{nickName}), '%')
        </if>
        ORDER BY update_time DESC
    </select>

    <select id="pcInfo" parameterType="java.lang.Long" resultType="java.util.Map">
        select su.user_id userId,su.user_name userName,su.user_type userType,
        su.nick_name nickName, su.email,su.status,su.avatar,
        su.create_by createBy ,su.create_time createTime,su.`status` ,
        CASE WHEN su.`status` = 0 THEN '正常' else  '禁用'  end statusName,
        sdd.dict_label sexName,(select role_id from sys_user_role where user_id = su.user_id LIMIT 1) roleId
        from sys_user su
        LEFT JOIN sys_dict_data sdd on sdd.`dict_type` = "sys_user_sex" and sdd.dict_code = su.sex
        WHERE su.del_flag = 0 and su.user_id = #{userId}
    </select>

    <select id="selectOperatorUserList" parameterType="java.lang.String" resultType="java.util.Map">
        select su.user_id userId,su.user_name userName,
        su.nick_name nickName, su.email,
        su.create_by createBy ,su.create_time createTime,
        xo.`name` operatorName,su.`status` ,
        CASE WHEN su.`status` = 0 THEN '正常' else '禁用' end statusName
        from sys_user su
        LEFT JOIN xhpc_operator xo on xo.operator_id = su.operator_id
        WHERE su.del_flag = 0 and user_type = '03'
        <if test="userName != null and userName != ''">
            and su.user_name like concat(concat('%', #{userName}), '%')
        </if>
        <if test="nickName != null and nickName != ''">
            and su.nick_name like concat(concat('%', #{nickName}), '%')
        </if>
        <if test="operatorId != null and operatorId != ''">
            and su.operator_id = #{operatorId}
        </if>
        ORDER BY su.update_time DESC
    </select>

    <select id="operatorInfo" parameterType="java.lang.Long" resultType="java.util.Map">
        select su.user_id userId,su.user_name userName,
        su.nick_name nickName, su.email,su.sex,su.avatar,
        su.create_by createBy ,su.create_time createTime,xo.operator_id operatorId,
        xo.`name` operatorName,su.`status` ,
        CASE WHEN su.`status` = 0 THEN '正常' else  '禁用'  end statusName,
        sdd.dict_label sexName,(select role_id from sys_user_role where user_id = su.user_id LIMIT 1) roleId
        from sys_user su
        LEFT JOIN xhpc_operator  xo on xo.operator_id = su.operator_id
        LEFT JOIN sys_dict_data sdd on sdd.`dict_type` = "sys_user_sex" and sdd.dict_value = su.sex
        WHERE su.del_flag = 0 and user_id = #{userId}
    </select>

    <select id="selectUserByUserName" parameterType="java.lang.String" 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 user_name = #{userName}  LIMIT 1
    </select>
    <select id="selectUserByCreateBy" parameterType="java.lang.String" 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 create_by = #{createBy}
    </select>

    <select id="selectUserByRoleName" parameterType="java.lang.String" resultType="com.xhpc.system.api.domain.SysRole">
        select  role_id     roleId,
                role_name roleName,
                role_key  roleKey,
                role_sort roleSort,
                data_scope  dataScope,
                menu_check_strictly menuCheckStrictly,
                dept_check_strictly deptCheckStrictly,
               status,
               del_flag         delFlag,
               create_by        createBy,
               create_time      createTime,
               update_by        updateBy,
               update_time      updateTime,
               remark
        from sys_role
        where role_name = #{userName} ORDER BY create_time DESC LIMIT 1
    </select>

    <insert id="insertRole" parameterType="com.xhpc.system.api.domain.SysRole" useGeneratedKeys="true"
            keyProperty="roleId">
        insert into sys_role(
        <if test="roleId != null and roleId != 0">role_id,</if>
        <if test="roleName != null and roleName != ''">role_name,</if>
        <if test="roleKey != null and roleKey != ''">role_key,</if>
        <if test="roleSort != null and roleSort != ''">role_sort,</if>
        <if test="dataScope != null and dataScope != ''">data_scope,</if>
        <if test="menuCheckStrictly != null">menu_check_strictly,</if>
        <if test="deptCheckStrictly != null">dept_check_strictly,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="roleId != null and roleId != 0">#{roleId},</if>
        <if test="roleName != null and roleName != ''">#{roleName},</if>
        <if test="roleKey != null and roleKey != ''">#{roleKey},</if>
        <if test="roleSort != null and roleSort != ''">#{roleSort},</if>
        <if test="dataScope != null and dataScope != ''">#{dataScope},</if>
        <if test="menuCheckStrictly != null">#{menuCheckStrictly},</if>
        <if test="deptCheckStrictly != null">#{deptCheckStrictly},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <insert id="insertUser" parameterType="com.xhpc.system.api.domain.SysUser" useGeneratedKeys="true"
            keyProperty="userId">
        insert into sys_user(
        <if test="userId != null and userId != 0">user_id,</if>
        <if test="deptId != null and deptId != 0">dept_id,</if>
        <if test="userName != null and userName != ''">user_name,</if>
        <if test="userType != null and userType != ''">user_type,</if>
        <if test="nickName != null and nickName != ''">nick_name,</if>
        <if test="email != null and email != ''">email,</if>
        <if test="avatar != null and avatar != ''">avatar,</if>
        <if test="phonenumber != null and phonenumber != ''">phonenumber,</if>
        <if test="operatorId != null and operatorId != ''">operator_id,</if>
        <if test="internetUserId != null and internetUserId != ''">internet_user_id,</if>
        <if test="dataPowerType != null and dataPowerType != ''">data_power_type,</if>
        <if test="sex != null and sex != ''">sex,</if>
        <if test="password != null and password != ''">password,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="remark != null and remark != ''">remark,</if>
        create_time
        )values(
        <if test="userId != null and userId != ''">#{userId},</if>
        <if test="deptId != null and deptId != ''">#{deptId},</if>
        <if test="userName != null and userName != ''">#{userName},</if>
        <if test="userType != null and userType != ''">#{userType},</if>
        <if test="nickName != null and nickName != ''">#{nickName},</if>
        <if test="email != null and email != ''">#{email},</if>
        <if test="avatar != null and avatar != ''">#{avatar},</if>
        <if test="phonenumber != null and phonenumber != ''">#{phonenumber},</if>
        <if test="operatorId != null and operatorId != ''">#{operatorId},</if>
        <if test="internetUserId != null and internetUserId != ''">#{internetUserId},</if>
        <if test="dataPowerType != null and dataPowerType != ''">#{dataPowerType},</if>
        <if test="sex != null and sex != ''">#{sex},</if>
        <if test="password != null and password != ''">#{password},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        sysdate()
        )
    </insert>

    <insert id="batchUserRole">
        insert into sys_user_role(user_id, role_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId},#{item.roleId})
        </foreach>
    </insert>

    <select id="checkUserNameUnique" parameterType="java.lang.String" resultType="int">
		select count(1) from sys_user where user_name = #{userName}
		 <if test="userId !=null">
             and user_id=#{userId}
         </if>
		 limit 1
	</select>

    <insert id="addXhpcDataDimension">
        insert into xhpc_data_dimension(user_id, charging_station_id) values(#{userId},#{chargingStationId})
    </insert>

    <update id="updateXhpcDataDimension">
      update xhpc_data_dimension set del_flag = 1 where user_id = #{userId}
    </update>
</mapper>
