<?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.work.dao.review.WorkReportReviewDao">
    
	<sql id="workReportReviewColumns">
		a.id AS "id",
		a.parent_id AS "parent.id",
		a.parent_ids AS "parentIds",
		a.report_id AS "reportId",
		a.type AS "type",
		a.content AS "content",
		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",
		createBy.name AS "createBy.name"
	</sql>
	
	<sql id="workReportReviewJoins">
		LEFT JOIN  sys_user createBy ON  createBy.id = a.create_by
	</sql>
    
	<select id="get" resultType="WorkReportReview">
		SELECT 
			<include refid="workReportReviewColumns"/>
		FROM work_report_review a
		<include refid="workReportReviewJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReportReview">
		SELECT 
			<include refid="workReportReviewColumns"/>
		FROM work_report_review a
		<include refid="workReportReviewJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="parent != null and parent.id != null and parent.id != ''">
				AND a.parent_id = #{parent.id}
			</if>
			<if test="parentIds != null and parentIds != ''">
				AND a.parent_ids LIKE 
					<if test="dbName == 'oracle'">'%'||#{parentIds}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{parentIds}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{parentIds},'%')</if>
			</if>
			<if test="reportId != null and reportId != ''">
				AND a.report_id = #{reportId}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by = #{createBy.id}
			</if>
			<if test="createDate != null and createDate != ''">
				AND a.create_date = #{createDate}
			</if>
		</where>
	</select>

	<select id="findListByCompany" resultType="WorkReportReview">
		SELECT
		<include refid="workReportReviewColumns"/>
		FROM work_report_review a
		LEFT JOIN work_report w ON  w.id = a.report_id
		<include refid="workReportReviewJoins"/>
		<where>
			a.del_flag = #{wrr.DEL_FLAG_NORMAL}
			<if test="wrr.parent != null and wrr.parent.id != null and wrr.parent.id != ''">
				AND a.parent_id = #{wrr.parent.id}
			</if>
			<if test="wrr.parentIds != null and wrr.parentIds != ''">
				AND a.parent_ids LIKE
				<if test="dbName == 'oracle'">'%'||#{wrr.parentIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{wrr.parentIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{wrr.parentIds},'%')</if>
			</if>
			<if test="wrr.reportId != null and wrr.reportId != ''">
				AND a.report_id = #{wrr.reportId}
			</if>
			<if test="wrr.type != null and wrr.type != ''">
				AND a.type = #{wrr.type}
			</if>
			<if test="companyId != null and companyId != '' ">
				AND w.company_id = #{companyId}
			</if>
			<if test="wrr.createBy != null and wrr.createBy.id != null and wrr.createBy.id != ''">
				AND a.create_by = #{wrr.createBy.id}
			</if>
			<if test="wrr.createDate != null and wrr.createDate != ''">
				AND a.create_date = #{wrr.createDate}
			</if>
		</where>
	</select>

	<select id="findAllList" resultType="WorkReportReview">
		SELECT 
			<include refid="workReportReviewColumns"/>
		FROM work_report_review a
		<include refid="workReportReviewJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
	</select>
	
	<select id="findByParentIdsLike" resultType="WorkReportReview">
		SELECT
			a.id,
			a.parent_id AS "parent.id",
			a.parent_ids
		FROM work_report_review a
		<include refid="workReportReviewJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.parent_ids LIKE #{parentIds}
		</where>
	</select>
	<!-- 根据工作报告ID查询 -->
	<select id="findListByReportId" resultType="WorkReportReview">
		SELECT 
			<include refid="workReportReviewColumns"/>
		FROM work_report_review a
		<include refid="workReportReviewJoins"/>
		WHERE
			a.del_flag = #{DEL_FLAG_NORMAL}
		AND 
		    a.report_id = #{reportId}
        ORDER BY a.create_date DESC
	</select>

	<!-- 根据工作报告ID查询 评论或回复  -->
	<select id="getReviewByReportId" resultType="WorkReportReview">
		SELECT
		<include refid="workReportReviewColumns"/>
		FROM work_report_review a
		<include refid="workReportReviewJoins"/>
		WHERE
		a.del_flag = '0'
		AND
		a.report_id = #{workReportId}
		ORDER BY a.create_date DESC
	</select>


	<insert id="insert">
		INSERT INTO work_report_review(
			id,
			parent_id,
			parent_ids,
			report_id,
			type,
			content,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{parent.id},
			#{parentIds},
			#{reportId},
			#{type},
			#{content},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE work_report_review SET 	
			parent_id = #{parent.id},
			parent_ids = #{parentIds},
			report_id = #{reportId},
			type = #{type},
			content = #{content},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	<update id="updateParentIds">
		UPDATE work_report_review SET 
			parent_id = #{parent.id}, 
			parent_ids = #{parentIds}
		WHERE id = #{id}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_report_review
		WHERE id = #{id} OR parent_ids LIKE 
		<if test="dbName == 'oracle'">#{id}||'%'</if>
		<if test="dbName == 'mssql'">#{id}+'%'</if>
		<if test="dbName == 'mysql'">concat(#{id},'%')</if>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_report_review SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id} OR parent_ids LIKE 
		<if test="dbName == 'oracle'">#{id}||'%'</if>
		<if test="dbName == 'mssql'">#{id}+'%'</if>
		<if test="dbName == 'mysql'">concat(#{id},'%')</if>
	</update>
	
</mapper>