<?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.card.mapper.XhpcRateTimeMapper">
    <resultMap id="BaseResultMap" type="com.xhpc.card.pojo.XhpcRateTime">
        <id column="rate_time_id" jdbcType="BIGINT" property="rateTimeId"/>
        <result column="charging_station_id" jdbcType="BIGINT" property="chargingStationId"/>
        <result column="rate_id" jdbcType="BIGINT" property="rateId"/>
        <result column="start_time" jdbcType="TIME" property="startTime"/>
        <result column="end_time" jdbcType="TIME" property="endTime"/>
        <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="rate_model_id" jdbcType="BIGINT" property="rateModelId"/>
        <result column="sort" jdbcType="INTEGER" property="sort"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="rate_value" jdbcType="VARCHAR" property="rateValue"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
    </resultMap>
    <sql id="Base_Column_List">
        rate_time_id
        , charging_station_id, rate_id, start_time, end_time, `status`, del_flag,
    create_time, create_by, update_time, update_by, remark, rate_model_id, sort, `type`, 
    rate_value, tenant_id
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from xhpc_rate_time
        where rate_time_id = #{rateTimeId,jdbcType=BIGINT}
    </select>
    <select id="selectByTenantIdsAndCharingStationIds" resultType="Map">
        select
        ifnull(ROUND(ro.charging_degree,2),0) as chargingDegree,
        DATE_FORMAT(co.start_time,"%H:%i") as startTime,
        ifnull(ro.amount_charged,0) amountCharged,
        ifnull(ro.electric_current,0) electricCurrent,
        ro.real_time_order_id as realTimeOrderId,
        ifnull(ro.voltage,0) as voltage,
        ifnull(ro.soc,0) as soc,
        ro.charging_time chargingTime,
        CASE
        WHEN co.charging_mode = "新电途" THEN
        "新电途用户"
        WHEN co.charging_mode = "快电" THEN
        "快电用户"
        WHEN co.charging_mode = "恒大" THEN
        "恒大用户"
        WHEN co.charging_mode = "小桔" THEN
        "小桔用户"
        ELSE
        "C端用户"
        END chargingMode
        from xhpc_charge_order co
        left join xhpc_internet_user as inu on inu.internet_user_id = co.user_id and co.source =1
        left join xhpc_app_user as apu on apu.app_user_id = co.user_id and co.source =0
        left join xhpc_community_personnel as cop on cop.community_personnel_id = co.user_id and co.source =2
        left join xhpc_customers_personnel as cup on cup.customers_personnel_id = co.user_id and co.source =3
        LEFT JOIN (
        SELECT charging_order_id,real_time_order_id,soc,charging_degree,charging_time,amount_charged,electric_current,voltage FROM xhpc_real_time_order
        WHERE real_time_order_id IN ( SELECT max( real_time_order_id ) FROM xhpc_real_time_order GROUP BY charging_order_id )
        ) ro ON ro.charging_order_id = co.charge_order_id
        where co.status=0 and co.del_flag=0
        <if test="tenantIdsStr!=null and tenantIdsStr!='' ">
            and find_in_set(co.tenant_id, #{tenantIdsStr})
        </if>
        <if test="xhpcChargingStationList!=null and xhpcChargingStationList.size()!=0">
            and co.charging_station_id in
            <foreach collection="xhpcChargingStationList" open="(" close=")" separator="," item="chargingStationId">
                #{chargingStationId}
            </foreach>
        </if>
        order by co.create_time desc
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from xhpc_rate_time
        where rate_time_id = #{rateTimeId,jdbcType=BIGINT}
    </delete>
    <insert id="insert" keyColumn="rate_time_id" keyProperty="rateTimeId"
            parameterType="com.xhpc.card.pojo.XhpcRateTime" useGeneratedKeys="true">
        insert into xhpc_rate_time (charging_station_id, rate_id, start_time,
                                    end_time, `status`, del_flag,
                                    create_time, create_by, update_time,
                                    update_by, remark, rate_model_id,
                                    sort, `type`, rate_value,
                                    tenant_id)
        values (#{chargingStationId,jdbcType=BIGINT}, #{rateId,jdbcType=BIGINT}, #{startTime,jdbcType=TIME},
                #{endTime,jdbcType=TIME}, #{status,jdbcType=INTEGER}, #{delFlag,jdbcType=INTEGER},
                #{createTime,jdbcType=TIMESTAMP}, #{createBy,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP},
                #{updateBy,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{rateModelId,jdbcType=BIGINT},
                #{sort,jdbcType=INTEGER}, #{type,jdbcType=INTEGER}, #{rateValue,jdbcType=VARCHAR},
                #{tenantId,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" keyColumn="rate_time_id" keyProperty="rateTimeId"
            parameterType="com.xhpc.card.pojo.XhpcRateTime" useGeneratedKeys="true">
        insert into xhpc_rate_time
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="chargingStationId != null">
                charging_station_id,
            </if>
            <if test="rateId != null">
                rate_id,
            </if>
            <if test="startTime != null">
                start_time,
            </if>
            <if test="endTime != null">
                end_time,
            </if>
            <if test="status != null">
                `status`,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="rateModelId != null">
                rate_model_id,
            </if>
            <if test="sort != null">
                sort,
            </if>
            <if test="type != null">
                `type`,
            </if>
            <if test="rateValue != null">
                rate_value,
            </if>
            <if test="tenantId != null">
                tenant_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="chargingStationId != null">
                #{chargingStationId,jdbcType=BIGINT},
            </if>
            <if test="rateId != null">
                #{rateId,jdbcType=BIGINT},
            </if>
            <if test="startTime != null">
                #{startTime,jdbcType=TIME},
            </if>
            <if test="endTime != null">
                #{endTime,jdbcType=TIME},
            </if>
            <if test="status != null">
                #{status,jdbcType=INTEGER},
            </if>
            <if test="delFlag != null">
                #{delFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createBy != null">
                #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="rateModelId != null">
                #{rateModelId,jdbcType=BIGINT},
            </if>
            <if test="sort != null">
                #{sort,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                #{type,jdbcType=INTEGER},
            </if>
            <if test="rateValue != null">
                #{rateValue,jdbcType=VARCHAR},
            </if>
            <if test="tenantId != null">
                #{tenantId,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.xhpc.card.pojo.XhpcRateTime">
        update xhpc_rate_time
        <set>
            <if test="chargingStationId != null">
                charging_station_id = #{chargingStationId,jdbcType=BIGINT},
            </if>
            <if test="rateId != null">
                rate_id = #{rateId,jdbcType=BIGINT},
            </if>
            <if test="startTime != null">
                start_time = #{startTime,jdbcType=TIME},
            </if>
            <if test="endTime != null">
                end_time = #{endTime,jdbcType=TIME},
            </if>
            <if test="status != null">
                `status` = #{status,jdbcType=INTEGER},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createBy != null">
                create_by = #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="rateModelId != null">
                rate_model_id = #{rateModelId,jdbcType=BIGINT},
            </if>
            <if test="sort != null">
                sort = #{sort,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                `type` = #{type,jdbcType=INTEGER},
            </if>
            <if test="rateValue != null">
                rate_value = #{rateValue,jdbcType=VARCHAR},
            </if>
            <if test="tenantId != null">
                tenant_id = #{tenantId,jdbcType=VARCHAR},
            </if>
        </set>
        where rate_time_id = #{rateTimeId,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.xhpc.card.pojo.XhpcRateTime">
        update xhpc_rate_time
        set charging_station_id = #{chargingStationId,jdbcType=BIGINT},
            rate_id             = #{rateId,jdbcType=BIGINT},
            start_time          = #{startTime,jdbcType=TIME},
            end_time            = #{endTime,jdbcType=TIME},
            `status`            = #{status,jdbcType=INTEGER},
            del_flag            = #{delFlag,jdbcType=INTEGER},
            create_time         = #{createTime,jdbcType=TIMESTAMP},
            create_by           = #{createBy,jdbcType=VARCHAR},
            update_time         = #{updateTime,jdbcType=TIMESTAMP},
            update_by           = #{updateBy,jdbcType=VARCHAR},
            remark              = #{remark,jdbcType=VARCHAR},
            rate_model_id       = #{rateModelId,jdbcType=BIGINT},
            sort                = #{sort,jdbcType=INTEGER},
            `type`              = #{type,jdbcType=INTEGER},
            rate_value          = #{rateValue,jdbcType=VARCHAR},
            tenant_id           = #{tenantId,jdbcType=VARCHAR}
        where rate_time_id = #{rateTimeId,jdbcType=BIGINT}
    </update>
</mapper>