<?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.zg.project.Inventory.Visualization.mapper.OdtlMapper">
    
    <resultMap type="Odtl" id="OdtlResult">
        <result property="Id"    column="Id"    />
        <result property="rdid"    column="rdid"    />
        <result property="sid"    column="sid"    />
        <result property="amt"    column="amt"    />
        <result property="pcde"    column="pcde"    />
        <result property="ste"    column="ste"    />
        <result property="rfid"    column="rfid"    />
        <result property="wsid"    column="wsid"    />
        <result property="vlid"    column="vlid"    />
        <result property="flg"    column="flg"    />
        <result property="prf"    column="prf"    />
    </resultMap>

    <sql id="selectOdtlVo">
        select Id, rdid, sid, amt, pcde, ste, rfid, wsid, vlid, flg, prf from odtl
    </sql>

    <select id="selectOdtlList" parameterType="Odtl" resultMap="OdtlResult">
        <include refid="selectOdtlVo"/>
        <where>  
            <if test="rdid != null "> and rdid = #{rdid}</if>
            <if test="sid != null "> and sid = #{sid}</if>
            <if test="amt != null "> and amt = #{amt}</if>
            <if test="pcde != null  and pcde != ''"> and pcde = #{pcde}</if>
            <if test="ste != null  and ste != ''"> and ste = #{ste}</if>
            <if test="rfid != null  and rfid != ''"> and rfid = #{rfid}</if>
            <if test="wsid != null "> and wsid = #{wsid}</if>
            <if test="vlid != null  and vlid != ''"> and vlid = #{vlid}</if>
            <if test="flg != null "> and flg = #{flg}</if>
            <if test="prf != null  and prf != ''"> and prf = #{prf}</if>
        </where>
    </select>
    
    <select id="selectOdtlById" parameterType="Long" resultMap="OdtlResult">
        <include refid="selectOdtlVo"/>
        where Id = #{Id}
    </select>

    <select id="queryAll" resultType="com.zg.project.Inventory.Visualization.domain.vo.OdtlVO">
        SELECT
            A.amt AS aamt,
            A.pcde AS bcde,
            A.sid,
            B.mid,
            B.erpid AS berpid,
            B.des_pro AS bdes_pro,
            B.prf AS bprf,
            B.mrk,
            B.ste,
            B.prc,
            NULLIF(TRIM(B.bth), '') AS bth,
            B.gys AS bgys,
            B.fycde_1,
            B.fycde_2,

            C.id AS rid,
            C.erpid AS cerpid,
            C.des_pro AS cdes_pro,
            C.bnum,
            C.tpe,
            C.usr,
            C.nme,
            C.tme,
            C.cde,
            C.tnme,
            DATE_FORMAT(C.ptme, '%Y-%m') AS ptme,
            C.tpnme,
            C.cgy,
            C.wtpe,
            C.wh,
            C.tme AS ctme,

            D.des_mat,
            D.unt,

            E.mid AS smid,
            E.bnum AS sbnum,
            E.pcde AS spcde,
            E.gys AS egys,
            E.mrk AS smrk

        FROM odtl A
                 LEFT JOIN rdtl B ON A.rdid = B.id
                 LEFT JOIN rcp  C ON B.rid  = C.id
                 LEFT JOIN mtd  D ON B.mid = D.mid
                 LEFT JOIN sod  E ON A.sid = E.id

        WHERE B.ste = 1
          AND C.del = 0
        ORDER BY C.ptme DESC
    </select>

    <insert id="insertOdtl" parameterType="Odtl" useGeneratedKeys="true" keyProperty="Id">
        insert into odtl
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="rdid != null">rdid,</if>
            <if test="sid != null">sid,</if>
            <if test="amt != null">amt,</if>
            <if test="pcde != null">pcde,</if>
            <if test="ste != null">ste,</if>
            <if test="rfid != null">rfid,</if>
            <if test="wsid != null">wsid,</if>
            <if test="vlid != null">vlid,</if>
            <if test="flg != null">flg,</if>
            <if test="prf != null">prf,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="rdid != null">#{rdid},</if>
            <if test="sid != null">#{sid},</if>
            <if test="amt != null">#{amt},</if>
            <if test="pcde != null">#{pcde},</if>
            <if test="ste != null">#{ste},</if>
            <if test="rfid != null">#{rfid},</if>
            <if test="wsid != null">#{wsid},</if>
            <if test="vlid != null">#{vlid},</if>
            <if test="flg != null">#{flg},</if>
            <if test="prf != null">#{prf},</if>
         </trim>
    </insert>

    <update id="updateOdtl" parameterType="Odtl">
        update odtl
        <trim prefix="SET" suffixOverrides=",">
            <if test="rdid != null">rdid = #{rdid},</if>
            <if test="sid != null">sid = #{sid},</if>
            <if test="amt != null">amt = #{amt},</if>
            <if test="pcde != null">pcde = #{pcde},</if>
            <if test="ste != null">ste = #{ste},</if>
            <if test="rfid != null">rfid = #{rfid},</if>
            <if test="wsid != null">wsid = #{wsid},</if>
            <if test="vlid != null">vlid = #{vlid},</if>
            <if test="flg != null">flg = #{flg},</if>
            <if test="prf != null">prf = #{prf},</if>
        </trim>
        where Id = #{Id}
    </update>

    <delete id="deleteOdtlById" parameterType="Long">
        delete from odtl where Id = #{Id}
    </delete>

    <delete id="deleteOdtlByIds" parameterType="String">
        delete from odtl where Id in 
        <foreach item="Id" collection="array" open="(" separator="," close=")">
            #{Id}
        </foreach>
    </delete>
</mapper>