<?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">
<!--suppress ALL -->
<mapper namespace="com.xhpc.user.mapper.XhpcCommonMapper">


    <select id="getAppUser" resultType="map">
        select
            xau.app_user_id as appUserId,
            xau.phone as phone,
            xau.is_refund_application as isRefundApplication,
            xau.is_refund as isRefund,
            xau.soc as socUser,
            xau.balance as balance,
            xau.avatar as avatar,
            xau.status,
            xau.weixin_open_id as weixinOpenId,
            xau.alipay_open_id as alipayOpenId,
            xau.weixin_login as weixinLogin,
            xau.alipay_login as alipayLogin,
            xau.del_flag delFlag,
            concat(0) as userType,
            concat("C") as userTypeName,
            xau.tenant_id tenantId,
            xau.soc_protect socProtect,
            ten.status tenantStatus,
            xau.create_time as createTime
        from xhpc_app_user xau
        left join xhpc_tenant ten on ten.tenant_id = xau.tenant_id and ten.is_deleted =0
        where xau.del_flag=0
        <if test="phone !=null and phone !=''">
            and xau.phone =#{phone}
        </if>
        <if test="userId !=null">
            and xau.app_user_id =#{userId}
        </if>
        <if test="tenantId !=null and tenantId !=''">
            and xau.tenant_id =#{tenantId}
        </if>
    </select>

    <select id="getCommunityUser" resultType="map">
        select
            xcp.community_personnel_id as appUserId,
            xcp.account as account,
            xcp.phone as phone,
            xcp.is_refund_application as isRefundApplication,
            xcp.is_refund as isRefund,
            xcp.soc as socUser,
            xcp.surplus_money as balance,
            xcp.avatar as avatar,
            xcp.status,
            xcp.weixin_open_id as weixinOpenId,
            xcp.alipay_open_id as alipayOpenId,
            xcp.weixin_login as weixinLogin,
            xcp.alipay_login as alipayLogin,
            xcp.tenant_id tenantId,
            xcp.del_flag delFlag,
            concat(2) as userType,
            xcp.soc_protect socProtect,
            ten.status tenantStatus,
            concat("ST") as userTypeName,
        <if test="serialNumber !=null and serialNumber !=''">
            (select count(charging_station_id) from xhpc_mechanism where community_id=mechanism_id and source=0 and charging_station_id in (select charging_station_id from xhpc_terminal where 1=1 and del_flag=0 and serial_number=#{serialNumber})) as number,
        </if>
            xcp.create_time as createTime
        from xhpc_community_personnel xcp
        left join xhpc_tenant ten on ten.tenant_id = xcp.tenant_id and ten.is_deleted =0
        where xcp.del_flag=0
        <if test="phone !=null and phone !=''">
            and xcp.account =#{phone}
        </if>
        <if test="userId !=null">
            and xcp.community_personnel_id =#{userId}
        </if>
        <if test="tenantId !=null and tenantId !=''">
            and xcp.tenant_id =#{tenantId}
        </if>
    </select>

    <select id="getCustomersUser" resultType="map">
        select
            xcp.customers_personnel_id as appUserId,
            xcp.account as account,
            xcp.phone as phone,
            xcp.is_refund_application as isRefundApplication,
            xcp.is_refund as isRefund,
            xcp.soc as socUser,
            xcp.surplus_money as balance,
            xcp.avatar as avatar,
            xcp.status,
            xcp.weixin_open_id as weixinOpenId,
            xcp.alipay_open_id as alipayOpenId,
            xcp.weixin_login as weixinLogin,
            xcp.alipay_login as alipayLogin,
            xcp.del_flag delFlag,
            xcp.tenant_id tenantId,
            concat(3) as userType,
            xcp.soc_protect socProtect,
            ten.status tenantStatus,
            concat("BE") as userTypeName,
         <if test="serialNumber !=null and serialNumber !=''">
             (select count(charging_station_id) from xhpc_mechanism where customers_id=mechanism_id and source=1 and charging_station_id in (select charging_station_id from xhpc_terminal where 1=1 and del_flag=0 and serial_number=#{serialNumber})) as number,
         </if>
            xcp.create_time as createTime
        from xhpc_customers_personnel xcp
        left join xhpc_tenant ten on ten.tenant_id = xcp.tenant_id and ten.is_deleted =0
        where xcp.del_flag=0
        <if test="phone !=null and phone !=''">
            and xcp.account =#{phone}
        </if>
        <if test="userId !=null">
            and xcp.customers_personnel_id =#{userId}
        </if>
        <if test="tenantId !=null and tenantId !=''">
            and xcp.tenant_id =#{tenantId}
        </if>
    </select>

    <select id="getPersonnelStatistics" resultType="map">
            select
            concat("社区用户") as mechanismName,
            xcp.account as account,
            xcp.name as name,
            xcp.phone as phone,
            count(xho.history_order_id) number,
            IFNULL(sum(xho.total_power),0) totalPower,
            IFNULL(sum(xho.total_price),0) totalPrice,
            IFNULL(sum(xho.promotion_discount),0) promotionDiscount
            from xhpc_community_personnel as xcp
            left join xhpc_community xc on xc.community_id = xcp.community_id and xc.del_flag =0
            left join xhpc_history_order xho on xho.source =2 and xho.user_id =xcp.community_personnel_id
            where xcp.del_flag=0
            <if test="tenantId !=null and tenantId!=''">
                and xcp.tenant_id=#{tenantId}
            </if>
            <if test="number==1 or number==2">
                and xcp.create_type=2 and xcp.create_id=#{userId}
            </if>
            <if test="type==1">
                and xcp.create_type=2 and xcp.create_id=#{userId}
            </if>
            <if test="terminalId !=null">
                and xho.terminal_id=#{terminalId}
            </if>
            <if test="chargingStationId !=null">
                and xcp.create_id=(select operator_id from xhpc_charging_station where charging_station_id=#{chargingStationId})
            </if>
            <if test="operatorId !=null">
                and xcp.create_id=#{operatorId}
            </if>
            <if test="chargingPileId !=null">
                and xcp.create_id=(select operator_id from xhpc_charging_station where charging_station_id=(select terminal_id from xhpc_charging_pile where charging_pile_id=#{chargingPileId}))
            </if>
            <if test="name !=null and name !=''">
                and xcp.name like concat('%',#{name},'%')
            </if>
            <if test="account !=null and account !=''">
                and xcp.account like concat('%',#{account},'%')
            </if>
            <if test="phone !=null and phone !=''">
                and xcp.phone like concat('%',#{phone},'%')
            </if>
            <if test="startTime !=null and '' !=startTime">
                and xho.end_time &gt;=#{startTime}
            </if>
            <if test="endTime !=null and '' !=endTime">
                and xho.end_time &lt;=#{endTime}
            </if>
            GROUP BY xcp.account
            union
            select
            concat("B端用户") as mechanismName,
            xcup.account as account,
            xcup.name as name,
            xcup.phone as phone,
            count(xho.history_order_id) number,
            IFNULL(sum(xho.total_power),0) totalPower,
            IFNULL(sum(xho.total_price),0) totalPrice,
            IFNULL(sum(xho.promotion_discount),0) promotionDiscount
            from xhpc_customers_personnel as xcup
            left join xhpc_customers xuc on xuc.customers_id = xcup.customers_personnel_id and xuc.del_flag =0
            left join xhpc_history_order xho on xho.source =3 and xho.user_id =xcup.customers_personnel_id
            where xcup.del_flag=0
            <if test="tenantId !=null and tenantId!=''">
                and xcup.tenant_id=#{tenantId}
            </if>
            <if test="number==1 or number==2">
                and xcup.create_type=2 and xcup.create_id=#{userId}
            </if>
            <if test="type==1">
                and xcup.create_type=2 and xcup.create_id=#{userId}
            </if>
            <if test="terminalId !=null">
                and xho.terminal_id=#{terminalId}
            </if>
            <if test="chargingStationId !=null">
                and xcup.create_id=(select operator_id from xhpc_charging_station where charging_station_id=#{chargingStationId})
            </if>
            <if test="operatorId !=null">
                and xcup.create_id=#{operatorId}
            </if>
            <if test="chargingPileId !=null">
                and xcup.create_id=(select operator_id from xhpc_charging_station where charging_station_id=(select terminal_id from xhpc_charging_pile where charging_pile_id=#{chargingPileId}))
            </if>
            <if test="name !=null and name !=''">
                and xcup.name like concat('%',#{name},'%')
            </if>
            <if test="account !=null and account !=''">
                and xcup.account like concat('%',#{account},'%')
            </if>
            <if test="phone !=null and phone !=''">
                and xcup.phone like concat('%',#{phone},'%')
            </if>
            <if test="startTime !=null and '' !=startTime">
                and xho.end_time &gt;=#{startTime}
            </if>
            <if test="endTime !=null and '' !=endTime">
                and xho.end_time &lt;=#{endTime}
            </if>
            GROUP BY xcup.account
    </select>

    <select id="getCommunityPersonnel" resultType="map">
            select
            concat("社区用户") as mechanismName,
            xcp.account as account,
            xcp.name as name,
            xcp.phone as phone,
            count(xho.history_order_id) number,
            IFNULL(sum(xho.total_power),0) totalPower,
            IFNULL(sum(xho.total_price),0) totalPrice,
            IFNULL(sum(xho.promotion_discount),0) promotionDiscount
            from xhpc_community_personnel as xcp
            left join xhpc_community xc on xc.community_id = xcp.community_id and xc.del_flag =0
            left join xhpc_history_order xho on xho.source =2 and xho.user_id =xcp.community_personnel_id
            where   xcp.del_flag=0
            <if test="tenantId !=null and tenantId!=''">
                and xcp.tenant_id=#{tenantId}
            </if>
              <if test="number==1 or number==2">
                and xcp.create_type=2 and xcp.create_id=#{userId}
            </if>
            <if test="type==1">
                and xcp.create_type=2 and xcp.create_id=#{userId}
            </if>
            <if test="chargingStationId !=null">
                and xcp.create_id=(select operator_id from xhpc_charging_station where charging_station_id=#{chargingStationId})
            </if>
            <if test="operatorId !=null">
                and xcp.create_id=#{operatorId}
            </if>
            <if test="terminalId !=null">
                and xho.terminal_id=#{terminalId}
            </if>
            <if test="chargingPileId !=null">
                and xcp.create_id=(select operator_id from xhpc_charging_station where charging_station_id=(select terminal_id from xhpc_charging_pile where charging_pile_id=#{chargingPileId}))
            </if>
            <if test="name !=null and name !=''">
                and xcp.name like concat('%',#{name},'%')
            </if>
            <if test="account !=null and account !=''">
                and xcp.account like concat('%',#{account},'%')
            </if>
            <if test="phone !=null and phone !=''">
                and xcp.phone like concat('%',#{phone},'%')
            </if>
            <if test="startTime !=null and '' !=startTime">
                and xho.end_time &gt;=#{startTime}
            </if>
            <if test="endTime !=null and '' !=endTime">
                and xho.end_time &lt;=#{endTime}
            </if>
            GROUP BY xcp.account
    </select>

    <select id="getCustomersPersonnel" resultType="map">
            select
            concat("B端用户") as mechanismName,
            xcup.account as account,
            xcup.name as name,
            xcup.phone as phone,
            count(xho.history_order_id) number,
            IFNULL(sum(xho.total_power),0) totalPower,
            IFNULL(sum(xho.total_price),0) totalPrice,
            IFNULL(sum(xho.promotion_discount),0) promotionDiscount
            from xhpc_customers_personnel as xcup
            left join xhpc_customers xuc on xuc.customers_id = xcup.customers_personnel_id and xuc.del_flag =0
            left join xhpc_history_order xho on xho.source =3 and xho.user_id =xcup.customers_personnel_id
            where  xcup.del_flag=0

            <if test="tenantId !=null and tenantId!=''">
                and xcup.tenant_id=#{tenantId}
            </if>
              <if test="number==1 or number==2">
                and xcup.create_type=2 and xcup.create_id=#{userId}
            </if>
            <if test="type==1">
                and xcup.create_type=2 and xcup.create_id=#{userId}
            </if>
            <if test="chargingStationId !=null">
                and xcup.create_id=(select operator_id from xhpc_charging_station where charging_station_id=#{chargingStationId})
            </if>
            <if test="operatorId !=null">
                and xcup.create_id=#{operatorId}
            </if>
            <if test="terminalId !=null">
                and xho.terminal_id=#{terminalId}
            </if>
            <if test="chargingPileId !=null">
                and xcup.create_id=(select operator_id from xhpc_charging_station where charging_station_id=(select terminal_id from xhpc_charging_pile where charging_pile_id=#{chargingPileId}))
            </if>
            <if test="name !=null and name !=''">
                and xcup.name like concat('%',#{name},'%')
            </if>
            <if test="account !=null and account !=''">
                and xcup.account like concat('%',#{account},'%')
            </if>
            <if test="phone !=null and phone !=''">
                and xcup.phone like concat('%',#{phone},'%')
            </if>
            <if test="startTime !=null and '' !=startTime">
                and xho.end_time &gt;=#{startTime}
            </if>
            <if test="endTime !=null and '' !=endTime">
                and xho.end_time &lt;=#{endTime}
            </if>
            GROUP BY xcup.account

    </select>
</mapper>
