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

    <resultMap type="DmsFlowCheckplan" id="DmsFlowCheckplanResult">
        <result property="flowCheckplanId"    column="flow_checkplan_id"    />
        <result property="checkCode"    column="check_code"    />
        <result property="planTime"    column="plan_time"    />
        <result property="checkBy"    column="check_by"    />
        <result property="deviceTypeId"    column="device_type_id"    />
        <result property="checkAmount"    column="check_amount"    />
        <result property="surplusCheckAmount"    column="surplus_check_amount"    />
        <result property="checkState"    column="check_state"    />
        <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"    />
    </resultMap>

    <resultMap id="DmsFlowCheckplanDmsFlowCheckplanDetailResult" type="DmsFlowCheckplan" extends="DmsFlowCheckplanResult">
        <collection property="dmsFlowCheckplanDetailList" notNullColumn="sub_checkplan_detail_id" javaType="java.util.List" resultMap="DmsFlowCheckplanDetailResult" />
    </resultMap>

    <resultMap type="DmsFlowCheckplanDetail" id="DmsFlowCheckplanDetailResult">
        <result property="checkplanDetailId"    column="sub_checkplan_detail_id"    />
        <result property="flowCheckplanId"    column="sub_flow_checkplan_id"    />
        <result property="deviceId"    column="sub_device_id"    />
        <result property="checkTime"    column="sub_check_time"    />
        <result property="variationFlag"    column="sub_variation_flag"    />
        <result property="skipCause"    column="sub_skip_cause"    />
        <result property="checkPerson"    column="sub_check_person"    />
        <result property="lastCheckTime"    column="sub_last_check_time"    />
    </resultMap>

    <sql id="selectDmsFlowCheckplanVo">
        select flow_checkplan_id, check_code, plan_time, check_by, device_type_id, check_amount, surplus_check_amount, check_state, remark, create_by, create_time, update_by, update_time from dms_flow_checkplan
    </sql>

    <select id="selectDmsFlowCheckplanList" parameterType="DmsFlowCheckplan" resultMap="DmsFlowCheckplanResult">
        <include refid="selectDmsFlowCheckplanVo"/>
        <where>
            <if test="flowCheckplanId!=null and flowCheckplanId!=''">and flow_checkplan_id = #{flowCheckplanId}</if>
            <if test="checkCode != null  and checkCode != ''"> and check_code like concat( '%',#{checkCode},'%')</if>
            <if test="planTime != null "> and plan_time = #{planTime}</if>
            <if test="checkBy != null  and checkBy != ''"> and check_by = #{checkBy}</if>
            <if test="deviceTypeId != null "> and device_type_id = #{deviceTypeId}</if>
            <if test="checkAmount != null "> and check_amount = #{checkAmount}</if>
            <if test="surplusCheckAmount != null "> and surplus_check_amount = #{surplusCheckAmount}</if>
            <if test="checkState != null "> and check_state = #{checkState}</if>
        </where>
    </select>

    <select id="selectDmsFlowCheckplanByFlowCheckplanId" parameterType="Long" resultMap="DmsFlowCheckplanDmsFlowCheckplanDetailResult">
        select a.flow_checkplan_id, a.check_code, a.plan_time, a.check_by, a.device_type_id, a.check_amount, a.surplus_check_amount, a.check_state, a.remark, a.create_by, a.create_time, a.update_by, a.update_time,
 b.checkplan_detail_id as sub_checkplan_detail_id, b.flow_checkplan_id as sub_flow_checkplan_id, b.device_id as sub_device_id, b.check_time as sub_check_time, b.variation_flag as sub_variation_flag, b.skip_cause as sub_skip_cause, b.check_person as sub_check_person, b.last_check_time as sub_last_check_time
        from dms_flow_checkplan a
        left join dms_flow_checkplan_detail b on b.flow_checkplan_id = a.flow_checkplan_id
        where a.flow_checkplan_id = #{flowCheckplanId}
    </select>

    <insert id="insertDmsFlowCheckplan" parameterType="DmsFlowCheckplan" useGeneratedKeys="true" keyProperty="flowCheckplanId">
        insert into dms_flow_checkplan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="checkCode != null">check_code,</if>
            <if test="planTime != null">plan_time,</if>
            <if test="checkBy != null">check_by,</if>
            <if test="deviceTypeId != null">device_type_id,</if>
            <if test="checkAmount != null">check_amount,</if>
            <if test="surplusCheckAmount != null">surplus_check_amount,</if>
            <if test="checkState != null">check_state,</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="checkCode != null">#{checkCode},</if>
            <if test="planTime != null">#{planTime},</if>
            <if test="checkBy != null">#{checkBy},</if>
            <if test="deviceTypeId != null">#{deviceTypeId},</if>
            <if test="checkAmount != null">#{checkAmount},</if>
            <if test="surplusCheckAmount != null">#{surplusCheckAmount},</if>
            <if test="checkState != null">#{checkState},</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="updateDmsFlowCheckplan" parameterType="DmsFlowCheckplan">
        update dms_flow_checkplan
        <trim prefix="SET" suffixOverrides=",">
            <if test="checkCode != null">check_code = #{checkCode},</if>
            <if test="planTime != null">plan_time = #{planTime},</if>
            <if test="checkBy != null">check_by = #{checkBy},</if>
            <if test="deviceTypeId != null">device_type_id = #{deviceTypeId},</if>
            <if test="checkAmount != null">check_amount = #{checkAmount},</if>
            <if test="surplusCheckAmount != null">surplus_check_amount = #{surplusCheckAmount},</if>
            <if test="checkState != null">check_state = #{checkState},</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 flow_checkplan_id = #{flowCheckplanId}
    </update>

    <delete id="deleteDmsFlowCheckplanByFlowCheckplanId" parameterType="Long">
        delete from dms_flow_checkplan where flow_checkplan_id = #{flowCheckplanId}
    </delete>

    <delete id="deleteDmsFlowCheckplanByFlowCheckplanIds" parameterType="String">
        delete from dms_flow_checkplan where flow_checkplan_id in
        <foreach item="flowCheckplanId" collection="array" open="(" separator="," close=")">
            #{flowCheckplanId}
        </foreach>
    </delete>

    <delete id="deleteDmsFlowCheckplanDetailByFlowCheckplanIds" parameterType="String">
        delete from dms_flow_checkplan_detail where flow_checkplan_id in
        <foreach item="flowCheckplanId" collection="array" open="(" separator="," close=")">
            #{flowCheckplanId}
        </foreach>
    </delete>

    <delete id="deleteDmsFlowCheckplanDetailByFlowCheckplanId" parameterType="Long">
        delete from dms_flow_checkplan_detail where flow_checkplan_id = #{flowCheckplanId}
    </delete>

    <insert id="batchDmsFlowCheckplanDetail">
        insert into dms_flow_checkplan_detail( checkplan_detail_id, flow_checkplan_id, device_id, check_time, variation_flag, skip_cause, check_person, last_check_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.checkplanDetailId}, #{item.flowCheckplanId}, #{item.deviceId}, #{item.checkTime}, #{item.variationFlag}, #{item.skipCause}, #{item.checkPerson}, #{item.lastCheckTime})
        </foreach>
    </insert>
</mapper>
