<?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.activity.mapper.XhpcHistoryOrderMapper">

    <resultMap type="com.xhpc.order.domain.XhpcHistoryOrder" id="XhpcHistoryOrderResult">
        <id column="history_order_id" jdbcType="BIGINT" property="historyOrderId" />
        <result column="charging_station_id" jdbcType="BIGINT" property="chargingStationId" />
        <result column="charge_order_id" jdbcType="BIGINT" property="chargeOrderId" />
        <result column="user_id" jdbcType="BIGINT" property="userId" />
        <result column="terminal_id" jdbcType="BIGINT" property="terminalId" />
        <result column="serial_number" jdbcType="VARCHAR" property="serialNumber" />
        <result column="internet_serial_number" jdbcType="VARCHAR" property="internetSerialNumber" />
        <result column="power_price_total" jdbcType="DECIMAL" property="powerPriceTotal" />
        <result column="service_price_total" jdbcType="DECIMAL" property="servicePriceTotal" />
        <result column="total_price" jdbcType="DECIMAL" property="totalPrice" />
        <result column="activity_power_price_total" jdbcType="DECIMAL" property="activityPowerPriceTotal"/>
        <result column="activity_service_price_total" jdbcType="DECIMAL" property="activityServicePriceTotal"/>
        <result column="activity_total_price" jdbcType="DECIMAL" property="activityTotalPrice"/>
        <result column="promotion_discount" jdbcType="DECIMAL" property="promotionDiscount" />
        <result column="act_price" jdbcType="DECIMAL" property="actPrice" />
        <result column="act_power_price" jdbcType="DECIMAL" property="actPowerPrice" />
        <result column="act_service_price" jdbcType="DECIMAL" property="actServicePrice" />
        <result column="internet_commission" jdbcType="DECIMAL" property="internetCommission" />
        <result column="internet_svc_commission" jdbcType="DECIMAL" property="internetSvcCommission" />
        <result column="platform_commission" jdbcType="DECIMAL" property="platformCommission" />
        <result column="platform_svc_commisssion" jdbcType="DECIMAL" property="platformSvcCommisssion" />
        <result column="operation_commission" jdbcType="DECIMAL" property="operationCommission" />
        <result column="operation_svc_commission" jdbcType="DECIMAL" property="operationSvcCommission" />
        <result column="start_soc" jdbcType="VARCHAR" property="startSoc" />
        <result column="end_soc" jdbcType="VARCHAR" property="endSoc" />
        <result column="reconciliation_status" jdbcType="INTEGER" property="reconciliationStatus" />
        <result column="sorting_status" jdbcType="INTEGER" property="sortingStatus" />
        <result column="type" jdbcType="TINYINT" property="type" />
        <result column="status" jdbcType="INTEGER" property="status" />
        <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="state" jdbcType="INTEGER" property="state" />
        <result column="vin_normal" jdbcType="VARCHAR" property="vinNormal" />
        <result column="search_value" jdbcType="VARCHAR" property="searchValue" />
        <result column="operator_id_evcs" jdbcType="VARCHAR" property="operatorIdEvcs" />
        <result column="charge_model_evcs" jdbcType="INTEGER" property="chargeModelEvcs" />
        <result column="connector_power_evcs" jdbcType="DOUBLE" property="connectorPowerEvcs" />
        <result column="meter_value_end_evcs" jdbcType="DOUBLE" property="meterValueEndEvcs" />
        <result column="meter_value_start_evcs" jdbcType="DOUBLE" property="meterValueStartEvcs" />
        <result column="operator_id3rdpty_evcs" jdbcType="VARCHAR" property="operatorId3rdptyEvcs" />
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime" />
        <result column="stop_reason_evcs" jdbcType="VARCHAR" property="stopReasonEvcs" />
        <result column="total_power" jdbcType="DOUBLE" property="totalPower" />
        <result column="user_name_evcs" jdbcType="VARCHAR" property="userNameEvcs" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
        <result column="evcs_order_no" jdbcType="VARCHAR" property="evcsOrderNo" />
        <result column="confirm_Result" jdbcType="INTEGER" property="confirmResult" />
        <result column="rate_model_id" jdbcType="BIGINT" property="rateModelId" />
        <result column="charging_mode" jdbcType="VARCHAR" property="chargingMode" />
        <result column="internet_degree_commission" jdbcType="DECIMAL" property="internetDegreeCommission" />
        <result column="source" jdbcType="INTEGER" property="source" />
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
    </resultMap>
    <sql id="Base_Column_List">
        ho.history_order_id, ho.charging_station_id, ho.charge_order_id, ho.user_id, ho.terminal_id, ho.serial_number,
    ho.internet_serial_number, ho.power_price_total, ho.service_price_total, ho.total_price, ho.promotion_discount,
    ho.act_price, ho.act_power_price, ho.act_service_price, ho.internet_commission, ho.internet_svc_commission,
    ho.platform_commission, ho.platform_svc_commisssion, ho.operation_commission, ho.operation_svc_commission,
    ho.start_soc, ho.end_soc, ho.reconciliation_status, ho.sorting_status, ho.`type`, ho.`status`, ho.del_flag,
    ho.create_time, ho.create_by, ho.update_time, ho.update_by, ho.remark, ho.`state`, ho.vin_normal, ho.search_value,
    ho.operator_id_evcs, ho.charge_model_evcs, ho.connector_power_evcs, ho.meter_value_end_evcs,
    ho.meter_value_start_evcs, ho.operator_id3rdpty_evcs, ho.start_time, ho.end_time, ho.stop_reason_evcs,
    ho.total_power, ho.user_name_evcs, ho.phone, ho.evcs_order_no, ho.confirm_Result, ho.rate_model_id,
    ho.charging_mode, ho.internet_degree_commission, ho.`source`, ho.tenant_id, ho.activity_power_price_total,
    ho.activity_service_price_total, ho.activity_total_price
    </sql>


    <select id="selectUnclearingAmount" resultType="java.math.BigDecimal">
        select
        ifnull(sum(ho.act_power_price + ho.act_service_price), 0)
        from xhpc_clearing_history_order as ho
        left join xhpc_charging_station as ct on ct.charging_station_id = ho.charging_station_id
        left join xhpc_operator as op on op.operator_id = ct.operator_id
        where ho.del_flag=0 and ho.check_status not in (1,2) and ct.operator_id=#{operatorId}
    </select>


    <update id="updateStatusBatchByOrderIds">
        update xhpc_history_order set state=#{status}
        where find_in_set(history_order_id, #{orderIds})
    </update>


    <select id="getOrderPage" resultType="map">
        select
        co.serial_number as 'serialNumber',
        co.source as 'source',
        case co.source when 0 then 'C端用户'
        when 1 then '流量方用户'
        when 2 then '社区用户'
        when 3 then 'B端用户' end  as 'sourceName',
        replace(co.start_time, 'T', ' ')  as 'startTime',
        replace(co.end_time, 'T', ' ') as 'endTime'
        from xhpc_charge_order as co
        left join xhpc_charging_station as st on st.charging_station_id =co.charging_station_id
        where co.del_flag =0
        <if test="params.orderId!=null and params.orderId!=''">
            and co.serial_number like concat('%', #{params.orderId},'%')
        </if>
        <if test="params.number !=0 and params.number ==1">
            and co.charging_station_id in(select charging_station_id from xhpc_charging_station where operator_id=#{params.operatorId})
        </if>
        <if test="params.number !=0 and params.number ==2">
            and co.charging_station_id in(select charging_station_id from xhpc_user_privilege where user_id=#{params.operatorId})
        </if>
        <if test="params.tenantId != null and params.tenantId != ''">
            and co.tenant_id = #{params.tenantId}
        </if>
        <if test="params.startTime!=null and params.startTime!=''">
            and co.start_time <![CDATA[ >=  ]]> #{params.startTime}
        </if>
        <if test="params.endTime !=null and params.endTime!=''">
            and co.create_time <![CDATA[ <=  ]]> #{params.endTime}
        </if>

        order by co.create_time desc
    </select>


    <select id="findOneBySerialNumber" resultType="com.xhpc.activity.domain.XhpcHistoryOrderDomain">
        select
        <include refid="Base_Column_List" />,
            s.name as 'charging_station_name'
        from
            xhpc_history_order ho
                left join xhpc_charging_station s on ho.charging_station_id=s.charging_station_id
        where ho.serial_number=#{serialNumber}
    </select>

    <select id="findOneByInternetSerialNumber" resultType="com.xhpc.activity.domain.XhpcHistoryOrderDomain">
        select
        <include refid="Base_Column_List" />,
            s.name as 'charging_station_name',
                c.charging_degree as 'charging_degree',
                c.charging_time as 'charging_time'
        from xhpc_history_order ho
                 left join xhpc_charging_station s on ho.charging_station_id=s.charging_station_id
                 left join xhpc_charge_order c on ho.charge_order_id=c.charge_order_id
        where ho.internet_serial_number=#{internetSerialNumber}
    </select>

    <select id="findListByInternetSerialNumbers" resultType="com.xhpc.activity.domain.XhpcHistoryOrderDomain">
        select
        <include refid="Base_Column_List" />,
        s.name as 'charging_station_name',
        c.charging_degree as 'charging_degree',
        c.charging_time as 'charging_time'
        from xhpc_history_order ho
        left join xhpc_charging_station s on ho.charging_station_id=s.charging_station_id
        left join xhpc_charge_order c on ho.charge_order_id=c.charge_order_id
        where ho.internet_serial_number in
        <foreach collection="orderNumbers" item="orderNumber" open="(" close=")" separator=",">
            #{orderNumber}
        </foreach>
    </select>

    <select id="findListByTimeBetween" resultType="com.xhpc.activity.domain.XhpcHistoryOrderDomain">
        select
        <include refid="Base_Column_List" />,
        s.name as 'charging_station_name',
        c.charging_degree as 'charging_degree',
        c.charging_time as 'charging_time'
        from xhpc_history_order ho
        left join xhpc_charging_station s on ho.charging_station_id=s.charging_station_id
        left join xhpc_charge_order c on ho.charge_order_id=c.charge_order_id
        where ho.del_flag =0 and ho.source=1 and ho.tenant_id=#{tenantId}
        <if test="startTime!=null and startTime != ''">
            and ho.create_time <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime !=''">
            and ho.create_time <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="chargingMode != null and chargingMode !=''">
            and ho.charging_mode=#{chargingMode}
        </if>
    </select>


    <select id="selectUnsyncOrderListCount" resultType="java.lang.Integer">
        select
            count(1)
        from xhpc_history_order as ho
        where ho.state <![CDATA[ <>  ]]> 3 and ho.del_flag=0
        <if test="endTime !=null and endTime !=''">
            and ho.update_time <![CDATA[ <=  ]]> #{endTime}
        </if>
    </select>



    <select id="selectUnsyncOrderList" resultType="com.xhpc.activity.domain.XhpcHistoryOrderDomain">
        select
        <include refid="Base_Column_List" />,
        op.operator_id,
        op.name as 'operator_name'
        from xhpc_history_order as ho
        left join xhpc_charging_station as ct on ct.charging_station_id = ho.charging_station_id
        left join xhpc_operator as op on op.operator_id = ct.operator_id
        where ho.state <![CDATA[ <>  ]]> 3 and ho.del_flag=0 limit 1000
    </select>


    <select id="selectSyncedOrderIds" resultType="java.lang.String">
        SELECT GROUP_CONCAT( `history_order_id`)   FROM `xhpc_history_order` WHERE state <![CDATA[ <>  ]]> 3 and `del_flag` = 0 and `history_order_id`  in ( SELECT clearing_order_id from `xhpc_clearing_history_order`)
    </select>
</mapper>
