<?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.workinvoicereturn.dao.WorkInvoiceReturnDao">
    
	<sql id="workInvoiceReturnColumns">
		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.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.process_instance_id AS "processInstanceId",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.status AS "status",
		a.invoice_id AS "invoiceDetail.id",
		a.return_number AS "returnNumber",
		a.reason AS "reason",
		a.cancle_return AS "cancleReturn",
		a.return_type AS "returnType",
		a.code AS "code",
		a.number AS "number",
		a.uname AS "uname",
		w1.number AS "invoiceDetail.number",
		w1.total_money AS "invoiceDetail.totalMoney",
		w3.name As "invoiceDetail.workInvoice.client.name",
		s2.top_company AS "invoiceDetail.workInvoice.office.name",
		a.return_date AS "returnDate"
	</sql>
	
	<sql id="workInvoiceReturnJoins">
		left join work_invoice_detail w1 on w1.id = a.invoice_id
		left join work_invoice w2 on w2.id = w1.invoice_id
		LEFT JOIN work_client_info w3 ON  w3.id = w2.client_id
		left join sys_office s2 on s2.id = w2.office_id
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		left JOIN sys_user su on su.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkInvoiceReturn" >
		SELECT 
			<include refid="workInvoiceReturnColumns"/>
		FROM work_invoicereturn a
		<include refid="workInvoiceReturnJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkInvoiceReturn" >
		SELECT 
			<include refid="workInvoiceReturnColumns"/>
		FROM work_invoicereturn a
		<include refid="workInvoiceReturnJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="beginContractDate != null and endContractDate != null and beginContractDate != '' and endContractDate != ''">
				AND a.return_date BETWEEN #{beginContractDate} AND #{endContractDate}
			</if>
			<if test="invoiceDetail != null and invoiceDetail.workInvoice!=null and invoiceDetail.workInvoice.client !=null and invoiceDetail.workInvoice.client.name != null and invoiceDetail.workInvoice.client.name != ''">
				AND w3.name LIKE
				<if test="dbName == 'oracle'">'%'||#{invoiceDetail.workInvoice.client.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{invoiceDetail.workInvoice.client.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{invoiceDetail.workInvoice.client.name},'%')</if>
			</if>
			<if test="invoiceDetail != null and invoiceDetail.workInvoice!=null and invoiceDetail.workInvoice.office !=null and invoiceDetail.workInvoice.office.name != null and invoiceDetail.workInvoice.office.name != ''">
				AND s2.name LIKE
				<if test="dbName == 'oracle'">'%'||#{invoiceDetail.workInvoice.office.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{invoiceDetail.workInvoice.office.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{invoiceDetail.workInvoice.office.name},'%')</if>
			</if>
			<if test="invoiceDetail != null and invoiceDetail.number != null and invoiceDetail.number != ''">
				AND w1.number = #{invoiceDetail.number}
			</if>

			<if test="returnNumber != null and returnNumber != ''">
				AND a.return_number = #{returnNumber}
			</if>
			<if test="number != null and number != ''">
				AND a.number = #{number}
			</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.create_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkInvoiceReturn" >
		SELECT 
			<include refid="workInvoiceReturnColumns"/>
		FROM work_invoicereturn a
		<include refid="workInvoiceReturnJoins"/>
		<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_invoicereturn(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_instance_id,
			office_id,
			company_id,
			status,
			invoice_id,
			reason,
			cancle_return,
			return_type,
			code,
			number,
			uname,
			return_number,
			return_date
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processInstanceId},
			#{officeId},
			#{companyId},
			#{status},
			#{invoiceDetail.id},
			#{reason},
			#{cancleReturn},
			#{returnType},
			#{code},
			#{number},
			#{uname},
			#{returnNumber},
			#{returnDate}
		)
	</insert>
	
	<update id="update">
		UPDATE work_invoicereturn SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_instance_id = #{processInstanceId},
			office_id = #{officeId},
			company_id = #{companyId},
			status = #{status},
			invoice_id = #{invoiceDetail.id},
			return_number = #{returnNumber},
			reason = #{reason},
			cancle_return = #{cancleReturn},
			return_type = #{returnType},
			code = #{code},
			number = #{number},
			uname = #{uname},
			return_date = #{returnDate}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_invoicereturn
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_invoicereturn SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkInvoiceReturn" statementType="STATEMENT">
		select * FROM work_invoicereturn  where ${propertyName} = '${value}'
	</select>

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

	<!-- 获取流程ID -->
	<select id="getByProcessInstanceId" resultType="WorkInvoiceReturn">
		SELECT
		<include refid="workInvoiceReturnColumns"/>
		FROM work_invoicereturn a
		<include refid="workInvoiceReturnJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

    <select id="countByNumAndComp" resultType="int">
        SELECT count(1) from work_invoicereturn WHERE  number = #{invoiceNum} AND company_id = #{comId}
    </select>
</mapper>