<?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.ProjectBasedDataDao">
    
	<sql id="projectBasedDataColumns">
		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.name AS "name",
		a.number AS "number",
		a.type AS "type",
		a.provider AS "provider",
		a.provide_date AS "provideDate",
		a.project_id AS "project.id",
		a.upload_environment AS "uploadEnvironment",
		a.upload_user AS "uploadUser.id",
		a.upload_date AS "uploadDate",
		u.name AS "uploadUser.name"
	</sql>
	
	<sql id="projectBasedDataJoins">
		LEFT JOIN project_records r ON r.id = a.project_id
		LEFT JOIN sys_user u ON u.id = a.upload_user
	</sql>
	
    
	<select id="get" resultType="ProjectBasedData" >
		SELECT 
			<include refid="projectBasedDataColumns"/>
		FROM project_based_data a
		<include refid="projectBasedDataJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findReportBased" resultType="ProjectBasedData" >
		SELECT
		<include refid="projectBasedDataColumns"/>
		FROM project_based_data a
		<include refid="projectBasedDataJoins"/>
		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="findList" resultType="ProjectBasedData" >
		SELECT 
			<include refid="projectBasedDataColumns"/>
		FROM project_based_data a
		<include refid="projectBasedDataJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<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="name != null and name != ''">
				AND a.name like
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
			</if>
			<if test="number != null and number != ''">
				AND a.number = #{number}
			</if>
			<if test="uploadUser!=null and uploadUser.id != null and uploadUser.id != ''">
				AND a.upload_user = #{uploadUser.id}
			</if>
			<if test="provider!=null and provider != ''">
				AND a.provider like
				<if test="dbName == 'oracle'">'%'||#{provider}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{provider}, '%')</if>
			</if>
			<if test="startDate != null and startDate != ''">
				AND a.upload_date &gt;= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND a.upload_date &lt;= #{endDate}
			</if>
		</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="ProjectBasedData" >
		SELECT 
			<include refid="projectBasedDataColumns"/>
		FROM project_based_data a
		<include refid="projectBasedDataJoins"/>
		<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="ProjectBasedData" >
		SELECT
			a.id AS "id",
			a.number AS "number"
		FROM project_based_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_based_data(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			name,
			number,
			type,
			provider,
			provide_date,
			project_id,
			upload_environment,
			upload_user,
			upload_date
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{name},
			#{number},
			#{type},
			#{provider},
			#{provideDate},
			#{project.id},
			#{uploadEnvironment},
			#{uploadUser.id},
			#{uploadDate}
		)
	</insert>
	
	<update id="update">
		UPDATE project_based_data SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			name = #{name},
			number = #{number},
			type = #{type},
			provider = #{provider},
			provide_date = #{provideDate},
			project_id = #{project.id},
			upload_environment = #{uploadEnvironment},
			upload_user = #{uploadUser.id},
			upload_date = #{uploadDate}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM project_based_data
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE project_based_data SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="ProjectBasedData" statementType="STATEMENT">
		select * FROM project_based_data  where ${propertyName} = '${value}'
	</select>

    <select id="queryListByContentId" resultType="ProjectBasedData">
        SELECT
        <include refid="projectBasedDataColumns"/>
        FROM project_based_data a
        <include refid="projectBasedDataJoins"/>
        LEFT JOIN project_content_based pb ON pb.based_id = a.id
        WHERE
          pb.content_id = #{contentId}
        ORDER BY a.update_date DESC
    </select>
</mapper>