<?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.worksendmessage.dao.WorkSendMessageDao">
    
	<sql id="workSendMessageColumns">
		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.title AS "title",
		a.num AS "num",
		a.doc_type AS "docType",
		a.person AS "person",
		a.summary AS "summary",
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		a.state AS "state",
		a.process_instance_id AS "processInstanceId",
		u.name AS "createBy.name",
		o.name AS "office.name",
		o.top_company AS "office.topCompany"
	</sql>
	
	<sql id="workSendMessageJoins">
        LEFT JOIN sys_office o on a.office_id = o.id
        LEFT JOIN sys_office s ON s.id = a.company_id
        LEFT JOIN sys_user u ON u.id = a.create_by
	</sql>

	<select id="get" resultType="WorkSendMessage" >
		SELECT 
			<include refid="workSendMessageColumns"/>
		FROM work_send_message a
		<include refid="workSendMessageJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkSendMessage" >
		SELECT 
			<include refid="workSendMessageColumns"/>
		FROM work_send_message a
		<include refid="workSendMessageJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="beginCreateDate != null">
				AND a.create_date >= #{beginCreateDate}
			</if>
			<if test="endCreateDate != null">
				AND a.create_date &lt;= #{endCreateDate}
			</if>
			<if test="title != null and title != ''">
				AND a.title LIKE 
					<if test="dbName == 'oracle'">'%'||#{title}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{title}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{title},'%')</if>
			</if>
			<if test="num != null and num != ''">
				AND a.num LIKE concat('%',#{num},'%')
			</if>
			<if test="person != null and person != ''">
				AND a.person LIKE concat('%',#{person},'%')
			</if>
			<if test="docType != null and docType != ''">
				AND a.doc_type = #{docType}
			</if>
            <if test="state != null">
                AND a.state = #{state}
            </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="WorkSendMessage" >
		SELECT 
			<include refid="workSendMessageColumns"/>
		FROM work_send_message a
		<include refid="workSendMessageJoins"/>
		<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_send_message(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			title,
			num,
			doc_type,
			person,
			summary,
			office_id,
			company_id,
			state,
		process_instance_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{title},
			#{num},
			#{docType},
			#{person},
			#{summary},
			#{office.id},
			#{company.id},
			#{state},
		#{processInstanceId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_send_message SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			title = #{title},
			num = #{num},
			doc_type = #{docType},
			person = #{person},
			summary = #{summary},
			office_id = #{office.id},
			company_id = #{company.id},
			state = #{state},
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		UPDATE work_send_message SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_send_message SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkSendMessage" statementType="STATEMENT">
		select * FROM work_send_message  where ${propertyName} = '${value}'
	</select>

    <update id="updateProcessIdAndStatus" >
        UPDATE  work_send_message SET
        process_instance_id = #{processInstanceId},
        state = #{state}
        WHERE id = #{id}
    </update>

    <select id="queryUserLists" resultType="User">
        SELECT t1.id,t1.name,t1.photo,t2.top_company AS 'officeName'
        FROM work_send_message_detail t
        LEFT JOIN sys_user t1 ON t.handler_id = t1.id
        LEFT JOIN sys_office t2 ON t1.office_id = t2.id
	    WHERE t.message_id = #{id} AND flag = 0
	    GROUP BY t1.id;
    </select>
    <select id="queryOfficeLists" resultType="Office">
        SELECT t1.id,t1.top_company AS 'name'
        FROM work_send_message_detail t
        LEFT JOIN sys_office t1 ON t.handler_id = t1.id
	    WHERE t.message_id = #{id} AND flag = 1
	    GROUP BY t1.id;
    </select>

    <select id="queryMemberPage" resultType="WorkSendMessage">
        SELECT DISTINCT
        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.title AS "title",
		a.num AS "num",
		a.doc_type AS "docType",
		a.person AS "person",
		a.summary AS "summary",
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		a.state AS "state",
		a.process_instance_id AS "processInstanceId",
        o.name AS "office.name",
        o.top_company AS "office.topCompany"
        FROM work_send_message a
        INNER JOIN work_send_message_detail t1 on t1.message_id= a.id
        LEFT JOIN sys_office o ON o.id = a.office_id
        <where>
            ((t1.handler_id = #{currentUser.id} and t1.flag = 0) or (t1.handler_id = #{currentUser.office.id} and t1.flag = 1)) and a.company_id = #{currentUser.company.id}
            and a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="beginCreateDate != null">
                AND a.create_date >= #{beginCreateDate}
            </if>
            <if test="endCreateDate != null">
                AND a.create_date &lt;= #{endCreateDate}
            </if>
            <if test="title != null and title != ''">
                AND a.title LIKE
                <if test="dbName == 'oracle'">'%'||#{title}||'%'</if>
                <if test="dbName == 'mssql'">'%'+#{title}+'%'</if>
                <if test="dbName == 'mysql'">concat('%',#{title},'%')</if>
            </if>
            <if test="num != null and num != ''">
                AND a.num LIKE concat('%',#{num},'%')
            </if>
            <if test="person != null and person != ''">
                AND a.person LIKE concat('%',#{person},'%')
            </if>
            <if test="docType != null and docType != ''">
                AND a.doc_type = #{docType}
            </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="updateStatusById">
        UPDATE work_send_message SET
            state = #{state}
            WHERE id = #{id}
    </update>
</mapper>