<?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.aucma.dms.mapper.DmsBillsMaintInstanceMapper">

    <resultMap type="DmsBillsMaintInstance" id="DmsBillsMaintInstanceResult">
        <result property="maintInstanceId"    column="maint_instance_id"    />
        <result property="planMaintId"    column="plan_maint_id"    />
        <!-- 已废弃审批流功能 -->
        <!-- <result property="wfProcessId"    column="wf_process_id"    /> -->
        <result property="billsMaintCode"    column="bills_maint_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="maintStatus"    column="maint_status"    />
        <result property="maintCompRate"    column="maint_comp_rate"    />
        <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="maintLevel"    column="maint_level"    />
        <result property="maintGroup"    column="maint_group"    />
        <result property="maintSupervisor"    column="maint_supervisor"    />

    </resultMap>

    <sql id="selectDmsBillsMaintInstanceVo">
        select
            a.maint_instance_id,
            a.plan_maint_id,
            -- a.wf_process_id, -- 已废弃审批流
            a.bills_maint_code,
            a.plan_begin_time,
            a.real_begin_time,
            a.plan_end_time,
            a.real_end_time,
            a.maint_status,
            a.maint_comp_rate,
            a.is_flag,
            a.create_by,
            a.create_time,
            a.update_by,
            a.update_time,
            b.maint_supervisor,
            b.remark
        from
            dms_bills_maint_instance a left join dms_plan_maint b on a.plan_maint_id = b.plan_maint_id
    </sql>

    <select id="selectDmsBillsMaintInstanceList" parameterType="DmsBillsMaintInstance" resultMap="DmsBillsMaintInstanceResult">
        <include refid="selectDmsBillsMaintInstanceVo"/>
        <where>
            <if test="planMaintId != null "> and a.plan_maint_id = #{planMaintId}</if>
            <if test="maintInstanceId != null "> and a.maint_instance_id = #{maintInstanceId}</if>
            <!-- 已废弃审批流 -->
            <!-- <if test="wfProcessId != null "> and a.wf_process_id = #{wfProcessId}</if> -->
            <if test="billsMaintCode != null  and billsMaintCode != ''"> and a.bills_maint_code like concat('%',#{billsMaintCode},'%') </if>
            <if test="planBeginTime != null "> and a.plan_begin_time > #{planBeginTime}</if>
            <if test="planEndTime != null "> and   #{planEndTime}>a.plan_end_time</if>
            <if test="realBeginTime != null "> and a.real_begin_time > #{realBeginTime}</if>
            <if test="realEndTime != null "> and   #{realEndTime} > a.real_end_time</if>
            <if test="maintStatus != null "> and a.maint_status = #{maintStatus}</if>
            <if test="maintCompRate != null "> and a.maint_comp_rate = #{maintCompRate}</if>
            <if test="isFlag != null "> and a.is_flag = #{isFlag}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectDmsBillsMaintInstanceByMaintInstanceId" parameterType="Long" resultMap="DmsBillsMaintInstanceResult">
        <include refid="selectDmsBillsMaintInstanceVo"/>
        where maint_instance_id = #{maintInstanceId}
    </select>

    <insert id="insertDmsBillsMaintInstance" parameterType="DmsBillsMaintInstance">
        <selectKey keyProperty="maintInstanceId" resultType="long" order="BEFORE">
            SELECT SEQ_DMS_MAINT_INST.NEXTVAL FROM DUAL
        </selectKey>
        insert into dms_bills_maint_instance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="maintInstanceId != null">maint_instance_id,</if>
            <if test="planMaintId != null">plan_maint_id,</if>
            <!-- 已废弃审批流 -->
            <!-- <if test="wfProcessId != null">wf_process_id,</if> -->
            <if test="billsMaintCode != null and billsMaintCode != ''">bills_maint_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="maintStatus != null">maint_status,</if>
            <if test="maintCompRate != null">maint_comp_rate,</if>
            <if test="isFlag != null">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="maintInstanceId != null">#{maintInstanceId},</if>
            <if test="planMaintId != null">#{planMaintId},</if>
            <!-- 已废弃审批流 -->
            <!-- <if test="wfProcessId != null">#{wfProcessId},</if> -->
            <if test="billsMaintCode != null and billsMaintCode != ''">#{billsMaintCode},</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="maintStatus != null">#{maintStatus},</if>
            <if test="maintCompRate != null">#{maintCompRate},</if>
            <if test="isFlag != null">#{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="updateDmsBillsMaintInstance" parameterType="DmsBillsMaintInstance">
        update dms_bills_maint_instance
        <trim prefix="SET" suffixOverrides=",">
            <if test="planMaintId != null">plan_maint_id = #{planMaintId},</if>
            <!-- 已废弃审批流 -->
            <!-- <if test="wfProcessId != null">wf_process_id = #{wfProcessId},</if> -->
            <if test="billsMaintCode != null and billsMaintCode != ''">bills_maint_code = #{billsMaintCode},</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="maintStatus != null">maint_status = #{maintStatus},</if>
            <if test="maintCompRate != null">maint_comp_rate = #{maintCompRate},</if>
            <if test="isFlag != null">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 maint_instance_id = #{maintInstanceId}
    </update>

    <delete id="deleteDmsBillsMaintInstanceByMaintInstanceId" parameterType="Long">
        delete from dms_bills_maint_instance where maint_instance_id = #{maintInstanceId}
    </delete>

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







    <insert id="batchDmsBillsMaintDetail">
        insert into dms_bills_maint_detail( bills_maint_detail_id, maint_instance_id, device_id, station_id, maint_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.billsMaintDetailId}, #{item.maintInstanceId}, #{item.deviceId}, #{item.stationId}, #{item.maintStandardId}, #{item.operationDescription}, #{item.maintStatus}, #{item.isFlag}, #{item.remark}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>
    <insert id="batchDmsProject">
        insert into dms_bills_maint_detail_project( bills_maint_detail_id, maint_project_id, maint_project_name, maint_project_desc, maint_project_status, remark, create_by, create_time, update_by, update_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.billsMaintDetailId}, #{item.maintProjectId}, #{item.maintProjectName}, #{item.maintProjectDesc}, #{item.maintProjectStatus}, #{item.remark},  #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>

    <select id="selectNewestDmsBillsMaintInstance" parameterType="DmsBillsMaintInstance" resultMap="DmsBillsMaintInstanceResult">
        select
        dbmi.maint_instance_id,
        dbmi.plan_maint_id,
        -- dbmi.wf_process_id, -- 已废弃审批流
        dbmi.bills_maint_code,
        dbmi.plan_begin_time,
        dbmi.real_begin_time,
        dbmi.plan_end_time,
        dbmi.real_end_time,
        dbmi.maint_status,
        dbmi.maint_comp_rate,
        dbmi.create_by,
        dbmi.create_time,
        a.maint_level,
        a.maint_group,
        a.maint_supervisor
        from
        dms_bills_maint_instance dbmi left join dms_plan_maint a on dbmi.plan_maint_id = a.plan_maint_id

        <where>
            <if test="maintStatusStr != null  and maintStatusStr != ''"> and dbmi.maint_status in (${maintStatusStr})</if>
        </where>
        order by dbmi.create_time desc limit 1
    </select>
    <select id="selectDmsMaintInfo" resultType="com.aucma.dms.domain.DmsMaintInstanceActivity"
            parameterType="java.lang.Long">
        select
            a.maint_level,
            a.maint_group,
            a.maint_supervisor,
            a.remark
        from
            dms_bills_maint_instance x
                left join dms_plan_maint a on
                x.plan_maint_id = a.plan_maint_id
        where
            x.maint_instance_id = 25
    </select>
</mapper>
