<?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.workreceiptsregister.dao.WorkReceiptsRegisterDetailDao">
    
	<sql id="workReceiptsRegisterDetailColumns">
		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.office_id AS "office.id",
		a.invoice_id AS "invoice.id",
		a.register_amt AS "registerAmt",
		a.register_date AS "registerDate",
		a.status AS "status",
		a.register_id AS "registerId",
		office.name AS "office.name",
		invoice.number AS "invoice.number",
		invoice.total_money AS "invoice.totalMoney",
		invoice.income_money AS "invoice.incomeMoney",
		invoice.date AS "invoice.date",
		invoice.tax_rate AS "invoice.taxRate"
	</sql>
	
	<sql id="workReceiptsRegisterDetailJoins">
		LEFT JOIN sys_office office ON office.id = a.office_id
		LEFT JOIN work_invoice_detail invoice ON invoice.id = a.invoice_id
	</sql>
	
    
	<select id="get" resultType="WorkReceiptsRegisterDetail" >
		SELECT 
			<include refid="workReceiptsRegisterDetailColumns"/>
		FROM work_receipts_register_detail a
		<include refid="workReceiptsRegisterDetailJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReceiptsRegisterDetail" >
		SELECT 
			<include refid="workReceiptsRegisterDetailColumns"/>
		FROM work_receipts_register_detail a
		<include refid="workReceiptsRegisterDetailJoins"/>
		<where>
            <if test="registerId!=null and registerId!=''">
                AND a.register_id = #{registerId}
            </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="WorkReceiptsRegisterDetail" >
		SELECT 
			<include refid="workReceiptsRegisterDetailColumns"/>
		FROM work_receipts_register_detail a
		<include refid="workReceiptsRegisterDetailJoins"/>
		<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_receipts_register_detail(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			office_id,
			invoice_id,
			register_amt,
			register_date,
			status,
			register_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{office.id},
			#{invoice.id},
			#{registerAmt},
			#{registerDate},
			#{status},
			#{registerId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_receipts_register_detail SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			office_id = #{office.id},
			invoice_id = #{invoice.id},
			register_amt = #{registerAmt},
			register_date = #{registerDate},
			status = #{status},
			register_id = #{registerId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_receipts_register_detail
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_receipts_register_detail SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReceiptsRegisterDetail" statementType="STATEMENT">
		select * FROM work_receipts_register_detail  where ${propertyName} = '${value}'
	</select>
	
</mapper>