<?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.steerinfo.ems.ifmesemsswapfile.mapper.IfMesEmsSwapfileMapper">
  <resultMap id="BaseResultMap" type="com.steerinfo.ems.ifmesemsswapfile.model.IfMesEmsSwapfile">
    <result column="FID" jdbcType="VARCHAR" property="fid" />
    <result column="TYPE" jdbcType="VARCHAR" property="type" />
    <result column="WORKS" jdbcType="VARCHAR" property="works" />
    <result column="PRODLINE" jdbcType="VARCHAR" property="prodline" />
    <result column="PRODATE" jdbcType="DATE" property="prodate" />
    <result column="ORDERNO" jdbcType="VARCHAR" property="orderno" />
    <result column="MATERIALCODE" jdbcType="VARCHAR" property="materialcode" />
    <result column="MATERIALNAME" jdbcType="VARCHAR" property="materialname" />
    <result column="UNIT" jdbcType="VARCHAR" property="unit" />
    <result column="QTY" jdbcType="DECIMAL" property="qty" />
    <result column="CREATETIME" jdbcType="DATE" property="createtime" />
    <result column="STATUS" jdbcType="VARCHAR" property="status" />
    <result column="SENDTIME" jdbcType="DATE" property="sendtime" />
    <result column="MEMO" jdbcType="DATE" property="memo" />
  </resultMap>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select FID, TYPE, WORKS, PRODLINE, PRODATE, ORDERNO, MATERIALCODE, MATERIALNAME, UNIT, QTY, CREATETIME, STATUS, SENDTIME,MEMO
    from IF_MES_EMS_SWAPFILE
    where FID = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteBySelectiveElement" parameterType="java.util.HashMap">
    delete from IF_MES_EMS_SWAPFILE
    <where>
        <if test="fid != null and fid != ''">
            and FID = #{fid}
        </if>
        <if test="type != null and type != ''">
            and TYPE = #{type}
        </if>
        <if test="works != null and works != ''">
            and WORKS = #{works}
        </if>
        <if test="prodline != null and prodline != ''">
            and PRODLINE = #{prodline}
        </if>
        <if test="prodate != null">
            and PRODATE = #{prodate}
        </if>
        <if test="orderno != null and orderno != ''">
            and ORDERNO = #{orderno}
        </if>
        <if test="materialcode != null and materialcode != ''">
            and MATERIALCODE = #{materialcode}
        </if>
        <if test="materialname != null and materialname != ''">
            and MATERIALNAME = #{materialname}
        </if>
        <if test="unit != null and unit != ''">
            and UNIT = #{unit}
        </if>
        <if test="qty != null and qty != ''">
            and QTY = #{qty}
        </if>
        <if test="createtime != null and createtime != ''">
            and CREATETIME = #{createtime}
        </if>
        <if test="status != null and status != ''">
            and STATUS = #{status}
        </if>
        <if test="sendtime != null and sendtime != ''">
            and SENDTIME = #{sendtime}
        </if>
    </where>
  </delete>
  
  <select id="selectByParameters" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    select FID, TYPE, WORKS, PRODLINE, PRODATE, ORDERNO, MATERIALCODE, MATERIALNAME, UNIT, QTY, CREATETIME, STATUS, SENDTIME,MEMO
      from IF_MES_EMS_SWAPFILE
    <where>
        <if test="fid != null and fid != ''">
            and FID = #{fid}
        </if>
        <if test="type != null and type != ''">
            and TYPE = #{type}
        </if>
        <if test="works != null and works != ''">
            and WORKS = #{works}
        </if>
        <if test="prodline != null and prodline != ''">
            and PRODLINE = #{prodline}
        </if>
        <if test="prodate != null and prodate != ''">
            and to_char(PRODATE,'yyyy-MM-dd') LIKE '${prodate}%'
        </if>
        <if test="orderno != null and orderno != ''">
            and ORDERNO = #{orderno}
        </if>
        <if test="materialcode != null and materialcode != ''">
            and MATERIALCODE = #{materialcode}
        </if>
        <if test="materialname != null and materialname != ''">
            and MATERIALNAME = #{materialname}
        </if>
        <if test="unit != null and unit != ''">
            and UNIT = #{unit}
        </if>
        <if test="qty != null and qty != ''">
            and QTY = #{qty}
        </if>
        <if test="createtime != null and createtime != ''">
            and CREATETIME = #{createtime}
        </if>
        <if test="status != null and status != ''">
            and STATUS = #{status}
        </if>
        <if test="sendtime != null and sendtime != ''">
            and SENDTIME = #{sendtime}
        </if>
    </where>
  </select>

  <select id="selectLikeByParameters" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    select FID, TYPE, WORKS, PRODLINE, PRODATE, ORDERNO, MATERIALCODE, MATERIALNAME, UNIT, QTY, CREATETIME, STATUS, SENDTIME,MEMO
      from IF_MES_EMS_SWAPFILE
    <where>
        <if test="fid != null and fid != ''">
            and FID LIKE '%${fid}%'
        </if>
        <if test="type != null and type != ''">
            and TYPE = #{type}
        </if>
        <if test="works != null and works != ''">
            and WORKS LIKE '%${works}%'
        </if>
        <if test="prodline != null and prodline != ''">
            and PRODLINE LIKE '%${prodline}%'
        </if>
        <if test="prodate != null and prodate != ''">
            and PRODATE LIKE '%${prodate}%'
        </if>
        <if test="orderno != null and orderno != ''">
            and ORDERNO LIKE '%${orderno}%'
        </if>
        <if test="materialcode != null and materialcode != ''">
            and MATERIALCODE LIKE '%${materialcode}%'
        </if>
        <if test="materialname != null and materialname != ''">
            and MATERIALNAME LIKE '%${materialname}%'
        </if>
        <if test="unit != null and unit != ''">
            and UNIT LIKE '%${unit}%'
        </if>
        <if test="qty != null and qty != ''">
            and QTY LIKE '%${qty}%'
        </if>
        <if test="createtime != null and createtime != ''">
            and CREATETIME LIKE '%${createtime}%'
        </if>
        <if test="status != null and status != ''">
            and STATUS = #{status}
        </if>
        <if test="sendtime != null and sendtime != ''">
            and SENDTIME LIKE '%${sendtime}%'
        </if>
    </where>
  </select>

  <select id="selectNewMaterial" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    select FID, TYPE, WORKS, PRODLINE, PRODATE, ORDERNO, MATERIALCODE, MATERIALNAME, UNIT, QTY, CREATETIME, STATUS, SENDTIME,MEMO
      from IF_MES_EMS_SWAPFILE
      where TYPE = #{type,jdbcType=VARCHAR}
        and STATUS in ('0','9')
      order by PRODATE, PRODLINE, MATERIALCODE
  </select>

  <select id="getSUM" parameterType="java.util.HashMap" resultType="java.math.BigDecimal">
    SELECT
	SUM (qty) SUMQTY
    FROM IF_MES_EMS_SWAPFILE T
    WHERE T.TYPE = '1'
    AND T.PRODLINE IN (${prodlines})
    AND T.MATERIALCODE 
    <if test="isnot == 'not'">
        NOT
    </if>
    IN (${materialcodes})
    AND TO_CHAR(T.PRODATE, 'yyyy-MM-dd') = #{dataDay,jdbcType=VARCHAR}
  </select>
  
  <update id="updateByPrimaryKey" parameterType="com.steerinfo.ems.ifmesemsswapfile.model.IfMesEmsSwapfile">
    update IF_MES_EMS_SWAPFILE
    set TYPE = #{type,jdbcType=VARCHAR},
      WORKS = #{works,jdbcType=VARCHAR},
      PRODLINE = #{prodline,jdbcType=VARCHAR},
      PRODATE = to_date(#{prodate}, 'YYYY-MM-DD HH24:MI:SS'),
      ORDERNO = #{orderno,jdbcType=VARCHAR},
      MATERIALCODE = #{materialcode,jdbcType=VARCHAR},
      MATERIALNAME = #{materialname,jdbcType=VARCHAR},
      UNIT = #{unit,jdbcType=VARCHAR},
      QTY = #{qty,jdbcType=VARCHAR},
      CREATETIME = to_date(#{createtime}, 'YYYY-MM-DD HH24:MI:SS'),
      STATUS = #{status,jdbcType=VARCHAR}
    where FID = #{fid,jdbcType=VARCHAR}
  </update>
  
  <update id="updateStatus" parameterType="java.util.HashMap">
    update IF_MES_EMS_SWAPFILE
    <set>
      <if test="status != null">
        STATUS = #{status,jdbcType=VARCHAR},
        SENDTIME = SYSDATE,
      </if>
    </set>
    where FID = #{fid,jdbcType=VARCHAR}
  </update>

  <insert id="insert" parameterType="com.steerinfo.ems.ifmesemsswapfile.model.IfMesEmsSwapfile">
    insert into IF_MES_EMS_SWAPFILE
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="fid != null">
            FID,
        </if>
        <if test="type != null">
            TYPE,
        </if>
        <if test="works != null">
            WORKS,
        </if>
        <if test="prodline != null">
            PRODLINE,
        </if>
        <if test="prodate != null">
            PRODATE,
        </if>
        <if test="orderno != null">
            ORDERNO,
        </if>
        <if test="materialcode != null">
            MATERIALCODE,
        </if>
        <if test="materialname != null">
            MATERIALNAME,
        </if>
        <if test="unit != null">
            UNIT,
        </if>
        <if test="qty != null">
            QTY,
        </if>
        <if test="createtime != null">
            CREATETIME,
        </if>
        <if test="status != null">
            STATUS,
        </if>
        <if test="sendtime != null">
            SENDTIME,
        </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="fid != null">
            #{fid,jdbcType=VARCHAR},
        </if>
        <if test="type != null">
            #{type,jdbcType=VARCHAR},
        </if>
        <if test="works != null">
            #{works,jdbcType=VARCHAR},
        </if>
        <if test="prodline != null">
            #{prodline,jdbcType=VARCHAR},
        </if>
        <if test="prodate != null">
            to_date ( #{prodate} , 'YYYY-MM-DD HH24:MI:SS' ),
        </if>
        <if test="orderno != null">
            #{orderno,jdbcType=VARCHAR},
        </if>
        <if test="materialcode != null">
            #{materialcode,jdbcType=VARCHAR},
        </if>
        <if test="materialname != null">
            #{materialname,jdbcType=VARCHAR},
        </if>
        <if test="unit != null">
            #{unit,jdbcType=VARCHAR},
        </if>
        <if test="qty != null">
            #{qty,jdbcType=DECIMAL},
        </if>
        <if test="createtime != null">
            to_date ( #{createtime} , 'YYYY-MM-DD HH24:MI:SS' ),
        </if>
        <if test="status != null">
            #{status,jdbcType=VARCHAR},
        </if>
        <if test="sendtime != null">
            to_date ( #{sendtime} , 'YYYY-MM-DD HH24:MI:SS' ),
        </if>
    </trim>
  </insert>

  <select id="getQueryList" parameterType="java.util.HashMap" resultType = "Map">
      SELECT s.FID, s.TYPE, s.WORKS, s.PRODLINE, CASE WHEN s.TYPE = 1 THEN m.WORKPROCID WHEN s.TYPE = 2 THEN p.WORKPROCID END AS WORKPROCID, s.PRODATE, s.ORDERNO, s.MATERIALCODE, s.MATERIALNAME, s.UNIT, s.QTY, s.CREATETIME, s.STATUS, s.SENDTIME, s.MEMO
      FROM IF_MES_EMS_SWAPFILE s
      LEFT JOIN T_RM_WORKPROC_PRODUCT p ON s.MATERIALCODE = p.MES_MATERIALCODE
      LEFT JOIN T_RM_WORKPROC_MATERIAL m ON s.MATERIALCODE = m.MES_MATERIALCODE
      WHERE (m.WORKPROCID IN (${workProcids}) OR p.WORKPROCID IN (${workProcids}))
      <if test="prodate != null and prodate != ''">
      AND s.PRODATE >= to_date(#{prodate}, 'yyyy-mm-dd')
      </if>
      <if test="prodate2 != null and prodate2 != ''">
          AND s.PRODATE &lt;= to_date(#{prodate2}, 'yyyy-mm-dd')
      </if>
      <if test="workproc != null and workproc != ''">
          AND (p.WORKPROCID LIKE '%${workproc}%' OR m.WORKPROCID LIKE '%${workproc}%')
      </if>
      <if test="materialname != null and materialname != ''">
          AND s.MATERIALNAME LIKE '%${materialname}%'
      </if>
      <if test="materialcode != null and materialcode != ''">
          AND s.MATERIALCODE LIKE '%${materialcode}%'
      </if>
      <if test="prodline != null and prodline != ''">
          AND s.PRODLINE LIKE '%${prodline}%'
      </if>
      <if test="type != null and type != ''">
          AND s.TYPE LIKE '%${type}%'
      </if>
      ORDER BY s.PRODATE
  </select>

    <select id="queryProductAndMaterial" parameterType="java.util.HashMap" resultType = "Map">
        SELECT A.ITEMID, A.CLOCK, A.TIMEGRANID, A.ACTUALVALUE, A.CORRECTVALUE
             , A.APPORTVALUE, A.BZ, A.USERID, A.UPDATETIME, A.UPMAN
             , A.UPTIME, B.WORKPROCID, B.MES_MATERIALCODE, B.UNITID, B.${listing} AS MATERIALNAME
        FROM ${table_name2} A
        LEFT JOIN ${table_name} B ON A.ITEMID = B.ITEMID
        WHERE B.WORKPROCID IN (${workProcids})
        <if test="clock != null and clock != ''">
            AND A.CLOCK >= #{clock}
        </if>
        <if test="clock2 != null and clock2 != ''">
            AND A.CLOCK &lt;= #{clock2}
        </if>
        AND B.ITEMTYPE = 'R'
        AND B.USEFLAG = '1'
        AND B.ISFINANCEDATA = '1'
        <if test="workproc != null and workproc != ''">
            AND B.WORKPROCID IN (${workProcs})
        </if>
        <if test="materialcode != null and materialcode != ''">
            AND B.MES_MATERIALCODE LIKE '%${materialcode}%'
        </if>
        <if test="itemid != null and itemid != ''">
            AND A.ITEMID LIKE '%${itemid}%'
        </if>
        <if test="itemtype !=null and itemtype!='' ">
            AND B.ITEMTYPE = #{itemtype}
        </if>
        <if test="materialname !=null and materialname!='' ">
           AND B.${listing} = #{materialname}
        </if>
        ORDER BY A.CLOCK,B.SEQNO
    </select>

    <update id="updateProductAndMaterial" parameterType="java.util.HashMap">
        UPDATE ${table_name}
        SET APPORTVALUE = #{apportvalue},
        CORRECTVALUE = #{correctvalue},
        BZ = #{bz},
        UPMAN = #{upman},
        UPTIME = #{uptime}
        WHERE ITEMID = #{itemid}
        AND CLOCK = #{clock}
        AND TIMEGRANID = #{timegranid}
        AND ACTUALVALUE = #{actualvalue}
    </update>
</mapper>