<?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.oaall.dao.OaAllDao">
    
	<sql id="oaAllColumns">
		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.apply_content AS "applyContent",
		files AS "files",
		a.approve_detail AS "approveDetail",
		a.process_instance_id AS "processInstanceId",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.status AS "status",
		(select name from sys_user where id = a.create_by)  AS "createBy.name"
	</sql>
	
	<sql id="oaAllJoins">
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
	</sql>
	
    
	<select id="get" resultType="OaAll" >
		SELECT 
			<include refid="oaAllColumns"/>
		FROM oa_all a
		<include refid="oaAllJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="OaAll" >
		SELECT 
			<include refid="oaAllColumns"/>
		FROM oa_all a
		<include refid="oaAllJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="createDateStart != null and createDateStart != ''">
				AND a.create_date &gt;= #{createDateStart}
			</if>
			<if test="createDateEnd != null and createDateEnd != ''">
				AND a.create_date &lt;= #{createDateEnd}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="updateBy != null and updateBy.id != null and updateBy.id != ''">
				AND a.update_by = #{updateBy.id}
			</if>
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by = #{createBy.id}
			</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.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="OaAll" >
		SELECT 
			<include refid="oaAllColumns"/>
		FROM oa_all a
		<include refid="oaAllJoins"/>
		<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 oa_all(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			apply_content,
			approve_detail,
			company_id,
			office_id,
			status,
			files
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{applyContent},
			#{approveDetail},
			#{companyId},
			#{officeId},
			#{status},
			#{files}
		)
	</insert>
	
	<update id="update">
		UPDATE oa_all SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			apply_content = #{applyContent},
			approve_detail = #{approveDetail},
			company_id = #{companyId},
			office_id = #{officeId},
			status = #{status},
			files = #{files}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM oa_all
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE oa_all SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="OaAll" statementType="STATEMENT">
		select * FROM oa_all  where ${propertyName} = '${value}'
	</select>

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

	<select id="getByProcessInstanceId" resultType="OaAll">
		SELECT
		<include refid="oaAllColumns"/>
		FROM oa_all a
		<include refid="oaAllJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>
	
</mapper>