<?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.workreimbursement.dao.WorkAccountDao">
    
	<sql id="workAccountColumns">
		a.id AS "id",
		a.type AS "type",
		a.money AS "money",
		a.financial_subjects AS "financialSubjects",
		a.money_abstract AS "moneyAbstract",
		a.tax AS "tax",
		a.invoice_type as "invoiceType",
		a.work_reimbursement_id AS "workReimbursement.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"
	</sql>
	
	<sql id="workAccountJoins">
	</sql>
	
    
	<select id="get" resultType="WorkAccount" >
		SELECT 
			<include refid="workAccountColumns"/>
		FROM work_account a
		<include refid="workAccountJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkAccount" >
		SELECT 
			<include refid="workAccountColumns"/>
		FROM work_account a
		<include refid="workAccountJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="workReimbursement != null and workReimbursement != ''">
				AND a.work_reimbursement_id = #{workReimbursement.id}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	<select id="findByWorkAccount" resultType="WorkAccountAudit" >
		SELECT
			a.work_account_id AS "workAccount.id",
			a.check_subjects AS "checkSubjects",
			a.u8_subjects AS "u8Subjects",
			a.check_money AS "checkMoney"
		FROM work_account_audit a
		WHERE a.work_account_id = #{id}
	</select>

	<select id="findAllList" resultType="WorkAccount" >
		SELECT 
			<include refid="workAccountColumns"/>
		FROM work_account a
		<include refid="workAccountJoins"/>
		<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.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_account(
			id,
			type,
			money,
			financial_subjects,
			money_abstract,
			tax,
			work_reimbursement_id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			invoice_type
		) VALUES (
			#{id},
			#{type},
			#{money},
			#{financialSubjects},
			#{moneyAbstract},
			#{tax},
			#{workReimbursement.id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{invoiceType}
		)
	</insert>

	<insert id="insertAudits">
		INSERT INTO work_account_audit(work_account_id,check_subjects, check_money,u8_subjects)
		<foreach collection="workAccountAuditList" item="workAccountAudit" separator=" union all ">
			SELECT #{id}, #{workAccountAudit.checkSubjects},#{workAccountAudit.checkMoney},#{workAccountAudit.u8Subjects} FROM dual
		</foreach>
	</insert>

	<!--物理删除-->
	<update id="deleteAudits">
		DELETE FROM work_account_audit
		WHERE work_account_id = #{id}
	</update>
	
	<update id="update">
		UPDATE work_account SET 	
			type = #{type},
			money = #{money},
			financial_subjects = #{financialSubjects},
			money_abstract = #{moneyAbstract},
			tax = #{tax},
			work_reimbursement_id = #{workReimbursement.id},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			invoice_type = #{invoiceType}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_account
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE work_reimbursement_id = #{workReimbursement.id}
			</otherwise>
		</choose>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_account SET 
			del_flag = #{DEL_FLAG_DELETE}
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE work_reimbursement_id = #{workReimbursement.id}
			</otherwise>
		</choose>
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkAccount" statementType="STATEMENT">
		select * FROM work_account  where ${propertyName} = '${value}'
	</select>
	
</mapper>