<?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.workcontractsignature.dao.WorkContractSignatureDao">
    
	<sql id="workContractSignatureColumns">
		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.contract_info_id AS "contractInfoId",
		a.process_instance_id AS "processInstanceId",
		a.instruction AS "instruction",
		a.status AS "status",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.type AS "type",
		o.name AS "officeName",
		workContractInfo.id AS "workContractInfo.id",
		workContractInfo.contract_num AS "workContractInfo.contractNum",
		workContractInfo.name AS "workContractInfo.name",
		workContractInfo.another_contract_num AS "workContractInfo.anotherContractNum",
		wc.name AS "workContractInfo.client.name",
		oo.top_company AS "workContractInfo.chargeCompanyName",
		su.name AS "createBy.name"
	</sql>
	
	<sql id="workContractSignatureJoins">
		LEFT JOIN work_contract_info workContractInfo ON workContractInfo.id = a.contract_info_id
		LEFT JOIN work_client_info wc ON workContractInfo.client_id = wc.id
		LEFT JOIN sys_office oo ON oo.id = workContractInfo.charge_company
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		left JOIN sys_user su on su.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkContractSignature" >
		SELECT 
			<include refid="workContractSignatureColumns"/>
		FROM work_contract_signature a
		<include refid="workContractSignatureJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkContractSignature" >
		SELECT 
			<include refid="workContractSignatureColumns"/>
		FROM work_contract_signature a
		<include refid="workContractSignatureJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="beginCreateDate != null and endCreateDate != null and beginCreateDate != '' and endCreateDate != ''">
				AND a.create_date BETWEEN #{beginCreateDate} AND #{endCreateDate}
			</if>
			<if test="workContractInfo != null and workContractInfo.name != null and workContractInfo.name != ''">
				AND workContractInfo.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workContractInfo.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workContractInfo.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workContractInfo.name},'%')</if>
			</if>
			<if test="workContractInfo != null and workContractInfo.contractNum != null and workContractInfo.contractNum != ''">
				AND workContractInfo.contract_num LIKE
				<if test="dbName == 'oracle'">'%'||#{workContractInfo.contractNum}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workContractInfo.contractNum}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workContractInfo.contractNum},'%')</if>
			</if>
			<if test="processInstanceId != null and processInstanceId != ''">
				AND a.process_instance_id = #{processInstanceId}
			</if>
			<if test="instruction != null and instruction != ''">
				AND a.instruction = #{instruction}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			${sqlMap.dsf}
		</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="WorkContractSignature" >
		SELECT 
			<include refid="workContractSignatureColumns"/>
		FROM work_contract_signature a
		<include refid="workContractSignatureJoins"/>
		<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 work_contract_signature(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			contract_info_id,
			process_instance_id,
			instruction,
			status,
			company_id,
			office_id,
			type
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{workContractInfo.id},
			#{processInstanceId},
			#{instruction},
			#{status},
			#{companyId},
			#{officeId},
			#{type}
		)
	</insert>
	
	<update id="update">
		UPDATE work_contract_signature SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			contract_info_id = #{workContractInfo.id},
			process_instance_id = #{processInstanceId},
			instruction = #{instruction},
			status = #{status},
			company_id = #{companyId},
			office_id = #{officeId},
			type = #{type}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_contract_signature
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_contract_signature SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkContractSignature" statementType="STATEMENT">
		select * FROM work_contract_signature  where ${propertyName} = '${value}'
	</select>
	<update id="updateProcessInstanceId" >
        UPDATE  work_contract_signature SET
        process_instance_id = #{processInstanceId},
        status = #{status}
        WHERE id = #{id}
    </update>
</mapper>