<?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.workpettycash.dao.WorkPettyCashDao">
    
	<sql id="workPettyCashColumns">
		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.company_id AS "company.id",
		a.company_id AS "companyId",
		a.office_id AS "office.id",
		a.office_id AS "officeId",
		a.reason AS "reason",
		a.amount AS "amount",
		a.use_date AS "useDate",
		a.return_date AS "returnDate",
		a.applicant_name AS "applicantName",
		a.applicant_office_id AS "applicantOffice.id",
		a.casher_name AS "casherName",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		user.id AS "user.id",
		user.name AS "userName",
		user.name AS "user.name",
		company.name AS "companyName",
		company.name AS "company.name",
		office.name AS "officeName",
		office.name AS "office.name",
		applicantOffice.name AS "applicantOffice.name",
		applicantOffice.name AS "applicantOfficeName"
	</sql>
	
	<sql id="workPettyCashJoins">
		LEFT JOIN sys_user user ON user.id = a.create_by
		LEFT JOIN sys_office office ON office.id = a.office_id
		LEFT JOIN sys_office company ON company.id = a.company_id
		LEFT JOIN sys_office applicantOffice ON applicantOffice.id = a.applicant_office_id
	</sql>
	
    
	<select id="get" resultType="WorkPettyCash" >
		SELECT 
			<include refid="workPettyCashColumns"/>
		FROM work_petty_cash a
		<include refid="workPettyCashJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkPettyCash" >
		SELECT 
			<include refid="workPettyCashColumns"/>
		FROM work_petty_cash a
		<include refid="workPettyCashJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="createBy != null and createBy.id != null and createBy.id != ''">
				AND a.create_by = #{createBy.id}
			</if>
			<if test="applicantOfficeId != null and applicantOfficeId != ''">
				AND a.applicant_office_id = #{applicantOfficeId}
			</if>
			<if test="applicantName !=null and applicantName !=''">
				AND a.applicant_name LIKE
					<if test="dbName == 'oracle'">'%'||#{applicantName}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{applicantName}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{applicantName},'%')</if>
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="officeId != null and officeId !=''">
				AND a.office_id = #{officeId}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</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="getByProcessInstanceId" resultType="WorkPettyCash">
		SELECT
		<include refid="workPettyCashColumns"/>
		FROM work_petty_cash a
		<include refid="workPettyCashJoins"/>
		WHERE a.process_instance_id = #{processInstanceId}
	</select>

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

	<select id="findAllList" resultType="WorkPettyCash" >
		SELECT 
			<include refid="workPettyCashColumns"/>
		FROM work_petty_cash a
		<include refid="workPettyCashJoins"/>
		<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_petty_cash(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			reason,
			amount,
			use_date,
			return_date,
			applicant_name,
			applicant_office_id,
			casher_name,
			status,
			process_instance_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{company.id},
			#{office.id},
			#{reason},
			#{amount},
			#{useDate},
			#{returnDate},
			#{applicantName},
			#{applicantOffice.id},
			#{casherName},
			#{status},
			#{processInstanceId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_petty_cash SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{company.id},
			office_id = #{office.id},
			reason = #{reason},
			amount = #{amount},
			use_date = #{useDate},
			return_date = #{returnDate},
			applicant_name = #{applicantName},
			applicant_office_id = #{applicantOffice.id},
			casher_name = #{casherName},
			status = #{status},
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_petty_cash
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_petty_cash SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkPettyCash" statementType="STATEMENT">
		select * FROM work_petty_cash  where ${propertyName} = '${value}'
	</select>
	
</mapper>