<?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.hzya.frame.sys.businessFile.dao.impl.BusinessFileDaoImpl">
	<resultMap id="get-BusinessFileEntity-result" type="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		<!--唯一标识码 -->
		<result property="id" column="id" />
		<!--附件表生成的唯一新名称 -->
		<result property="file_save_name" column="file_save_name" />
		<!--业务表ID（合同，预报，单票，国际物流） -->
		<result property="business_id" column="business_id" />
		<!--业务类型(contract/合同prediction/预报changeOrder /纯换单customsClearance /报关报检unboxing/纯开箱单shipSeal/船司盖章harbourSeal /港区盖章) -->
		<result property="business_type" column="business_type" />
		<!--单票ID -->
		<result property="ticket_id" column="ticket_id" />
		<!--附件类型 -->
		<result property="file_type" column="file_type" />
		<!--附件名称 -->
		<result property="file_name" column="file_name" />
		<!--上传人名称 -->
		<result property="upload_user" column="upload_user" />
		<!--创建人 -->
		<result property="create_user_id" column="create_user_id" />
		<!--创建时间 -->
		<result property="create_time" column="create_time" />
		<!--修改人 -->
		<result property="modify_user_id" column="modify_user_id" />
		<!--修改时间 -->
		<result property="modify_time" column="modify_time" />
		<!--状态 -->
		<result property="sts" column="sts" />
		<!--单证号码 -->
		<result property="file_number" column="file_number" />
		<!--单证名称 -->
		<result property="documents_name" column="documents_name" />
		<!--备注 -->
		<result property="remark" column="remark" />
	</resultMap>

	<sql id="BusinessFileEntity_Base_Column_List">
		id,
		file_save_name,
		business_id,
		business_type,
		ticket_id,
		file_type,
		file_name,
		upload_user,
		create_user_id,
		create_time,
		modify_user_id,
		modify_time,
		sts,
		file_number,
		documents_name,
		remark
	</sql>
	<!-- 分组查询-->
	<sql id="BusinessFileEntity_Base_Column_List_group">
		COUNT(1) as file_count,
		file_save_name,
		business_id,
		business_type,
		ticket_id,
		file_type,
		file_name,
		upload_user,
		create_user_id,
		create_time,
		modify_user_id,
		modify_time,
		sts,
		file_number,
		documents_name,
		remark
	</sql>

	<!--通过ID获取数据 -->
	<select id="BusinessFileEntity_get" resultMap="get-BusinessFileEntity-result">
		select
		<include refid="BusinessFileEntity_Base_Column_List" />
		from sys_business_file where id = #{ id } and sts='Y'
	</select>

	<!-- 查询 采用==查询 -->
	<select id="BusinessFileEntity_list_base" resultMap="get-BusinessFileEntity-result" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		select
		<include refid="BusinessFileEntity_Base_Column_List" />
		from sys_business_file
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null ">id=#{id}</if>
			<if test="file_save_name != null and file_save_name !='' "> and file_save_name = #{file_save_name}</if>
			<if test="business_id != null and business_id !='' "> and business_id = #{business_id}</if>
			<if test="business_type != null and business_type !='' "> and business_type = #{business_type}</if>
			<if test="ticket_id != null "> and ticket_id = #{ticket_id}</if>
			<if test="file_type != null and file_type !='' "> and file_type = #{file_type}</if>
			<if test="file_name != null and file_name !='' "> and file_name = #{file_name}</if>
			<if test="upload_user != null and upload_user !='' "> and upload_user = #{upload_user}</if>
			<if test="create_user_id != null "> and create_user_id = #{create_user_id}</if>
			<if test="create_time != null "> and create_time = #{create_time}</if>
			<if test="modify_user_id != null "> and modify_user_id = #{modify_user_id}</if>
			<if test="modify_time != null "> and modify_time = #{modify_time}</if>
			<if test="sts != null and sts !='' "> and sts = #{sts}</if>
			<if test="file_number != null and file_number !='' "> and file_number = #{file_number}</if>
			<if test="documents_name != null and documents_name !='' "> and documents_name = #{documents_name}</if>
			<if test="remark != null and remark !='' "> and remark = #{remark}</if>
			and sts='Y'
		</trim>
		<if test=" sort =='' "> order by id desc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null ">order by ${sort} ${order}</if>
	</select>

	<!-- 根据单证编号查询列表-->
	<select id="BusinessFileEntity_list_base_group" resultMap="get-BusinessFileEntity-result" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		select
		<include refid="BusinessFileEntity_Base_Column_List_group" />
		from sys_business_file
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null ">id=#{id}</if>
			<if test="file_save_name != null and file_save_name !='' "> and file_save_name = #{file_save_name}</if>
			<if test="business_id != null and business_id !='' "> and business_id = #{business_id}</if>
			<if test="business_type != null and business_type !='' "> and business_type = #{business_type}</if>
			<if test="ticket_id != null "> and ticket_id = #{ticket_id}</if>
			<if test="file_type != null and file_type !='' "> and file_type = #{file_type}</if>
			<if test="file_name != null and file_name !='' "> and file_name = #{file_name}</if>
			<if test="upload_user != null and upload_user !='' "> and upload_user = #{upload_user}</if>
			<if test="create_user_id != null "> and create_user_id = #{create_user_id}</if>
			<if test="create_time != null "> and create_time = #{create_time}</if>
			<if test="modify_user_id != null "> and modify_user_id = #{modify_user_id}</if>
			<if test="modify_time != null "> and modify_time = #{modify_time}</if>
			<if test="sts != null and sts !='' "> and sts = #{sts}</if>
			<if test="file_number != null and file_number !='' "> and file_number = #{file_number}</if>
			<if test="documents_name != null and documents_name !='' "> and documents_name = #{documents_name}</if>
			<if test="remark != null and remark !='' "> and remark = #{remark}</if>
			and sts='Y'
		</trim>
		GROUP BY file_number ,documents_name,business_id,business_type
		<if test=" sort =='' "> order by id desc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null ">order by ${sort} ${order}</if>
	</select>

	<!-- 根据服务项列表获取附件列表-->
	<select id="BusinessFileEntity_queryBusinessFileByServiceType" resultMap="get-BusinessFileEntity-result" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		select
		<include refid="BusinessFileEntity_Base_Column_List" />
		from sys_business_file
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null ">id=#{id}</if>
			<if test="file_save_name != null and file_save_name !='' "> and file_save_name = #{file_save_name}</if>
			<if test="business_id != null and business_id !='' "> and business_id = #{business_id}</if>
			<if test="business_type != null and business_type !='' "> and business_type = #{business_type}</if>
			<if test="ticket_id != null "> and ticket_id = #{ticket_id}</if>
			<if test="file_type != null and file_type !='' "> and file_type = #{file_type}</if>
			<if test="ticketAllServiceEntities != null and ticketAllServiceEntities.size() > 0 ">
				and
				<foreach collection="ticketAllServiceEntities" index="index" item="item" open="(" separator="or" close=")">
					(business_type = #{item} )
 				</foreach>
			</if>
			<if test="file_name != null and file_name !='' "> and file_name = #{file_name}</if>
			<if test="upload_user != null and upload_user !='' "> and upload_user = #{upload_user}</if>
			<if test="create_user_id != null "> and create_user_id = #{create_user_id}</if>
			<if test="create_time != null "> and create_time = #{create_time}</if>
			<if test="modify_user_id != null "> and modify_user_id = #{modify_user_id}</if>
			<if test="modify_time != null "> and modify_time = #{modify_time}</if>
			<if test="sts != null and sts !='' "> and sts = #{sts}</if>
			<if test="file_number != null and file_number !='' "> and file_number = #{file_number}</if>
			<if test="documents_name != null and documents_name !='' "> and documents_name = #{documents_name}</if>
			<if test="remark != null and remark !='' "> and remark = #{remark}</if>
			and sts='Y'
		</trim>
 		<if test=" sort =='' "> order by id desc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null ">order by ${sort} ${order}</if>
	</select>

	<!-- 查询符合条件的数量 -->
	<select id="BusinessFileEntity_count" resultType="Integer" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		select count(1) from sys_business_file
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null ">id = #{id}</if>
			<if test="file_save_name != null and file_save_name !='' "> and file_save_name = #{file_save_name} </if>
			<if test="business_id != null and business_id !='' "> and business_id = #{business_id} </if>
			<if test="business_type != null and business_type !='' "> and business_type = #{business_type} </if>
			<if test="ticket_id != null "> and ticket_id = #{ticket_id}  </if>
			<if test="file_type != null and file_type !='' "> and file_type = #{file_type} </if>
			<if test="file_name != null and file_name !='' "> and file_name = #{file_name} </if>
			<if test="upload_user != null and upload_user !='' "> and upload_user = #{upload_user} </if>
			<if test="create_user_id != null "> and create_user_id = #{create_user_id}  </if>
			<if test="create_time != null "> and create_time = #{create_time}  </if>
			<if test="modify_user_id != null "> and modify_user_id = #{modify_user_id}  </if>
			<if test="modify_time != null "> and modify_time = #{modify_time}  </if>
			<if test="sts != null and sts !='' "> and sts = #{sts} </if>
			<if test="file_number != null and file_number !='' "> and file_number = #{file_number} </if>
			<if test="documents_name != null and documents_name !='' "> and documents_name = #{documents_name} </if>
			<if test="remark != null and remark !='' "> and remark = #{remark} </if>
			and sts='Y'
		</trim>
		<if test=" sort =='' "> order by id desc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null "> order by ${sort} ${order}</if>
	</select>

	<!-- 分页查询列表 采用like格式 -->
	<select id="BusinessFileEntity_list_like" resultMap="get-BusinessFileEntity-result" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		select
		<include refid="BusinessFileEntity_Base_Column_List" />
		from sys_business_file
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null ">id = #{id}</if>
			<if test="file_save_name != null and file_save_name !='' "> and file_save_name like concat('%',#{file_save_name},'%') </if>
			<if test="business_id != null and business_id !='' "> and business_id =  #{business_id}   </if>
			<if test="business_type != null and business_type !='' "> and business_type like concat('%',#{business_type},'%') </if>
			<if test="ticket_id != null "> and ticket_id = #{ticket_id}</if>
			<if test="file_type != null and file_type !='' "> and file_type like concat('%',#{file_type},'%') </if>
			<if test="file_name != null and file_name !='' "> and file_name like concat('%',#{file_name},'%') </if>
			<if test="upload_user != null and upload_user !='' "> and upload_user like concat('%',#{upload_user},'%') </if>
			<if test="create_user_id != null "> and create_user_id = #{create_user_id}</if>
			<if test="create_time != null and create_time !='' "> and create_time like concat('%',#{create_time},'%') </if>
			<if test="modify_user_id != null "> and modify_user_id = #{modify_user_id}</if>
			<if test="modify_time != null and modify_time !='' "> and modify_time like concat('%',#{modify_time},'%') </if>
			<if test="sts != null and sts !='' "> and sts like concat('%',#{sts},'%') </if>
			<if test="file_number != null and file_number !='' "> and file_number like concat('%',#{file_number},'%') </if>
			<if test="documents_name != null and documents_name !='' "> and documents_name like concat('%',#{documents_name},'%') </if>
			<if test="remark != null and remark !='' "> and remark like concat('%',#{remark},'%') </if>

			<if test="business_type_list !=null and business_type_list.size > 0">
				and (
				<!-- 根据服务项查找 -->
				<foreach collection="business_type_list" index="index" item="item" open="(" separator="or"
						 close=")">
					business_type LIKE concat('%',#{item},'%')
				</foreach>
				)
			</if>
			<!-- 登陆人及下级人员 -->
			<if test="subUserIdList != null and subUserIdList.size > 0">
				and (
				<foreach collection="subUserIdList" index="index" item="item" separator="or">
					create_user_id = #{item}
				</foreach>
				)
			</if>
			and sts='Y'
		</trim>
		<if test=" sort =='' "> order by id desc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null ">order by  ${sort} ${order}</if>
	</select>

	<!-- 查询列表 字段采用or格式 -->
	<select id="BusinessFileEntity_list_or" resultMap="get-BusinessFileEntity-result" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		select
		<include refid="BusinessFileEntity_Base_Column_List" />
		from sys_business_file
		<trim prefix="where" prefixOverrides="or">
			<if test="id != null ">id = #{id}</if>
			<if test="file_save_name != null and file_save_name !='' "> or file_save_name = #{file_save_name} </if>
			<if test="business_id != null and business_id !='' "> or business_id = #{business_id} </if>
			<if test="business_type != null and business_type !='' "> or business_type = #{business_type} </if>
			<if test="ticket_id != null "> or ticket_id = #{ticket_id}</if>
			<if test="file_type != null and file_type !='' "> or file_type = #{file_type} </if>
			<if test="file_name != null and file_name !='' "> or file_name = #{file_name} </if>
			<if test="upload_user != null and upload_user !='' "> or upload_user = #{upload_user} </if>
			<if test="create_user_id != null "> or create_user_id = #{create_user_id}</if>
			<if test="create_time != null and create_time !='' "> or create_time = #{create_time} </if>
			<if test="modify_user_id != null "> or modify_user_id = #{modify_user_id}</if>
			<if test="modify_time != null and modify_time !='' "> or modify_time = #{modify_time} </if>
			<if test="sts != null and sts !='' "> or sts = #{sts} </if>
			<if test="file_number != null and file_number !='' "> or file_number = #{file_number} </if>
			<if test="documents_name != null and documents_name !='' "> or documents_name = #{documents_name} </if>
			<if test="remark != null and remark !='' "> or remark = #{remark} </if>
			and sts='Y'
		</trim>
		<if test=" sort =='' "> order by id desc</if>
		<if test=" sort !='' and sort!=null and order !='' and order!=null ">order by  ${sort} ${order}</if>
	</select>
	<!-- 新增 -->
	<insert id="entity_insert" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		insert into sys_business_file(
		<trim suffix="" suffixOverrides=",">
			<if test="id != null and id !='' "> id, </if>
			<if test="file_save_name != null and file_save_name !='' "> file_save_name, </if>
			<if test="business_id != null and business_id !='' "> business_id, </if>
			<if test="business_type != null and business_type !='' "> business_type, </if>
			<if test="ticket_id != null ">ticket_id,</if>
			<if test="file_type != null and file_type !='' "> file_type, </if>
			<if test="file_name != null and file_name !='' "> file_name, </if>
			<if test="upload_user != null and upload_user !='' "> upload_user, </if>
			<if test="create_user_id != null ">create_user_id,</if>
			<if test="modify_user_id != null ">modify_user_id,</if>
			<if test="file_number != null and file_number !='' "> file_number, </if>
			<if test="documents_name != null and documents_name !='' "> documents_name, </if>
			<if test="remark != null and remark !='' "> remark, </if>
			create_time,
			modify_time,
			sts
		</trim>
		)values
		(
		<trim suffix="" suffixOverrides=",">
			<if test="id != null and id !='' "> #{id}, </if>
			<if test="file_save_name != null and file_save_name !='' "> #{file_save_name}, </if>
			<if test="business_id != null and business_id !='' "> #{business_id}, </if>
			<if test="business_type != null and business_type !='' "> #{business_type}, </if>
			<if test="ticket_id != null ">#{ticket_id},</if>
			<if test="file_type != null and file_type !='' "> #{file_type}, </if>
			<if test="file_name != null and file_name !='' "> #{file_name}, </if>
			<if test="upload_user != null and upload_user !='' "> #{upload_user}, </if>
			<if test="create_user_id != null ">#{create_user_id},</if>
			<if test="modify_user_id != null ">#{modify_user_id},</if>
			<if test="file_number != null and file_number !='' "> #{file_number}, </if>
			<if test="documents_name != null and documents_name !='' "> #{documents_name}, </if>
			<if test="remark != null and remark !='' "> #{remark}, </if>
			<if test="create_time != null "> #{create_time} ,</if>
			<if test="create_time  == null "> now(),</if>
			<if test="modify_time != null "> #{modify_time},</if>
			<if test="modify_time  == null "> now(),</if>
			'Y'
		</trim>
		)

	</insert>

	<!-- 修改 -->
	<update id="BusinessFileEntity_update" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		update sys_business_file set
		<trim suffix="" suffixOverrides=",">
			<if test="file_save_name != null and file_save_name !='' "> file_save_name = #{file_save_name},</if>
			<if test="business_id != null and business_id !='' "> business_id = #{business_id},</if>
			<if test="business_type != null and business_type !='' "> business_type = #{business_type},</if>
			<if test="ticket_id != null ">ticket_id = #{ticket_id},</if>
			<if test="file_type != null and file_type !='' "> file_type = #{file_type},</if>
			<if test="file_name != null and file_name !='' "> file_name = #{file_name},</if>
			<if test="upload_user != null and upload_user !='' "> upload_user = #{upload_user},</if>
			<if test="create_user_id != null ">create_user_id = #{create_user_id},</if>
			<if test="modify_user_id != null ">modify_user_id = #{modify_user_id},</if>
			<if test="file_number != null and file_number !='' "> file_number = #{file_number},</if>
			<if test="documents_name != null and documents_name !='' "> documents_name = #{documents_name},</if>
			<if test="remark != null and remark !='' "> remark = #{remark},</if>
			<if test="modify_time != null "> modify_time =#{modify_time}  </if>
			<if test="modify_time  == null "> modify_time = now()</if>
		</trim>
		where id = #{id}
	</update>

	<!-- 逻辑删除 -->
	<update id="BusinessFileEntity_logicDelete" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		update sys_business_file set
		sts='N',modify_time = now(),modify_user_id = #{modify_user_id} where
		id = #{id}
	</update>

	<!-- 逻辑删除多条件 -->
	<update id="BusinessFileEntity_logicDelete_Multi_Condition" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		update sys_business_file set
		sts='N',modify_time = now(),modify_user_id = #{modify_user_id}
		<trim prefix="where" prefixOverrides="and">
			<if test="id != null ">id=#{id}</if>
			<if test="file_save_name != null and file_save_name !='' "> and file_save_name = #{file_save_name}</if>
			<if test="business_id != null and business_id !='' "> and business_id = #{business_id}</if>
			<if test="business_type != null and business_type !='' "> and business_type = #{business_type}</if>
			<if test="ticket_id != null "> and ticket_id = #{ticket_id}</if>
			<if test="file_type != null and file_type !='' "> and file_type = #{file_type}</if>
			<if test="file_name != null and file_name !='' "> and file_name = #{file_name}</if>
			<if test="upload_user != null and upload_user !='' "> and upload_user = #{upload_user}</if>
			<if test="create_user_id != null "> and create_user_id = #{create_user_id}</if>
			<if test="create_time != null and create_time !='' "> and create_time = #{create_time}</if>
			<if test="modify_user_id != null "> and modify_user_id = #{modify_user_id}</if>
			<if test="modify_time != null and modify_time !='' "> and modify_time = #{modify_time}</if>
			<if test="sts != null and sts !='' "> and sts = #{sts}</if>
			<if test="file_number != null and file_number !='' "> and file_number = #{file_number}</if>
			<if test="remark != null and remark !='' "> and remark = #{remark}</if>
		</trim>
	</update>

	<!-- 物理删除 -->
	<delete id="BusinessFileEntity_delete" parameterType="com.hzya.frame.sys.businessFile.entity.BusinessFileEntity">
		delete from sys_business_file where id =#{id}
	</delete>
</mapper>
