<?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.ProjectcontentinfoDao">
    
	<sql id="projectcontentinfoColumns">
		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.parent_id AS "parent.id",
		a.parent_ids AS "parentIds",
		a.sort AS "sort",
		a.project_id AS "project.id",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.type AS "type",
		a.info_id AS "infoId",
		a.dict_type AS "dictType",
		a.name AS "name",
		a.link_id AS "linkId"
	</sql>
	
	<sql id="projectcontentinfoJoins">
	</sql>
	
    
	<select id="get" resultType="Projectcontentinfo" >
		SELECT 
			<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		WHERE a.id = #{id}
	</select>
	<select id="getByInfoId" resultType="Projectcontentinfo" >
		SELECT
			<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		WHERE a.info_id = #{infoId} AND (a.link_id is null or a.link_id='')
	</select>

	<select id="findList" resultType="Projectcontentinfo" >
		SELECT 
			<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name =  #{name}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="parentIds != null and parentIds != ''">
				AND a.parent_ids = #{parentIds}
			</if>
			<if test="project != null and project.id != null and project.id != ''">
				AND a.project_id = #{project.id}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.sort ASC
			</otherwise>
		</choose>
	</select>

	<select id="findByParentIds" resultType="Projectcontentinfo" >
		SELECT
			<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = '0'
			AND a.project_id = #{projectId}
			AND a.parent_ids LIKE concat('%',#{parentId},'%')
		</where>
		ORDER BY a.sort ASC
	</select>

	<select id="findListByProject" resultType="Projectcontentinfo" >
		SELECT
		<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.parent_ids = #{parentIds}
			AND a.project_id = #{project.id}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.sort DESC
			</otherwise>
		</choose>
	</select>

	<select id="findByType" resultType="Projectcontentinfo" >
		SELECT
		<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.project_id = #{project.id}
			AND a.type = #{type}
			AND a.dict_type = #{dictType}
		</where>
		ORDER BY a.create_date DESC
		limit 1
	</select>
	<select id="findByInfoId" resultType="Projectcontentinfo" >
		SELECT
		<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.info_id = #{infoId}
            AND (a.link_id is null or a.link_id='')
		</where>
		ORDER BY a.create_date DESC
	</select>

	<select id="findListByProjectAndType" resultType="Projectcontentinfo" >
		SELECT
		<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.type = #{type}
			AND a.parent_ids = #{parentIds}
			AND a.project_id = #{project.id}
			AND a.dict_type is NULL
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.sort DESC
			</otherwise>
		</choose>
	</select>
	<select id="findListByProjectAndTypes" resultType="Projectcontentinfo" >
		SELECT
		<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.type = #{type}
			AND a.parent_ids = #{parentIds}
			AND a.project_id = #{project.id}
			AND a.dict_type is NOT NULL
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.sort DESC
			</otherwise>
		</choose>
	</select>

	<select id="findListByProjectAndDictType" resultType="Projectcontentinfo" >
		SELECT
		<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.type = #{type}
			AND a.dict_type = #{dictType}
			AND a.parent_ids = #{parentIds}
			AND a.project_id = #{project.id}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.sort DESC
			</otherwise>
		</choose>
	</select>

	<select id="findListByProjectType" resultType="Projectcontentinfo" >
		SELECT
		<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.type = #{type}
			AND a.parent_ids = #{parentIds}
			AND a.project_id = #{project.id}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.sort DESC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultType="Projectcontentinfo" >
		SELECT 
			<include refid="projectcontentinfoColumns"/>
		FROM project_content_info a
		<include refid="projectcontentinfoJoins"/>
		<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 project_content_info(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			parent_id,
			parent_ids,
			sort,
			project_id,
			company_id,
			office_id,
			type,
			info_id,
			dict_type,
			name,
			link_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{parent.id},
			#{parentIds},
			#{sort},
			#{project.id},
			#{companyId},
			#{officeId},
			#{type},
			#{infoId},
			#{dictType},
			#{name},
			#{linkId}
		)
	</insert>
	
	<update id="update">
		UPDATE project_content_info SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			type = #{type},
			info_id = #{infoId},
			dict_type = #{dictType},
			parent_id = #{parent.id},
			parent_ids = #{parentIds},
			sort = #{sort},
			project_id = #{project.id},
			name = #{name},
			link_id = #{linkId}
		WHERE id = #{id}
	</update>
	<update id="updateByLinkId">
		UPDATE project_content_info SET
			name = #{dataName}
		WHERE link_id = #{linkId}
	</update>

	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM project_content_info
		WHERE id = #{id}
	</update>
	<!--物理删除-->
	<update id="deleteInfoId">
		DELETE FROM project_content_info
		WHERE info_id = #{infoId}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE project_content_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Projectcontentinfo" statementType="STATEMENT">
		select * FROM project_content_info  where ${propertyName} = '${value}'
	</select>

    <select id="queryMaxSort" resultType="Integer">
        SELECT MAX(sort) FROM project_content_info a
        <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>
        </where>
    </select>

    <select id="findListByParentSelective" resultType="Projectcontentinfo" >
        SELECT
        <include refid="projectcontentinfoColumns"/>
        FROM project_content_info a
        <include refid="projectcontentinfoJoins"/>
        <where>
            <if test="project!=null and project.id!=null and project.id!=''">
                AND a.project_id = #{project.id}
            </if>
            <if test="parent!=null and parent.id!=null and parent.id!=''">
                AND a.parent_id = #{parent.id}
            </if>
            <if test="name!=null and name!=''">
                AND a.name = #{name}
            </if>
            <if test="infoId!=null and infoId!=''">
                AND a.info_id = #{infoId}
            </if>
            <if test="linkId!=null and linkId!=''">
                AND a.link_id = #{linkId}
            </if>
            AND a.del_flag = #{DEL_FLAG_NORMAL}
        </where>
    </select>

    <update id="deleteByParentAndLinkId">
        DELETE FROM project_content_info
        <where>
            <if test="project!=null and project.id!=null and project.id!=''">
                AND project_id = #{project.id}
            </if>
            <if test="parent!=null and parent.id!=null and parent.id!=''">
                AND parent_id = #{parent.id}
            </if>
            <if test="name!=null and name!=''">
                AND name = #{name}
            </if>
            <if test="infoId!=null and infoId!=''">
                AND info_id = #{infoId}
            </if>
            <if test="linkId!=null and linkId!=''">
                AND link_id = #{linkId}
            </if>
        </where>
    </update>

    <update id="deleteInfosByParent">
        DELETE FROM project_content_info WHERE parent_ids LIKE concat(#{parentIds},'%');
    </update>

	<select id="findType" resultType="Projectcontentinfo">
		SELECT id from project_content_info
		<where>
			project_id = #{projectId}
			and name = #{type} and info_id is null
		</where>
	</select>
</mapper>