<?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.workprojectnotify.dao.WorkProjectNotifyDao">
    
	<sql id="workProjectNotifyColumns">
		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.company_id AS "companyId",
		a.title AS "title",
		a.content AS "content",
		a.type AS "type",
		a.notify_user AS "user.id",
		a.notify_id AS "notifyId",
		a.notify_role AS "notifyRole",
		a.status AS "status",
		a.kind AS "kind",
		a.officeId AS "officeId"
	</sql>
	
	<sql id="workProjectNotifyJoins">
	</sql>
	
    <select id="getByWorkBidingDocument" parameterType="string"  resultType="WorkProjectNotify">
		SELECT
		<include refid="workProjectNotifyColumns"/>
		FROM work_project_notify a
		<include refid="workProjectNotifyJoins"/>
		<where>
			a.del_flag = '0'
			AND a.type = '6'
			AND a.status = '0'
			<if test="workBidingDocumentId != null and workBidingDocumentId !=''">
				AND a.notify_id = #{workBidingDocumentId}
			</if>
		</where>
	</select>


	<select id="get" resultType="WorkProjectNotify" >
		SELECT 
			<include refid="workProjectNotifyColumns"/>
		FROM work_project_notify a
		<include refid="workProjectNotifyJoins"/>
		WHERE a.id = #{id}
	</select>
	<select id="getByNotifyId" resultType="WorkProjectNotify" >
		SELECT
			<include refid="workProjectNotifyColumns"/>
		FROM work_project_notify a
		<include refid="workProjectNotifyJoins"/>
		WHERE a.notify_id = #{notifyId}
	</select>

	<select id="getByNotifyIdAndNotifyUser" resultType="WorkProjectNotify">
		SELECT <include refid="workProjectNotifyColumns"/>
		FROM work_project_notify a
		<include refid="workProjectNotifyJoins"/>
		WHERE a.notify_id = #{notifyId} AND a.notify_user = #{notifyUserId}
	</select>
	
	<select id="findList" resultType="WorkProjectNotify" >
		SELECT 
			<include refid="workProjectNotifyColumns"/>
		FROM work_project_notify a
		<include refid="workProjectNotifyJoins"/>
		<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="content != null and content != ''">
				AND a.content LIKE
					<if test="dbName == 'oracle'">'%'||#{content}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{content}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{content},'%')</if>
			</if>
			<if test="user != null and user.id != null and user.id != ''">
				AND a.notify_user = #{user.id}
			</if>
			<if test="userName != null and userName != ''">
				AND u.name LIKE
				<if test="dbName == 'oracle'">'%'||#{userName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{userName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{userName},'%')</if>
			</if>

			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="types != null and types != ''">
				AND a.type in (${types})
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.create_date &gt; #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.create_date &lt; #{endDate}
			</if>
			<choose>
				<when test="status !=null and status != '' and status == '-1'">
				</when>
				<when test="status !=null and status != '' ">
					AND a.status = #{status}
				</when>
				<otherwise>
					AND a.status != '1'
				</otherwise>
			</choose>
			<choose>
				<when test="remarks !=null and remarks != '' and remarks == '待通知'">
					AND a.remarks = #{remarks}
				</when>
				<otherwise>
					AND a.remarks != '待通知'
				</otherwise>
			</choose>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="queryCount" resultType="Integer" >
		SELECT count(1)
		FROM work_project_notify a
		<if test="userName != null and userName != ''">
			LEFT JOIN sys_user u ON u.id = a.create_by
		</if>
		<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="content != null and content != ''">
				AND a.content LIKE
				<if test="dbName == 'oracle'">'%'||#{content}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{content}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{content},'%')</if>
			</if>
			<if test="user != null and user.id != null and user.id != ''">
				AND a.notify_user = #{user.id}
			</if>
			<if test="userName != null and userName != ''">
				AND u.name LIKE
				<if test="dbName == 'oracle'">'%'||#{userName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{userName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{userName},'%')</if>
			</if>

			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="types != null and types != ''">
				AND a.type in (${types})
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.create_date &gt; #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.create_date &lt; #{endDate}
			</if>
			<choose>
				<when test="status !=null and status != '' and status == '-1'">
				</when>
				<when test="status !=null and status != '' ">
					AND a.status = #{status}
				</when>
				<otherwise>
					AND a.status != '1'
				</otherwise>
			</choose>
			<choose>
				<when test="remarks !=null and remarks != '' and remarks == '待通知'">
					AND a.remarks = #{remarks}
				</when>
				<otherwise>
					AND a.remarks != '待通知'
				</otherwise>
			</choose>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findListByAPP" resultType="WorkProjectNotify" >
		SELECT
		<include refid="workProjectNotifyColumns"/>
		FROM work_project_notify a
		<if test="userName != null and userName != ''">
			LEFT JOIN sys_user u ON u.id = a.create_by
		</if>
		<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="content != null and content != ''">
				AND a.content LIKE
				<if test="dbName == 'oracle'">'%'||#{content}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{content}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{content},'%')</if>
			</if>
			<if test="user != null and user.id != null and user.id != ''">
				AND a.notify_user = #{user.id}
			</if>
			<if test="userName != null and userName != ''">
				AND u.name LIKE
				<if test="dbName == 'oracle'">'%'||#{userName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{userName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{userName},'%')</if>
			</if>

			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="types != null and types != ''">
				AND a.type in (${types})
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.create_date &gt; #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.create_date &lt; #{endDate}
			</if>
			<if test="remarks != null and remarks != ''">
				AND a.remarks = #{remarks}
			</if>
			<choose>
				<when test="status !=null and status != '' and status == '-1'">
				</when>
				<when test="status !=null and status != '' ">
					AND a.status = #{status}
				</when>
				<otherwise>
					AND a.status != '1'
				</otherwise>
			</choose>
			AND a.type not in ('24','25','26','27','28','29','32','33','34','35','36','37')
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="findReadStateByNotifyId" resultType="User" >
		SELECT
			u.id ,u.name,u.login_name AS "loginName",u.photo
		FROM work_project_notify a
		LEFT JOIN sys_user u ON u.id = a.notify_user
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} AND a.status != '1' AND a.notify_id = #{notifyId}
		</where>
		<choose>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultType="WorkProjectNotify" >
		SELECT 
			<include refid="workProjectNotifyColumns"/>
		FROM work_project_notify a
		<include refid="workProjectNotifyJoins"/>
		<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_project_notify(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			title,
			content,
			type,
			notify_user,
			notify_id,
			notify_role,
			status,
			kind,
			officeId
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{title},
			#{content},
			#{type},
			#{user.id},
			#{notifyId},
			#{notifyRole},
			#{status},
			#{kind},
			#{officeId}
		)
	</insert>

	<update id="update">
		UPDATE work_project_notify SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			title = #{title},
			content = #{content},
			type = #{type},
			notify_user = #{user.id},
			notify_id = #{notifyId},
			notify_role = #{notifyRole},
			status = #{status},
			kind = #{kind},
			officeId = #{officeId}
		WHERE id = #{id}
	</update>
	<update id="updateReadStateByNotifyIdAndNotifyUser">
		UPDATE work_project_notify SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			status = #{status}
		WHERE notify_id = #{notifyId}
		AND notify_user = #{user.id}
	</update>
	<update id="updateReadStateByNotifyIdAndNotifyRole">
		UPDATE work_project_notify SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			status = #{status}
		WHERE notify_id = #{notifyId}
		AND notify_role = #{notifyRole}
	</update>
	<update id="updateReadStateByNotifyRoleAndUser">
		UPDATE work_project_notify SET
		update_by = #{updateBy.id},
		update_date = #{updateDate},
		status = #{status}
		WHERE notify_user = #{user.id}
		AND notify_role = #{notifyRole}
	</update>


	<update id="updateDelflagByNotifyId">
		UPDATE work_project_notify SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			del_flag = #{delFlag}
		WHERE notify_id = #{notifyId}
	</update>

	<update id="updateReadStateByNotifyId">
		UPDATE work_project_notify SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			status = #{status}
		WHERE notify_id = #{notifyId} AND status != '1'
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_project_notify
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_project_notify SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkProjectNotify" statementType="STATEMENT">
		select * FROM work_project_notify  where ${propertyName} = '${value}'
	</select>
	
</mapper>