<?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.workbidingproject.dao.WorkBidingProjectDao">
    
	<sql id="workBidingProjectColumns">
		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.del_flag AS "delFlag",
		a.remarks AS "remarks",
		a.client_id AS "client.id",
		a.contract_id AS "contract.id",
		a.bidding_type AS "biddingType",
		a.company_id AS "companyId",
		a.office_id AS "office.id",
		a.batch AS "batch",
		a.psort AS "psort",
		a.ssort AS "ssort",
		a.investment_amount AS "investmentAmount",
		a.project_number AS "projectNumber",
		a.project_name AS "projectName",
		a.control_price AS "controlPrice",
		a.is_control_price AS "isControlPrice",
		a.demand_office AS "demandOffice.id",
		a.job_location AS "area.id",
		a.supervisory_office AS "supervisoryOffice",
		a.capital_attribute AS "capitalAttribute",
		a.project_start_date AS "projectStartDate",
		a.project_end_date AS "projectEndDate",
		a.scope_of_bidding AS "scopeOfBidding",
		a.bid_evaluation_method AS "bidEvaluationMethod",
		a.investor_qualification AS "investorQualification",
		a.host_project_qualification AS "hostProjectQualification",
		a.other_requirement AS "otherRequirement",
		a.project_master AS "projectMaster",
		a.project_member AS "projectMember",
		a.inventor_user AS "inventorUser",
		a.status AS "status",
		o.name AS "office.name",
		ce.name AS "client.name",
		a.work_project_id AS "workProject.id"
	</sql>

	<sql id="workBidingProjectJoins">
		JOIN sys_office o ON o.id = a.office_id
		JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN  work_client_info ce ON ce.id = a.client_id
		LEFT JOIN  work_contract_info ca ON ca.id = a.contract_id
	</sql>
	
    
	<select id="get" resultType="WorkBidingProject" >
		SELECT 
			<include refid="workBidingProjectColumns"/>,
		ca.contract_num AS "contract.contractNum",
		ca.name AS "contract.name"
		FROM work_biding_project a
		<include refid="workBidingProjectJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkBidingProject" >
		SELECT 
			<include refid="workBidingProjectColumns"/>
		FROM work_biding_project a
		<include refid="workBidingProjectJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="client != null and client.id != null and client.id != ''">
				AND a.client_id LIKE 
					<if test="dbName == 'oracle'">'%'||#{client.id}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{client.id}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{client.id},'%')</if>
			</if>
			<if test="contract != null and contract.id != null and contract.id != ''">
				AND a.contract_id LIKE 
					<if test="dbName == 'oracle'">'%'||#{contract.id}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{contract.id}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{contract.id},'%')</if>
			</if>
			<if test="area != null and area.id != null and area.id != ''">
				AND (a.job_location = #{area.id} OR find_in_set(#{area.id},a.area_parent_ids))
			</if>
			<if test="psort != null and psort != ''">
				AND a.psort = #{psort}
			</if>
			<if test="ssort != null and ssort != ''">
				AND a.ssort = #{ssort}
			</if>
			<if test="biddingType != null and biddingType != ''">
				AND a.bidding_type = #{biddingType}
			</if>
			<if test="projectNumber != null and projectNumber != ''">
				AND a.project_number LIKE 
					<if test="dbName == 'oracle'">'%'||#{projectNumber}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{projectNumber}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{projectNumber},'%')</if>
			</if>
			<if test="projectName != null and projectName != ''">
				AND a.project_name LIKE 
					<if test="dbName == 'oracle'">'%'||#{projectName}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{projectName}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{projectName},'%')</if>
			</if>
			<choose>
				<when test="queryCon != null and queryCon != ''">
					AND (
					find_in_set(#{queryCon},a.project_master)
					or
					find_in_set(#{queryCon},a.project_member)
					OR a.create_by = #{queryCon}
					${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="findAllList" resultType="WorkBidingProject" >
		SELECT 
			<include refid="workBidingProjectColumns"/>
		FROM work_biding_project a
		<include refid="workBidingProjectJoins"/>
		<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_biding_project(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			remarks,
			client_id,
			contract_id,
			bidding_type,
			company_id,
			office_id,
			batch,
			project_number,
			project_name,
			control_price,
			is_control_price,
			demand_office,
			job_location,
			area_parent_ids,
			supervisory_office,
			capital_attribute,
			project_start_date,
			project_end_date,
			scope_of_bidding,
			bid_evaluation_method,
			investor_qualification,
			host_project_qualification,
			other_requirement,
			project_master,
			project_member,
			investment_amount,
			status,
			inventor_user,
			psort,
			ssort,
			work_project_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{remarks},
			#{client.id},
			#{contract.id},
			#{biddingType},
			#{companyId},
			#{office.id},
			#{batch},
			#{projectNumber},
			#{projectName},
			#{controlPrice},
			#{isControlPrice},
			#{demandOffice.id},
			#{area.id},
			#{area.parentIds},
			#{supervisoryOffice},
			#{capitalAttribute},
			#{projectStartDate},
			#{projectEndDate},
			#{scopeOfBidding},
			#{bidEvaluationMethod},
			#{investorQualification},
			#{hostProjectQualification},
			#{otherRequirement},
			#{projectMaster},
			#{projectMember},
			#{investmentAmount},
			#{status},
			#{inventorUser},
			#{psort},
	 		#{ssort},
			#{workProject.id}
		)
	</insert>
	
	<update id="update">
		UPDATE work_biding_project SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			client_id = #{client.id},
			contract_id = #{contract.id},
			bidding_type = #{biddingType},
			company_id = #{companyId},
			office_id = #{office.id},
			batch = #{batch},
			project_number = #{projectNumber},
			project_name = #{projectName},
			control_price = #{controlPrice},
			is_control_price = #{isControlPrice},
			demand_office = #{demandOffice.id},
			job_location = #{area.id},
			supervisory_office = #{supervisoryOffice},
			capital_attribute = #{capitalAttribute},
			project_start_date = #{projectStartDate},
			project_end_date = #{projectEndDate},
			scope_of_bidding = #{scopeOfBidding},
			bid_evaluation_method = #{bidEvaluationMethod},
			investor_qualification = #{investorQualification},
			host_project_qualification = #{hostProjectQualification},
			other_requirement = #{otherRequirement},
			project_master = #{projectMaster},
			area_parent_ids = #{area.parentIds},
			project_member = #{projectMember},
			investment_amount = #{investmentAmount},
			status = #{status},
			inventor_user = #{inventorUser},
			psort = #{psort},
			ssort = #{ssort},
			work_project_id = #{workProject.id}
		WHERE id = #{id}
	</update>
	<update id="updateStatus">
		UPDATE work_biding_project SET
		status = #{status}
		WHERE id = #{id}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_biding_project
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_biding_project SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkBidingProject" statementType="STATEMENT">
		SELECT
		<include refid="workBidingProjectColumns"/>,
		ca.contract_num AS "contract.contractNum",
		ca.name AS "contract.name"
		FROM work_biding_project a
		<include refid="workBidingProjectJoins"/>
		where ${propertyName} = '${value}'
	</select>
	
</mapper>