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

    <select id="getLandUser" resultType="map">
        select user_id as userId,user_type as userType,operator_id as operatorId  from sys_user where user_id =#{userId}
    </select>

    <select id="groupProvinceById" 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="type==1">
            and xcs.charging_station_id in (select charging_station_id from xhpc_charging_station where operator_id=#{userId})
        </if>
        <if test="type==2">
            and xcs.charging_station_id in (select charging_station_id from xhpc_user_privilege where user_id=#{userId})
        </if>
        <if test="type==3">
            and xcs.operator_id=#{operatorId}
        </if>
        GROUP BY `pxa`.pcode
        )
    </select>

    <select id="groupCityById" 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="type==1">
            and charging_station_id in (select charging_station_id from xhpc_charging_station where operator_id=#{userId})
        </if>
        <if test="type==2">
            and charging_station_id in (select charging_station_id from xhpc_user_privilege where user_id=#{userId})
        </if>
        <if test="null != provinceCode and '' != provinceCode">
            and pxa.pcode = #{provinceCode}
        </if>
        <if test="type==3">
            and xcs.operator_id=#{operatorId}
        </if>
        GROUP BY `pxa`.code
        )
    </select>

    <select id="groupAreaById" 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="type==1">
            and charging_station_id in (select charging_station_id from xhpc_charging_station where operator_id=#{userId})
        </if>
        <if test="type==2">
            and charging_station_id in (select charging_station_id from xhpc_user_privilege where user_id=#{userId})
        </if>
        <if test="null != cityCode and '' != cityCode">
            and xa.pcode = #{cityCode}
        </if>
        <if test="type==3">
            and xcs.operator_id=#{operatorId}
        </if>
        GROUP BY `xa`.code
        )
    </select>

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

        select operator_id operatorId,name from xhpc_operator where operator_id in (
        select xcs.operator_id
        from xhpc_charging_station xcs
        where xcs.del_flag = 0
        <if test="type==1">
            and charging_station_id in (select charging_station_id from xhpc_charging_station where operator_id=#{userId})
        </if>
        <if test="type==2">
            and charging_station_id in (select charging_station_id from xhpc_user_privilege where user_id=#{userId})
        </if>
        <if test="null != code and '' != code">
            and xcs.area_code = #{code}
        </if>
        )
        <if test="tenantId != null and '' != tenantId">
            and tenant_id =#{tenantId}
        </if>
    </select>

    <select id="dataCodeById" 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="type==1">
            and charging_station_id in (select charging_station_id from xhpc_charging_station where operator_id=#{userId})
        </if>
        <if test="type==2">
            and charging_station_id in (select charging_station_id from xhpc_user_privilege where user_id=#{userId})
        </if>
        <if test="null != code and '' != code">
            and `xa`.`code` = #{code}
        </if>
    </select>

    <select id="dataChargingStationId" 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="type==1">
            and charging_station_id in (select charging_station_id from xhpc_charging_station where operator_id=#{userId})
        </if>
        <if test="type==2">
            and charging_station_id in (select charging_station_id from xhpc_user_privilege where user_id=#{userId})
        </if>
        <if test="null != operatorId ">
            and xcs.operator_id =#{operatorId}
        </if>
        <if test="null != code and '' != code">
            and xcs.area_code = #{code}
        </if>
    </select>

    <update id="updateMechanism">
        update xhpc_mechanism set del_flag =1 where mechanism_id=#{mechanismId} and source=#{source }
    </update>

    <insert id="insertMechanism">
        insert into xhpc_mechanism (charging_station_id,mechanism_id,source) VALUES
        <foreach collection="chargingStationIds" item="item" open="(" separator="),(" close=")">
            #{item},#{mechanismId},#{source}
        </foreach>
    </insert>

    <insert id="insertRechargeOrder" parameterType="com.xhpc.common.domain.XhpcRechargeOrder" useGeneratedKeys="true"
            keyProperty="rechargeOrderId">
        INSERT INTO xhpc_recharge_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != userId">
                user_id,
            </if>
            <if test="null != rechargeOrderNumber and '' != rechargeOrderNumber">
                recharge_order_number,
            </if>
            <if test="null != prepayId and '' != prepayId">
                prepay_id,
            </if>
            <if test="null != alipayNumber and '' != alipayNumber">
                alipay_number,
            </if>
            <if test="null != amount and '' != amount">
                amount,
            </if>
            <if test="null != type and '' != type">
                type,
            </if>
            <if test="null != status and '' != status">
                status,
            </if>
            <if test="null != delFlag and '' != delFlag">
                del_flag,
            </if>
            <if test="null != createTime">
                create_time,
            </if>
            <if test="null != createBy and '' != createBy">
                create_by,
            </if>
            <if test="null != updateTime">
                update_time,
            </if>
            <if test="null != updateBy and '' != updateBy">
                update_by,
            </if>
            <if test="null != remark and '' != remark">
                remark,
            </if>
            <if test="null != source">
                source
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != userId">
                #{userId},
            </if>
            <if test="null != rechargeOrderNumber and '' != rechargeOrderNumber">
                #{rechargeOrderNumber},
            </if>
            <if test="null != prepayId and '' != prepayId">
                #{prepayId},
            </if>
            <if test="null != alipayNumber and '' != alipayNumber">
                #{alipayNumber},
            </if>
            <if test="null != amount and '' != amount">
                #{amount},
            </if>
            <if test="null != type and '' != type">
                #{type},
            </if>
            <if test="null != status and '' != status">
                #{status},
            </if>
            <if test="null != delFlag and '' != delFlag">
                #{delFlag},
            </if>
            <if test="null != createTime">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="null != createBy and '' != createBy">
                #{createBy},
            </if>
            <if test="null != updateTime">
                #{updateTime},
            </if>
            <if test="null != updateBy and '' != updateBy">
                #{updateBy},
            </if>
            <if test="null != remark and '' != remark">
                #{remark},
            </if>
            <if test="null != source">
                #{source}
            </if>
        </trim>
    </insert>

    <insert id="insertUserAccount">
        insert into xhpc_user_account_statement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != userId">
                user_id,
            </if>
            <if test="null != amount">
                amount,
            </if>
            <if test="null != remainingSum">
                remaining_sum,
            </if>
            <if test="null != chargeOrderId">
                charge_order_id,
            </if>
            <if test="null != rechargeOrderId">
                recharge_order_id,
            </if>
            <if test="null != refundOrderId">
                refund_order_id,
            </if>
            <if test="null != type">
                type,
            </if>
            <if test="null != tenantId and tenantId !=''">
                tenant_id,
            </if>
            <if test="null != source">
                source,
            </if>
            <if test="null != createTime">
                create_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != userId">
                #{userId},
            </if>
            <if test="null != amount">
                #{amount},
            </if>
            <if test="null != remainingSum">
                #{remainingSum},
            </if>
            <if test="null != chargeOrderId">
                #{chargeOrderId},
            </if>
            <if test="null != rechargeOrderId">
                #{rechargeOrderId},
            </if>
            <if test="null != refundOrderId">
                #{refundOrderId},
            </if>
            <if test="null != type">
                #{type},
            </if>
            <if test="null != tenantId and tenantId !=''">
                #{tenantId},
            </if>
            <if test="null != source">
                #{source},
            </if>
            <if test="null != createTime">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
   </insert>

    <select id="getTenantList" resultType="map">
            select
                xt.tenant_id as tenantId,
                xt.tenant_name as tenantName
            from xhpc_charging_station as xcs
            join xhpc_tenant as xt on xcs.tenant_id = xt.tenant_id and xcs.del_flag=0
            where xcs.del_flag=0
            <if test="number==1">
                and xcs.charging_station_id in (select charging_station_id from xhpc_charging_station where operator_id=#{userId})
            </if>
            <if test="number==2">
                and xcs.charging_station_id in (select charging_station_id from xhpc_user_privilege where user_id=#{userId})
            </if>
            GROUP BY xt.tenant_id
    </select>
</mapper>
