<?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.mes.mapper.MesProductPlanMapper">

    <resultMap type="MesProductPlan" id="MesProductPlanResult">
        <result property="planId" column="plan_id"/>
        <result property="productOrderId" column="product_order_id"/>
        <result property="orderCode" column="order_code"/>
        <result property="planCode" column="plan_code"/>
        <result property="dispatchCode" column="dispatch_code"/>
        <result property="materialId" column="material_id"/>
        <result property="materialName" column="material_name"/>
        <result property="materialBomId" column="material_bom_id"/>
        <result property="materialBomName" column="materialBomName"/>
        <result property="processId" column="process_id"/>
        <result property="processOrder" column="process_order"/>
        <result property="lastProcessId" column="last_process_id"/>
        <result property="lastProcessName" column="lastProcessName"/>
        <result property="stationId" column="station_id"/>
        <result property="userId" column="user_id"/>
        <result property="productionTime" column="production_time"/>
        <result property="planAmount" column="plan_amount"/>
        <result property="completeAmount" column="complete_amount"/>
        <result property="planBeginTime" column="plan_begin_time"/>
        <result property="planEndTime" column="plan_end_time"/>
        <result property="realBeginTime" column="real_begin_time"/>
        <result property="realEndTime" column="real_end_time"/>
        <result property="attachId" column="attach_id"/>
        <result property="planStatus" column="plan_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="planDeliveryDate" column="plan_delivery_date"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="processName" column="process_name"/>
        <result property="stationName" column="station_name"/>
    </resultMap>

    <resultMap id="MesProductPlanMesProductPlanDetailResult" type="MesProductPlan" extends="MesProductPlanResult">
        <collection property="mesProductPlanDetailList" notNullColumn="sub_plan_detail_id" javaType="java.util.List"
                    resultMap="MesProductPlanDetailResult"/>
    </resultMap>

    <resultMap type="MesProductPlanDetail" id="MesProductPlanDetailResult">
        <result property="planDetailId" column="sub_plan_detail_id"/>
        <result property="planDetailCode" column="sub_plan_detail_code"/>
        <result property="planId" column="sub_plan_id"/>
        <result property="planCode" column="sub_plan_code"/>
        <result property="userId" column="sub_user_id"/>
        <result property="userName" column="sub_user_name"/>
        <result property="realBeginTime" column="sub_real_begin_time"/>
        <result property="realEndTime" column="sub_real_end_time"/>
        <result property="planDetailStatus" column="sub_plan_detail_status"/>
        <result property="isFlag" column="sub_is_flag"/>
        <result property="remark" column="sub_remark"/>
        <result property="createBy" column="sub_create_by"/>
        <result property="createTime" column="sub_create_time"/>
        <result property="updateBy" column="sub_update_by"/>
        <result property="updateTime" column="sub_update_time"/>
    </resultMap>

    <sql id="selectMesProductPlanVo">
        select mpp.plan_id,
               mpp.product_order_id,
               po.order_code,
               mpp.plan_code,
               mpp.dispatch_code,
               mpp.material_id,
               mi.material_name,
               mpp.material_bom_id,
               concat(mb.material_name, '-', mb.material_bom_desc) materialBomName,
               mpp.process_id,
               bpi.process_name,
               mpp.process_order,
               mpp.last_process_id,
               bp2.process_name                                    lastProcessName,
               mpp.station_id,
               bsi.station_name,
               mpp.user_id,
               mpp.production_time,
               mpp.plan_amount,
               mpp.complete_amount,
               mpp.plan_begin_time,
               mpp.plan_end_time,
               mpp.real_begin_time,
               mpp.real_end_time,
               mpp.attach_id,
               mpp.plan_status,
               mpp.is_flag,
               mpp.remark,
               mpp.create_by,
               mpp.create_time,
               mpp.update_by,
               mpp.update_time
        from mes_product_plan mpp
                 left join mes_base_process_info bpi on bpi.process_id = mpp.process_id
                 left join mes_base_station_info bsi on bsi.station_id = mpp.station_id
                 left join mes_base_material_info mi on mi.material_id = mpp.material_id
                 left join mes_material_bom mb on mb.material_bom_id = mpp.material_bom_id
                 left join mes_base_process_info bp2 on bp2.process_id = mpp.last_process_id
                 left join mes_product_order po on po.product_order_id = mpp.product_order_id
    </sql>

    <select id="selectMesProductPlanList" parameterType="MesProductPlan" resultMap="MesProductPlanResult">
        <include refid="selectMesProductPlanVo"/>
        <where>
            <if test="productOrderId != null ">and  mpp.product_order_id = #{productOrderId}</if>
            <if test="planCode != null  and planCode != ''">and  mpp.plan_code = #{planCode}</if>
            <if test="dispatchCode != null  and dispatchCode != ''">and  mpp.dispatch_code = #{dispatchCode}</if>
            <if test="materialId != null ">and  mpp.material_id = #{materialId}</if>
            <if test="materialBomId != null ">and  mpp.material_bom_id = #{materialBomId}</if>
            <if test="processId != null ">and  mpp.process_id = #{processId}</if>
            <if test="processOrder != null ">and  mpp.process_order = #{processOrder}</if>
            <if test="lastProcessId != null ">and  mpp.last_process_id = #{lastProcessId}</if>
            <if test="stationId != null ">and  mpp.station_id = #{stationId}</if>
            <if test="userId != null ">and  mpp.user_id = #{userId}</if>
            <if test="productionTime != null ">and  mpp.production_time = #{productionTime}</if>
            <if test="planAmount != null ">and  mpp.plan_amount = #{planAmount}</if>
            <if test="completeAmount != null ">and  mpp.complete_amount = #{completeAmount}</if>
            <if test="planBeginTime != null ">and  mpp.plan_begin_time = #{planBeginTime}</if>
            <if test="planEndTime != null ">and  mpp.plan_end_time = #{planEndTime}</if>
            <if test="realBeginTime != null ">and  mpp.real_begin_time = #{realBeginTime}</if>
            <if test="realEndTime != null ">and  mpp.real_end_time = #{realEndTime}</if>
            <if test="attachId != null  and attachId != ''">and  mpp.attach_id = #{attachId}</if>
            <if test="planStatus != null  and planStatus != ''">and  mpp.plan_status = #{planStatus}</if>
            <if test="isFlag != null  and isFlag != ''">and  mpp.is_flag = #{isFlag}</if>
        </where>
    </select>

    <select id="selectMesProductPlanByPlanId" parameterType="Long" resultMap="MesProductPlanMesProductPlanDetailResult">
        select a.plan_id,
               a.product_order_id,
               a.plan_code,
               a.dispatch_code,
               a.material_id,
               a.material_bom_id,
               a.process_id,
               a.process_order,
               a.last_process_id,
               a.station_id,
               a.user_id,
               a.production_time,
               a.plan_amount,
               a.complete_amount,
               a.plan_begin_time,
               a.plan_end_time,
               a.real_begin_time,
               a.real_end_time,
               a.attach_id,
               a.plan_status,
               a.is_flag,
               a.remark,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               b.plan_detail_id     as sub_plan_detail_id,
               b.plan_detail_code   as sub_plan_detail_code,
               b.plan_id            as sub_plan_id,
               b.plan_code          as sub_plan_code,
               b.user_id            as sub_user_id,
               b.user_name          as sub_user_name,
               b.real_begin_time    as sub_real_begin_time,
               b.real_end_time      as sub_real_end_time,
               b.plan_detail_status as sub_plan_detail_status,
               b.is_flag            as sub_is_flag,
               b.remark             as sub_remark,
               b.create_by          as sub_create_by,
               b.create_time        as sub_create_time,
               b.update_by          as sub_update_by,
               b.update_time        as sub_update_time
        from mes_product_plan a
                 left join mes_product_plan_detail b on b.plan_id = a.plan_id
        where a.plan_id = #{planId}
    </select>

    <insert id="insertMesProductPlan" parameterType="MesProductPlan" useGeneratedKeys="true" keyProperty="planId">
        insert into mes_product_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productOrderId != null">product_order_id,</if>
            <if test="planCode != null and planCode != ''">plan_code,</if>
            <if test="dispatchCode != null and dispatchCode != ''">dispatch_code,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialBomId != null">material_bom_id,</if>
            <if test="processId != null">process_id,</if>
            <if test="processOrder != null">process_order,</if>
            <if test="lastProcessId != null">last_process_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="productionTime != null">production_time,</if>
            <if test="planAmount != null">plan_amount,</if>
            <if test="completeAmount != null">complete_amount,</if>
            <if test="planBeginTime != null">plan_begin_time,</if>
            <if test="planEndTime != null">plan_end_time,</if>
            <if test="realBeginTime != null">real_begin_time,</if>
            <if test="realEndTime != null">real_end_time,</if>
            <if test="attachId != null">attach_id,</if>
            <if test="planStatus != null and planStatus != ''">plan_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="productOrderId != null">#{productOrderId},</if>
            <if test="planCode != null and planCode != ''">#{planCode},</if>
            <if test="dispatchCode != null and dispatchCode != ''">#{dispatchCode},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialBomId != null">#{materialBomId},</if>
            <if test="processId != null">#{processId},</if>
            <if test="processOrder != null">#{processOrder},</if>
            <if test="lastProcessId != null">#{lastProcessId},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="productionTime != null">#{productionTime},</if>
            <if test="planAmount != null">#{planAmount},</if>
            <if test="completeAmount != null">#{completeAmount},</if>
            <if test="planBeginTime != null">#{planBeginTime},</if>
            <if test="planEndTime != null">#{planEndTime},</if>
            <if test="realBeginTime != null">#{realBeginTime},</if>
            <if test="realEndTime != null">#{realEndTime},</if>
            <if test="attachId != null">#{attachId},</if>
            <if test="planStatus != null and planStatus != ''">#{planStatus},</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="updateMesProductPlan" parameterType="MesProductPlan">
        update mes_product_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="productOrderId != null">product_order_id = #{productOrderId},</if>
            <if test="planCode != null and planCode != ''">plan_code = #{planCode},</if>
            <if test="dispatchCode != null and dispatchCode != ''">dispatch_code = #{dispatchCode},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialBomId != null">material_bom_id = #{materialBomId},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="processOrder != null">process_order = #{processOrder},</if>
            <if test="lastProcessId != null">last_process_id = #{lastProcessId},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="productionTime != null">production_time = #{productionTime},</if>
            <if test="planAmount != null">plan_amount = #{planAmount},</if>
            <if test="completeAmount != null">complete_amount = #{completeAmount},</if>
            <if test="planBeginTime != null">plan_begin_time = #{planBeginTime},</if>
            <if test="planEndTime != null">plan_end_time = #{planEndTime},</if>
            <if test="realBeginTime != null">real_begin_time = #{realBeginTime},</if>
            <if test="realEndTime != null">real_end_time = #{realEndTime},</if>
            <if test="attachId != null">attach_id = #{attachId},</if>
            <if test="planStatus != null and planStatus != ''">plan_status = #{planStatus},</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 plan_id = #{planId}
    </update>

    <delete id="deleteMesProductPlanByPlanId" parameterType="Long">
        delete
        from mes_product_plan
        where plan_id = #{planId}
    </delete>

    <delete id="deleteMesProductPlanByPlanIds" parameterType="String">
        delete from mes_product_plan where plan_id in
        <foreach item="planId" collection="array" open="(" separator="," close=")">
            #{planId}
        </foreach>
    </delete>

    <delete id="deleteMesProductPlanDetailByPlanIds" parameterType="String">
        delete from mes_product_plan_detail where plan_id in
        <foreach item="planId" collection="array" open="(" separator="," close=")">
            #{planId}
        </foreach>
    </delete>

    <delete id="deleteMesProductPlanDetailByPlanId" parameterType="Long">
        delete
        from mes_product_plan_detail
        where plan_id = #{planId}
    </delete>

    <insert id="batchMesProductPlanDetail">
        insert into mes_product_plan_detail( plan_detail_id, plan_detail_code, plan_id, plan_code, user_id, user_name,
        real_begin_time, real_end_time, plan_detail_status, is_flag, remark, create_by, create_time, update_by,
        update_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.planDetailId}, #{item.planDetailCode}, #{item.planId}, #{item.planCode}, #{item.userId},
            #{item.userName}, #{item.realBeginTime}, #{item.realEndTime}, #{item.planDetailStatus}, #{item.isFlag},
            #{item.remark}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>


    <select id="selectMesProductPlanJoinList" parameterType="MesProductPlan" resultMap="MesProductPlanResult">
        select mpp.plan_id, mpp.product_order_id, mpp.plan_code, mpp.dispatch_code, mpp.material_id,
        mpp.material_bom_id, mpp.process_id, mpp.process_order, mpp.last_process_id,
        mpp.station_id, mpp.plan_amount, mpp.complete_amount, mpp.plan_begin_time, mpp.plan_end_time,
        mpp.real_begin_time, mpp.real_end_time,
        mpp.attach_id, mpp.plan_status, mpo.plan_delivery_date,mbmi.material_code,mbmi.material_name
        from mes_product_plan mpp left join mes_product_order mpo on mpp.product_order_id = mpo.product_order_id
        left join mes_base_material_info mbmi on mpp.material_id=mbmi.material_id
        <where>
            <if test="stationId != null ">and mpp.station_id = #{stationId}</if>
            <if test="planStatus != null  and planStatus != ''">and mpp.plan_status = #{planStatus}</if>
        </where>
    </select>

</mapper>
