<?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.zehong.system.mapper.TLpgDeliveryRecordMapper">
    
    <resultMap type="TLpgDeliveryRecord" id="TLpgDeliveryRecordResult">
        <result property="deliveryRecordId"    column="delivery_record_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="deliveryPerson"    column="delivery_person"    />
        <result property="vehicleCode"    column="vehicle_code"    />
        <result property="gasUserType"    column="gas_user_type"    />
        <result property="deliveryAddress"    column="delivery_address"    />
        <result property="deliveryDate"    column="delivery_date"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remark"    column="remark"    />
        <result property="userId"    column="user_id"    />
        <result property="bottleCode"    column="f_bottle_code"    />
        <result property="gasUser"    column="nickName"    />
    </resultMap>

    <sql id="selectTLpgDeliveryRecordVo">
        SELECT
            delivery.delivery_record_id,
            delivery.station_name,
            delivery.delivery_person,
            delivery.vehicle_code,
            delivery.gas_user_type,
            delivery.delivery_address,
            delivery.delivery_date,
            delivery.create_time,
            delivery.update_time,
            delivery.is_del,
            delivery.remark,
            delivery.user_id,
            delivery.f_bottle_code,
            yequser.nick_name as nickName
        FROM
            t_lpg_delivery_record delivery
        left join t_detector_user yequser on delivery.user_id = yequser.user_id
    </sql>

    <select id="selectTLpgDeliveryRecordList" parameterType="TLpgDeliveryRecord" resultMap="TLpgDeliveryRecordResult">
        <include refid="selectTLpgDeliveryRecordVo"/>
        <where>  
            <if test="stationName != null "> and delivery.station_name like concat('%', #{stationName}, '%')</if>
            <if test="deliveryPerson != null  and deliveryPerson != ''"> and delivery.delivery_person like concat('%', #{deliveryPerson}, '%')</if>
            <if test="vehicleCode != null  and vehicleCode != ''"> and delivery.vehicle_code = #{vehicleCode}</if>
            <if test="gasUser != null  and gasUser != ''"> and yequser.nick_name like concat('%', #{gasUser}, '%')</if>
            <if test="gasUserType != null  and gasUserType != ''"> and delivery.gas_user_type = #{gasUserType}</if>
            <if test="deliveryAddress != null  and deliveryAddress != ''"> and delivery.delivery_address = #{deliveryAddress}</if>
            <if test="deliveryDate != null "> and delivery.delivery_date = #{deliveryDate}</if>
            <if test="isDel != null  and isDel != ''"> and delivery.is_del = #{isDel}</if>
            <if test="bottleCode != null  and bottleCode != ''"> and delivery.f_bottle_code like concat('%', #{bottleCode}, '%')</if>
        </where>
        order by  delivery_date desc
    </select>
    <select id="queryAllByBottleCodeLimit2" resultMap="TLpgDeliveryRecordResult">
        <include refid="selectTLpgDeliveryRecordVo"></include>
        where delivery.f_bottle_code = #{bottleCode}
    </select>
    
    <select id="selectTLpgDeliveryRecordById" parameterType="Long" resultMap="TLpgDeliveryRecordResult">
        <include refid="selectTLpgDeliveryRecordVo"/>
        where delivery.delivery_record_id = #{deliveryRecordId}
    </select>
        
    <insert id="insertTLpgDeliveryRecord" parameterType="TLpgDeliveryRecord" useGeneratedKeys="true" keyProperty="deliveryRecordId">
        insert into t_lpg_delivery_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationName != null">station_name,</if>
            <if test="bottleId != null">bottle_id,</if>
            <if test="deliveryPerson != null">delivery_person,</if>
            <if test="vehicleCode != null">vehicle_code,</if>
            <if test="gasUserType != null">gas_user_type,</if>
            <if test="deliveryAddress != null">delivery_address,</if>
            <if test="deliveryDate != null">delivery_date,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remark != null">remark,</if>
            <if test="userId != null">user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationName != null">#{stationName},</if>
            <if test="bottleId != null">#{bottleId},</if>
            <if test="deliveryPerson != null">#{deliveryPerson},</if>
            <if test="vehicleCode != null">#{vehicleCode},</if>
            <if test="gasUserType != null">#{gasUserType},</if>
            <if test="deliveryAddress != null">#{deliveryAddress},</if>
            <if test="deliveryDate != null">#{deliveryDate},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remark != null">#{remark},</if>
            <if test="userId != null">#{userId},</if>
         </trim>
    </insert>

    <insert id="insertBatch" parameterType="list">

        insert into t_lpg_delivery_record (station_name, delivery_person,vehicle_code, gas_user_type, delivery_address,
                                           delivery_date,user_id,f_bottle_code)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.stationName,jdbcType=VARCHAR}, #{item.deliveryPerson,jdbcType=VARCHAR}, #{item.vehicleCode,jdbcType=VARCHAR},
            #{item.gasUserType,jdbcType=VARCHAR}, #{item.deliveryAddress,jdbcType=VARCHAR},
            #{item.deliveryDate},#{item.userId},#{item.bottleCode}
            )
        </foreach>
    </insert>

    <update id="updateTLpgDeliveryRecord" parameterType="TLpgDeliveryRecord">
        update t_lpg_delivery_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="bottleCode != null">f_bottle_code = #{bottleCode},</if>
            <if test="deliveryPerson != null">delivery_person = #{deliveryPerson},</if>
            <if test="vehicleCode != null">vehicle_code = #{vehicleCode},</if>
            <if test="gasUserType != null">gas_user_type = #{gasUserType},</if>
            <if test="deliveryAddress != null">delivery_address = #{deliveryAddress},</if>
            <if test="deliveryDate != null">delivery_date = #{deliveryDate},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="userId != null">user_id = #{userId},</if>
        </trim>
        where delivery_record_id = #{deliveryRecordId}
    </update>

    <delete id="deleteTLpgDeliveryRecordById" parameterType="Long">
        delete from t_lpg_delivery_record where delivery_record_id = #{deliveryRecordId}
    </delete>

    <delete id="deleteTLpgDeliveryRecordByIds" parameterType="String">
        delete from t_lpg_delivery_record where delivery_record_id in 
        <foreach item="deliveryRecordId" collection="array" open="(" separator="," close=")">
            #{deliveryRecordId}
        </foreach>
    </delete>
</mapper>