<?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.WorkContractListInfoDao">
    
	<sql id="workContractListInfoColumns">
		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.ext AS "ext",
		p.project_name AS "projectName",
		p.project_number AS "projectNum",
		a.del_flag AS "delFlag",
		a.remarks AS "remarks",
		a.contract_date AS "contractDate",
		a.effective_date AS "effectiveDate",
		a.closing_date AS "closingDate",
		a.name AS "name",
		a.contract_num AS "contractNum",
		a.contract_price AS "contractPrice",
		a.another_contract_num AS "anotherContractNum",
		a.contract_type AS "contractType",
		a.contract_special AS "contractSpecial",
		a.contract_state AS "contractState",
		a.charge_company_name AS "officeName",
		a.record_state AS "recordState",
		a.summary_payment AS "summaryPayment",
		a.investment_amount AS "investmentAmount",
		b.name AS "totalContractName",
		a.contractTypeDoc AS "contractTypeDoc",
		(SELECT GROUP_CONCAT(`name`) FROM work_client_info c
         WHERE
	     c.id IN (
		 SELECT
			w.client_id
		FROM
			`work_contract_client` w
		WHERE
			w.contract_id = a.id and w.del_flag='0'
	)) as "clientName"

	</sql>
	
	<sql id="workContractListInfoJoins">
		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
		LEFT JOIN work_contract_info b ON b.id = a.total_contract_id
	</sql>

	<select id="findList" resultType="WorkContractListInfo" >
		SELECT
		<include refid="workContractListInfoColumns"/>
		FROM work_contract_info a
		<include refid="workContractListInfoJoins"/>
		<where>
			a.del_flag = '0' and (a.alter_state is null or a.alter_state ="")
			<if test="id != null and id != ''">
				AND a.id = #{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="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>
			${sqlMap.dsf}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
                group by a.id
				ORDER BY a.contract_date DESC
			</otherwise>
		</choose>
	</select>
</mapper>