<?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.jeeplus.modules.workscrap.dao.WorkScrapDao">
    
	<sql id="workScrapColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.process_instance_id AS "processInstanceId",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.status AS "status",
		a.number AS "number",
		a.office_name AS "officeName",
		a.scrap_date AS "scrapDate",
		a.reason AS "reason",
		su.name AS "createBy.name",
		w.id as "item.workCatalogueItem.id",
		w.name as "item.itemName",
		w.name as "item.workCatalogueItem.name",
		w.number as "item.workCatalogueItem.number",
		w.type as "item.workCatalogueItem.type",
		w.model as "item.workCatalogueItem.model",
		w.primary_unit as "item.workCatalogueItem.primaryUnit",
		item.number as "item.number",
		a.item_id AS "item.id"
	</sql>
	
	<sql id="workScrapJoins">
		LEFT JOIN work_item item ON item.id = a.item_id
		 LEFT JOIN work_catalogue_item w ON w.id = item.catalogue_item_id
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		left JOIN sys_user su on su.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkScrap" >
		SELECT 
			<include refid="workScrapColumns"/>
		FROM work_scrap a
		<include refid="workScrapJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkScrap" >
		SELECT 
			<include refid="workScrapColumns"/>
		FROM work_scrap a
		<include refid="workScrapJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="number != null and number != ''">
				AND a.number = #{number}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.scrap_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<if test="item != null and item.workCatalogueItem != null and item.workCatalogueItem.name != null and item.workCatalogueItem.name != ''">
				AND w.name LIKE
				<if test="dbName == 'oracle'">'%'||#{item.workCatalogueItem.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{item.workCatalogueItem.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{item.workCatalogueItem.name},'%')</if>
			</if>
			<if test="item != null and item.number != null and item.number != ''">
				AND w.number = #{item.number}
			</if>
			${sqlMap.dsf}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.scrap_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkScrap" >
		SELECT 
			<include refid="workScrapColumns"/>
		FROM work_scrap a
		<include refid="workScrapJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_scrap(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_instance_id,
			office_id,
			company_id,
			status,
			number,
			office_name,
			scrap_date,
			reason,
			item_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processInstanceId},
			#{officeId},
			#{companyId},
			#{status},
			#{number},
			#{officeName},
			#{scrapDate},
			#{reason},
			#{item.id}
		)
	</insert>
	
	<update id="update">
		UPDATE work_scrap SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_instance_id = #{processInstanceId},
			office_id = #{officeId},
			company_id = #{companyId},
			status = #{status},
			number = #{number},
			office_name = #{officeName},
			scrap_date = #{scrapDate},
			reason = #{reason},
			item_id = #{item.id}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_scrap
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_scrap SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkScrap" statementType="STATEMENT">
		select * FROM work_scrap  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByitem" resultType="WorkItem">
		SELECT 
			*
		FROM work_item a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test=" != null and  != ''">
				AND a. LIKE 
				<if test="dbName == 'oracle'">#{}||'%'</if>
				<if test="dbName == 'mssql'">#{}+'%'</if>
				<if test="dbName == 'mysql'">concat(#{},'%')</if>
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<update id="updateProcessInstanceId">
		UPDATE work_scrap SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkScrap">
		SELECT
		<include refid="workScrapColumns"/>
		FROM work_scrap a
		<include refid="workScrapJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>
</mapper>