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

    <resultMap type="CalendarInfo" id="CalendarInfoResult">
        <result property="objId" column="obj_id"/>
        <result property="sapPlanCode" column="sap_plan_code"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="planStartDate" column="plan_start_date"/>
        <result property="planEndDate" column="plan_end_date"/>
        <result property="planAmount" column="plan_amount"/>
        <result property="schedulingDate" column="scheduling_date"/>
        <result property="isScheduling" column="is_scheduling"/>
        <result property="schedulingTeam" column="scheduling_team"/>
        <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="selectCalendarInfoVo">
        select obj_id,
               sap_plan_code,
               material_code,
               material_name,
               plan_start_date,
               plan_end_date,
               plan_amount,
               scheduling_date,
               is_scheduling,
               scheduling_team,
               is_flag,
               created_by,
               created_time,
               updated_by,
               updated_time
        from product_calendar_info
    </sql>

    <select id="selectCalendarInfoList" parameterType="CalendarInfo" resultMap="CalendarInfoResult">
        <include refid="selectCalendarInfoVo"/>
        <where>
            material_code like concat('90', '%')
            <if test="sapPlanCode != null  and sapPlanCode != ''">and sap_plan_code = #{sapPlanCode}</if>
            <if test="materialCode != null  and materialCode != ''">and material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''">and material_name like concat(concat('%',
                #{materialName}), '%')
            </if>
            <if test="params.beginPlanStartDate != null and params.beginPlanStartDate != '' and params.endPlanStartDate != null and params.endPlanStartDate != ''">
                and plan_start_date between to_date(#{params.beginPlanStartDate}, 'yyyy-mm-dd hh24:mi:ss') and
                to_date(#{params.endPlanStartDate}, 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="planEndDate != null ">and plan_end_date = #{planEndDate}</if>
            <if test="planAmount != null ">and plan_amount = #{planAmount}</if>
            <if test="params.beginSchedulingDate != null and params.beginSchedulingDate != '' and params.endSchedulingDate != null and params.endSchedulingDate != ''">
                and scheduling_date between to_date(#{params.beginSchedulingDate}, 'yyyy-mm-dd hh24:mi:ss') and
                to_date(#{params.endSchedulingDate}, 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="isScheduling != null ">and is_scheduling = #{isScheduling}</if>
            <if test="schedulingTeam != null  and schedulingTeam != ''">and scheduling_team = #{schedulingTeam}</if>
            <if test="isFlag != null ">and is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and created_by = #{createdBy}</if>
            <if test="createdTime != null ">and created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and updated_time = #{updatedTime}</if>
        </where>
    </select>

    <select id="selectCalendarInfoByObjId" parameterType="Long" resultMap="CalendarInfoResult">
        <include refid="selectCalendarInfoVo"/>
        where obj_id = #{objId}
    </select>

    <insert id="insertCalendarInfo" parameterType="CalendarInfo">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_product_calendar_info.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into product_calendar_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="sapPlanCode != null">sap_plan_code,</if>
            <if test="materialCode != null">material_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="planStartDate != null">plan_start_date,</if>
            <if test="planEndDate != null">plan_end_date,</if>
            <if test="planAmount != null">plan_amount,</if>
            <if test="schedulingDate != null">scheduling_date,</if>
            <if test="isScheduling != null">is_scheduling,</if>
            <if test="schedulingTeam != null">scheduling_team,</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="sapPlanCode != null">#{sapPlanCode},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName,jdbcType=NVARCHAR},</if>
            <if test="planStartDate != null">#{planStartDate},</if>
            <if test="planEndDate != null">#{planEndDate},</if>
            <if test="planAmount != null">#{planAmount},</if>
            <if test="schedulingDate != null">#{schedulingDate},</if>
            <if test="isScheduling != null">#{isScheduling},</if>
            <if test="schedulingTeam != null">#{schedulingTeam},</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="updateCalendarInfo" parameterType="CalendarInfo">
        update product_calendar_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="sapPlanCode != null">sap_plan_code = #{sapPlanCode},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName,jdbcType=NVARCHAR},</if>
            <if test="planStartDate != null">plan_start_date = #{planStartDate},</if>
            <if test="planEndDate != null">plan_end_date = #{planEndDate},</if>
            <if test="planAmount != null">plan_amount = #{planAmount},</if>
            <if test="schedulingDate != null">scheduling_date = #{schedulingDate},</if>
            <if test="isScheduling != null">is_scheduling = #{isScheduling},</if>
            <if test="schedulingTeam != null">scheduling_team = #{schedulingTeam},</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="deleteCalendarInfoByObjId" parameterType="Long">
        delete
        from product_calendar_info
        where obj_id = #{objId}
    </delete>

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