<?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.prolog.cs.base.gsp.dao.GspUpkeepCheckPlanMapper">
	<resultMap id="map" type="com.prolog.cs.base.gsp.model.GspUpkeepCheckPlan">
		<result column="id" property="id"/>
		<result column="code" property="code"/>
		<result column="enterprise_id" property="enterpriseId"/>
		<result column="dc_id" property="dcId"/>
		<result column="warehouse_id" property="warehouseId"/>
		<result column="cargo_owner_id" property="cargoOwnerId"/>
		<result column="status" property="status"/>
		<result column="source" property="source"/>
		<result column="gmt_create" property="gmtCreate"/>
		<result column="creator" property="creator"/>
		<result column="creator_name" property="creatorName"/>
		<result column="gmt_modified" property="gmtModified"/>
		<result column="modifier" property="modifier"/>
		<result column="modifier_name" property="modifierName"/>
		<result column="remark" property="remark"/>
		<result column="PRINT_COUNT" property="printCount"/>
		<result column="BLDG_ID" property="bldgId"/>
		<result column="IS_PROTECTED" property="isProtected"/>
		<result column="UPKEEP_TYPE" property="upkeepType"/>
	</resultMap>

	<select id="findList" resultMap="map">
		SELECT
			p.*
		FROM
			GSP_UPKEEP_CHECK_PLAN p
		WHERE
			EXISTS (
				SELECT
				*
				FROM
				GSP_UPKEEP_CHECK c
				WHERE
				p.id = c.plan_id
				<if test='type != null and type =="1"'>
					and c.STATUS = 0
				</if>
				<if test='type != null and type =="2"'>
					and c.conclusion IN(2, 3) and status &lt;&gt; 6
				</if>
				<if test='locatorId != null and locatorId !=""'>
					and c.locator_id = #{locatorId}
				</if>
				<if test='locatorCode != null and locatorCode !=""'>
					and c.locator_code = #{locatorCode}
				</if>
				<if test='lotNum != null and lotNum !=""'>
					and c.LOT_NUM = #{lotNum}
				</if>

				<if test="upkeepType != null  and upkeepType !=''">
					and c.UPKEEP_TYPE = #{upkeepType}
				</if>	  
				<if test="enterpriseId != null  and enterpriseId !=''">
					and c.enterprise_id = #{enterpriseId}
				</if>
				<if test="warehouseId != null  and warehouseId !=''">
					and c.warehouse_id = #{warehouseId}
				</if>
				<if test="cargoOwnerId != null  and cargoOwnerId !=''">
					and c.CARGO_OWNER_ID = #{cargoOwnerId}
				</if>
				<if test="itemHealthCategory != null  and itemHealthCategory !=''">
					and c.ITEM_HEALTH_CATEGORY = #{itemHealthCategory}
				</if>
				<if test="itemId != null  and itemId !=''">
					and c.ITEM_ID = #{itemId}
				</if>
				<!-- 根据执行时间来做 -->
				<if test="startTime != null ">
					and c.PLAN_UPKEEP_DATE  &gt;= #{startTime}
				</if>
				<if test="endTime != null ">
					and c.PLAN_UPKEEP_DATE &lt; #{endTime}
				</if>
				)
		order by p.GMT_CREATE asc
	</select>

	<update id="updateGspYHPrintCount" >
		UPDATE GSP_UPKEEP_CHECK_PLAN set PRINT_COUNT = PRINT_COUNT+1  WHERE id = #{id}
	</update>
</mapper>
