<?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.jsowell.pile.mapper.PileBillingTemplateMapper">
    <resultMap type="com.jsowell.pile.domain.PileBillingTemplate" id="PileBillingTemplateResult">
        <result property="id" column="id"/>
        <result property="templateCode" column="template_code"/>
        <result property="name" column="name"/>
        <result property="status" column="status"/>
        <result property="templateRemark" column="template_remark"/>
        <result property="templateDesc" column="template_desc"/>
        <result property="type" column="type"/>
        <result property="stationId" column="station_id"/>
        <result property="freeTime" column="free_time"/>
        <result property="occupyFee" column="occupy_fee"/>
        <result property="maxOccupyFee" column="max_occupy_fee"/>
        <result property="publicFlag" column="public_flag"/>
        <result property="memberFlag" column="memberFlag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="Base_Column_List">
        <!--@mbg.generated PileBillingTemplate-->
        id, template_code, name, status, template_remark, template_desc, type, station_id, free_time, occupy_fee, max_occupy_fee, public_flag,
        member_flag, create_time, update_by, update_time, del_flag
    </sql>

    <resultMap type="com.jsowell.pile.domain.PileBillingDetail" id="PileBillingDetailResult">
        <result property="id" column="id"/>
        <result property="templateCode" column="template_code"/>
        <result property="timeType" column="time_type"/>
        <result property="electricityPrice" column="electricity_price"/>
        <result property="servicePrice" column="service_price"/>
        <result property="applyTime" column="apply_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="Detail_Base_Column_List">
        <!--@mbg.generated PileBillingDetail-->
        id, template_code, time_type, electricity_price, service_price, apply_time, create_by,
        create_time, update_by, update_time, del_flag
    </sql>

    <resultMap id="PileBillingTemplatePileBillingDetailResult" type="com.jsowell.pile.domain.PileBillingTemplate"
               extends="PileBillingTemplateResult">
        <collection property="pileBillingDetailList" ofType="com.jsowell.pile.domain.PileBillingDetail" javaType="java.util.List">
            <id property="id" column="sub_id"/>
            <result property="templateCode" column="sub_template_code"/>
            <result property="timeType" column="sub_time_type"/>
            <result property="electricityPrice" column="sub_electricity_price"/>
            <result property="servicePrice" column="sub_service_price"/>
            <result property="applyTime" column="sub_apply_time"/>
            <result property="createBy" column="sub_create_by"/>
            <result property="createTime" column="sub_create_time"/>
            <result property="updateBy" column="sub_update_by"/>
            <result property="updateTime" column="sub_update_time"/>
            <result property="delFlag" column="sub_del_flag"/>
        </collection>
    </resultMap>

    <sql id="selectPileBillingTemplateVo">
        select
        <include refid="Base_Column_List" />
        from pile_billing_template
    </sql>

    <select id="selectPileBillingTemplateList" parameterType="com.jsowell.pile.domain.PileBillingTemplate"
            resultMap="PileBillingTemplateResult">
        <include refid="selectPileBillingTemplateVo"/>
        <where>
            <if test="publicFlag != null and publicFlag != ''">
                and public_flag = #{publicFlag,jdbcType=VARCHAR}
            </if>
        </where>
    </select>

    <select id="selectPileBillingTemplateById" parameterType="Long"
            resultMap="PileBillingTemplatePileBillingDetailResult">
        select a.id as id,
               a.template_code,
               a.name,
                a.free_time,
                a.occupy_fee,
                a.max_occupy_fee,
               a.template_remark,
               a.type,
               a.station_id,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               a.del_flag,
               b.id as sub_id,
               b.template_code as sub_template_code,
               b.time_type as sub_time_type,
               b.electricity_price as sub_electricity_price,
               b.service_price as sub_service_price,
               b.apply_time as sub_apply_time,
               b.create_by as sub_create_by,
               b.create_time as sub_create_time,
               b.update_by as sub_update_by,
               b.update_time as sub_update_time,
               b.del_flag as sub_del_flag
        from pile_billing_template a
        left join pile_billing_detail b on b.template_code = a.template_code
        where a.id = #{id}
    </select>

    <insert id="insertPileBillingTemplate" parameterType="com.jsowell.pile.domain.PileBillingTemplate"
            useGeneratedKeys="true" keyProperty="id">
        insert into pile_billing_template
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="templateCode != null">
                template_code,
            </if>
            <if test="publicFlag != null">
                public_flag,
            </if>
            <if test="memberFlag != null">
                member_flag,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="templateRemark != null">
                template_remark,
            </if>
            <if test="templateDesc != null">
                template_desc,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="stationId != null">
                station_id,
            </if>
            <if test="freeTime != null">
                free_time,
            </if>
            <if test="occupyFee != null">
                occupy_fee,
            </if>
            <if test="maxOccupyFee != null">
                max_occupy_fee,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="templateCode != null">
                #{templateCode},
            </if>
            <if test="publicFlag != null">
                #{publicFlag},
            </if>
            <if test="memberFlag != null">
                #{memberFlag},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="status!= null">
                #{status},
            </if>
            <if test="templateRemark != null">
                #{templateRemark},
            </if>
            <if test="templateDesc != null">
                #{templateDesc},
            </if>
            <if test="type != null">
                #{type},
            </if>
            <if test="stationId != null">
                #{stationId},
            </if>
            <if test="freeTime != null">
                #{freeTime},
            </if>
            <if test="occupyFee != null">
                #{occupyFee},
            </if>
            <if test="maxOccupyFee != null">
                #{maxOccupyFee},
            </if>
            <if test="createBy != null">
                #{createBy},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateBy != null">
                #{updateBy},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="delFlag != null">
                #{delFlag},
            </if>
        </trim>
    </insert>

    <update id="updatePileBillingTemplate" parameterType="com.jsowell.pile.domain.PileBillingTemplate">
        update pile_billing_template
        <trim prefix="SET" suffixOverrides=",">
            <if test="templateCode != null">
                template_code = #{templateCode},
            </if>
            <if test="publicFlag != null">
                public_flag = #{publicFlag},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="templateRemark != null">
                template_remark = #{templateRemark},
            </if>
            <if test="templateDesc != null">
                template_desc = #{templateDesc},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="stationId != null">
                station_id = #{stationId},
            </if>
            <if test="freeTime != null">
                free_time = #{freeTime},
            </if>
            <if test="occupyFee != null and occupyFee != ''">
                occupy_fee = #{occupyFee},
            </if>
            <if test="maxOccupyFee != null">
                max_occupy_fee = #{maxOccupyFee},
            </if>
            <if test="publishTime != null">
                publish_time = #{publishTime},
            </if>
            <if test="createBy != null">
                create_by = #{createBy},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePileBillingTemplateById" parameterType="Long">
        delete
        from pile_billing_template
        where id = #{id}
    </delete>

    <delete id="deletePileBillingTemplateByIds" parameterType="String">
        delete
        from pile_billing_template where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deletePileBillingDetailByTemplateCodes" parameterType="String">
        delete
        from pile_billing_detail
        where template_code in
        <foreach item="templateCode" collection="templateCodes" open="(" separator="," close=")">
            #{templateCode}
        </foreach>
    </delete>

    <delete id="deletePileBillingDetailByTemplateCode" parameterType="String">
        delete
        from pile_billing_detail
        where template_code = #{templateCode}
    </delete>

    <insert id="batchPileBillingDetail">
        insert into pile_billing_detail
        (id, template_code, time_type, electricity_price, service_price, apply_time,
         create_by, update_by)
        values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.id}, #{item.templateCode}, #{item.timeType}, #{item.electricityPrice}, #{item.servicePrice},
             #{item.applyTime}, #{item.createBy}, #{item.updateBy})
        </foreach>
    </insert>

    <select id="queryPublicBillingTemplateList" resultType="com.jsowell.pile.vo.web.BillingTemplateVO">
        select
            t.id as templateId,
            t.template_code as templateCode,
            t.name as templateName
        from
            pile_billing_template t
        where
            t.del_flag = '0'
          and t.public_flag = '1'
    </select>

    <select id="queryStationBillingTemplateListWithAuth" resultType="com.jsowell.pile.vo.web.BillingTemplateVO">
        select
            t2.id as templateId,
            t2.template_code as templateCode,
            t2.name as templateName,
            t2.status,
            t2.template_remark as remark,
            t2.type as deviceType,
            t2.free_time as freeTime,
            t2.occupy_fee as occupyFee,
            t2.publish_time as publishTime,
            t2.member_flag as memberFlag,
            t3.electricity_price AS sharpElectricityPrice,
            t3.service_price AS sharpServicePrice,
            t3.apply_time AS sharpApplyDate,
            t4.electricity_price AS peakElectricityPrice,
            t4.service_price AS peakServicePrice,
            t4.apply_time AS peakApplyDate,
            t5.electricity_price AS flatElectricityPrice,
            t5.service_price AS flatServicePrice,
            t5.apply_time AS flatApplyDate,
            t6.electricity_price AS valleyElectricityPrice,
            t6.service_price AS valleyServicePrice,
            t6.apply_time AS valleyApplyDate
        from
            pile_billing_template t2
            left join pile_station_info t1 on t1.id = t2.station_id
            left JOIN pile_billing_detail t3 ON t3.template_code = t2.template_code AND t3.time_type = '1'
            left JOIN pile_billing_detail t4 ON t4.template_code = t2.template_code AND t4.time_type = '2'
            left JOIN pile_billing_detail t5 ON t5.template_code = t2.template_code AND t5.time_type = '3'
            left JOIN pile_billing_detail t6 ON t6.template_code = t2.template_code AND t6.time_type = '4'
        where
            t2.del_flag = '0'
          and t2.station_id = #{stationId,jdbcType=VARCHAR}
        <if test="memberFlag != null and memberFlag != ''">
            and t2.member_flag = #{memberFlag,jdbcType=VARCHAR}
        </if>
        <if test="memberFlag == null">
            and t2.member_flag = '0'
        </if>
        <!--数据范围过滤-->
        <if test="stationDeptIds != null and stationDeptIds.size() != 0">
            and t1.dept_id in
            <foreach collection="stationDeptIds" item="stationDeptId" open="(" separator="," close=")">
                #{stationDeptId,jdbcType=VARCHAR}
            </foreach>
        </if>
        order by t2.publish_time desc
    </select>

    <select id="selectBillingTemplateByPileSn" resultType="com.jsowell.pile.vo.web.BillingTemplateVO">
        SELECT
            t2.id as templateId,
            t2.template_code AS templateCode,
            t2.`name` AS templateName,
            t3.electricity_price AS sharpElectricityPrice,
            t3.service_price AS sharpServicePrice,
            t3.apply_time AS sharpApplyDate,
            t4.electricity_price AS peakElectricityPrice,
            t4.service_price AS peakServicePrice,
            t4.apply_time AS peakApplyDate,
            t5.electricity_price AS flatElectricityPrice,
            t5.service_price AS flatServicePrice,
            t5.apply_time AS flatApplyDate,
            t6.electricity_price AS valleyElectricityPrice,
            t6.service_price AS valleyServicePrice,
            t6.apply_time AS valleyApplyDate
        FROM
            pile_basic_info t1
            JOIN pile_billing_template t2 ON t1.station_id = t2.station_id and t2.status = '1' and t2.member_flag = '0'
            JOIN pile_billing_detail t3 ON t3.template_code = t2.template_code AND t3.time_type = '1'
            JOIN pile_billing_detail t4 ON t4.template_code = t2.template_code AND t4.time_type = '2'
            JOIN pile_billing_detail t5 ON t5.template_code = t2.template_code AND t5.time_type = '3'
            JOIN pile_billing_detail t6 ON t6.template_code = t2.template_code AND t6.time_type = '4'
        WHERE
            t1.sn = #{pileSn,jdbcType=VARCHAR};
    </select>

    <select id="selectBillingTemplateByTemplateId" resultType="com.jsowell.pile.vo.web.BillingTemplateVO">
        SELECT
            t2.id as templateId,
            t2.template_code AS templateCode,
            t2.`name` AS templateName,
            t3.electricity_price AS sharpElectricityPrice,
            t3.service_price AS sharpServicePrice,
            t3.apply_time AS sharpApplyDate,
            t4.electricity_price AS peakElectricityPrice,
            t4.service_price AS peakServicePrice,
            t4.apply_time AS peakApplyDate,
            t5.electricity_price AS flatElectricityPrice,
            t5.service_price AS flatServicePrice,
            t5.apply_time AS flatApplyDate,
            t6.electricity_price AS valleyElectricityPrice,
            t6.service_price AS valleyServicePrice,
            t6.apply_time AS valleyApplyDate
        FROM
            pile_billing_template t2
            left JOIN pile_billing_detail t3 ON t3.template_code = t2.template_code AND t3.time_type = '1'
            left JOIN pile_billing_detail t4 ON t4.template_code = t2.template_code AND t4.time_type = '2'
            left JOIN pile_billing_detail t5 ON t5.template_code = t2.template_code AND t5.time_type = '3'
            left JOIN pile_billing_detail t6 ON t6.template_code = t2.template_code AND t6.time_type = '4'
        WHERE
            t2.id = #{templateId,jdbcType=VARCHAR};
    </select>

    <select id="queryBillingDetailByTemplateIds" resultMap="PileBillingDetailResult">
        SELECT
            t1.id,
            t1.template_code,
            t1.time_type,
            t1.electricity_price,
            t1.service_price,
            t1.apply_time,
            t1.create_by,
            t1.create_time,
            t1.update_by,
            t1.update_time,
            t1.del_flag
        FROM pile_billing_detail t1
        JOIN pile_billing_template t2 ON t2.template_code = t1.template_code
        WHERE t2.id in 
        <foreach collection="templateIds" item="templateId" open="(" separator="," close=")">
            #{templateId,jdbcType=BIGINT}
        </foreach>
    </select>

    <select id="queryBillingDetailByTemplateId" resultMap="PileBillingDetailResult">
        SELECT
        t1.id,
        t1.template_code,
        t1.time_type,
        t1.electricity_price,
        t1.service_price,
        t1.apply_time,
        t1.create_by,
        t1.create_time,
        t1.update_by,
        t1.update_time,
        t1.del_flag
        FROM pile_billing_detail t1
        JOIN pile_billing_template t2 ON t2.template_code = t1.template_code
        WHERE t2.id = #{templateId,jdbcType=BIGINT}
    </select>

    <insert id="insertPileBillingRelation">
        insert into pile_billing_relation
        (pile_sn, billing_template_code, station_id)
        values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.pileSn}, #{item.billingTemplateCode}, #{item.stationId})
        </foreach>
    </insert>

    <delete id="deleteRelationByPileSn">
        delete
        from pile_billing_relation
        where pile_sn in
        <foreach collection="pileSnList" item="pileSn" open="(" separator="," close=")">
            #{pileSn,jdbcType=VARCHAR}
        </foreach>
    </delete>

    <select id="selectBillingTemplateByStationId" resultType="com.jsowell.pile.vo.web.BillingTemplateVO">
        SELECT
            t2.id as templateId,
            t2.template_code AS templateCode,
            t2.`name` AS templateName,
            t3.electricity_price AS sharpElectricityPrice,
            t3.service_price AS sharpServicePrice,
            t3.apply_time AS sharpApplyDate,
            t4.electricity_price AS peakElectricityPrice,
            t4.service_price AS peakServicePrice,
            t4.apply_time AS peakApplyDate,
            t5.electricity_price AS flatElectricityPrice,
            t5.service_price AS flatServicePrice,
            t5.apply_time AS flatApplyDate,
            t6.electricity_price AS valleyElectricityPrice,
            t6.service_price AS valleyServicePrice,
            t6.apply_time AS valleyApplyDate
        FROM
            pile_billing_template t2
        left JOIN pile_billing_detail t3 ON t3.template_code = t2.template_code AND t3.time_type = '1'
        left JOIN pile_billing_detail t4 ON t4.template_code = t2.template_code AND t4.time_type = '2'
        left JOIN pile_billing_detail t5 ON t5.template_code = t2.template_code AND t5.time_type = '3'
        left JOIN pile_billing_detail t6 ON t6.template_code = t2.template_code AND t6.time_type = '4'
        WHERE t2.del_flag = '0'
          and t2.station_id = #{stationId,jdbcType=VARCHAR}
        order by t2.publish_time DESC
        limit 1
    </select>

    <select id="queryStationBillingTemplateList" resultType="com.jsowell.pile.vo.web.BillingTemplateVO">
        select
        t2.id as templateId,
        t2.template_code as templateCode,
        t2.name as templateName,
        t2.template_remark as remark,
        t2.type as deviceType,
        t2.free_time as freeTime,
        t2.occupy_fee as occupyFee,
        t2.max_occupy_fee as maxOccupyFee,
        t1.park_fee_describe as parkFeeDescribe,
        t2.publish_time as publishTime,
        t3.electricity_price AS sharpElectricityPrice,
        t3.service_price AS sharpServicePrice,
        t3.apply_time AS sharpApplyDate,
        t4.electricity_price AS peakElectricityPrice,
        t4.service_price AS peakServicePrice,
        t4.apply_time AS peakApplyDate,
        t5.electricity_price AS flatElectricityPrice,
        t5.service_price AS flatServicePrice,
        t5.apply_time AS flatApplyDate,
        t6.electricity_price AS valleyElectricityPrice,
        t6.service_price AS valleyServicePrice,
        t6.apply_time AS valleyApplyDate
        from
        pile_billing_template t2
        left join pile_station_info t1 on t1.id = t2.station_id
        left JOIN pile_billing_detail t3 ON t3.template_code = t2.template_code AND t3.time_type = '1'
        left JOIN pile_billing_detail t4 ON t4.template_code = t2.template_code AND t4.time_type = '2'
        left JOIN pile_billing_detail t5 ON t5.template_code = t2.template_code AND t5.time_type = '3'
        left JOIN pile_billing_detail t6 ON t6.template_code = t2.template_code AND t6.time_type = '4'
        where
        t2.del_flag = '0'
        and t2.station_id = #{stationId,jdbcType=VARCHAR}
        order by t2.publish_time desc
    </select>

    <select id="selectBillingTemplateByStationIdList" resultType="com.jsowell.pile.vo.web.BillingTemplateVO">
        select
            t1.id as stationId,
            t2.id as templateId,
            t2.template_code as templateCode,
            t2.name as templateName,
            t2.template_remark as remark,
            t2.type as deviceType,
            t2.publish_time as publishTime,
            t3.electricity_price AS sharpElectricityPrice,
            t3.service_price AS sharpServicePrice,
            t3.apply_time AS sharpApplyDate,
            t4.electricity_price AS peakElectricityPrice,
            t4.service_price AS peakServicePrice,
            t4.apply_time AS peakApplyDate,
            t5.electricity_price AS flatElectricityPrice,
            t5.service_price AS flatServicePrice,
            t5.apply_time AS flatApplyDate,
            t6.electricity_price AS valleyElectricityPrice,
            t6.service_price AS valleyServicePrice,
            t6.apply_time AS valleyApplyDate
        from
            pile_billing_template t2
        left join pile_station_info t1 on t1.id = t2.station_id
        left JOIN pile_billing_detail t3 ON t3.template_code = t2.template_code AND t3.time_type = '1'
        left JOIN pile_billing_detail t4 ON t4.template_code = t2.template_code AND t4.time_type = '2'
        left JOIN pile_billing_detail t5 ON t5.template_code = t2.template_code AND t5.time_type = '3'
        left JOIN pile_billing_detail t6 ON t6.template_code = t2.template_code AND t6.time_type = '4'
        where
            t2.del_flag = '0'
        and t2.status = '1'
        and t2.member_flag = '0'
        and t2.station_id in
        <foreach collection="stationIdList" item="item" open="(" separator="," close=")">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>

    <update id="updateStatusByStationId">
        update pile_billing_template
        set status = #{status,jdbcType=VARCHAR}
        where station_id = #{stationId,jdbcType=VARCHAR}
    </update>

    <update id="updateStatusByTemplateId">
        update pile_billing_template
        set status = #{status,jdbcType=VARCHAR}
        where id = #{templateId,jdbcType=VARCHAR}
    </update>

    <select id="queryPreferentialBillingTemplate" resultType="com.jsowell.pile.vo.web.BillingTemplateVO">
        SELECT
            t2.id as templateId,
            t2.template_code AS templateCode,
            t2.`name` AS templateName,
            t3.electricity_price AS sharpElectricityPrice,
            t3.service_price AS sharpServicePrice,
            t3.apply_time AS sharpApplyDate,
            t4.electricity_price AS peakElectricityPrice,
            t4.service_price AS peakServicePrice,
            t4.apply_time AS peakApplyDate,
            t5.electricity_price AS flatElectricityPrice,
            t5.service_price AS flatServicePrice,
            t5.apply_time AS flatApplyDate,
            t6.electricity_price AS valleyElectricityPrice,
            t6.service_price AS valleyServicePrice,
            t6.apply_time AS valleyApplyDate
        FROM
            pile_billing_template t2
            left JOIN pile_billing_detail t3 ON t3.template_code = t2.template_code AND t3.time_type = '1'
            left JOIN pile_billing_detail t4 ON t4.template_code = t2.template_code AND t4.time_type = '2'
            left JOIN pile_billing_detail t5 ON t5.template_code = t2.template_code AND t5.time_type = '3'
            left JOIN pile_billing_detail t6 ON t6.template_code = t2.template_code AND t6.time_type = '4'
        WHERE t2.del_flag = '0'
            and t2.station_id = #{stationId,jdbcType=VARCHAR}
            and t2.public_flag = '0'
            and t2.member_flag = '1'
            order by t2.update_time DESC, t2.create_time DESC
            limit 1
    </select>
</mapper>