<?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.sys.dao.WorkattachmentDao">
    
	<sql id="workattachmentColumns">
		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.url AS "url",
		a.type AS "type",
		a.attachment_id AS "attachmentId",
		a.attachment_user AS "attachmentUser",
		a.attachment_name AS "attachmentName",
		a.company_id AS "companyId",
		a.attachment_flag AS "attachmentFlag",
		createBy.name AS "createBy.name"
	</sql>
	
	<sql id="workattachmentJoins">
		 LEFT JOIN sys_user createBy on createBy.id = a.create_by
	</sql>

	<select id="get" resultType="Workattachment" >
		SELECT 
			<include refid="workattachmentColumns"/>
		FROM work_attachment a
		<include refid="workattachmentJoins"/>
		WHERE a.id = #{id}
	</select>
	<select id="getByUrl" resultType="Workattachment" >
		SELECT
			<include refid="workattachmentColumns"/>
		FROM work_attachment a
		<include refid="workattachmentJoins"/>
		WHERE a.url = #{url}
	</select>

	<update id="updateAttachmentId">
		UPDATE work_attachment a
		SET attachment_id = #{newAttachmentId}
		WHERE attachment_id = #{oldAttachmentId}
	</update>

	<select id="getByAttachmentIdAndFlag" resultType="Workattachment">
		SELECT
		<include refid="workattachmentColumns"/>
		FROM work_attachment a
		<include refid="workattachmentJoins"/>
		WHERE a.del_flag = '0'
		<if test="attachmentId != null and attachmentId !=''">
			AND a.attachment_id = #{attachmentId}
		</if>
		<if test="attachmentFlag != null and attachmentFlag !=''">
			AND a.attachment_flag = #{attachmentFlag}
		</if>
	</select>
	<select id="getListByAttachmentIdAndFlag" resultType="Workattachment">
		SELECT
		<include refid="workattachmentColumns"/>
		FROM work_attachment a
		<include refid="workattachmentJoins"/>
		WHERE a.del_flag = '0'
		<if test="attachmentId != null and attachmentId !=''">
			AND a.attachment_id = #{attachmentId}
		</if>
		<if test="attachmentFlag != null and attachmentFlag !=''">
			AND a.attachment_flag = #{attachmentFlag}
		</if>
	</select>

	<select id="getByAttachmentId" resultType="Workattachment">
		SELECT
			<include refid="workattachmentColumns"/>
		FROM work_attachment a
		WHERE a.del_flag = '0'
			<if test="attachmentId != null and attachmentId !=''">
				AND a.attachment_id = #{attachmentId}
			</if>
	</select>
	
	<select id="getByAttachmentIdAndCompanyIdAndFlag" resultType="Workattachment">
		SELECT <include refid="workattachmentColumns"/>
		FROM work_attachment a
		<include refid="workattachmentJoins"/>
		WHERE a.del_flag = '0'
			<if test="attachmentId != null and attachmentId !=''">
				AND a.attachment_id = #{attachmentId}
			</if>
			<if test="companyId != null and companyId !=''">
				AND a.company_id = #{companyId}
			</if>
			<if test="attachmentFlag != null and attachmentFlag !=''">
				AND a.attachment_flag = #{attachmentFlag}
			</if>
	</select>
	
	<select id="findList" resultType="Workattachment" >
		SELECT 
			<include refid="workattachmentColumns"/>
		FROM work_attachment a
		<include refid="workattachmentJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="attachmentId != null and attachmentId != ''">
				AND a.attachment_id = #{attachmentId}
			</if>
			<if test="attachmentFlag != null and attachmentFlag != ''">
				AND attachment_flag = #{attachmentFlag}
			</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="Workattachment" >
		SELECT 
			<include refid="workattachmentColumns"/>
		FROM work_attachment a
		<include refid="workattachmentJoins"/>
		<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="getListOfAttachment" resultType="Workattachment">
		SELECT
		t.id AS "id",
		t.create_by AS "createBy.id",
		t.create_date AS "createDate",
		t.update_by AS "updateBy.id",
		t.update_date AS "updateDate",
		t.remarks AS "remarks",
		t.del_flag AS "delFlag",
		t.url AS "url",
		t.type AS "type",
		t.attachment_id AS "attachmentId",
		t.attachment_user AS "u.id",
		t.attachment_name AS "attachmentName",
		t.company_id AS "companyId",
		t.attachment_flag AS "attachmentFlag",
		u.name AS "attachmentUser"
		FROM work_attachment t
		LEFT JOIN sys_user u on u.id = t.attachment_user
		<where>
		1=1
		<if test="attachmentId != null and attachmentId != ''">
			AND t.attachment_id = #{attachmentId}
		</if>
		<if test="type != null and type != ''">
			AND t.attachment_flag = #{type}
		</if>
		</where>
	</select>
	
	<insert id="insert">
		INSERT INTO work_attachment(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			url,
			type,
			attachment_id,
			attachment_user,
			attachment_name,
			company_id,
			attachment_flag
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{url},
			#{type},
			#{attachmentId},
			#{attachmentUser},
			#{attachmentName},
			#{companyId},
			#{attachmentFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE work_attachment SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			url = #{url},
			type = #{type},
			attachment_id = #{attachmentId},
			attachment_user = #{attachmentUser},
			attachment_name = #{attachmentName},
			company_id = #{companyId},
			attachment_flag = #{attachmentFlag}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_attachment
		WHERE id = #{id}
	</update>

	<!--物理删除-->
	<update id="deleteByUrl">
		DELETE FROM work_attachment
		WHERE url = #{url}
	</update>

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