<?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.hw.dms.mapper.DmsBillsLubeInstanceMapper">

    <resultMap type="DmsBillsLubeInstance" id="DmsBillsLubeInstanceResult">
        <result property="lubeInstanceId"    column="lube_instance_id"    />
        <result property="planLubeId"    column="plan_lube_id"    />
        <result property="billsLubeCode"    column="bills_lube_code"    />
        <result property="planBeginTime"    column="plan_begin_time"    />
        <result property="realBeginTime"    column="real_begin_time"    />
        <result property="planEndTime"    column="plan_end_time"    />
        <result property="realEndTime"    column="real_end_time"    />
        <result property="lubeStatus"    column="lube_status"    />
        <result property="isFlag"    column="is_flag"    />
        <result property="remark"    column="remark"    />
        <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="lubeGroup"    column="lube_group"    />
        <result property="lubeSupervisor"    column="lube_supervisor"    />
        <result property="lubeLevel"    column="lube_level"    />
    </resultMap>

    <sql id="selectDmsBillsLubeInstanceVo">
        select lube_instance_id, plan_lube_id, bills_lube_code, plan_begin_time, real_begin_time, plan_end_time, real_end_time, lube_status, is_flag, remark, create_by, create_time, update_by, update_time from dms_bills_lube_instance
    </sql>

    <select id="selectDmsBillsLubeInstanceList" parameterType="DmsBillsLubeInstance" resultMap="DmsBillsLubeInstanceResult">
        <include refid="selectDmsBillsLubeInstanceVo"/>
        <where>
            <if test="planLubeId != null "> and plan_lube_id = #{planLubeId}</if>
            <if test="billsLubeCode != null  and billsLubeCode != ''"> and bills_lube_code = #{billsLubeCode}</if>
            <if test="planBeginTime != null "> and plan_begin_time = #{planBeginTime}</if>
            <if test="realBeginTime != null "> and real_begin_time = #{realBeginTime}</if>
            <if test="planEndTime != null "> and plan_end_time = #{planEndTime}</if>
            <if test="realEndTime != null "> and real_end_time = #{realEndTime}</if>
            <if test="lubeStatus != null  and lubeStatus != ''"> and lube_status = #{lubeStatus}</if>
            <if test="isFlag != null  and isFlag != ''"> and is_flag = #{isFlag}</if>
        </where>
    </select>

    <select id="selectDmsBillsLubeInstanceByLubeInstanceId" parameterType="Long" resultMap="DmsBillsLubeInstanceResult">
        <include refid="selectDmsBillsLubeInstanceVo"/>
        where lube_instance_id = #{lubeInstanceId}
    </select>

    <insert id="insertDmsBillsLubeInstance" parameterType="DmsBillsLubeInstance" useGeneratedKeys="true" keyProperty="lubeInstanceId">
        insert into dms_bills_lube_instance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planLubeId != null">plan_lube_id,</if>
            <if test="billsLubeCode != null">bills_lube_code,</if>
            <if test="planBeginTime != null">plan_begin_time,</if>
            <if test="realBeginTime != null">real_begin_time,</if>
            <if test="planEndTime != null">plan_end_time,</if>
            <if test="realEndTime != null">real_end_time,</if>
            <if test="lubeStatus != null">lube_status,</if>
            <if test="isFlag != null and isFlag != ''">is_flag,</if>
            <if test="remark != null">remark,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planLubeId != null">#{planLubeId},</if>
            <if test="billsLubeCode != null">#{billsLubeCode},</if>
            <if test="planBeginTime != null">#{planBeginTime},</if>
            <if test="realBeginTime != null">#{realBeginTime},</if>
            <if test="planEndTime != null">#{planEndTime},</if>
            <if test="realEndTime != null">#{realEndTime},</if>
            <if test="lubeStatus != null">#{lubeStatus},</if>
            <if test="isFlag != null and isFlag != ''">#{isFlag},</if>
            <if test="remark != null">#{remark},</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>
         </trim>
    </insert>

    <update id="updateDmsBillsLubeInstance" parameterType="DmsBillsLubeInstance">
        update dms_bills_lube_instance
        <trim prefix="SET" suffixOverrides=",">
            <if test="planLubeId != null">plan_lube_id = #{planLubeId},</if>
            <if test="billsLubeCode != null">bills_lube_code = #{billsLubeCode},</if>
            <if test="planBeginTime != null">plan_begin_time = #{planBeginTime},</if>
            <if test="realBeginTime != null">real_begin_time = #{realBeginTime},</if>
            <if test="planEndTime != null">plan_end_time = #{planEndTime},</if>
            <if test="realEndTime != null">real_end_time = #{realEndTime},</if>
            <if test="lubeStatus != null">lube_status = #{lubeStatus},</if>
            <if test="isFlag != null and isFlag != ''">is_flag = #{isFlag},</if>
            <if test="remark != null">remark = #{remark},</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>
        </trim>
        where lube_instance_id = #{lubeInstanceId}
    </update>

    <delete id="deleteDmsBillsLubeInstanceByLubeInstanceId" parameterType="Long">
        delete from dms_bills_lube_instance where lube_instance_id = #{lubeInstanceId}
    </delete>

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




    <insert id="batchDmsBillsLubeDetail">
        insert into dms_bills_lube_detail( bills_lube_detail_id, lube_instance_id, device_id, lube_station_id, lube_standard_id, operation_description, maint_status, is_flag, remark, create_by, create_time, update_by, update_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.billsLubeDetailId}, #{item.lubeInstanceId}, #{item.deviceId}, #{item.lubeStationId}, #{item.lubeStandardId}, #{item.operationDescription}, #{item.maintStatus}, #{item.isFlag}, #{item.remark}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>

    <select id="selectNewestDmsBillsLubeInstance" parameterType="DmsBillsLubeInstance" resultMap="DmsBillsLubeInstanceResult">
        select dbli.lube_instance_id, dbli.plan_lube_id, dbli.bills_lube_code, dbli.plan_begin_time, dbli.real_begin_time, dbli.plan_end_time,
        dbli.real_end_time, dbli.lube_status,
        dbli.lube_group,dbli.lube_supervisor,dbli.lube_level
        from dms_bills_lube_instance dbli

        <where>
            <if test="lubeStatusStr != null  and lubeStatusStr != ''"> and dbli.lube_status in (${lubeStatusStr})</if>
        </where>
        order by dbli.create_time desc limit 1
    </select>

</mapper>
