<?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.XhpcInvoiceMapHistoryOrderMapper">
    <resultMap id="BaseResultMap" type="com.xhpc.user.pojo.XhpcInvoiceMapHistoryOrder">
        <result column="invoice_id" jdbcType="BIGINT" property="invoiceId"/>
        <result column="history_order_id" jdbcType="BIGINT" property="historyOrderId"/>
        <result column="history_user_id" jdbcType="BIGINT" property="historyUserId"/>
        <result column="history_user_type" jdbcType="BIGINT" property="historyUserType"/>
        <result column="history_serial_number" jdbcType="VARCHAR" property="historySerialNumber"/>
        <result column="power_price_total" jdbcType="DECIMAL" property="powerPriceTotal"/>
        <result column="service_price_total" jdbcType="DECIMAL" property="servicePriceTotal"/>
        <result column="promotion_discount" jdbcType="DECIMAL" property="promotionDiscount"/>
        <result column="history_act_price" jdbcType="DECIMAL" property="historyActPrice"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="charging_mode" jdbcType="VARCHAR" property="chargingMode"/>
        <result column="charging_station_id" jdbcType="BIGINT" property="chargingStationId"/>
        <result column="terminal_id" jdbcType="BIGINT" property="terminalId"/>
        <result column="lock_flag" jdbcType="TINYINT" property="lockFlag"/>   <!--他自己生成的方法没加这个字段，用到了的时候在加-->
        <result column="del_flag" jdbcType="INTEGER" property="delFlag"/>
    </resultMap>

    <sql id="Base_Column_List">
        `invoice_id`
        ,
      `history_order_id`,
      `history_user_id`,
      `history_user_type`,
      `history_serial_number`,
      `power_price_total`,
      `service_price_total`,
      `promotion_discount`,
      `history_act_price`,
      `create_time`,
      `charging_mode`,
      `charging_station_id`,
      `terminal_id`,
      `lock_flag`,
      `del_flag`
    </sql>

    <insert id="insert" parameterType="com.xhpc.user.pojo.XhpcInvoiceMapHistoryOrder">
        insert into xhpc_invoice_map_history_order (invoice_id, history_order_id, history_user_id,
                                                    history_user_type, hisotry_serial_number, power_price_total,
                                                    service_price_total, promotion_discount, history_act_price,
                                                    create_time, charging_mode, charging_station_id,
                                                    terminal_id, del_flag)
        values (#{invoiceId,jdbcType=BIGINT}, #{historyOrderId,jdbcType=BIGINT}, #{historyUserId,jdbcType=BIGINT},
                #{historyUserType,jdbcType=BIGINT}, #{hisotrySerialNumber,jdbcType=VARCHAR},
                #{powerPriceTotal,jdbcType=DECIMAL},
                #{servicePriceTotal,jdbcType=DECIMAL}, #{promotionDiscount,jdbcType=DECIMAL},
                #{historyActPrice,jdbcType=DECIMAL},
                #{createTime,jdbcType=TIMESTAMP}, #{chargingMode,jdbcType=VARCHAR},
                #{chargingStationId,jdbcType=BIGINT},
                #{terminalId,jdbcType=BIGINT}, #{delFlag,jdbcType=INTEGER})
    </insert>
    <insert id="insertSelective" parameterType="com.xhpc.user.pojo.XhpcInvoiceMapHistoryOrder">
        insert into xhpc_invoice_map_history_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="invoiceId != null">
                invoice_id,
            </if>
            <if test="historyOrderId != null">
                history_order_id,
            </if>
            <if test="historyUserId != null">
                history_user_id,
            </if>
            <if test="historyUserType != null">
                history_user_type,
            </if>
            <if test="historySerialNumber != null">
                history_serial_number,
            </if>
            <if test="powerPriceTotal != null">
                power_price_total,
            </if>
            <if test="servicePriceTotal != null">
                service_price_total,
            </if>
            <if test="promotionDiscount != null">
                promotion_discount,
            </if>
            <if test="historyActPrice != null">
                history_act_price,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="chargingMode != null">
                charging_mode,
            </if>
            <if test="chargingStationId != null">
                charging_station_id,
            </if>
            <if test="terminalId != null">
                terminal_id,
            </if>
            <if test="lockFlag!=null">
                lock_flag,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="invoiceId != null">
                #{invoiceId,jdbcType=BIGINT},
            </if>
            <if test="historyOrderId != null">
                #{historyOrderId,jdbcType=BIGINT},
            </if>
            <if test="historyUserId != null">
                #{historyUserId,jdbcType=BIGINT},
            </if>
            <if test="historyUserType != null">
                #{historyUserType,jdbcType=BIGINT},
            </if>
            <if test="historySerialNumber != null">
                #{historySerialNumber,jdbcType=VARCHAR},
            </if>
            <if test="powerPriceTotal != null">
                #{powerPriceTotal,jdbcType=DECIMAL},
            </if>
            <if test="servicePriceTotal != null">
                #{servicePriceTotal,jdbcType=DECIMAL},
            </if>
            <if test="promotionDiscount != null">
                #{promotionDiscount,jdbcType=DECIMAL},
            </if>
            <if test="historyActPrice != null">
                #{historyActPrice,jdbcType=DECIMAL},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="chargingMode != null">
                #{chargingMode,jdbcType=VARCHAR},
            </if>
            <if test="chargingStationId != null">
                #{chargingStationId,jdbcType=BIGINT},
            </if>
            <if test="terminalId != null">
                #{terminalId,jdbcType=BIGINT},
            </if>
            <if test="lockFlag!=null">
                #{lockFlag,jdbcType=INTEGER},
            </if>
            <if test="delFlag != null">
                #{delFlag,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <update id="updateStatus">
        update xhpc_invoice_map_history_order
        set lock_flag = 0
        where history_order_id in (
            (SELECT middle.history_order_id
             from (select history_order_id
                   from xhpc_invoice_map_history_order
                   where invoice_id = #{invoiceId}) as middle
            )
        )
    </update>
    <update id="updateUnlockHistoryOrdersByInvoiceId">
        UPDATE xhpc_invoice_map_history_order
        SET lock_flag = NULL
        WHERE invoice_id = #{invoiceId}
    </update>
    <select id="findOrdersByInvoiceId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `xhpc_invoice_map_history_order`
        WHERE invoice_id = #{invoiceId}
        AND del_flag IS NULL;
    </select>
    <select id="getHistoryOrderScopeByInvoiceId" resultType="java.lang.String">
        SELECT MIN(create_time)
        FROM `xhpc_invoice_map_history_order`
        WHERE invoice_id = #{invoiceId}
          AND del_flag IS NULL
        UNION ALL
        SELECT Max(create_time)
        FROM `xhpc_invoice_map_history_order`
        WHERE invoice_id = #{invoiceId}
          AND del_flag IS NULL
    </select>
    <select id="findLockOrdersByUserIdAndUserType" resultType="java.lang.Integer">
        SELECT history_order_id
        FROM xhpc_invoice_map_history_order
        WHERE lock_flag = 0
          AND del_flag IS NULL
          AND history_user_id = #{userId}
          AND history_user_type = #{userType};
    </select>
    <select id="getLockedOnesByHistoryOrderId" resultType="com.xhpc.user.pojo.XhpcInvoiceMapHistoryOrder">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        xhpc_invoice_map_history_order
        WHERE
        history_order_id IN
        <foreach collection="collection" open="(" close=")" separator="," item="historyOrderId">
            #{historyOrderId}
        </foreach>
        AND lock_flag = 0;
    </select>
</mapper>