<?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.workincomingmessage.dao.WorkIncomingMessageDao">
    
	<sql id="workIncomingMessageColumns">
		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.word_size AS "wordSize",
		a.income_date AS "incomeDate",
		a.income_part AS "incomePart",
		a.counts AS "counts",
		a.opinion AS "opinion",
		a.handler AS "handler",
		a.state AS "state",
		a.process_instance_id AS "processInstanceId",
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		a.daily_id AS "dailyId",
		u.name AS "createBy.name",
		o.name AS "office.name",
		o.top_company AS "office.topCompany"
	</sql>
	
	<sql id="workIncomingMessageJoins">
        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="WorkIncomingMessage" >
		SELECT 
			<include refid="workIncomingMessageColumns"/>
		FROM work_incoming_message a
		<include refid="workIncomingMessageJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkIncomingMessage" >
		SELECT 
			<include refid="workIncomingMessageColumns"/>
		FROM work_incoming_message a
		<include refid="workIncomingMessageJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<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="wordSize != null and wordSize != ''">
				AND a.word_size like concat('%',#{wordSize},'%')
			</if>
            <if test="beginIncomeDate != null">
                AND a.income_date >= #{beginIncomeDate}
            </if>
            <if test="endIncomeDate != null">
                AND a.income_date &lt;= #{endIncomeDate}
            </if>
			<if test="incomePart != null and incomePart != ''">
				AND a.income_part LIKE 
					<if test="dbName == 'oracle'">'%'||#{incomePart}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{incomePart}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{incomePart},'%')</if>
			</if>
			<if test="handler != null and handler != ''">
				AND find_in_set(#{handler},a.handler)
			</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="WorkIncomingMessage" >
		SELECT 
			<include refid="workIncomingMessageColumns"/>
		FROM work_incoming_message a
		<include refid="workIncomingMessageJoins"/>
		<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_incoming_message(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			title,
			num,
			word_size,
			income_date,
			income_part,
			counts,
			opinion,
			handler,
			state,
            process_instance_id,
            office_id,
		company_id,
		daily_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{title},
			#{num},
			#{wordSize},
			#{incomeDate},
			#{incomePart},
			#{counts},
			#{opinion},
			#{handler},
			#{state},
		#{processInstanceId},
		#{office.id},
		#{company.id},
		#{dailyId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_incoming_message SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			title = #{title},
			num = #{num},
			word_size = #{wordSize},
			income_date = #{incomeDate},
			income_part = #{incomePart},
			counts = #{counts},
			opinion = #{opinion},
			handler = #{handler},
			state = #{state},
		process_instance_id = #{processInstanceId},
		office_id = #{office.id},
		company_id = #{company.id},
		daily_id = #{dailyId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		UPDATE work_incoming_message SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_incoming_message SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkIncomingMessage" statementType="STATEMENT">
		select * FROM work_incoming_message  where ${propertyName} = '${value}'
	</select>

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