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

    <resultMap type="com.xhpc.system.domain.XhpcUserPrivilege" id="XhpcUserPrivilegeResult">
        <result column="user_id" property="userId"/>
        <result column="charging_station_id" property="chargingStationId"/>
    </resultMap>


    <insert id="insert" parameterType="com.xhpc.system.domain.XhpcUserPrivilege">
        insert into xhpc_user_privilege(
        <if test="null != userId and '' != userId">
            user_id,
        </if>
        <if test="null != chargingStationId and '' != chargingStationId">
            charging_station_id
        </if>
        )values(
        <if test="null != userId and '' != userId">
            #{userId},
        </if>
        <if test="null != chargingStationId and '' != chargingStationId">
            #{chargingStationId}
        </if>
        )
    </insert>

    <delete id="delete" parameterType="Long">
        delete from xhpc_user_privilege where user_id = #{userId}
    </delete>

    <select id="dataCheckedKeys" parameterType="Long" resultType="java.lang.Long">
		select charging_station_id chargingStationId from xhpc_user_privilege where user_id = #{userId}
	</select>

    <select id="getOperatorId" parameterType="java.lang.String" resultType="java.util.Map">
        select distinct xo.operator_id operatorId, xo.name, xo.contact_name contactName,
        xo.contact_phone contactPhone, xo.phone, xo.attribute,
        xdb.dict_value attributenName
        from xhpc_operator `xo`
        LEFT JOIN xhpc_dict_biz xdb on xdb.`code` = 'operator_attribute' and xdb.dict_key = xo.attribute
        JOIN xhpc_charging_station xcs on xcs.operator_id = xo.operator_id
        where xo.del_flag = 0
        <if test="null != operatorIds and '' != operatorIds">
            and xo.operator_id in
            <foreach collection="operatorIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY xo.create_time DESC
    </select>

    <select id="getOperatorIdByUserId" parameterType="java.lang.String" resultType="java.util.Map">
        select xo.operator_id operatorId, xo.name, xo.contact_name contactName,
        xo.contact_phone contactPhone, xo.phone, xo.attribute,
        xdb.dict_value attributenName
        from xhpc_operator `xo`
        LEFT JOIN xhpc_dict_biz xdb on xdb.`code` = 'operator_attribute' and xdb.dict_key = xo.attribute
        where xo.del_flag = 0
        <if test="null != operatorIds and '' != operatorIds">
            and operator_id in
            (select operator_id
            from xhpc_charging_station
            where charging_station_id in (
            select charging_station_id from xhpc_user_privilege where user_id = #{userId})
            GROUP BY operator_id)
        </if>
        ORDER BY xo.create_time DESC
    </select>

    <select id="groupProvinceByOperatorId" parameterType="java.lang.String" resultType="java.util.Map">
        select `code` ,`name` from xhpc_area where `code` in (
        select `pxa`.pcode province
        from xhpc_charging_station xcs
        LEFT JOIN xhpc_area `xa` on `xa`.`code` = xcs.area_code
        LEFT JOIN xhpc_area pxa on `xa`.pcode = pxa.`code`
        where xcs.del_flag = 0
        <if test="null != userId and '' != userId">
            and  xcs.charging_station_id in (
            select charging_station_id from xhpc_user_privilege where user_id = #{userId})
        </if>
        <if test="null != operatorId and '' != operatorId">
            and xcs.operator_id = #{operatorId}
        </if>
        GROUP BY `pxa`.pcode
        ) and code != 100000
    </select>

    <select id="groupCityByOperatorId" parameterType="java.lang.String" resultType="java.util.Map">
        select `code` ,`name` from xhpc_area where `code` in (
        select `xa`.pcode city
        from xhpc_charging_station xcs
        LEFT JOIN xhpc_area `xa` on `xa`.`code` = xcs.area_code
        LEFT JOIN xhpc_area pxa on `xa`.pcode = pxa.`code`
        where xcs.del_flag = 0
        <if test="null != userId and '' != userId">
            and xcs.charging_station_id in (
            select charging_station_id from xhpc_user_privilege where user_id = #{userId})
        </if>
        <if test="null != operatorId and '' != operatorId">
            and xcs.operator_id = #{operatorId}
        </if>
        <if test="null != provinceCode and '' != provinceCode">
            and pxa.pcode = #{provinceCode}
        </if>
        GROUP BY `pxa`.code
        )
    </select>

    <select id="groupAreaByOperatorId" parameterType="java.lang.String" resultType="java.util.Map">
        select `code` ,`name` from xhpc_area where `code` in (
        select `xa`.code
        from xhpc_charging_station xcs
        LEFT JOIN xhpc_area `xa` on `xa`.`code` = xcs.area_code
        LEFT JOIN xhpc_area pxa on `xa`.pcode = pxa.`code`
        where xcs.del_flag = 0
        <if test="null != userId and '' != userId">
          and   xcs.charging_station_id in (
            select charging_station_id from xhpc_user_privilege where user_id = #{userId})
        </if>
        <if test="null != operatorId and '' != operatorId">
            and xcs.operator_id = #{operatorId}
        </if>
        <if test="null != cityCode and '' != cityCode">
            and xa.pcode = #{cityCode}
        </if>
        GROUP BY `xa`.code
        )
    </select>

    <select id="dataPowerByOperatorId" parameterType="java.lang.String" resultType="java.util.Map">

        select xcs.charging_station_id chargingStationId,xcs.name
        from xhpc_charging_station xcs
        LEFT JOIN xhpc_area `xa` on `xa`.`code` = xcs.area_code
        LEFT JOIN xhpc_area pxa on `xa`.pcode = pxa.`code`
        where xcs.del_flag = 0
        <if test="null != userId and '' != userId">
            and xcs.charging_station_id in (
            select charging_station_id from xhpc_user_privilege where user_id = #{userId})
        </if>
        <if test="null != operatorId and '' != operatorId">
            and xcs.operator_id = #{operatorId}
        </if>
        <if test="null != code and '' != code">
            and `xa`.`code` = #{code}
        </if>
    </select>
</mapper>
