<?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.workcontractrecord.dao.WorkContractRecordDao">
    
	<sql id="workContractRecordColumns">
		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.process_instance_id AS "processInstanceId",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.contract_info_id AS "workContractInfo.id",
		a.record_num AS "recordNum",
		a.file_num AS "fileNum",
		a.instruction AS "instruction",
		workContractInfo.contract_num AS "workContractInfo.contractNum",
		workContractInfo.name AS "workContractInfo.name",
		wc.name AS "workContractInfo.client.name",
		workContractInfo.contract_type AS "workContractInfo.contractType",
		su.name AS "createBy.name",
		a.status AS "status"
	</sql>
	
	<sql id="workContractRecordJoins">
		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 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="WorkContractRecord" >
		SELECT 
			<include refid="workContractRecordColumns"/>
		FROM work_contract_record a
		<include refid="workContractRecordJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkContractRecord" >
		SELECT 
			<include refid="workContractRecordColumns"/>
		FROM work_contract_record a
		<include refid="workContractRecordJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="recordNum != null and recordNum != ''">
				AND a.record_num = #{recordNum}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.create_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</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="workContractInfo != null and workContractInfo.contractType != null and workContractInfo.contractType != ''">
				AND workContractInfo.contract_type = #{workContractInfo.contractType}
			</if>
			<if test="workContractInfo != null and workContractInfo.id != null and workContractInfo.id != ''">
				AND a.contract_info_id = #{workContractInfo.id}
			</if>
			<if test="workContractInfo != null and workContractInfo.client != null and workContractInfo.client.name != null and workContractInfo.client.name != ''">
				AND wc.name LIKE
				<if test="dbName == 'oracle'">'%'||#{workContractInfo.client.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workContractInfo.client.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workContractInfo.client.name},'%')</if>
			</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="WorkContractRecord" >
		SELECT 
			<include refid="workContractRecordColumns"/>
		FROM work_contract_record a
		<include refid="workContractRecordJoins"/>
		<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_record(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_instance_id,
			office_id,
			company_id,
			contract_info_id,
			record_num,
			file_num,
			instruction,
			status
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processInstanceId},
			#{officeId},
			#{companyId},
			#{workContractInfo.id},
			#{recordNum},
			#{fileNum},
			#{instruction},
			#{status}
		)
	</insert>
	
	<update id="update">
		UPDATE work_contract_record SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_instance_id = #{processInstanceId},
			office_id = #{officeId},
			company_id = #{companyId},
			contract_info_id = #{workContractInfo.id},
			record_num = #{recordNum},
			file_num = #{fileNum},
			instruction = #{instruction},
			status = #{status}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_contract_record
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_contract_record SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkContractRecord" statementType="STATEMENT">
		select * FROM work_contract_record  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByworkContractInfo" resultType="WorkContractInfo">
		SELECT
		a.id AS "id",
		a.create_by AS "createBy.id",
		(select name from sys_user user where user.id=a.create_by) AS "createBy.name",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.project_id AS "project.id",
		a.project_name AS "projectName",
		a.ext AS "ext",
		p.project_name AS "project.projectName",
		p.project_number AS "project.projectNumber",
		a.del_flag AS "delFlag",
		a.remarks AS "remarks",
		a.client_id AS "client.id",
		a.contract_date AS "contractDate",
		a.effective_date AS "effectiveDate",
		a.closing_date AS "closingDate",
		a.name AS "name",
		a.contract_category AS "contractCategory",
		a.contract_num AS "contractNum",
		a.contract_price AS "contractPrice",
		a.contract_opposite AS "contractOpposite",
		a.contract_type AS "contractType",
		a.contract_fee AS "contractFee",
		a.contract_special AS "contractSpecial",
		a.contract_state AS "contractState",
		a.create_name AS "createName",
		a.charge_company AS "chargeCompany",
		a.charge_company_name AS "chargeCompanyName",
		a.contract_file_id AS "contractFileId",
		a.contract_role AS  "contractRole",
		a.apply_view AS  "applyView",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.contractFeeDoc AS "contractFeeDoc",
		a.contractTypeDoc AS "contractTypeDoc",
		a.process_instance_id AS "processInstanceId",
		client.name AS "client.name",
		a.is_total AS "isTotal",
		a.total_contract_id AS "totalContractId",
		a.common_flag AS "commonFlag",
		a.status AS "status",
		a.investment_scale AS "investmentScale",
		a.summary_payment AS "summaryPayment",
		a.investment_amount AS "investmentAmount",
		a.create_bys AS "createBys",
		a.construction_project_type AS "constructionProjectType",
		a.invalidate_process_instance_id AS "invalidateProcessInstanceId",
		a.alter_process_instance_id AS "alterProcessInstanceId",
		a.invalidate_status AS "invalidateStatus",
		a.invalidate_user AS "invalidateUserId",
		a.alter_state AS "alterState",
		a.alterbefore_id AS "alterBeforeId",
		a.alter_sign AS "alterSign",
		a.record_state AS "recordState",
		(select count(1) from work_contract_info c where c.total_contract_id = a.id)  cnt
		FROM work_contract_info a
		LEFT JOIN work_client_info client ON client.id = a.client_id
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.charge_company
		LEFT JOIN work_bid_project p ON  p.id = a.project_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and (a.contract_state =5 or a.contract_state =7 ) and a.record_state = 1 and alter_state is null
			<if test="name != null and name != ''">
				AND a.name LIKE
				<if test="dbName == 'oracle'">#{name}||'%'</if>
				<if test="dbName == 'mssql'">#{name}+'%'</if>
				<if test="dbName == 'mysql'">concat(#{name},'%')</if>
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</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>

	<update id="updateProcessInstanceId">
		UPDATE work_contract_record SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>

	<update id="updateRecordStatus">
		UPDATE work_contract_info SET
		record_state = 3
		WHERE id = #{id}
	</update>
	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkContractRecord">
		SELECT
		<include refid="workContractRecordColumns"/>
		FROM work_contract_record a
		<include refid="workContractRecordJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<select id="findRecordNum" resultType="java.lang.String">
		 SELECT a2.record_num from(select * from work_contract_record a
		 where a.del_flag = 0 and a.record_num like concat(#{num},'%') ORDER BY a.create_date desc LIMIT 1) a2
	</select>
</mapper>