<?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，namespace的值习惯上设置成包名+sql映射文件名，这样就能够保证namespace的值是唯一的,例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
-->               
<mapper namespace="com.fuzamei.mapperInterface.ProjectFileMapper"> 
	<!-- 根据操作记录给的条件查询分页数据 -->
	<select id="queryProjectFileInformation" resultMap="ProjectFileRM">
		select 
			pf.id,
			pf.project_id,
			pf.project_name,
			pf.update_time,
			att.id as id2,
			att.attachment_id,
			att.attachment_name,
			att.url,
			att.upload_person_id,
			att.create_time
		from 
			(select 
				*
			 from 
				project_file
			 <where>
				<if test="projectName!='' and projectName!=null">
					project_name=#{projectName}
				</if>
				<if test="startTime!=null and endTime!=null">
					and update_time between #{startTime} and #{endTime}
				</if>
			</where>) pf
		left join 
			project_file_id pfi
		on 
			pf.project_id=pfi.project_id
		left join 
			attachment att
		on 
			att.attachment_id=pfi.project_file_id
		order by 
			pf.id asc 
		limit 
			#{startPage},#{rowNum}
	</select>
	
	<resultMap type="com.fuzamei.entity.ProjectFile" id="ProjectFileRM" autoMapping="true">
		<id column="id" property="id"/>
		<collection property="attachments" autoMapping="true" javaType="com.fuzamei.entity.Attachment">
			<id column="id2" property="id"/>
		</collection>
	</resultMap>
	
	<!-- 分页用查询总记录条数 -->
	<select id="findAllInformation" resultType="int">
		select 
			count(*) 
		from 
			project_file
		<where>
			<if test="projectName!='' and projectName!=null">
				project_name=#{projectName}
			</if>
			<if test="startTime!=null and endTime!=null">
				and update_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
	<select id="checkoutProjectFiles" resultType="com.fuzamei.entity.ProjectFile">
		select 
			pf.project_id,
			pf.project_name,
			att.attachment_name,
			pf.hash,
			att.url
		from 
			project_file pf 
		left join 
			attachment att 
		on 
			pf.project_file_id = att.attachment_id
		where 
			pf.project_id=#{projectId} 
			and
			pf.project_name=#{projectName}
		order by 
			pf.update_time asc
	</select>
	
	<insert id="insertInformationIntoTable">
		insert into 
			project_file(project_id,
						 project_name,
						 project_file_id,
						 update_time,hash) 
			values(
					#{projectId},
					#{projectName},
					#{attachmentId},
					#{updateTime},
					#{hash})
	</insert>
	
	<!-- 将上传的文件信息插入到附件表中 -->
	<insert id="insertInformationToAttachment">
			insert into 
				attachment(attachment_id,
					   attachment_name,url,
					   upload_person_id,
					   create_time) 
			values(#{attachmentId},
				   #{attachmentName},
				   #{url},
				   #{userId},
				   #{addTime})
	</insert>
	
	<!-- 将上传项目文档的这个操作信息插入到操作记录表 -->
	<insert id="insertOperationHistoryInformationToDao">
		insert into 
			operation_history(operator_type_id,
							  operator_account,
							  operator_role,
							  operator_person,
							  operator_time,
							  hash)
		values(#{operatorTypeId},
			   #{operatorAccount},
			   #{operatorRole},
			   #{operatorPerson},
			   #{operatorTime},
			   #{hash})
	</insert>
</mapper>
