<?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.isignature.dao.ISignatureDocumentDao">
    
	<sql id="iSignatureDocumentColumns">
		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.record_id AS "recordId",
		a.subject AS "subject",
		a.type AS "type",
		a.status AS "status",
		a.company_id AS "companyId",
		a.file_path AS "filePath",
		a.file_name AS "fileName",
		a.image_path AS "imagePath",
		a.isignature_status as "isignatureStatus"
	</sql>
	
	<sql id="iSignatureDocumentJoins">
	</sql>
	
    
	<select id="get" resultType="ISignatureDocument" >
		SELECT 
			<include refid="iSignatureDocumentColumns"/>
		FROM isignature_document a
		<include refid="iSignatureDocumentJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByRecordId" resultType="ISignatureDocument" >
		SELECT
			<include refid="iSignatureDocumentColumns"/>
		FROM isignature_document a
		<include refid="iSignatureDocumentJoins"/>
		WHERE a.record_id = #{recordId}
	</select>
	
	<select id="findList" resultType="ISignatureDocument" >
		SELECT 
			<include refid="iSignatureDocumentColumns"/>
		FROM isignature_document a
		<include refid="iSignatureDocumentJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="companyId != null and companyId !=''">
				AND a.company_id = #{companyId}
			</if>
			<!--<if test="createBy != null and createBy.id !=null and createBy.id !=''">
				AND a.create_by = #{createBy.id}
			</if>-->
			<if test="subject != null and subject !=''">
				AND a.subject like "%"#{subject}"%"
			</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="ISignatureDocument" >
		SELECT 
			<include refid="iSignatureDocumentColumns"/>
		FROM isignature_document a
		<include refid="iSignatureDocumentJoins"/>
		<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>
	
	<insert id="insert">
		INSERT INTO isignature_document(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			record_id,
			subject,
			type,
			status,
			company_id,
			file_path,
			file_name,
			image_path,
			isignature_status
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{recordId},
			#{subject},
			#{type},
			#{status},
			#{companyId},
			#{filePath},
			#{fileName},
			#{imagePath},
			#{isignatureStatus}
		)
	</insert>
	
	<update id="update">
		UPDATE isignature_document SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			record_id = #{recordId},
			subject = #{subject},
			type = #{type},
			status = #{status},
			company_id = #{companyId},
			file_path = #{filePath},
			file_name = #{fileName},
			image_path = #{imagePath},
			isignature_status = #{isignatureStatus}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM isignature_document
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE isignature_document SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="ISignatureDocument" statementType="STATEMENT">
		select * FROM isignature_document  where ${propertyName} = '${value}'
	</select>

	<update id="updateSelective">
		UPDATE isignature_document
		SET
		<trim suffixOverrides=",">
			<if test="updateBy!=null">
				update_by = #{updateBy.id},
			</if>
			<if test="updateDate!=null">
				update_date = #{updateDate},
			</if>
			<if test="remarks!=null">
				remarks = #{remarks},
			</if>
			<if test="recordId!=null">
				record_id = #{recordId},
			</if>
			<if test="subject!=null">
				subject = #{subject},
			</if>
			<if test="type!=null">
				type = #{type},
			</if>
			<if test="status!=null">
				status = #{status},
			</if>
			<if test="companyId!=null">
				company_id = #{companyId},
			</if>
			<if test="filePath!=null">
				file_path = #{filePath},
			</if>
			<if test="fileName!=null">
				file_name = #{fileName},
			</if>
			<if test="imagePath!=null">
				image_path = #{imagePath},
			</if>
			<if test="isignatureStatus!=null">
				isignature_status = #{isignatureStatus}
			</if>
		</trim>
		WHERE id = #{id}
	</update>

	<select id="getCount" resultType="int" >
		SELECT
		count(*)
		FROM isignature_document a
		join work_attachment w on w.id=a.record_id and w.attachment_id=#{id}
		where a.isignature_status='1'
	</select>
</mapper>