<?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.workbidingdocument.dao.WorkBidingDocumentDao">
    
	<sql id="workBidingDocumentColumns">
		a.id AS "id",
		a.work_bid_project_id AS "workBidProject.id",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		a.quote AS "quote",
		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.company_id AS "companyId",
		a.office_id AS "officeId",
		a.sender AS "sender",
		a.bid_status AS "bidStatus"
	</sql>
	
	<sql id="workBidingDocumentJoins">
		LEFT JOIN work_bid_project workBidProject ON workBidProject.id = a.work_bid_project_id
	</sql>

	<select id="getByProcessInstanceId" resultType="WorkBidingDocument">
		SELECT
		<include refid="workBidingDocumentColumns"/>
		FROM work_biding_document a
		<include refid="workBidingDocumentJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

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

	<select id="getListOfAttachment" resultType="com.jeeplus.modules.sys.entity.Workattachment">
		SELECT
			t.id AS "id",
			t.create_by AS "createBy.id",
			t.create_date AS "createDate",
			t.update_by AS "updateBy.id",
			t.update_date AS "updateDate",
			t.remarks AS "remarks",
			t.del_flag AS "delFlag",
			t.url AS "url",
			t.type AS "type",
			t.attachment_id AS "attachmentId",
			u.name AS "attachmentUser",
			t.attachment_name AS "attachmentName",
			t.attachment_flag AS "attachmentFlag"
		FROM work_attachment t
		INNER JOIN sys_user u on t.attachment_user = u.id
		WHERE t.attachment_id = #{attachmentId}
	</select>

	<insert id="insertProjectMemberOfWorkProjectUser" parameterType="java.util.Map">
		INSERT INTO work_project_user
		(user_id , project_id , is_master)
		VALUES
		<foreach collection="memberIds" item="str" separator=",">
			(#{str} , #{workBidProjectId} , '0')
		</foreach>
	</insert>
	<delete id="deleteProjectMemberOfWorkProjectUser">
		DELETE FROM work_project_user
		WHERE project_id = #{workBidProjectId}
		AND is_master = '0'
	</delete>
	<update id="updateMemberOfWorkBidProject">
		UPDATE work_bid_project
		SET project_member = #{projectMember}
		WHERE id = #{workBidProjectId}
	</update>
	<update id="updateMasterAndMemberOfWorkBidProject">
		UPDATE work_bid_project
		SET project_master = #{projectMaster}, project_member = #{projectMember}
		WHERE id = #{id}
	</update>
	<update id="updateMasterOfWorkBidProject">
		UPDATE work_bid_project
		SET project_master = #{projectMaster}
		WHERE id = #{workBidProjectId}
	</update>
	<delete id="deleteProjectMasterOfWorkProjectUser">
		DELETE FROM work_project_user
		WHERE project_id = #{workBidProjectId}
		AND is_master = '1'
	</delete>
	<insert id="insertProjectMasterOfWorkProjectUser" parameterType="java.util.Map">
		INSERT INTO work_project_user
		(user_id , project_id , is_master)
		VALUES 
		<foreach collection="masterIds" item="str" separator="," >
			(#{str} , #{workBidProjectId} , '1')
		</foreach>
	</insert>
	<select id="getByWorkBidProject" resultType="WorkBidingDocument">
		SELECT
			<include refid="workBidingDocumentColumns"></include>
		FROM work_biding_document a
		WHERE a.work_bid_project_id = #{id}
	</select>

	<update id="updateBidStatus" parameterType="WorkBidingDocument">
		UPDATE work_biding_document SET bid_status = '1'
		WHERE id = #{id}
	</update>
	<select id="findConfirmlistWithWorkBidProject" resultType="WorkBidingDocument">
		SELECT
		a.id AS "id",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		a.quote AS "quote",
		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.company_id AS "companyId",
		a.office_id AS "officeId",
		a.sender AS "sender",
		a.bid_status AS "bidStatus",
		w.id AS "workBidProject.id",
		w.project_number AS "workBidProject.projectNumber",
		w.project_name AS "workBidProject.projectName",
		w.project_type AS "workBidProject.projectType",
		w.type_explain AS "workBidProject.typeExplain",
		w.project_source AS "workBidProject.projectSource",
		w.source_explain AS "workBidProject.sourceExplain",
		w.is_ext AS "workBidProject.isExt",
		w.ext_company AS "workBidProject.extCompany",
		w.contacts AS "workBidProject.contacts",
		w.registration_date AS "workBidProject.registrationDate",
		w.bid_date AS "workBidProject.bidDate",
		w.trial_date AS "workBidProject.trialDate",
		w.trial_type AS "workBidProject.trialType",
		w.document_cost AS "workBidProject.documentCost",
		w.purchase_date AS "workBidProject.purchaseDate",
		w.bind_cost AS "workBidProject.bindCost",
		w.other_cost AS "workBidProject.otherCost",
		w.bid_bond AS "workBidProject.bidBond",
		w.bond_style AS "workBidProject.bondStyle",
		w.refund_date AS "workBidProject.refundDate",
		w.offer AS "workBidProject.offer",
		w.is_bid AS "workBidProject.isBid",
		w.implementing_dept AS "workBidProject.implementingDept.id",
		w.is_subcontract AS "workBidProject.isSubcontract",
		w.subcontract_company AS "workBidProject.subcontractCompany",
		w.project_situation AS "workBidProject.projectSituation",
		w.status AS "workBidProject.status",
		w.create_by AS "workBidProject.createBy.id",
		w.create_date AS "workBidProject.createDate",
		w.update_by AS "workBidProject.updateBy.id",
		w.update_date AS "workBidProject.updateDate",
		w.del_flag AS "workBidProject.delFlag",
		bidCompany.name AS "workBidProject.bidCompany.name",
		w.project_master AS "workBidProject.projectMaster",
		w.project_member AS "workBidProject.projectMember",
		u.name AS "workBidProject.createBy.name",
		w.company_id AS "workBidProject.companyId",
		w.office_id AS "workBidProject.officeId"
		FROM  work_biding_document a
		LEFT JOIN work_bid_project w ON a.work_bid_project_id = w.id
		LEFT JOIN work_client_info bidCompany ON bidCompany.id = w.bid_company
		LEFT JOIN sys_office implementingDept ON implementingDept.id = w.implementing_dept
		LEFT JOIN sys_user u ON u.id = w.create_by
		LEFT JOIN sys_office o ON o.id = u.office_id
		<where>
			w.del_flag = #{DEL_FLAG_NORMAL}
			AND a.status = '3'
			<if test="name !=null and name !=''">
				AND a.sender = #{name}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="workBidProject != null and workBidProject.projectNumber != null and workBidProject.projectNumber != ''">
				AND w.project_number LIKE
				<if test="dbName == 'oracle'">'%'||#{workBidProject.projectNumber}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workBidProject.projectNumber}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workBidProject.projectNumber},'%')</if>
			</if>
			<if test="workBidProject != null and workBidProject.projectName != null and workBidProject.projectName != ''">
				AND w.project_name LIKE
				<if test="dbName == 'oracle'">'%'||#{workBidProject.projectName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workBidProject.projectName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workBidProject.projectName},'%')</if>
			</if>
			<if test="workBidProject != null and workBidProject.projectType != null and workBidProject.projectType != ''">
				AND w.project_type = #{workBidProject.projectType}
			</if>
			<if test="workBidProject != null and workBidProject.trialDate != null and workBidProject.trialDate != ''">
				AND w.trial_date = #{workBidProject.trialDate}
			</if>
			<if test="workBidProject != null and workBidProject.isSubcontract != null and workBidProject.isSubcontract != ''">
				AND w.is_subcontract = #{workBidProject.isSubcontract}
			</if>
			<if test="workBidProject != null and workBidProject.status != null and workBidProject.status != ''">
				AND w.status = #{workBidProject.status}
			</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="findMaintainlistWithWorkBidProject" resultType="WorkBidingDocument">
		SELECT
		a.id AS "id",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		a.quote AS "quote",
		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.company_id AS "companyId",
		a.office_id AS "officeId",
		a.sender AS "sender",
		a.bid_status AS "bidStatus",
		w.id AS "workBidProject.id",
		w.project_number AS "workBidProject.projectNumber",
		w.project_name AS "workBidProject.projectName",
		w.project_type AS "workBidProject.projectType",
		w.type_explain AS "workBidProject.typeExplain",
		w.project_source AS "workBidProject.projectSource",
		w.source_explain AS "workBidProject.sourceExplain",
		w.is_ext AS "workBidProject.isExt",
		w.ext_company AS "workBidProject.extCompany",
		w.contacts AS "workBidProject.contacts",
		w.registration_date AS "workBidProject.registrationDate",
		w.bid_date AS "workBidProject.bidDate",
		w.trial_date AS "workBidProject.trialDate",
		w.trial_type AS "workBidProject.trialType",
		w.document_cost AS "workBidProject.documentCost",
		w.purchase_date AS "workBidProject.purchaseDate",
		w.bind_cost AS "workBidProject.bindCost",
		w.other_cost AS "workBidProject.otherCost",
		w.bid_bond AS "workBidProject.bidBond",
		w.bond_style AS "workBidProject.bondStyle",
		w.refund_date AS "workBidProject.refundDate",
		w.offer AS "workBidProject.offer",
		w.is_bid AS "workBidProject.isBid",
		w.implementing_dept AS "workBidProject.implementingDept.id",
		w.is_subcontract AS "workBidProject.isSubcontract",
		w.subcontract_company AS "workBidProject.subcontractCompany",
		w.project_situation AS "workBidProject.projectSituation",
		w.status AS "workBidProject.status",
		w.create_by AS "workBidProject.createBy.id",
		w.create_date AS "workBidProject.createDate",
		w.update_by AS "workBidProject.updateBy.id",
		w.update_date AS "workBidProject.updateDate",
		w.del_flag AS "workBidProject.delFlag",
		bidCompany.name AS "workBidProject.bidCompany.name",
		w.project_master AS "workBidProject.projectMaster",
		w.project_member AS "workBidProject.projectMember",
		u.name AS "workBidProject.createBy.name",
		w.company_id AS "workBidProject.companyId",
		w.office_id AS "workBidProject.officeId"
		FROM  work_biding_document a
		LEFT JOIN work_bid_project w ON a.work_bid_project_id = w.id
		LEFT JOIN work_client_info bidCompany ON bidCompany.id = w.bid_company
		LEFT JOIN sys_office implementingDept ON implementingDept.id = w.implementing_dept
		LEFT JOIN sys_user u ON u.id = w.create_by
		LEFT JOIN sys_office o ON o.id = u.office_id
		<where>
			w.del_flag = #{DEL_FLAG_NORMAL}
			AND a.status = '3'
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="officeId != null and officeId !=''">
				AND a.office_id = #{officeId}
			</if>
			<if test="workBidProject != null and workBidProject.projectNumber != null and workBidProject.projectNumber != ''">
				AND w.project_number LIKE
				<if test="dbName == 'oracle'">'%'||#{workBidProject.projectNumber}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workBidProject.projectNumber}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workBidProject.projectNumber},'%')</if>
			</if>
			<if test="workBidProject != null and workBidProject.projectName != null and workBidProject.projectName != ''">
				AND w.project_name LIKE
				<if test="dbName == 'oracle'">'%'||#{workBidProject.projectName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workBidProject.projectName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workBidProject.projectName},'%')</if>
			</if>
			<if test="workBidProject != null and workBidProject.projectType != null and workBidProject.projectType != ''">
				AND w.project_type = #{workBidProject.projectType}
			</if>
			<if test="workBidProject != null and workBidProject.trialDate != null and workBidProject.trialDate != ''">
				AND w.trial_date = #{workBidProject.trialDate}
			</if>
			<if test="workBidProject != null and workBidProject.isSubcontract != null and workBidProject.isSubcontract != ''">
				AND w.is_subcontract = #{workBidProject.isSubcontract}
			</if>
			<if test="workBidProject != null and workBidProject.status != null and workBidProject.status != ''">
				AND w.status = #{workBidProject.status}
			</if>
			${sqlMap.dsf}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY w.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findlistWithWorkBidProject" resultType="WorkBidingDocument">
		SELECT
			workBidingDocument.id AS "id",
			workBidingDocument.status AS "status",
			workBidingDocument.process_instance_id AS "processInstanceId",
			workBidingDocument.quote AS "quote",
			workBidingDocument.create_by AS "createBy.id",
			workBidingDocument.create_date AS "createDate",
			workBidingDocument.update_by AS "updateBy.id",
			workBidingDocument.update_date AS "updateDate",
			workBidingDocument.remarks AS "remarks",
			workBidingDocument.del_flag AS "delFlag",
			workBidingDocument.company_id AS "companyId",
			workBidingDocument.office_id AS "officeId",
			workBidingDocument.sender AS "sender",
			workBidingDocument.bid_status AS "bidStatus",
			a.id AS "workBidProject.id",
			a.project_number AS "workBidProject.projectNumber",
			a.project_name AS "workBidProject.projectName",
			a.project_type AS "workBidProject.projectType",
			a.type_explain AS "workBidProject.typeExplain",
			a.project_source AS "workBidProject.projectSource",
			a.source_explain AS "workBidProject.sourceExplain",
			a.is_ext AS "workBidProject.isExt",
			a.ext_company AS "workBidProject.extCompany",
			a.contacts AS "workBidProject.contacts",
			a.registration_date AS "workBidProject.registrationDate",
			a.bid_date AS "workBidProject.bidDate",
			a.trial_date AS "workBidProject.trialDate",
			a.trial_type AS "workBidProject.trialType",
			a.document_cost AS "workBidProject.documentCost",
			a.purchase_date AS "workBidProject.purchaseDate",
			a.bind_cost AS "workBidProject.bindCost",
			a.other_cost AS "workBidProject.otherCost",
			a.bid_bond AS "workBidProject.bidBond",
			a.bond_style AS "workBidProject.bondStyle",
			a.refund_date AS "workBidProject.refundDate",
			a.offer AS "workBidProject.offer",
			a.is_bid AS "workBidProject.isBid",
			a.implementing_dept AS "workBidProject.implementingDept.id",
			a.is_subcontract AS "workBidProject.isSubcontract",
			a.subcontract_company AS "workBidProject.subcontractCompany",
			a.project_situation AS "workBidProject.projectSituation",
			a.status AS "workBidProject.status",
			a.create_by AS "workBidProject.createBy.id",
			a.create_date AS "workBidProject.createDate",
			a.update_by AS "workBidProject.updateBy.id",
			a.update_date AS "workBidProject.updateDate",
			/*w.remarks AS "workBidProject.remarks",*/
			a.del_flag AS "workBidProject.delFlag",
			bidCompany.name AS "workBidProject.bidCompany.name",
			a.project_master AS "workBidProject.projectMaster",
			a.project_member AS "workBidProject.projectMember",
			u.name AS "workBidProject.createBy.name",
			a.company_id AS "workBidProject.companyId",
			a.office_id AS "workBidProject.officeId"
		FROM work_bid_project a
		LEFT JOIN work_client_info bidCompany ON bidCompany.id = a.bid_company
		LEFT JOIN sys_office implementingDept ON implementingDept.id = a.implementing_dept
		LEFT JOIN sys_user u ON u.id = a.create_by
		JOIN sys_office o ON o.id = a.office_id
		JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN work_biding_document workBidingDocument ON workBidingDocument.work_bid_project_id = a.id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="status != null and status != ''">
				<choose>
					<when test="status == 0">
						AND workBidingDocument.status is NULL
					</when>
					<otherwise>
						AND workBidingDocument.status = #{status}
					</otherwise>
				</choose>
			</if>
			<if test="workBidProject != null and workBidProject.projectNumber != null and workBidProject.projectNumber != ''">
				AND a.project_number LIKE
				<if test="dbName == 'oracle'">'%'||#{workBidProject.projectNumber}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workBidProject.projectNumber}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workBidProject.projectNumber},'%')</if>
			</if>
			<if test="workBidProject != null and workBidProject.projectName != null and workBidProject.projectName != ''">
				AND a.project_name LIKE
				<if test="dbName == 'oracle'">'%'||#{workBidProject.projectName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{workBidProject.projectName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{workBidProject.projectName},'%')</if>
			</if>
			<if test="workBidProject != null and workBidProject.projectType != null and workBidProject.projectType != ''">
				AND a.project_type = #{workBidProject.projectType}
			</if>
			<if test="workBidProject != null and workBidProject.trialDate != null and workBidProject.trialDate != ''">
				AND a.trial_date = #{workBidProject.trialDate}
			</if>
			<if test="workBidProject != null and workBidProject.isSubcontract != null and workBidProject.isSubcontract != ''">
				AND a.is_subcontract = #{workBidProject.isSubcontract}
			</if>
			<if test="workBidProject != null and workBidProject.status != null and workBidProject.status != ''">
				AND a.status = #{workBidProject.status}
			</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="get" resultType="WorkBidingDocument" >
		SELECT 
			<include refid="workBidingDocumentColumns"/>
		FROM work_biding_document a
		<include refid="workBidingDocumentJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findAllList" resultType="WorkBidingDocument" >
		SELECT 
			<include refid="workBidingDocumentColumns"/>
		FROM work_biding_document a
		<include refid="workBidingDocumentJoins"/>
		<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_document(
			id,
			work_bid_project_id,
			status,
			process_instance_id,
			quote,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			sender,
			bid_status
		) VALUES (
			#{id},
			#{workBidProject.id},
			#{status},
			#{processInstanceId},
			#{quote},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{sender},
			#{bidStatus}
		)
	</insert>
	
	<update id="update">
		UPDATE work_biding_document SET 	
			work_bid_project_id = #{workBidProject.id},
			status = #{status},
			process_instance_id = #{processInstanceId},
			quote = #{quote},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			sender = #{sender},
			bid_status = #{bidStatus}
		WHERE id = #{id}
	</update>
	
	<update id="updateStatus">
		UPDATE work_biding_document SET status = #{status}
		WHERE id = #{id}
	</update>
	
	<update id="updateSender">
		UPDATE work_biding_document SET sender = #{senderId}
		WHERE id = #{workBidingDocumentId}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_biding_document
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_biding_document SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkBidingDocument" statementType="STATEMENT">
		select * FROM work_biding_document  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByworkBidProject" resultType="WorkBidProject">
		SELECT 
			*
		FROM work_bid_project a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<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>
		</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>
</mapper>