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

    <resultMap type="MrpComponentWeeklyReq" id="MrpComponentWeeklyReqResult">
            <result property="id"    column="id"    />
            <result property="yearWeek"    column="year_week"    />
            <result property="weekStartDate"    column="week_start_date"    />
            <result property="itemCode"    column="item_code"    />
            <result property="itemDesc"    column="item_desc"    />
            <result property="reqMon"    column="req_mon"    />
            <result property="reqTue"    column="req_tue"    />
            <result property="reqWed"    column="req_wed"    />
            <result property="reqThu"    column="req_thu"    />
            <result property="reqFri"    column="req_fri"    />
            <result property="reqSat"    column="req_sat"    />
            <result property="reqSun"    column="req_sun"    />
            <result property="reqWeekTotal"    column="req_week_total"    />
            <result property="createdAt"    column="created_at"    />
            <result property="updatedAt"    column="updated_at"    />
    </resultMap>

    <sql id="selectMrpComponentWeeklyReqVo">
        select id, year_week, week_start_date, item_code, item_desc, req_mon, req_tue, req_wed, req_thu, req_fri, req_sat, req_sun, req_week_total, created_at, updated_at from mrp_component_weekly_req
    </sql>

    <select id="selectMrpComponentWeeklyReqList" parameterType="MrpComponentWeeklyReq" resultMap="MrpComponentWeeklyReqResult">
        <include refid="selectMrpComponentWeeklyReqVo"/>
        <where>
                        <if test="yearWeek != null  and yearWeek != ''"> and year_week = #{yearWeek}</if>
                        <if test="weekStartDate != null "> and week_start_date = #{weekStartDate}</if>
                        <if test="params != null and params.beginWeekStartDate != null and params.beginWeekStartDate != ''"> and week_start_date &gt;= to_date(#{params.beginWeekStartDate}, 'yyyy-mm-dd')</if>
                        <if test="params != null and params.endWeekStartDate != null and params.endWeekStartDate != ''"> and week_start_date &lt;= to_date(#{params.endWeekStartDate}, 'yyyy-mm-dd')</if>
                        <if test="itemCode != null  and itemCode != ''"> and item_code = #{itemCode}</if>
                        <if test="itemDesc != null  and itemDesc != ''"> and item_desc = #{itemDesc}</if>
                        <if test="reqMon != null "> and req_mon = #{reqMon}</if>
                        <if test="reqTue != null "> and req_tue = #{reqTue}</if>
                        <if test="reqWed != null "> and req_wed = #{reqWed}</if>
                        <if test="reqThu != null "> and req_thu = #{reqThu}</if>
                        <if test="reqFri != null "> and req_fri = #{reqFri}</if>
                        <if test="reqSat != null "> and req_sat = #{reqSat}</if>
                        <if test="reqSun != null "> and req_sun = #{reqSun}</if>
                        <if test="reqWeekTotal != null "> and req_week_total = #{reqWeekTotal}</if>
                        <if test="createdAt != null "> and created_at = #{createdAt}</if>
                        <if test="updatedAt != null "> and updated_at = #{updatedAt}</if>
        </where>
    </select>

    <select id="selectMrpComponentWeeklyReqById" parameterType="Long" resultMap="MrpComponentWeeklyReqResult">
            <include refid="selectMrpComponentWeeklyReqVo"/>
            where id = #{id}
    </select>

    <insert id="insertMrpComponentWeeklyReq" parameterType="MrpComponentWeeklyReq">
            <selectKey keyProperty="id" resultType="long" order="BEFORE">
                SELECT seq_mrp_component_weekly_req.NEXTVAL as id FROM DUAL
            </selectKey>
        insert into mrp_component_weekly_req
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="id != null">id,</if>
                <if test="yearWeek != null and yearWeek != ''">year_week,</if>
                <if test="weekStartDate != null">week_start_date,</if>
                <if test="itemCode != null and itemCode != ''">item_code,</if>
                <if test="itemDesc != null">item_desc,</if>
                <if test="reqMon != null">req_mon,</if>
                <if test="reqTue != null">req_tue,</if>
                <if test="reqWed != null">req_wed,</if>
                <if test="reqThu != null">req_thu,</if>
                <if test="reqFri != null">req_fri,</if>
                <if test="reqSat != null">req_sat,</if>
                <if test="reqSun != null">req_sun,</if>
                <if test="reqWeekTotal != null">req_week_total,</if>
                <if test="createdAt != null">created_at,</if>
                <if test="updatedAt != null">updated_at,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="id != null">#{id},</if>
                <if test="yearWeek != null and yearWeek != ''">#{yearWeek},</if>
                <if test="weekStartDate != null">#{weekStartDate},</if>
                <if test="itemCode != null and itemCode != ''">#{itemCode},</if>
                <if test="itemDesc != null">#{itemDesc},</if>
                <if test="reqMon != null">#{reqMon},</if>
                <if test="reqTue != null">#{reqTue},</if>
                <if test="reqWed != null">#{reqWed},</if>
                <if test="reqThu != null">#{reqThu},</if>
                <if test="reqFri != null">#{reqFri},</if>
                <if test="reqSat != null">#{reqSat},</if>
                <if test="reqSun != null">#{reqSun},</if>
                <if test="reqWeekTotal != null">#{reqWeekTotal},</if>
                <if test="createdAt != null">#{createdAt},</if>
                <if test="updatedAt != null">#{updatedAt},</if>
        </trim>
    </insert>

    <update id="updateMrpComponentWeeklyReq" parameterType="MrpComponentWeeklyReq">
        update mrp_component_weekly_req
        <trim prefix="SET" suffixOverrides=",">
                    <if test="yearWeek != null and yearWeek != ''">year_week = #{yearWeek},</if>
                    <if test="weekStartDate != null">week_start_date = #{weekStartDate},</if>
                    <if test="itemCode != null and itemCode != ''">item_code = #{itemCode},</if>
                    <if test="itemDesc != null">item_desc = #{itemDesc},</if>
                    <if test="reqMon != null">req_mon = #{reqMon},</if>
                    <if test="reqTue != null">req_tue = #{reqTue},</if>
                    <if test="reqWed != null">req_wed = #{reqWed},</if>
                    <if test="reqThu != null">req_thu = #{reqThu},</if>
                    <if test="reqFri != null">req_fri = #{reqFri},</if>
                    <if test="reqSat != null">req_sat = #{reqSat},</if>
                    <if test="reqSun != null">req_sun = #{reqSun},</if>
                    <!-- req_week_total 是虚拟列，不能更新 -->
                    <if test="createdAt != null">created_at = #{createdAt},</if>
                    <if test="updatedAt != null">updated_at = #{updatedAt},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMrpComponentWeeklyReqById" parameterType="Long">
        delete from mrp_component_weekly_req where id = #{id}
    </delete>

    <delete id="deleteMrpComponentWeeklyReqByIds" parameterType="String">
        delete from mrp_component_weekly_req where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id,jdbcType=NUMERIC}
        </foreach>
    </delete>

    <insert id="batchInsert" parameterType="java.util.List">
        INSERT ALL
        <foreach collection="list" item="item" separator=" ">
            INTO mrp_component_weekly_req (id, year_week, week_start_date, item_code, item_desc, req_mon, req_tue, req_wed, req_thu, req_fri, req_sat, req_sun, created_at)
            VALUES (seq_mrp_component_weekly_req.NEXTVAL, #{item.yearWeek,jdbcType=VARCHAR}, #{item.weekStartDate,jdbcType=DATE}, #{item.itemCode,jdbcType=VARCHAR}, #{item.itemDesc,jdbcType=VARCHAR}, #{item.reqMon,jdbcType=NUMERIC}, #{item.reqTue,jdbcType=NUMERIC}, #{item.reqWed,jdbcType=NUMERIC}, #{item.reqThu,jdbcType=NUMERIC}, #{item.reqFri,jdbcType=NUMERIC}, #{item.reqSat,jdbcType=NUMERIC}, #{item.reqSun,jdbcType=NUMERIC}, #{item.createdAt,jdbcType=TIMESTAMP})
        </foreach>
        SELECT 1 FROM DUAL
    </insert>
</mapper>