<?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.workprequalification.dao.WorkPrequalificationDao">
    
	<sql id="workPrequalificationColumns">
		a.id AS "id",
		a.number AS "number",
		a.project_name AS "projectName",
		a.project_type AS "projectType",
		a.project_source AS "projectSource",
		a.is_external_contacts AS "isExternalContacts",
		a.bidding_company AS "biddingCompany.id",
		a.contacts AS "contacts.id",
		a.ext_company AS "extCompany",
		a.ext_contacts AS "extContacts",
		a.prequalification_date AS "prequalificationDate",
		a.registration_date AS "registrationDate",
		a.is_subcontract AS "isSubcontract",
		a.project_member AS "projectMember",
		a.purchase_date AS "purchaseDate",
		a.document_cost AS "documentCost",
		a.bind_cost AS "bindCost",
		a.project_situation AS "projectSituation",
		a.status AS "status",
		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",
		biddingCompany.name AS "biddingCompany.name",
		contacts.name AS "contacts.name"
	</sql>
	
	<sql id="workPrequalificationJoins">
		LEFT JOIN work_client_info biddingCompany ON biddingCompany.id = a.bidding_company
		LEFT JOIN sys_user contacts ON contacts.id = a.contacts
	</sql>
	
    
	<select id="get" resultType="WorkPrequalification" >
		SELECT 
			<include refid="workPrequalificationColumns"/>
		FROM work_prequalification a
		<include refid="workPrequalificationJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkPrequalification" >
		SELECT 
			<include refid="workPrequalificationColumns"/>
		FROM work_prequalification a
		<include refid="workPrequalificationJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="projectName != null and projectName != ''">
				AND a.project_name = #{projectName}
			</if>
		</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="WorkPrequalification" >
		SELECT 
			<include refid="workPrequalificationColumns"/>
		FROM work_prequalification a
		<include refid="workPrequalificationJoins"/>
		<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_prequalification(
			id,
			number,
			project_name,
			project_type,
			project_source,
			is_external_contacts,
			bidding_company,
			contacts,
			ext_company,
			ext_contacts,
			prequalification_date,
			registration_date,
			is_subcontract,
			project_member,
			purchase_date,
			document_cost,
			bind_cost,
			project_situation,
			status,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{number},
			#{projectName},
			#{projectType},
			#{projectSource},
			#{isExternalContacts},
			#{biddingCompany.id},
			#{contacts.id},
			#{extCompany},
			#{extContacts},
			#{prequalificationDate},
			#{registrationDate},
			#{isSubcontract},
			#{projectMember},
			#{purchaseDate},
			#{documentCost},
			#{bindCost},
			#{projectSituation},
			#{status},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE work_prequalification SET 	
			number = #{number},
			project_name = #{projectName},
			project_type = #{projectType},
			project_source = #{projectSource},
			is_external_contacts = #{isExternalContacts},
			bidding_company = #{biddingCompany.id},
			contacts = #{contacts.id},
			ext_company = #{extCompany},
			ext_contacts = #{extContacts},
			prequalification_date = #{prequalificationDate},
			registration_date = #{registrationDate},
			is_subcontract = #{isSubcontract},
			project_member = #{projectMember},
			purchase_date = #{purchaseDate},
			document_cost = #{documentCost},
			bind_cost = #{bindCost},
			project_situation = #{projectSituation},
			status = #{status},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_prequalification
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_prequalification SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkPrequalification" statementType="STATEMENT">
		select * FROM work_prequalification  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListBybiddingCompany" resultType="WorkClientInfo">
		SELECT 
			*
		FROM work_client_info a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id LIKE 
				<if test="dbName == 'oracle'">#{id}||'%'</if>
				<if test="dbName == 'mssql'">#{id}+'%'</if>
				<if test="dbName == 'mysql'">concat(#{id},'%')</if>
			</if>
		</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="getProjectMember" resultType="java.lang.String">
		select user_id FROM work_project_user WHERE project_id = #{id} and is_master <![CDATA[!=]]> '1'
	</select>
	
	<select id="getProjectMaster" resultType="java.lang.String">
		select user_id FROM work_project_user WHERE project_id = #{id} and is_master = '1'
	</select>
	
		<!-- 删除用户和资格预审工程关联表数据 -->
	<delete id="deleteProjectUser">
		DELETE FROM work_project_user WHERE project_id = #{id}
	</delete>
	
			<!-- 删除负责人用户和资格预审工程关联表数据 -->
	<delete id="deleteProjectMaster">
		DELETE FROM work_project_user WHERE project_id = #{id} and is_master ='1'
	</delete>
	
				<!-- 删除成员用户和资格预审工程关联表数据 -->
	<delete id="deleteProjectMember">
		DELETE FROM work_project_user WHERE project_id = #{id} and is_master  <![CDATA[!=]]> '1'
	</delete>
	
	<!-- 插入成员用户和资格预审工程关联表数据 -->
	<insert id="insertProjectMember">
		INSERT INTO work_project_user(project_id,user_id,is_master)
		<foreach collection="memberList" item="user" separator=" union all ">
			SELECT #{id}, #{user.id} ,'0' FROM dual
		</foreach>
	</insert>
	
		<!-- 插入负责人用户和资格预审工程关联表数据 -->
	<insert id="insertProjectMaster">
		INSERT INTO work_project_user(project_id,user_id,,is_master)
		<foreach collection="masterList" item="user" separator=" union all ">
			SELECT #{id}, #{user.id},'1' FROM dual
		</foreach>
	</insert>
</mapper>