<?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.foreverwin.mesnac.quality.mapper.InspectionTaskManageMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.foreverwin.mesnac.quality.model.InspectionTaskManage">
        <result column="HANDLE" property="handle" />
        <result column="SITE" property="site" />
        <result column="CATEGORY" property="category" />
        <result column="TASK_NO" property="taskNo" />
        <result column="DESCRIPTION" property="description" />
        <result column="STATUS" property="status" />
        <result column="INSPECTION_ITEM_BO" property="inspectionItemBo" />
        <result column="WORK_CENTER" property="workCenter" />
        <result column="SHOP_ORDER" property="shopOrder" />
        <result column="SFC" property="sfc" />
        <result column="ITEM" property="item" />
        <result column="ITEM_DESCRIPTION" property="itemDescription" />
        <result column="OPERATION" property="operation" />
        <result column="STEP_ID" property="stepId" />
        <result column="RESRCE" property="resrce" />
        <result column="RESULT" property="result" />
        <result column="COMMENTS" property="comments" />
        <result column="CREATE_USER" property="createUser" />
        <result column="CREATED_DATE_TIME" property="createdDateTime" />
        <result column="MODIFY_USER" property="modifyUser" />
        <result column="MODIFIED_DATE_TIME" property="modifiedDateTime" />
    </resultMap>

    <select id="findInspectionTask" resultMap="BaseResultMap">
        SELECT  B.HANDLE, B.SITE, B.CATEGORY, B.TASK_NO, B.DESCRIPTION, B.STATUS, B.INSPECTION_ITEM_BO, B.WORK_CENTER, B.SHOP_ORDER, B.SFC, B.OPERATION, B.STEP_ID, B.RESRCE, B.RESULT, B.COMMENTS, B.CREATE_USER, B.CREATED_DATE_TIME, B.MODIFY_USER, B.MODIFIED_DATE_TIME,
                M.ITEM, Z.DESCRIPTION  ITEM_DESCRIPTION
        FROM Z_INSPECTION_TASK B
        INNER JOIN SFC S ON S.SITE = B.SITE AND S.SFC = B.SFC
        INNER JOIN SHOP_ORDER P ON P.HANDLE = S.SHOP_ORDER_BO
        INNER JOIN ITEM M ON M.HANDLE = P.ITEM_BO
        LEFT JOIN ITEM_T Z ON Z.ITEM_BO = M.HANDLE AND Z.LOCALE = 'zh'
        WHERE B.SITE = #{site}
        <if test="workCenter != null and workCenter != ''">
            AND B.WORK_CENTER = #{workCenter}
        </if>
          <choose>
              <when test="category != null and category != ''">
                  AND B.CATEGORY = #{category}
              </when>
              <otherwise>
                  AND (B.CATEGORY = 'S' OR B.CATEGORY = 'P')
              </otherwise>
          </choose>
        <if test="status != null and status != ''">
            AND B.STATUS = #{status}
        </if>
        <if test="shopOrder != null and shopOrder != ''">
            AND B.SHOP_ORDER = #{shopOrder}
        </if>
        <if test="sfc != null and sfc != ''">
            AND B.SFC = #{sfc}
        </if>
        <if test="item != null and item != ''">
            AND M.ITEM = #{item}
        </if>
        <if test="operation != null and operation != ''">
            AND B.OPERATION = #{operation}
        </if>
        <if test="stepId != null and stepId != ''">
            AND B.STEP_ID = #{stepId}
        </if>
        <if test="dateFrom != null">
            AND B.CREATED_DATE_TIME >= #{dateFrom}
        </if>
        <if test="dateTo != null">
            AND B.CREATED_DATE_TIME &lt;= #{dateTo}
        </if>
    </select>

    <select id="selectInspectionTaskDetail" resultType="com.foreverwin.mesnac.quality.model.InspectionTaskManageDetail" flushCache="true">
        SELECT  ZD.HANDLE, ZD.TASK_BO, ZD.SITE, ZL.SEQ, ZL.PARAM_TYPE, ZL.INSPECTION_METHOD, ZL.PARAM_NO, ZL.DESCRIPTION, ZL.MIN_VALUE, ZL.STANDARD_VALUE, ZL.MAX_VALUE, ZL.IS_REQUIRED,
                ZL.TRUE_VALUE, ZL.FALSE_VALUE, ZL.CHECK_NUM, ZD.CHECK_VALUES, ZD.RESULT, ZD.REMARK
        FROM Z_INSPECTION_TASK_DETAIL ZD
        INNER JOIN Z_INSPECTION_ITEM_DETAIL ZL ON ZD.INSPECTION_ITEM_DETAIL_BO = ZL.HANDLE
        WHERE ZD.TASK_BO = #{inspectionTaskBo}
    </select>

    <select id="selectInspectionItemDetail" resultType="com.foreverwin.mesnac.quality.model.InspectionTaskManageDetail">
        SELECT  ZL.HANDLE, ZL.SEQ, ZL.PARAM_TYPE, ZL.PARAM_NO, ZL.DESCRIPTION, ZL.INSPECTION_METHOD, ZL.MIN_VALUE, ZL.STANDARD_VALUE, ZL.MAX_VALUE, ZL.TRUE_VALUE, ZL.FALSE_VALUE, ZL.CHECK_NUM, ZL.IS_REQUIRED
        FROM  Z_INSPECTION_ITEM_DETAIL ZL
        WHERE ZL.INSPECTION_ITEM_BO = #{inspectionItemBo}
    </select>
</mapper>