<?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.workinvoicealter.dao.WorkInvoiceAlterDao">
    
	<sql id="workInvoiceColumns">
		a.id AS "id",
		a.number AS "number",
		a.process_instance_id AS "processInstanceId",
		a.client_id AS "client.id",
		a.project_id AS "project.id",
		a.project_name AS "projectName",
		a.money AS "money",
		a.invoice_type AS "invoiceType",
		a.charge_type AS "chargeType",
		a.content AS "content",
		a.drawer_id AS "drawer.id",
		a.office_id AS "office.id",
		a.invoice_number AS "invoiceNumber",
		a.invoice_date AS "invoiceDate",
		a.drawer_name AS "drawerName",
		a.take_date AS "takeDate",
		a.invoice_remarks AS "invoiceRemarks",
		a.is_invoice AS "isInvoice",
		a.is_charge AS "isCharge",
		a.is_invalid AS "isInvalid",
		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.orUnicode AS "orUnicode",
		a.address AS "address",
		a.telephone AS "telephone",
		a.bank AS "bank",
		a.bank_number AS "bankNumber",
		a.cancle_reason AS "cancaleReason",
		u.name AS "drawer.name",
		so.top_company AS "office.name",
		w.name AS "client.name",
		p.project_name AS "project.projectName",
		a.invoice_state AS "invoiceState",
		p.project_id AS "project.projectId",
		ci.name AS "project.workContractInfo.name",
		w.usc_code AS "client.uscCode",
		w.fax AS "client.fax",
		w.or_unicode AS "client.orUnicode",
		w.telephone AS "client.telephone",
		a.company_id AS "companyId",
		a.cancle_reason AS "cancleReason",
		w.address AS "client.address",
		a.officee_id AS "officeId",
		a.province AS "province",
		invoice_id AS "workInvoice.id",
		a.ext AS "ext"
	</sql>
	
	<sql id="workInvoiceJoins">
		LEFT JOIN sys_user u ON u.id = a.drawer_id
		LEFT JOIN work_client_info w ON  w.id = a.client_id
	  	LEFT JOIN project_records p ON  p.id = a.project_id
	  	left JOIN work_contract_info ci on ci.id = p.contract_id
	  	JOIN sys_office o ON o.id = a.office_id
		JOIN sys_office s ON s.id = a.company_id
		left jOIN sys_office so ON so.id = a.office_id
	</sql>
	
    
	<select id="get" resultType="WorkInvoiceAlter" >
		SELECT 
			<include refid="workInvoiceColumns"/>
		FROM work_invoicealter a
		<include refid="workInvoiceJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkInvoiceAlter" >
		SELECT 
			<include refid="workInvoiceColumns"/>
		FROM work_invoicealter a
		<include refid="workInvoiceJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="number != null and number != ''">
				AND a.number = #{number}
			</if>
			<if test="invoiceState != null and invoiceState != ''">
				AND a.invoice_state = #{invoiceState}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.update_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<if test="client != null and client.id != null and client.id != ''">
				AND a.client_id = #{client.id}
			</if>
			<if test="project != null and project.id != null and project.id != ''">
				AND a.project_id = #{project.id}
			</if>
			<if test="project != null and project.projectId != null and project.projectId != ''">
				AND p.project_id LIKE
				<if test="dbName == 'oracle'">'%'||#{project.projectId}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{project.projectId}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{project.projectId},'%')</if>
			</if>
			<if test="project != null and project.projectName != null and project.projectName != ''">
				AND p.project_name LIKE
				<if test="dbName == 'oracle'">'%'||#{project.projectName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{project.projectName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{project.projectName},'%')</if>
			</if>
			<if test="client != null and client.name != null and client.name != ''">
				AND w.name LIKE
				<if test="dbName == 'oracle'">'%'||#{client.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{client.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{client.name},'%')</if>
			</if>
			<if test="office != null and office.name != null and office.name != ''">
				AND so.name LIKE
				<if test="dbName == 'oracle'">'%'||#{office.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{office.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{office.name},'%')</if>
			</if>
			<if test="money != null and money != ''">
				AND a.money = #{money}
			</if>
			<if test="invoiceType != null and invoiceType != ''">
				AND a.invoice_type = #{invoiceType}
			</if>
			<if test="chargeType != null and chargeType != ''">
				AND a.charge_type = #{chargeType}
			</if>
			<if test="content != null and content != ''">
				AND a.content = #{content}
			</if>
			<if test="drawer != null and drawer.id != null and drawer.id != ''">
				AND a.drawer_id = #{drawer.id}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="office != null and office.id != null and office.id != ''">
				AND a.office_id = #{office.id}
			</if>
			<if test="invoiceNumber != null and invoiceNumber != ''">
				AND a.invoice_number LIKE 
					<if test="dbName == 'oracle'">'%'||#{invoiceNumber}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{invoiceNumber}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{invoiceNumber},'%')</if>
			</if>
			<if test="invoiceDate != null and invoiceDate != ''">
				AND a.invoice_date = #{invoiceDate}
			</if>
			<if test="isInvoice != null and isInvoice != ''">
				AND a.is_invoice = #{isInvoice}
			</if>
			<if test="isCharge != null and isCharge != ''">
				AND a.is_charge = #{isCharge}
			</if>
			<if test="isInvalid != null and isInvalid != ''">
				AND a.is_invalid = #{isInvalid}
			</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="findAllList" resultType="WorkInvoiceAlter" >
		SELECT 
			<include refid="workInvoiceColumns"/>
		FROM work_invoicealter a
		<include refid="workInvoiceJoins"/>
		<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>
	
	<update id="updateProcessInstanceId">
		UPDATE work_invoicealter SET
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	<insert id="insert">
		INSERT INTO work_invoicealter(
			id,
			number,
			client_id,
			project_id,
			project_name,
			money,
			invoice_type,
			charge_type,
			content,
			drawer_id,
			office_id,
			invoice_number,
			invoice_date,
			drawer_name,
			take_date,
			invoice_remarks,
			is_invoice,
			is_charge,
			is_invalid,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			invoice_state,
			orUnicode,
			address,
			telephone,
			bank,
			company_id,
			bank_number,
			cancle_reason,
			officee_id,
			province,
			invoice_id,
			ext
		) VALUES (
			#{id},
			#{number},
			#{client.id},
			#{project.id},
			#{projectName},
			#{money},
			#{invoiceType},
			#{chargeType},
			#{content},
			#{drawer.id},
			#{office.id},
			#{invoiceNumber},
			#{invoiceDate},
			#{drawerName},
			#{takeDate},
			#{invoiceRemarks},
			#{isInvoice},
			#{isCharge},
			#{isInvalid},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{invoiceState},
			#{orUnicode},
			#{address},
			#{telephone},
			#{bank},
			#{companyId},
			#{bankNumber},
			#{cancleReason},
			#{officeId},
			#{province},
			#{workInvoice.id},
			#{ext}
		)
	</insert>
	
	<update id="update">
		UPDATE work_invoicealter SET
			number = #{number},
			client_id = #{client.id},
			invoice_id = #{workInvoice.id},
			project_id = #{project.id},
			project_name = #{projectName},
			money = #{money},
			invoice_type = #{invoiceType},
			charge_type = #{chargeType},
			content = #{content},
			drawer_id = #{drawer.id},
			office_id = #{office.id},
			invoice_number = #{invoiceNumber},
			invoice_date = #{invoiceDate},
			drawer_name = #{drawerName},
			take_date = #{takeDate},
			invoice_remarks = #{invoiceRemarks},
			is_invoice = #{isInvoice},
			is_charge = #{isCharge},
			is_invalid = #{isInvalid},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			invoice_state=#{invoiceState},
			orUnicode=#{orUnicode},
			address=#{address},
			telephone=#{telephone},
			bank=#{bank},
			company_id = #{companyId},
			bank_number=#{bankNumber},
			cancle_reason=#{cancleReason},
			officee_id=#{officeId},
			province = #{province},
			ext = #{ext}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_invoicealter
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_invoicealter SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkInvoiceAlter" statementType="STATEMENT">
		select * FROM work_invoicealter  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByclient" resultType="WorkClientInfo">
		SELECT 
			*
		FROM work_client_info a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="name != null and name != ''">
				AND a.name LIKE
				<if test="dbName == 'oracle'">'%'||#{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>
	<!--<select id="findListByproject" resultType="WorkBidProject">-->
		<!--SELECT -->
			<!--*-->
		<!--FROM work_client_info a-->
		<!--<where>-->
			<!--a.del_flag = #{DEL_FLAG_NORMAL}-->
			<!--<if test="name != null and name != ''">-->
				<!--AND a.project.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>-->

	<select id="getByProcInsId" resultType="WorkInvoiceAlter">
		SELECT
		<include refid="workInvoiceColumns"/>
		FROM work_invoicealter a
		<include refid="workInvoiceJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

	<select id="findListByproject" resultType="WorkBidProject">
		select
		a.id AS "id",
		a.project_number AS "projectNumber",
		a.project_name AS "projectName",
		a.project_type AS "projectType",
		a.type_explain AS "typeExplain",
		a.project_source AS "projectSource",
		a.source_explain AS "sourceExplain",
		a.is_ext AS "isExt",
		a.bid_company AS "bidCompany.id",
		a.ext_company AS "extCompany",
		a.contacts AS "contacts",
		a.registration_date AS "registrationDate",
		a.bid_date AS "bidDate",
		a.trial_date AS "trialDate",
		a.trial_type AS "trialType",
		a.document_cost AS "documentCost",
		a.purchase_date AS "purchaseDate",
		a.bind_cost AS "bindCost",
		a.other_cost AS "otherCost",
		a.bid_bond AS "bidBond",
		a.bond_style AS "bondStyle",
		a.refund_date AS "refundDate",
		a.offer AS "offer",
		a.is_bid AS "isBid",
		a.implementing_dept AS "implementingDept.id",
		a.is_subcontract AS "isSubcontract",
		a.subcontract_company AS "subcontractCompany",
		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",
		a.work_project_id AS "workProject.id"
		FROM work_client_info w LEFT JOIN work_bid_project a on w.id = a.bid_company
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<!--<if-->
			<if test="bidCompany != null  and bidCompany.id != null and bidCompany.id != ''">
				AND a.bid_company =  #{bidCompany.id}
			</if>
			<if test="companyId != null and companyId != ''">
				AND (a.company_id = #{companyId} or find_in_set(a.company_id ,#{parentIds}))
			</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>
		</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>