<?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="production">

  <select id="getProductionList" parameterType="map" resultType="map">
    SELECT
      ID, COMPANY_CODE, PLAN_NO, PLAN_DATE,
      ITEM_CODE, ITEM_NAME, PRODUCT_TYPE,
      PLAN_QTY, COMPLETED_QTY, PROGRESS_RATE,
      START_DATE, END_DATE, DUE_DATE,
      EQUIPMENT_ID, EQUIPMENT_CODE, EQUIPMENT_NAME,
      STATUS, PRIORITY, HOURLY_CAPACITY, DAILY_CAPACITY, LEAD_TIME,
      WORK_SHIFT, WORK_ORDER_NO, MANAGER_NAME, ORDER_NO, PARTNER_ID,
      PARENT_PLAN_ID, REMARKS, CREATED_DATE, CREATED_BY, UPDATED_DATE, UPDATED_BY
    FROM PRODUCTION_PLAN_MNG
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <if test="item_code != null and item_code != ''">
      AND ITEM_CODE ILIKE CONCAT('%', #{item_code}, '%')
    </if>
    <if test="item_name != null and item_name != ''">
      AND ITEM_NAME ILIKE CONCAT('%', #{item_name}, '%')
    </if>
    <if test="status != null and status != ''">
      AND STATUS = #{status}
    </if>
    <if test="product_type != null and product_type != ''">
      AND PRODUCT_TYPE = #{product_type}
    </if>
    <if test="start_date_from != null and start_date_from != ''">
      AND START_DATE &gt;= #{start_date_from}::DATE
    </if>
    <if test="start_date_to != null and start_date_to != ''">
      AND START_DATE &lt;= #{start_date_to}::DATE
    </if>
    ORDER BY CREATED_DATE DESC
    <include refid="common.pagination"/>
  </select>

  <select id="getProductionListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)
    FROM PRODUCTION_PLAN_MNG
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <if test="item_code != null and item_code != ''">
      AND ITEM_CODE ILIKE CONCAT('%', #{item_code}, '%')
    </if>
    <if test="item_name != null and item_name != ''">
      AND ITEM_NAME ILIKE CONCAT('%', #{item_name}, '%')
    </if>
    <if test="status != null and status != ''">
      AND STATUS = #{status}
    </if>
    <if test="product_type != null and product_type != ''">
      AND PRODUCT_TYPE = #{product_type}
    </if>
    <if test="start_date_from != null and start_date_from != ''">
      AND START_DATE &gt;= #{start_date_from}::DATE
    </if>
    <if test="start_date_to != null and start_date_to != ''">
      AND START_DATE &lt;= #{start_date_to}::DATE
    </if>
  </select>

  <select id="getProductionInfo" parameterType="map" resultType="map">
    SELECT
      ID, COMPANY_CODE, PLAN_NO, PLAN_DATE,
      ITEM_CODE, ITEM_NAME, PRODUCT_TYPE,
      PLAN_QTY, COMPLETED_QTY, PROGRESS_RATE,
      START_DATE, END_DATE, DUE_DATE,
      EQUIPMENT_ID, EQUIPMENT_CODE, EQUIPMENT_NAME,
      STATUS, PRIORITY, HOURLY_CAPACITY, DAILY_CAPACITY, LEAD_TIME,
      WORK_SHIFT, WORK_ORDER_NO, MANAGER_NAME, ORDER_NO, PARTNER_ID,
      PARENT_PLAN_ID, REMARKS, CREATED_DATE, CREATED_BY, UPDATED_DATE, UPDATED_BY
    FROM PRODUCTION_PLAN_MNG
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </select>

  <insert id="insertProduction" parameterType="map" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO PRODUCTION_PLAN_MNG (
      COMPANY_CODE, PLAN_NO, PLAN_DATE, ITEM_CODE, ITEM_NAME,
      PRODUCT_TYPE, PLAN_QTY, COMPLETED_QTY, PROGRESS_RATE,
      START_DATE, END_DATE, DUE_DATE,
      EQUIPMENT_ID, EQUIPMENT_CODE, EQUIPMENT_NAME,
      STATUS, PRIORITY, HOURLY_CAPACITY, DAILY_CAPACITY, LEAD_TIME,
      WORK_SHIFT, WORK_ORDER_NO, MANAGER_NAME, ORDER_NO, PARTNER_ID,
      PARENT_PLAN_ID, REMARKS, CREATED_BY, CREATED_DATE, UPDATED_DATE
    ) VALUES (
      #{company_code}, #{plan_no}, #{plan_date}, #{item_code}, #{item_name},
      #{product_type}, #{plan_qty}, #{completed_qty}, #{progress_rate},
      #{start_date}, #{end_date}, #{due_date},
      #{equipment_id}, #{equipment_code}, #{equipment_name},
      #{status}, #{priority}, #{hourly_capacity}, #{daily_capacity}, #{lead_time},
      #{work_shift}, #{work_order_no}, #{manager_name}, #{order_no}, #{partner_id},
      #{parent_plan_id}, #{remarks}, #{created_by}, NOW(), NOW()
    )
  </insert>

  <update id="updateProduction" parameterType="map">
    UPDATE PRODUCTION_PLAN_MNG
    <set>
      <if test="plan_qty != null">
        PLAN_QTY = #{plan_qty},
      </if>
      <if test="completed_qty != null">
        COMPLETED_QTY = #{completed_qty},
      </if>
      <if test="progress_rate != null">
        PROGRESS_RATE = #{progress_rate},
      </if>
      <if test="start_date != null">
        START_DATE = #{start_date},
      </if>
      <if test="end_date != null">
        END_DATE = #{end_date},
      </if>
      <if test="due_date != null">
        DUE_DATE = #{due_date},
      </if>
      <if test="equipment_id != null">
        EQUIPMENT_ID = #{equipment_id},
      </if>
      <if test="equipment_code != null">
        EQUIPMENT_CODE = #{equipment_code},
      </if>
      <if test="equipment_name != null">
        EQUIPMENT_NAME = #{equipment_name},
      </if>
      <if test="status != null">
        STATUS = #{status},
      </if>
      <if test="priority != null">
        PRIORITY = #{priority},
      </if>
      <if test="manager_name != null">
        MANAGER_NAME = #{manager_name},
      </if>
      <if test="work_shift != null">
        WORK_SHIFT = #{work_shift},
      </if>
      <if test="remarks != null">
        REMARKS = #{remarks},
      </if>
      <if test="item_code != null">
        ITEM_CODE = #{item_code},
      </if>
      <if test="item_name != null">
        ITEM_NAME = #{item_name},
      </if>
      <if test="product_type != null">
        PRODUCT_TYPE = #{product_type},
      </if>
      <if test="order_no != null">
        ORDER_NO = #{order_no},
      </if>
      <if test="updated_by != null">
        UPDATED_BY = #{updated_by},
      </if>
      UPDATED_DATE = NOW()
    </set>
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </update>

  <delete id="deleteProduction" parameterType="map">
    DELETE FROM PRODUCTION_PLAN_MNG
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </delete>

</mapper>
