<?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.sunxung.factoring.mapper.sys.FileStorageMapper">

	<resultMap type="FileStorage" id="fileStorageResult">
		<result property="id" column="id"/>
		<result property="belong.belongId" column="belongId"/>
		<result property="belong.belongType" column="belongType"/>
		<result property="fileType" column="fileType"/>
		<result property="name" column="name"/>
		<result property="originalName" column="originalName"/>
		<result property="path" column="path"/>
		<result property="absolutePath" column="absolutePath"/>
		<result property="size" column="size"/>
		<result property="userCreate" column="user_create"/>
		<result property="userModified" column="user_modified"/>
		<result property="gmtCreate" column="gmt_create"/>
		<result property="gmtModified" column="gmt_modified"/>
	</resultMap>

	<insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="FileStorage">
		insert into t_file_storage
			( belongId, belongType, fileType, name, originalName, path, absolutePath, size, user_create, user_modified )
		values
			(#{belong.belongId}, #{belong.belongType}, #{fileType}, #{name}, #{originalName}, #{path}, #{absolutePath}, #{size},  #{userCreate}, #{userModified} )
	</insert>

	<update id="update" parameterType="FileStorage">
		update
			t_file_storage
		set
			name=#{name}, originalName=#{originalName}, path=#{path}, absolutePath=#{absolutePath},
			size=#{size}, gmt_modified=#{gmtModified}, updateFileStorage=#{updateFileStorage}
		where id=#{id}
	</update>

	<update id="updateBelongById">
		update
			t_file_storage
		set
			belongId=#{belong.belongId}, belongType=#{belong.belongType}
		where id=#{id}
	</update>

	<select id="get" parameterType="java.lang.Long" resultMap="fileStorageResult">
		select * from t_file_storage where id=#{value}
	</select>

	<select id="delete" parameterType="java.lang.Long">
		delete from t_file_storage where id=#{value}
	</select>

	<select id="getByType" parameterType="java.lang.String" resultMap="fileStorageResult">
		select * from t_file_storage where fileType=#{value}
	</select>

	<select id="findByBelongId" parameterType="java.lang.String" resultMap="fileStorageResult">
		select * from t_file_storage where belongId=#{value}
	</select>

	<select id="findByBelongIdAndBelongType" resultMap="fileStorageResult">
		select * from t_file_storage where belongId=#{belongId} and belongType=#{belongType}
	</select>

	<select id="selectInvalidFileStorage" resultType="java.lang.Long">
        SELECT
			t1.*
		FROM
			t_file_storage t1
			LEFT JOIN sys_attachment_ref t2 ON t2.file_storage_id = t1.id
		WHERE
			t2.id IS NULL
			AND NOT EXISTS ( SELECT 1 FROM t_file_storage a1 INNER JOIN sys_attachment_ref a2 ON a1.id = a2.file_storage_id WHERE a1.path = t1.path );

    </select>

	<update id="updateBusinessId">
		update t_file_storage set belongId = #{businessId}
		where id = #{fileId}
	</update>
</mapper>
