<?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.workcontractinfo.dao.WorkContractInfoDao">
    
	<sql id="workContractInfoColumns">
		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_num_path AS "contractNumPath",
		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.office_id 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.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",
		a.completion_status AS "completionStatus",
		a.signature_status as "signatureStatus",
		a.office_id AS "officeId",
		(select count(1) from work_contract_info c where c.total_contract_id = a.id)  cnt
	</sql>
	
	<sql id="workContractInfoJoins">
		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.office_id
		LEFT JOIN work_bid_project p ON  p.id = a.project_id
		LEFT JOIN sys_office s ON s.id = a.company_id
	</sql>
	
    
	<select id="get" resultType="WorkContractInfo" >
		SELECT
		a.id AS "id",
		a.project_id AS "project.id",
		a.ext AS  "ext",
		u.name AS "createBy.name",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		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_num_path AS "contractNumPath",
		a.create_name AS "createName",
		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.office_id 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.process_instance_id AS "processInstanceId",
		a.company_id as companyId,
		a.contractFeeDoc AS "contractFeeDoc",
		a.contractTypeDoc AS "contractTypeDoc",
		a.another_contract_num AS "anotherContractNum",
		client.name AS "client.name",
		a.is_total AS "isTotal",
		a.total_contract_id AS "totalContractId",
		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.alter_sign AS "alterSign",
		a.record_state AS "recordState",
		a.alterbefore_id AS "alterBeforeId",
		a.completion_status AS "completionStatus",
		a.common_flag AS "commonFlag",
		a.office_id AS "officeId",
		a.signature_status as "signatureStatus"
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findByReport" resultType="java.lang.Integer" >
		SELECT
		count(*)
		FROM work_contract_info a
		LEFT JOIN project_records r ON r.contract_id = a.id
		LEFT JOIN project_report_data d ON  d.project_id = r.id
		WHERE a.id = #{id}  AND  (d.status != '5' or d.file_status != '3')
	</select>

	<select id="getByBeforeId" resultType="WorkContractInfo" >
		SELECT
		a.*,
		a.project_id AS "project.id",
		p.project_name AS "project.projectName",
		p.project_number AS "project.projectNumber",
		a.create_by AS "createBy.id",
		a.invalidate_process_instance_id AS "invalidateProcessInstanceId",
		a.alter_process_instance_id AS "alterProcessInstanceId",
		a.invalidate_status AS "invalidateStatus",
		a.invalidate_user AS "invalidateUserId",
		client.id AS "client.id",
		a.alter_state AS "alterState",

		a.alterbefore_id AS "alterBeforeId",
		a.alter_sign AS "alterSign",
		a.record_state AS "recordState",
		client.name AS "client.name"
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		WHERE a.alterbefore_id = #{alterBeforeId} and a.del_flag = 0 and contract_state is null
	</select>

	<select id="getClientInfoByClientId"  parameterType="java.lang.String" resultType="WorkClientInfo" >
		SELECT
		id As "id",
		name As "name",
		name_for_short AS "nameForShort",
		or_unicode AS "orUnicode",
		company_type AS "companyType",
		company_industry AS "companyIndustry",
		client_type AS "clientType",
		credit_rank AS "creditRank",
		area_id AS "area",
		zip_code AS "zipCode",
		fax AS "fax",
		tax_id AS "taxId",
		email AS "email",
		address AS address,
		register_address AS registerAddress,
		company_url AS companyUrl
		  FROM
		  work_client_info
		  WHERE id=#{id} and del_flag = 0
	</select>

	<select id="findList" resultType="WorkContractInfo" >
		SELECT
		a.id AS "id",
		u.name AS "createBy.name",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		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_num_path AS "contractNumPath",
		a.create_name AS "createName",
		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.office_id 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.process_instance_id AS "processInstanceId",
		a.company_id as companyId,
		a.contractFeeDoc AS "contractFeeDoc",
		a.contractTypeDoc AS "contractTypeDoc",
		a.another_contract_num AS "anotherContractNum",
		client.name AS "client.name",
		a.is_total AS "isTotal",
		a.total_contract_id AS "totalContractId",
		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.alter_sign AS "alterSign",
		a.record_state AS "recordState",
		a.alterbefore_id AS "alterBeforeId",
		a.completion_status AS "completionStatus",
		a.common_flag AS "commonFlag",
		a.signature_status as "signatureStatus"
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and (a.alter_state is null or a.alter_state ="")
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="client != null and client.id != null and client.id != ''">
				AND a.client_id = #{client.id}
			</if>
			<if test="client != null and client.name != null and client.name != ''">
				AND client.name LIKE concat('%',#{client.name},'%')
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.contract_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<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="totalContractIds != null and totalContractIds != ''">
				AND a.total_contract_ids LIKE
				<if test="dbName == 'oracle'">'%'||#{totalContractIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{totalContractIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{totalContractIds},'%')</if>
			</if>
			<!--<if test="totalContractId != null and totalContractId != ''">
				AND a.total_contract_id =#{totalContractId}
			</if>-->
			<if test="beginInvestmentAmount != null and endInvestmentAmount != null and beginInvestmentAmount != '' and endInvestmentAmount != ''">
				AND a.investment_amount &gt;= #{beginInvestmentAmount} AND a.investment_amount &lt;= #{endInvestmentAmount}
			</if>
			<if test="contractPrice != '' and contractPrice != null">
				AND a.contract_price like concat('%',#{contractPrice},'%')
			</if>
			<if test="contractState != null and contractState != ''">
				AND a.contract_state = #{contractState}
			</if>
			<if test="contractNum != null and contractNum!= ''">
				AND a.contract_num like  concat('%',#{contractNum},'%')
			</if>
			<if test="isTotal != null and isTotal != ''">
				AND a.is_total = #{isTotal}
			</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.create_date DESC
			</otherwise>
		</choose>
	</select>


	<select id="queryCount" resultType="int" >
		SELECT count(1)
		FROM work_contract_info a
		<if test="client != null and client.name != null and client.name != ''">
			LEFT JOIN work_client_info client ON client.id = a.client_id
		</if>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and (a.alter_state is null or a.alter_state ="")
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="client != null and client.id != null and client.id != ''">
				AND a.client_id = #{client.id}
			</if>
			<if test="client != null and client.name != null and client.name != ''">
				AND client.name LIKE concat('%',#{client.name},'%')
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.contract_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<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="totalContractIds != null and totalContractIds != ''">
				AND a.total_contract_ids LIKE
				<if test="dbName == 'oracle'">'%'||#{totalContractIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{totalContractIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{totalContractIds},'%')</if>
			</if>
			<!--<if test="totalContractId != null and totalContractId != ''">
				AND a.total_contract_id =#{totalContractId}
			</if>-->
			<if test="beginInvestmentAmount != null and endInvestmentAmount != null and beginInvestmentAmount != '' and endInvestmentAmount != ''">
				AND a.investment_amount &gt;= #{beginInvestmentAmount} AND a.investment_amount &lt;= #{endInvestmentAmount}
			</if>
			<if test="contractPrice != '' and contractPrice != null">
				AND a.contract_price = #{contractPrice}
			</if>
			<if test="contractState != null and contractState != ''">
				AND a.contract_state = #{contractState}
			</if>
			<if test="contractNum != null and contractNum!= ''">
				AND a.contract_num = #{contractNum}
			</if>
			<if test="isTotal != null and isTotal != ''">
				AND a.is_total = #{isTotal}
			</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.contract_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findListReceipts" resultType="WorkContractInfo" >
		SELECT
		a.id AS "id",
		(select name from sys_user user where user.id=a.create_by) AS "createBy.name",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		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_num_path AS "contractNumPath",
		a.create_name AS "createName",
		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.office_id 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.process_instance_id AS "processInstanceId",
		a.company_id as companyId,
		a.contractFeeDoc AS "contractFeeDoc",
		a.contractTypeDoc AS "contractTypeDoc",
		a.another_contract_num AS "anotherContractNum",
		client.name AS "client.name",
		a.is_total AS "isTotal",
		a.total_contract_id AS "totalContractId",
		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.alter_sign AS "alterSign",
		a.record_state AS "recordState",
		a.completion_status AS "completionStatus",
		a.alterbefore_id AS "alterBeforeId",
		(select name from work_contract_info where id  = a.total_contract_id) AS "totalContractName",
		a.common_flag AS "commonFlag",
		(select count(1) from work_contract_info c where c.total_contract_id = a.id)  cnt
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and (a.alter_state is null or a.alter_state ="")
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="contractState != null and contractState != ''">
				AND a.contract_state = #{contractState}
			</if>
			<!--<if test="createBy != null and createBy.id != null and createBy.id != '' and-->
			 <!--chargeCompany != null and chargeCompany != ''-->
			<!--">-->
				<!--AND (a.create_by = #{createBy.id}  or a.office_id= #{chargeCompany})-->
			<!--</if>-->
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.contract_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="ifindList" resultType="WorkContractInfo" >
		SELECT
		a.id AS "id",
		(select name from sys_user user where user.id=a.create_by) AS "createBy.name",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		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.create_name AS "createName",
		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.office_id 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.process_instance_id AS "processInstanceId",
		a.company_id as companyId,
		a.contractFeeDoc AS "contractFeeDoc",
		a.contractTypeDoc AS "contractTypeDoc",
		a.another_contract_num AS "anotherContractNum",
		client.name AS "client.name",
		a.is_total AS "isTotal",
		a.total_contract_id AS "totalContractId",
		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.alter_sign AS "alterSign",
		a.record_state AS "recordState",
		a.completion_status AS "completionStatus",
		a.alterbefore_id AS "alterBeforeId",
		(select name from work_contract_info where id  = a.total_contract_id) AS "totalContractName",
		a.common_flag AS "commonFlag",
		(select count(1) from work_contract_info c where c.total_contract_id = a.id)  cnt
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and (a.contract_state = 6 or a.contract_state = 7)
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="client != null and client.id != null and client.id != ''">
				AND a.client_id = #{client.id}
			</if>
			<if test="client != null and client.name != null and client.name != ''">
				AND client.name LIKE concat('%',#{client.name},'%')
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.contract_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<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="totalContractIds != null and totalContractIds != ''">
				AND a.total_contract_ids LIKE
				<if test="dbName == 'oracle'">'%'||#{totalContractIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{totalContractIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{totalContractIds},'%')</if>
			</if>
			<!--<if test="totalContractId != null and totalContractId != ''">
				AND a.total_contract_id =#{totalContractId}
			</if>-->

			<if test="contractPrice != '' and contractPrice != null">
				AND a.contract_price like concat('%',#{contractPrice},'%')
			</if>
			<if test="contractState != null and contractState != ''">
				AND a.contract_state = #{contractState}
			</if>
			<if test="contractNum != null and contractNum!= ''">
				AND a.contract_num like  concat('%',#{contractNum},'%')
			</if>
			<!--<if test="isTotal != null and isTotal != ''">
				AND a.is_total = #{isTotal}
			</if>-->
			<choose>
				<when test="queryCon != null and queryCon != ''">
					AND (
					find_in_set(#{queryCon},a.create_bys)
					${sqlMap.dsf}
					)
				</when>
				<otherwise>
					${sqlMap.dsf}
				</otherwise>
			</choose>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.contract_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="afindList" resultType="WorkContractInfo" >
		SELECT
		a.id AS "id",
		(select name from sys_user user where user.id=a.create_by) AS "createBy.name",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		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_num_path AS "contractNumPath",
		a.create_name AS "createName",
		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.office_id 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.process_instance_id AS "processInstanceId",
		a.company_id as companyId,
		a.contractFeeDoc AS "contractFeeDoc",
		a.contractTypeDoc AS "contractTypeDoc",
		a.another_contract_num AS "anotherContractNum",
		client.name AS "client.name",
		a.is_total AS "isTotal",
		a.total_contract_id AS "totalContractId",
		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.alter_sign AS "alterSign",
		a.record_state AS "recordState",
		a.alterbefore_id AS "alterBeforeId",
		a.completion_status AS "completionStatus",
		(select name from work_contract_info where id  = a.total_contract_id) AS "totalContractName",
		a.common_flag AS "commonFlag",
		(select count(1) from work_contract_info c where c.total_contract_id = a.id)  cnt
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and a.alterbefore_id is not null
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="client != null and client.id != null and client.id != ''">
				AND a.client_id = #{client.id}
			</if>
			<if test="client != null and client.name != null and client.name != ''">
				AND client.name LIKE concat('%',#{client.name},'%')
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.contract_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<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="totalContractIds != null and totalContractIds != ''">
				AND a.total_contract_ids LIKE
				<if test="dbName == 'oracle'">'%'||#{totalContractIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{totalContractIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{totalContractIds},'%')</if>
			</if>
			<!--<if test="totalContractId != null and totalContractId != ''">
				AND a.total_contract_id =#{totalContractId}
			</if>-->

			<if test="contractPrice != '' and contractPrice != null">
				AND a.contract_price like concat('%',#{contractPrice},'%')
			</if>
			<if test="contractState != null and contractState != ''">
				AND a.contract_state = #{contractState}
			</if>
			<if test="contractNum != null and contractNum!= ''">
				AND a.contract_num like  concat('%',#{contractNum},'%')
			</if>
			<!--<if test="isTotal != null and isTotal != ''">
				AND a.is_total = #{isTotal}
			</if>-->
			<choose>
				<when test="queryCon != null and queryCon != ''">
					AND (
					find_in_set(#{queryCon},a.create_bys)
					${sqlMap.dsf}
					)
				</when>
				<otherwise>
					${sqlMap.dsf}
				</otherwise>
			</choose>
		</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="findLists" resultType="WorkContractInfo" >
		SELECT
		a.id AS "id",
		(select name from sys_user user where user.id=a.create_by) AS "createBy.name",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		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_num_path AS "contractNumPath",
		a.create_name AS "createName",
		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.office_id 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.process_instance_id AS "processInstanceId",
		a.company_id as companyId,
		a.contractFeeDoc AS "contractFeeDoc",
		a.contractTypeDoc AS "contractTypeDoc",
		a.another_contract_num AS "anotherContractNum",
		client.name AS "client.name",
		a.is_total AS "isTotal",
		a.total_contract_id AS "totalContractId",
		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.alter_state AS "alterState",
		a.alter_sign AS "alterSign",
		a.record_state AS "recordState",
		a.alterbefore_id AS "alterBeforeId",
		a.completion_status AS "completionStatus",
		a.construction_project_type AS "constructionProjectType",
		(select name from work_contract_info where id  = a.total_contract_id) AS "totalContractName",
		a.common_flag AS "commonFlag",
		(select count(1) from work_contract_info c where c.total_contract_id = a.id)  cnt
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id = #{id}
			</if>
			<if test="client != null and client.id != null and client.id != ''">
				AND a.client_id = #{client.id}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.contract_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<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="totalContractIds != null and totalContractIds != ''">
				AND a.total_contract_ids LIKE
				<if test="dbName == 'oracle'">'%'||#{totalContractIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{totalContractIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{totalContractIds},'%')</if>
			</if>
			<!--<if test="totalContractId != null and totalContractId != ''">
				AND a.total_contract_id =#{totalContractId}
			</if>-->

			<if test="contractPrice != '' and contractPrice != null">
				AND a.contract_price like concat('%',#{contractPrice},'%')
			</if>
			<if test="contractState != null and contractState != ''">
				AND a.contract_state = #{contractState}
			</if>
			<if test="contractNum != null and contractNum!= ''">
				AND a.contract_num = #{contractNum}
			</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.contract_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkContractInfo" >
		SELECT
			<include refid="workContractInfoColumns"/>
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		<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_info(
			id,
			project_id,
			project_name,
			ext,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			remarks,
			client_id,
			contract_date,
			effective_date,
			closing_date,
			name,
			contract_category,
			contract_num,
			contract_price,
			contract_opposite,
			contract_type,
			contract_fee,
			contract_special,
			contract_state,
			office_id,
			contract_file_id,
			contract_role,
			apply_view,
			process_instance_id,
			create_name,
			create_company,
			company_id,
			contractFeeDoc,
			contractTypeDoc,
			charge_company_name,
			another_contract_num,
			is_total,
			total_contract_id,
			is_total_name,
			total_contract_ids,
			common_flag,
			status,
			investment_scale,
			summary_payment,
			investment_amount,
			construction_project_type,
			create_bys,
			alter_state,
			alter_sign,
			record_state,
			alterbefore_id,
			completion_status,
			sort,
			signature_status,
			contract_num_path
		) VALUES (
			#{id},
			#{project.id},
			#{projectName},
			#{ext},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{remarks},
			#{client.id},
			#{contractDate},
			#{effectiveDate},
			#{closingDate},
			#{name},
			#{contractCategory},
			#{contractNum},
			#{contractPrice},
			#{contractOpposite},
			#{contractType},
			#{contractFee},
			#{contractSpecial},
			#{contractState},
			#{chargeCompany},
			#{contractFileId},
			#{contractRole},
			#{applyView},
			#{processInstanceId},
			#{createName},
			#{createCompany},
			#{companyId},
			#{contractFeeDoc},
			#{contractTypeDoc},
			#{chargeCompanyName},
			#{anotherContractNum},
			#{isTotal},
			#{totalContractId},
			#{isTotalName},
			#{totalContractIds},
			#{commonFlag},
			#{status},
			#{investmentScale},
			#{summaryPayment},
			#{investmentAmount},
			#{constructionProjectType},
			#{createBys},
			#{alterState},
			#{alterSign},
			#{recordState},
			#{alterBeforeId},
			#{completionStatus},
			#{sort},
			#{signatureStatus},
			#{contractNumPath}
		)
	</insert>
	
	<update id="update">
		UPDATE work_contract_info SET 	
			update_by = #{updateBy.id},
			project_id = #{project.id},
			project_name = #{projectName},
			ext = #{ext},
			update_date = #{updateDate},
			remarks = #{remarks},
			client_id = #{client.id},
			contract_date = #{contractDate},
			effective_date = #{effectiveDate},
			closing_date = #{closingDate},
			name = #{name},
			contract_category = #{contractCategory},
			contract_num = #{contractNum},
			contract_price = #{contractPrice},
			contract_opposite = #{contractOpposite},
			contract_type = #{contractType},
			contract_fee = #{contractFee},
			contract_special = #{contractSpecial},
			contract_state = #{contractState},
			office_id = #{chargeCompany},
			contract_file_id = #{contractFileId},
			contract_role = #{contractRole},
			apply_view = #{applyView},
			process_instance_id = #{processInstanceId},
			company_id = #{companyId},
			contractFeeDoc = #{contractFeeDoc},
			contractTypeDoc = #{contractTypeDoc},
			charge_company_name = #{chargeCompanyName},
			another_contract_num = #{anotherContractNum},
			is_total = #{isTotal},
			total_contract_id = #{totalContractId},
			status = #{status},
			investment_scale = #{investmentScale},
			summary_payment = #{summaryPayment},
			investment_amount = #{investmentAmount},
			construction_project_type = #{constructionProjectType},
			create_bys = #{createBys},
			invalidate_process_instance_id = #{invalidateProcessInstanceId},
			alter_process_instance_id = #{alterProcessInstanceId},
			invalidate_status = #{invalidateStatus},
			invalidate_user = #{invalidateUserId},
			alter_state = #{alterState},
			alter_sign = #{alterSign},
			record_state = #{recordState},
			alterbefore_id = #{alterBeforeId},
			completion_status = #{completionStatus},
			sort = #{sort},
			signature_status=#{signatureStatus},
			contract_num_path=#{contractNumPath}

		WHERE id = #{id}
	</update>


	<update id="updateId">
		UPDATE work_contract_info SET
		id = #{beforeId}
		WHERE id = #{id}
	</update>

	<update id="updateBeforeId">
		UPDATE work_contract_info SET
		alterbefore_id = #{beforeId}
		WHERE id = #{id}
	</update>
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_contract_info
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_contract_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>



	<insert id="inertContractManger">

		INSERT INTO work_contract_annex(
		id,
		create_by,
		create_date,
		update_by,
		remarks,
		contract_id,
		name,
		url
		)
		VALUES

		<foreach collection="list" item="item" index="index" separator="," >
			(#{item.id},#{item.createBy.id},#{item.createDate},#{item.updateBy.id},#{item.remarks},
			#{item.contractNoId},#{item.name},#{item.url})
		</foreach>

	</insert>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkContractInfo" statementType="STATEMENT">
		select * FROM work_contract_info  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByclient" resultType="WorkClientInfo">
		SELECT 
			a.*
		FROM work_client_info a
		LEFT JOIN work_client_linkman b on a.id = b.client_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="companyId != null and companyId != ''">
				AND  a.company_id = #{companyId}
			</if>
			<if test="workLinkName != null and workLinkName != ''">
				AND	b.name like concat('%',#{workLinkName},'%')
			</if>
			<if test="name != null and name != ''">
				AND a.name like concat('%',#{name},'%')
			</if>
			GROUP BY a.id
		</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="updateInvalidateStatusAndUser">
		<!-- 不要判空，因为有需要置空 -->
		UPDATE work_contract_info SET
		invalidate_status = #{invalidateStatus},
		invalidate_user = #{invalidateUserId}
		WHERE id = #{id}
	</update>
	<!-- 更新作废流程id -->
	<update id="updateInvalidateProcessInstanceId">
		UPDATE work_contract_info SET
		invalidate_process_instance_id = #{invalidateProcessInstanceId}
		WHERE id = #{id}
	</update>
	<!-- 更新作废流程id -->
	<update id="saveContractPrice">
		UPDATE work_contract_info SET
		contract_price = #{contractPrice}
		WHERE id = #{id}
	</update>

	<update id="updatealterProcessInstanceId">
		UPDATE work_contract_info SET
		alter_process_instance_id = #{alterProcessInstanceId}
		WHERE id = #{id}
	</update>
	<!-- 根据合同作废流程ID获取合同信息 -->
	<select id="getByInvalidateProcessInstanceId" resultType="WorkContractInfo">
		SELECT
		<include refid="workContractInfoColumns"/>
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		WHERE a.invalidate_process_instance_id = #{invalidateProcessInstanceId}
	</select>

	<!-- 根据合同作废流程ID获取合同信息 -->
	<select id="getByalterProcessInstanceId" resultType="WorkContractInfo">
		SELECT
		<include refid="workContractInfoColumns"/>
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		WHERE a.alter_process_instance_id = #{alterProcessInstanceId}
	</select>


	<!--更新审批流程id-->
	<update id="updateProcessInstanceId">
		UPDATE work_contract_info SET
		process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkContractInfo">
		SELECT
		<include refid="workContractInfoColumns"/>
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>




	<select id="findWaitAndFinishList" parameterType="map" resultType="WorkContractInfo">
		SELECT
		a.id AS "id",
		(select name from sys_user user where user.id=a.create_by) AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		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_num_path as "contractNumPath",
		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.office_id AS "chargeCompany",
		a.contract_file_id AS "contractFileId",
		a.contract_role AS "contractRole",
		a.apply_view AS "applyView",
		a.process_instance_id AS "processInstanceId",
		a.company_id as companyId,
		client.name AS "client.name",
		a.contractTypeDoc,
		a.contractFeeDoc,
		a.charge_company_name AS "chargeCompanyName",
		a.another_contract_num AS "anotherContractNum",
		client.name AS "client.name",
		a.is_total AS "isTotal",
		a.total_contract_id AS "totalContractId"
		FROM work_contract_info a
		<include refid="workContractInfoJoins"/>
		WHERE
			process_instance_id in (${procInsIds})

	</select>
	<delete id="deleteFile">
		DELETE FROM work_contract_annex
		WHERE contract_id = #{id}
	</delete>

	<select id="findByList" resultType="WorkContractInfo">
		SELECT
		a.id,a.sort
		FROM work_contract_info a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND (
			a.total_contract_id = #{totalContractId} OR a.id= #{totalContractId})
		</where>
		ORDER BY a.sort DESC
	</select>
    <!--月计划管理选择已签未完成的合同-->
	<select id="findWorkContractInfoPage" resultType="WorkContractInfo">
			SELECT
			a.id AS "id",
			a.contract_date AS "contractDate",
			a.name AS "name",
			a.contract_num AS "contractNum",
			a.contract_price AS "contractPrice",
			a.contract_type AS "contractType",
			c.name AS "client.name",
			a.construction_project_type AS "constructionProjectType"
			FROM work_contract_info a
		   left join work_client_info c on c.id=a.client_id
			<where>
				a.del_flag = #{DEL_FLAG_NORMAL}
				<if test="client != null and client.name != null and client.name != ''">
					AND c.name LIKE concat('%',#{client.name},'%')
				</if>
				<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
					AND a.contract_date BETWEEN #{beginContractDate} AND #{endContractDate}
				</if>
				<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="contractPrice != '' and contractPrice != null">
					AND a.contract_price like concat('%',#{contractPrice},'%')
				</if>
				<if test="contractNum != null and contractNum!= ''">
					AND a.contract_num like  concat('%',#{contractNum},'%')
				</if>
				AND a.contract_state = '5'
				and a.office_id=#{officeId}
			</where>
			<choose>
				<when test="page !=null and page.orderBy != null and page.orderBy != ''">
					ORDER BY ${page.orderBy}
				</when>
				<otherwise>
					ORDER BY a.contract_date DESC
				</otherwise>
			</choose>
		</select>

    <select id="countByOffice" resultType="int">
        SELECT count(1) FROM work_contract_info a WHERE a.office_id = #{officeId} AND a.del_flag='0'
    </select>
	<update id="updateStatus">
		UPDATE work_contract_info SET
		contract_state = #{contractState}
		WHERE id = #{id}
	</update>

</mapper>