<?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.TGasBottleTrackRecordMapper">

    <resultMap type="TGasBottleTrackRecord" id="TGasBottleTrackRecordResult">
        <result property="trackRecordId"    column="track_record_id"    />
        <result property="stationId"    column="station_id"    />
        <result property="bottleId"    column="bottle_id"    />
        <result property="processesName"    column="processes_name"    />
        <result property="processesRelationId"    column="processes_relation_id"    />
        <result property="operateDate"    column="operate_date"    />
        <result property="operator"    column="operator"    />
        <result property="sender"    column="sender"    />
        <result property="recipient"    column="recipient"    />
        <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="stationName" column="station_name"/>
        <result property="bottleCode"    column="bottle_code"    />
        <result property="bottleCapacity"    column="bottle_capacity"    />
        <result property="operatorName" column="operator_name"/>
        <result property="name" column="name"/>
        <result property="gasUserName" column="gas_user_name"/>
    </resultMap>

    <sql id="selectTGasBottleTrackRecordVo">
         SELECT
            record.track_record_id,
            record.station_id,
            record.bottle_id,
            record.processes_name,
            record.processes_relation_id,
            record.operate_date,
            record.operator,
            record.sender,
            recipient,
            record.create_time,
            record.update_time,
            record.is_del,
            record.remark,
            station.station_name,
            bottle.bottle_code,
            bottle.bottle_capacity,
			practitioner.name,
			userinfo.gas_user_name,
            (select us.gas_user_name from t_gas_user_info us where us.gas_user_id = record.operator) as operator_name
        FROM
            t_gas_bottle_track_record record
        LEFT JOIN t_gas_storage_station_info station ON station.station_id = record.station_id
        LEFT JOIN t_gas_bottle_info bottle ON bottle.bottle_id = record.bottle_id
		LEFT JOIN t_practitioner_info practitioner ON  record.operator=practitioner.practitioner_id
	    LEFT JOIN t_gas_user_info userinfo ON  userinfo.gas_user_id=record.recipient
    </sql>

    <select id="selectTGasBottleTrackRecordList" parameterType="TGasBottleTrackRecord" resultMap="TGasBottleTrackRecordResult">
        <include refid="selectTGasBottleTrackRecordVo"/>
        <where>
            <if test="stationId != null "> and record.station_id = #{stationId}</if>
            <if test="bottleId != null "> and record.bottle_id = #{bottleId}</if>
            <if test="processesName != null  and processesName != ''"> and record.processes_name like concat('%', #{processesName}, '%')</if>
            <if test="processesRelationId != null "> and record.processes_relation_id = #{processesRelationId}</if>
            <if test="operateDate != null "> and record.operate_date = #{operateDate}</if>
            <if test="operator != null "> and record.operator = #{operator}</if>
            <if test="sender != null  and sender != ''"> and record.sender = #{sender}</if>
            <if test="recipient != null  and recipient != ''"> and record.recipient = #{recipient}</if>
            <if test="isDel != null  and isDel != ''"> and record.is_del = #{isDel}</if>
            <if test="bottleCode != null"> and bottle.bottle_code like concat('%', #{ bottleCode }, '%')</if>
        </where>
        ORDER BY record.operate_date DESC
    </select>

    <select id="selectTGasBottleTrackRecordById" parameterType="Long" resultMap="TGasBottleTrackRecordResult">
        <include refid="selectTGasBottleTrackRecordVo"/>
        where record.track_record_id = #{trackRecordId}
    </select>

    <insert id="insertTGasBottleTrackRecord" parameterType="TGasBottleTrackRecord" useGeneratedKeys="true" keyProperty="trackRecordId">
        insert into t_gas_bottle_track_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="bottleId != null">bottle_id,</if>
            <if test="processesName != null">processes_name,</if>
            <if test="processesRelationId != null">processes_relation_id,</if>
            <if test="operateDate != null">operate_date,</if>
            <if test="operator != null">operator,</if>
            <if test="sender != null">sender,</if>
            <if test="recipient != null">recipient,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationId != null">#{stationId},</if>
            <if test="bottleId != null">#{bottleId},</if>
            <if test="processesName != null">#{processesName},</if>
            <if test="processesRelationId != null">#{processesRelationId},</if>
            <if test="operateDate != null">#{operateDate},</if>
            <if test="operator != null">#{operator},</if>
            <if test="sender != null">#{sender},</if>
            <if test="recipient != null">#{recipient},</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>
         </trim>
    </insert>

    <update id="updateTGasBottleTrackRecord" parameterType="TGasBottleTrackRecord">
        update t_gas_bottle_track_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="bottleId != null">bottle_id = #{bottleId},</if>
            <if test="processesName != null">processes_name = #{processesName},</if>
            <if test="processesRelationId != null">processes_relation_id = #{processesRelationId},</if>
            <if test="operateDate != null">operate_date = #{operateDate},</if>
            <if test="operator != null">operator = #{operator},</if>
            <if test="sender != null">sender = #{sender},</if>
            <if test="recipient != null">recipient = #{recipient},</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>
        </trim>
        where track_record_id = #{trackRecordId}
    </update>

    <delete id="deleteTGasBottleTrackRecordById" parameterType="Long">
        delete from t_gas_bottle_track_record where track_record_id = #{trackRecordId}
    </delete>

    <delete id="deleteTGasBottleTrackRecordByIds" parameterType="String">
        delete from t_gas_bottle_track_record where track_record_id in
        <foreach item="trackRecordId" collection="array" open="(" separator="," close=")">
            #{trackRecordId}
        </foreach>
    </delete>
</mapper>
