<?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.projectcontentinfo.dao.ProjectContentDataDao">
    
	<sql id="projectContentDataColumns">
		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.office_id AS "officeId",
		a.project_id AS "project.id",
		a.name AS "name",
		a.number AS "number",
		a.type AS "type",
		a.master AS "master.id",
		a.content_details_id AS "contentDetailsId",
		su.name AS "master.name"
	</sql>
	
	<sql id="projectContentDataJoins">
        LEFT JOIN sys_user su ON su.id = a.master
	</sql>
	
    
	<select id="get" resultType="ProjectContentData" >
		SELECT 
			<include refid="projectContentDataColumns"/>
		FROM project_content_data a
		<include refid="projectContentDataJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="ProjectContentData" >
		SELECT 
			<include refid="projectContentDataColumns"/>
		FROM project_content_data a
		<include refid="projectContentDataJoins"/>
		<where>
            <if test="name !=null and name != ''">
                AND a.name LIKE concat('%',#{name},'%')
            </if>
            <if test="number !=null and number != ''">
                AND a.number = #{number}
            </if>
            <if test="master !=null and master.name != null and master.name !=''">
                AND su.name LIKE concat('%',#{master.name},'%')
            </if>
            <if test="project!=null and project.id!=null and project.id!=''">
                AND a.project_id = #{project.id}
            </if>
            <if test="type!=null and type!=''">
                AND a.type = #{type}
            </if>
            <if test="parentType != null">
                AND a.type LIKE concat(#{parentType},'%')
            </if>
            <if test="startDate != null">
                AND a.create_date >= #{startDate}
            </if>
            <if test="endDate != null">
                AND a.create_date &lt;= #{endDate}
            </if>
            <if test="extId !=null and extId!=''">
                AND a.id != #{extId}
            </if>
			AND 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>

	<select id="findReportContent" resultType="ProjectContentData" >
		SELECT
			<include refid="projectContentDataColumns"/>
		FROM project_content_data a
		<include refid="projectContentDataJoins"/>
		JOIN project_report_content rc ON rc.content_id = a.id
		WHERE rc.report_id = #{reportId}
		AND rc.type = #{type}
		AND a.del_flag = '0'
		ORDER BY a.update_date DESC
	</select>

	<select id="findAllList" resultType="ProjectContentData" >
		SELECT 
			<include refid="projectContentDataColumns"/>
		FROM project_content_data a
		<include refid="projectContentDataJoins"/>
		<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>

	<select id="getNumber" resultType="ProjectContentData" >
		SELECT
		<include refid="projectContentDataColumns"/>
		FROM project_content_data a
		WHERE
		a.del_flag = #{DEL_FLAG_NORMAL}
		AND a.project_id = #{project.id}
		ORDER BY a.number DESC
		limit 1
	</select>


	<insert id="insert">
		INSERT INTO project_content_data(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			project_id,
			name,
			number,
			type,
			master,
			content_details_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{project.id},
			#{name},
			#{number},
			#{type},
			#{master.id},
			#{contentDetailsId}
		)
	</insert>
	<insert id="saveReportContent">
		INSERT INTO project_report_content(
			report_id,
			content_id,
			type
		) VALUES (
			#{reportId},
			#{contentId},
			#{type}
		)
	</insert>

	<update id="update">
		UPDATE project_content_data SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			project_id = #{project.id},
			name = #{name},
			number = #{number},
			type = #{type},
			master = #{master.id},
			content_details_id = #{contentDetailsId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		UPDATE project_content_data SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE project_content_data SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="ProjectContentData" statementType="STATEMENT">
		select * FROM project_content_data  where ${propertyName} = '${value}'
	</select>

    <select id="querySerialNum" resultType="int">
        SELECT MAX(cast(`number` as UNSIGNED INTEGER))
        from project_content_data pd LEFT JOIN work_content_type wt ON wt.type_id = pd.type
        WHERE pd.project_id = #{project.id} AND wt.parent_id = (SELECT parent_id from work_content_type where type_id = #{type})
    </select>

    <update id="deleteBasedByContentId">
        DELETE FROM project_content_based WHERE content_id = #{contentId}
    </update>

    <update id="deleteBasedData">
        DELETE FROM project_content_based WHERE content_id = #{contentId} and based_id = #{basedId}
    </update>
    <update id="deleteReportDataInfo">
        DELETE FROM project_report_content WHERE content_id = #{contentId} and report_id = #{reportId} AND type = #{type}
    </update>
    <update id="deleteReportDataMenu">
        DELETE FROM project_content_change WHERE info_id = #{reportId} and project_id = #{projectId} and link_id = #{linkId}
    </update>

    <insert id="batchInsertBaseData" parameterType="java.util.Map">
        INSERT INTO project_content_based
        (content_id ,based_id)
        VALUES
        <foreach collection="projectBasedDataList" item="projectBasedData" separator=",">
            ( #{contentId}, #{projectBasedData.id})
        </foreach>
    </insert>

    <select id="countByBaseId" resultType="int">
        SELECT count(*) FROM project_content_based WHERE based_id = #{basedId}
    </select>

    <select id="findByLinkId" resultType="java.lang.String">
        SELECT id FROM project_content_info WHERE info_id = #{contentId} and project_id = #{projectId}
    </select>
</mapper>