<?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.charging.station.mapper.XhpcTimingChargingModelMapper">

    <resultMap id="BaseResultMap" type="com.xhpc.common.domain.XhpcTimingChargingModel">
        <result property="timingChargingModelId" column="timing_charging_model_id"/>
        <result property="reason" column="reason"/>
        <result property="chargingStationId" column="charging_station_id"/>
        <result property="timingTime" column="timing_time"/>
        <result property="errorReason" column="error_reason"/>
        <result property="number" column="number"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="phone" column="phone"/>
    </resultMap>

    <resultMap id="CacheRatetMap" type="com.xhpc.common.domain.XhpcTimingChargingCacheRateModel">
        <result property="timingChargingCacheRateModelId" column="timing_charging_cache_rate_model_id"/>
        <result property="timingChargingModelId" column="timing_charging_model_id"/>
        <result property="onePrice" column="one_price"/>
        <result property="oneSvcPrice" column="one_svc_price"/>
        <result property="twoPrice" column="two_price"/>
        <result property="twoSvcPrice" column="two_svc_price"/>
        <result property="threePrice" column="three_price"/>
        <result property="threeSvcPrice" column="three_svc_price"/>
        <result property="fourPrice" column="four_price"/>
        <result property="fourSvcPrice" column="four_svc_price"/>
        <result property="lossRate" column="loss_rate"/>
        <result property="tfPricesSeq" column="tf_prices_seq"/>
        <result property="pp" column="pp"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="chargingStationId" column="charging_station_id"/>
        <result property="phone" column="phone"/>
    </resultMap>

    <resultMap id="TimingChargingRateMap" type="com.xhpc.common.domain.XhpcTimingChargingRate">
        <result property="timingChargingRateId" column="timing_charging_cache_rate_model_id"/>
        <result property="chargingStationId" column="timing_charging_model_id"/>
        <result property="powerFee" column="power_fee"/>
        <result property="serviceFee" column="service_fee"/>
        <result property="name" column="name"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="rateValue" column="rate_value"/>
        <result property="tenantId" column="tenant_id"/>
    </resultMap>

    <resultMap id="TimingChargingTimeMap" type="com.xhpc.common.domain.XhpcTimingChargingTime">
        <result property="timingChargingTimeId" column="timing_charging_time_id"/>
        <result property="chargingStationId" column="timing_charging_model_id"/>
        <result property="chargingStationId" column="charging_station_id"/>
        <result property="timingChargingRateId" column="timing_charging_rate_id"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="sort" column="sort"/>
        <result property="type" column="type"/>
        <result property="rateValue" column="rate_value"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="rateValue" column="rate_value"/>
    </resultMap>

    <select id="list" resultType="map">
        select
            tcm.timing_charging_model_id as timingChargingModelId,
            tcm.reason as reason,
            cs.name as chargingStationName,
            tcm.timing_time as timingTime,
            tcm.status as status,
            tcm.create_time as createTime
        from xhpc_timing_charging_model as tcm
        left join xhpc_charging_station as cs on cs.charging_station_id = tcm.charging_station_id
        where tcm.del_flag =0
        <if test="operatorId !=null">
            and cs.operator_id = #{operatorId}
        </if>
        <if test="chargingStationId !=null">
            and tcm.charging_station_id = #{chargingStationId}
        </if>
        <if test="reason !=null and ''!=reason">
            and tcm.reason like concat('%', #{reason}, '%')
        </if>
        <if test="status !=null">
            and tcm.status = #{status}
        </if>
        <if test="startTime !=null and '' !=startTime">
            and tcm.timing_time &gt;=#{startTime}
        </if>
        <if test="endTime !=null and '' !=endTime">
            and tcm.timing_time &lt;=#{endTime}
        </if>
        <if test="tenantId !=null and '' !=tenantId">
            and tcm.tenant_id =#{tenantId}
        </if>
        <if test="type ==1">
            and cs.charging_station_id in(select charging_station_id from xhpc_charging_station where operator_id=#{logOperatorId})
        </if>
        <if test="type ==2">
            and cs.charging_station_id in(select charging_station_id from xhpc_user_privilege where user_id=#{logOperatorId})
        </if>
        order by tcm.status asc, tcm.create_time desc
    </select>

    <insert id="insertXhpcTimingChargingModel" parameterType="com.xhpc.common.domain.XhpcTimingChargingModel" useGeneratedKeys="true" keyProperty="timingChargingModelId">
        insert into xhpc_timing_charging_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != reason and ''!=reason">
                reason,
            </if>
            <if test="null != chargingStationId ">
                charging_station_id,
            </if>
            <if test="null != timingTime ">
                timing_time,
            </if>
            <if test="null != tenantId ">
                tenant_id,
            </if>
            <if test="null != status ">
                status,
            </if>
            <if test="null != 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 != phone and '' != phone">
                phone
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != reason and ''!=reason">
                #{reason},
            </if>
            <if test="null != chargingStationId ">
                #{chargingStationId},
            </if>
            <if test="null != timingTime ">
                #{timingTime},
            </if>
            <if test="null != tenantId ">
                #{tenantId},
            </if>
            <if test="null != status ">
                #{status},
            </if>
            <if test="null != delFlag ">
                #{delFlag},
            </if>
            <if test="null != createTime ">
                #{createTime},
            </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 != phone and '' != phone">
                #{phone}
            </if>
        </trim>
    </insert>

    <insert id="insertXhpcTtimingChargingRate" parameterType="com.xhpc.common.domain.XhpcTimingChargingRate" useGeneratedKeys="true" keyProperty="timingChargingRateId">
        insert into xhpc_timing_charging_rate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != powerFee">
                power_fee,
            </if>
            <if test="null != serviceFee">
                service_fee,
            </if>
            <if test="null != chargingStationId ">
                charging_station_id,
            </if>
            <if test="null != name and ''!=name">
                name,
            </if>
            <if test="null != timingChargingModelId ">
                timing_charging_model_id,
            </if>
            <if test="null != rateValue and ''!=rateValue ">
                rate_value,
            </if>
            <if test="null != tenantId ">
                tenant_id,
            </if>
            <if test="null != status ">
                status,
            </if>
            <if test="null != 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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != powerFee">
                #{powerFee},
            </if>
            <if test="null != serviceFee">
                #{serviceFee},
            </if>
            <if test="null != chargingStationId ">
                #{chargingStationId},
            </if>
            <if test="null != name and ''!=name">
                #{name},
            </if>
            <if test="null != timingChargingModelId ">
                #{timingChargingModelId},
            </if>
            <if test="null != rateValue and ''!=rateValue ">
                #{rateValue},
            </if>
            <if test="null != tenantId ">
                #{tenantId},
            </if>
            <if test="null != status ">
                #{status},
            </if>
            <if test="null != delFlag ">
                #{delFlag},
            </if>
            <if test="null != createTime ">
                #{createTime},
            </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>
        </trim>
    </insert>

    <insert id="insertXhpcTimingChargingTime" parameterType="com.xhpc.common.domain.XhpcTimingChargingTime" useGeneratedKeys="true" keyProperty="timingChargingTimeId">
        insert into xhpc_timing_charging_time
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != timingChargingRateId">
                timing_charging_rate_id,
            </if>
            <if test="null != chargingStationId ">
                charging_station_id,
            </if>
            <if test="null != startTime">
                start_time,
            </if>
            <if test="null != endTime">
                end_time,
            </if>
            <if test="null != timingChargingModelId ">
                timing_charging_model_id,
            </if>
            <if test="null != rateValue and ''!=rateValue ">
                rate_value,
            </if>
            <if test="null != type ">
                type,
            </if>
            <if test="null != status ">
                status,
            </if>
            <if test="null != 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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != timingChargingRateId">
                #{timingChargingRateId},
            </if>
            <if test="null != chargingStationId ">
                #{chargingStationId},
            </if>
            <if test="null != startTime">
                #{startTime},
            </if>
            <if test="null != endTime">
                #{endTime},
            </if>
            <if test="null != timingChargingModelId ">
                #{timingChargingModelId},
            </if>
            <if test="null != rateValue and ''!=rateValue ">
                #{rateValue},
            </if>
            <if test="null != type ">
                #{type},
            </if>
            <if test="null != status ">
                #{status},
            </if>
            <if test="null != delFlag ">
                #{delFlag},
            </if>
            <if test="null != createTime ">
                #{createTime},
            </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>
        </trim>
    </insert>

    <insert id="insertXhpcTimingChargingCacheRateModel">
        insert into xhpc_timing_charging_cache_rate_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != timingChargingModelId">
                timing_charging_model_id,
            </if>
            <if test="null != onePrice ">
                one_price,
            </if>
            <if test="null != oneSvcPrice">
                one_svc_price,
            </if>
            <if test="null != twoPrice">
                two_price,
            </if>
            <if test="null != twoSvcPrice ">
                two_svc_price,
            </if>
            <if test="null != threePrice ">
                three_price,
            </if>
            <if test="null != threeSvcPrice ">
                three_svc_price,
            </if>
            <if test="null != fourPrice ">
                four_price,
            </if>
            <if test="null != fourSvcPrice ">
                four_svc_price,
            </if>
            <if test="null != lossRate ">
                loss_rate,
            </if>
            <if test="null != tfPricesSeq and ''!=tfPricesSeq">
                tf_prices_seq,
            </if>
            <if test="null != pp and ''!=pp">
                pp,
            </if>
            <if test="null != tenantId ">
                tenant_id,
            </if>
            <if test="null != status ">
                status,
            </if>
            <if test="null != 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 != chargingStationId and '' != chargingStationId">
                charging_station_id,
            </if>
            <if test="null != phone and '' != phone">
                phone,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != timingChargingModelId">
                #{timingChargingModelId},
            </if>
            <if test="null != onePrice ">
                #{onePrice},
            </if>
            <if test="null != oneSvcPrice">
                #{oneSvcPrice},
            </if>
            <if test="null != twoPrice">
                #{twoPrice},
            </if>
            <if test="null != twoSvcPrice ">
                #{twoSvcPrice},
            </if>
            <if test="null != threePrice ">
                #{threePrice},
            </if>
            <if test="null != threeSvcPrice ">
                #{threeSvcPrice},
            </if>
            <if test="null != fourPrice ">
                #{fourPrice},
            </if>
            <if test="null != fourSvcPrice ">
                #{fourSvcPrice},
            </if>
            <if test="null != lossRate ">
                #{lossRate},
            </if>
            <if test="null != tfPricesSeq and ''!=tfPricesSeq">
                #{tfPricesSeq},
            </if>
            <if test="null != pp and ''!=pp">
                #{pp},
            </if>
            <if test="null != tenantId ">
                #{tenantId},
            </if>
            <if test="null != status ">
                #{status},
            </if>
            <if test="null != delFlag ">
                #{delFlag},
            </if>
            <if test="null != createTime ">
                #{createTime},
            </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 != chargingStationId and '' != chargingStationId">
                #{chargingStationId},
            </if>
            <if test="null != phone and '' != phone">
                #{phone},
            </if>
        </trim>
    </insert>

    <select id="getXhpcTimingChargingModelById" resultMap="BaseResultMap">
        select * from xhpc_timing_charging_model where timing_charging_model_id =#{timingChargingModelId} and del_flag=0
        <if test="tenantId !=null and ''!=tenantId">
            and tenant_id=#{tenantId}
        </if>
    </select>

    <update id="updateXhpcTimingChargingModel" parameterType="Integer">
        update xhpc_timing_charging_model
        <trim prefix="SET" suffixOverrides=",">
            <if test="chargingStationId != null">charging_station_id = #{chargingStationId},</if>
            <if test="reason != null and ''!=reason">reason = #{reason},</if>
            <if test="timingTime != null">timing_time = #{timingTime},</if>
            <if test="errorReason != null and ''!=errorReason">error_reason = #{errorReason},</if>
            <if test="number != null">number = #{number},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="phone != null">phone = #{phone},</if>
        </trim>
        where timing_charging_model_id =#{timingChargingModelId}
    </update>

    <update id="updateXhpcTtimingChargingRate">
        update xhpc_timing_charging_rate set del_flag=2 where timing_charging_model_id =#{timingChargingModelId}
    </update>

    <update id="updateXhpcTimingChargingTime">
        update xhpc_timing_charging_time set del_flag=2 where timing_charging_model_id =#{timingChargingModelId}
    </update>

    <update id="updateXhpcTimingChargingCacheRateModel">
        update xhpc_timing_charging_cache_rate_model set del_flag=2 where timing_charging_model_id =#{timingChargingModelId}
    </update>

    <select id="getXhpcTimingChargingRateList" resultType="map">
        select power_fee   as powerFee,
               service_fee as serviceFee,
               name        as name,
               rate_value  as id
        from xhpc_timing_charging_rate
        where timing_charging_model_id = #{timingChargingModelId}
          and del_flag = 0
        GROUP BY rate_value
    </select>

    <select id="getXhpcTimingChargingTimeTypeList" resultType="java.util.Map">
        select rt.start_time                                as startTime,
               replace(rt.end_time, '00:00:00', '24:00:00') AS endTime,
               rt.timing_charging_time_id                   as timingChargingTimeId,
               rt.rate_value                                as id,
               ra.name                                      as rateName,
               ra.power_fee                                 as powerFee,
               ra.service_fee                               as serviceFee
        from xhpc_timing_charging_time as rt
        left join xhpc_timing_charging_rate as ra on ra.timing_charging_rate_id = rt.timing_charging_rate_id
        where rt.timing_charging_model_id = #{timingChargingModelId}
          and rt.del_flag = 0
    </select>

    <select id="getXhpcTimingChargingModelMapById" resultType="map">
        select
            tcm.timing_charging_model_id as timingChargingModelId,
            tcm.reason as reason,
            cs.name as chargingStationName,
            cs.charging_station_id as chargingStationId,
            tcm.timing_time as timingTime,
            tcm.status as status,
            tcm.create_time as createTime,
            tcm.phone as phone
        from xhpc_timing_charging_model as tcm
        left join xhpc_charging_station as cs on cs.charging_station_id = tcm.charging_station_id
        where tcm.timing_charging_model_id =#{timingChargingModelId}
          and tcm.del_flag=0
        <if test="tenantId !=null and ''!=tenantId">
            and tcm.tenant_id=#{tenantId}
        </if>
    </select>

    <select id="getXhpcTimingChargingModelId" resultMap="CacheRatetMap">
        select
               crm.*
        from xhpc_timing_charging_cache_rate_model crm
        join xhpc_timing_charging_model tcm on crm.timing_charging_model_id = tcm.timing_charging_model_id and tcm.status =0 and tcm.del_flag =0 and now() &gt; tcm.timing_time
        where  crm.del_flag=0
    </select>

    <insert id="insertXhpcRateModel" parameterType="com.xhpc.common.domain.XhpcRateModel" useGeneratedKeys="true"
            keyProperty="rateModelId">
        insert into xhpc_rate_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != status ">
                status,
            </if>
            <if test="null != 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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != status ">
                #{status},
            </if>
            <if test="null != delFlag ">
                #{delFlag},
            </if>
            <if test="null != createTime ">
                #{createTime},
            </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>
        </trim>
    </insert>

    <select id="getRateTimingChargingModelId" resultMap="TimingChargingRateMap">
        select * from xhpc_timing_charging_rate where del_flag =0 and timing_charging_model_id =#{timingChargingModelId}
    </select>

    <select id="getTimeTimingChargingModelId" resultMap="TimingChargingTimeMap">
        select * from xhpc_timing_charging_time where del_flag =0 and timing_charging_model_id =#{timingChargingModelId}
    </select>

</mapper>
