<?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.oa_evection.dao.oa_evection.OaEvectionDao">
    
	<sql id="oaEvectionColumns">
		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.del_flag AS "delFlag",
		a.remarks AS "remarks",
		a.start_time AS "startTime",
		a.end_time AS "endTime",
		a.process_instance_id AS "processInstanceId",
		a.reality_start_time AS "realityStartTime",
		a.reality_end_time AS "realityEndTime",
		a.reason AS "reason",
		a.address AS "address",
		a.date_count AS "dateCount",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.files AS "files",
		a.status AS "status",
		u.name AS "createBy.name",
		o.id AS "createBy.office.id",
		o.name AS "createBy.office.name"
	</sql>
	
	<sql id="oaEvectionJoins">
		JOIN sys_user u ON u.id = a.create_by
		JOIN sys_office o ON o.id = u.office_id
	</sql>

	<select id="getCreateDateList" resultType="string">
		SELECT create_date FROM oa_evection
		WHERE  #{date} LIKE  LEFT (create_date,7)
		<if test="companyId != null and companyId !=''">
			AND    company_id = #{companyId}
		</if>
		ORDER BY  create_date asc
	</select>
    
	<select id="get" resultType="OaEvection" >
		SELECT 
			<include refid="oaEvectionColumns"/>
		FROM oa_evection a
		<include refid="oaEvectionJoins"/>
		WHERE a.id = #{id}
	</select>

    <select id="getByProcessInstanceId" resultType="OaEvection">
        SELECT
        <include refid="oaEvectionColumns"/>
        FROM oa_evection a
        <include refid="oaEvectionJoins"/>
        WHERE a.process_instance_id = #{processInstanceId}
    </select>

	<select id="findList" resultType="OaEvection" >
		SELECT 
			<include refid="oaEvectionColumns"/>
		FROM oa_evection a
		<include refid="oaEvectionJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="createDate != null and createDate != ''">
                AND Date(a.create_date) = #{createDate}
            </if>
			<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="companyId != null and companyId != ''">
                AND a.company_id = #{companyId}
            </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="findByCompany2" resultType="OaEvection" parameterType="java.util.Map">
		SELECT
		<include refid="oaEvectionColumns"/>
		FROM oa_evection a
		<include refid="oaEvectionJoins"/>
		<where>
			a.del_flag = #{evection.DEL_FLAG_NORMAL}
			<if test="date != null and date != ''">
				AND #{date} LIKE  LEFT (a.create_date , 7)
			</if>
			<if test="evection.companyId != null and evection.companyId != ''">
				AND a.company_id = #{evection.companyId}
			</if>
		</where>
		<choose>
			<when test="evection.page !=null and evection.page.orderBy != null and evection.page.orderBy != ''">
				ORDER BY ${evection.page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultType="OaEvection" >
		SELECT 
			<include refid="oaEvectionColumns"/>
		FROM oa_evection a
		<include refid="oaEvectionJoins"/>
		<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_evection(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			remarks,
			start_time,
			end_time,
			process_instance_id,
			reality_start_time,
			reality_end_time,
			reason,
			address,
			date_count,
			company_id,
			office_id,
			status,
			files
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{remarks},
			#{startTime},
			#{endTime},
			#{processInstanceId},
			#{realityStartTime},
			#{realityEndTime},
			#{reason},
			#{address},
			#{dateCount},
			#{companyId},
			#{officeId},
			#{status},
			#{files}
		)
	</insert>
	
	<update id="update">
		UPDATE oa_evection SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			start_time = #{startTime},
			end_time = #{endTime},
			process_instance_id = #{processInstanceId},
			reality_start_time = #{realityStartTime},
			reality_end_time = #{realityEndTime},
			reason = #{reason},
			address = #{address},
			date_count = #{dateCount},
			company_id = #{companyId},
			office_id = #{officeId},
			files = #{files},
			status = #{status}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM oa_evection
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE oa_evection SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<update id="updateProcessInstanceId">
		UPDATE oa_evection SET 
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="OaEvection" statementType="STATEMENT">
		select * FROM oa_evection  where ${propertyName} = '${value}'
	</select>


	<select id="getCreateDateList1" resultType="string">
		SELECT a.create_date FROM oa_evection a
		WHERE   DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
	       and a.create_by=#{userId}
	       ORDER BY  a.create_date asc
	</select>
	<select id="findByCompany1" resultType="OaEvection">
		SELECT
		<include refid="oaEvectionColumns"/>
		FROM oa_evection a
		<include refid="oaEvectionJoins"/>
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="getCreateDateListAll" resultType="string">
		SELECT a.create_date
		FROM oa_evection a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		WHERE   DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
	      ${sqlStr}
	       ORDER BY  a.create_date asc
	</select>
	<select id="findByCompanyAll" resultType="OaEvection">
		SELECT
		a.*
		FROM oa_evection a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByCompanySelf" resultType="OaEvection">
		SELECT
		<include refid="oaEvectionColumns"/>
		FROM oa_evection a
		<include refid="oaEvectionJoins"/>
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByManage" resultType="OaEvection">
		SELECT
		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.del_flag AS "delFlag",
		a.remarks AS "remarks",
		a.start_time AS "startTime",
		a.end_time AS "endTime",
		a.process_instance_id AS "processInstanceId",
		a.reality_start_time AS "realityStartTime",
		a.reality_end_time AS "realityEndTime",
		a.reason AS "reason",
		a.address AS "address",
		a.date_count AS "dateCount",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.files AS "files",
		a.status AS "status",
		u.name AS "createBy.name",
		o.id AS "createBy.office.id",
		o.name AS "createBy.office.name"
		FROM oa_evection a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>
</mapper>