<?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.production.mapper.ProductPlanInfoMapper">

    <resultMap type="ProductPlanInfo" id="ProductPlanInfoResult">
        <result property="objId" column="obj_id"/>
        <result property="planCode" column="plan_code"/>
        <result property="orderCode" column="order_code"/>
        <result property="saleOrderCode" column="saleorder_code"/>
        <result property="saleorderLinenumber" column="saleorder_linenumber"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="productLineCode" column="productline_code"/>
        <result property="productLineName" column="productLineName"/>
        <result property="planAmount" column="plan_amount"/>
        <result property="completeAmount" column="complete_amount"/>
        <result property="beginTime" column="begin_time"/>
        <result property="endTime" column="end_time"/>
        <result property="isFlag" column="is_flag"/>
        <result property="createdBy" column="created_by"/>
        <result property="createdTime" column="created_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
    </resultMap>

    <sql id="selectProductPlanInfoVo">
        select pp.obj_id,
               pp.plan_code,
               pp.order_code,
               pp.saleorder_code,
               pp.saleorder_linenumber,
               pp.material_code,
               pp.material_name,
               pp.productline_code,
               pl.PRODUCT_LINE_NAME productLineName,
               pp.plan_amount,
               pp.complete_amount,
               pp.begin_time,
               pp.end_time,
               pp.is_flag,
               pp.created_by,
               pp.created_time,
               pp.updated_by,
               pp.updated_time
        from product_planinfo pp
                 left join BASE_PRODUCTLINE pl on pl.PRODUCT_LINE_CODE = pp.productline_code
    </sql>

    <select id="selectProductPlanInfoList" parameterType="ProductPlanInfo" resultMap="ProductPlanInfoResult">
        <include refid="selectProductPlanInfoVo"/>
        <where>
            <if test="planCode != null  and planCode != ''">and pp.plan_code like concat(concat('%', #{planCode}), '%')
            </if>
            <if test="orderCode != null  and orderCode != ''">and pp.order_code = #{orderCode}</if>
            <if test="saleOrderCode != null  and saleOrderCode != ''">and pp.saleorder_code = #{saleOrderCode}</if>
            <if test="saleorderLinenumber != null  and saleorderLinenumber != ''">and pp.saleorder_linenumber =
                #{saleorderLinenumber}
            </if>
            <if test="materialCode != null  and materialCode != ''">and pp.material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''">and pp.material_name like concat(concat('%',
                #{materialName}), '%')
            </if>
            <if test="productLineCode != null  and productLineCode != ''">and pp.productline_code = #{productLineCode}</if>
            <if test="planAmount != null ">and pp.plan_amount = #{planAmount}</if>
            <if test="completeAmount != null ">and pp.complete_amount = #{completeAmount}</if>
            <if test="params.beginBeginTime != null and params.beginBeginTime != '' and params.endBeginTime != null and params.endBeginTime != ''">
                and pp.begin_time between to_date(#{params.beginBeginTime}, 'yyyy-mm-dd hh24:mi:ss') and
                to_date(#{params.endBeginTime}, 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="params.beginEndTime != null and params.beginEndTime != '' and params.endEndTime != null and params.endEndTime != ''">
                and pp.end_time between to_date(#{params.beginEndTime}, 'yyyy-mm-dd hh24:mi:ss') and
                to_date(#{params.endEndTime}, 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="isFlag != null ">and pp.is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and pp.created_by = #{createdBy}</if>
            <if test="createdTime != null ">and pp.created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and pp.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and pp.updated_time = #{updatedTime}</if>
        </where>
        order by pp.plan_code desc
    </select>

    <select id="selectProductPlanInfoByObjId" parameterType="Long" resultMap="ProductPlanInfoResult">
        <include refid="selectProductPlanInfoVo"/>
        where pp.obj_id = #{objId}
    </select>

    <insert id="insertProductPlanInfo" parameterType="ProductPlanInfo">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_product_planinfo.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into product_planinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="planCode != null">plan_code,</if>
            <if test="orderCode != null">order_code,</if>
            <if test="saleOrderCode != null">saleorder_code,</if>
            <if test="saleorderLinenumber != null">saleorder_linenumber,</if>
            <if test="materialCode != null">material_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="productLineCode != null">productline_code,</if>
            <if test="planAmount != null">plan_amount,</if>
            <if test="completeAmount != null">complete_amount,</if>
            <if test="beginTime != null">begin_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedTime != null">updated_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="planCode != null">#{planCode},</if>
            <if test="orderCode != null">#{orderCode},</if>
            <if test="saleOrderCode != null">#{saleOrderCode},</if>
            <if test="saleorderLinenumber != null">#{saleorderLinenumber},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="productLineCode != null">#{productLineCode},</if>
            <if test="planAmount != null">#{planAmount},</if>
            <if test="completeAmount != null">#{completeAmount},</if>
            <if test="beginTime != null">#{beginTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateProductPlanInfo" parameterType="ProductPlanInfo">
        update product_planinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="planCode != null">plan_code = #{planCode},</if>
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="saleOrderCode != null">saleorder_code = #{saleOrderCode},</if>
            <if test="saleorderLinenumber != null">saleorder_linenumber = #{saleorderLinenumber},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="productLineCode != null">productline_code = #{productLineCode},</if>
            <if test="planAmount != null">plan_amount = #{planAmount},</if>
            <if test="completeAmount != null">complete_amount = #{completeAmount},</if>
            <if test="beginTime != null">begin_time = #{beginTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="createdBy != null">created_by = #{createdBy},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedBy != null">updated_by = #{updatedBy},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteProductPlanInfoByObjId" parameterType="Long">
        delete
        from product_planinfo
        where obj_id = #{objId}
    </delete>

    <delete id="deleteProductPlanInfoByObjIds" parameterType="String">
        delete from product_planinfo where obj_id in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>
</mapper>