<?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.workieavetemplate.dao.WorkIeaveTemplateUserDao">
    
	<sql id="workIeaveTemplateUserColumns">
		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.office_id AS "officeId",
		a.company_id AS "companyId",
		a.auditor AS "auditor",
		a.comment as "comment",
		a.branch_office AS "branchOffice",
		a.template_id AS "templateId",
		o.name as "officeName",
		u.name as "userName"
	</sql>
	
	<sql id="workIeaveTemplateUserJoins">
		LEFT JOIN sys_office o on a.office_id = o.id
        LEFT JOIN sys_office s ON s.id = a.company_id
        LEFT JOIN sys_user u ON u.id = a.auditor
	</sql>
	
    
	<select id="get" resultType="WorkIeaveTemplateUser" >
		SELECT 
			<include refid="workIeaveTemplateUserColumns"/>
		FROM work_leave_template_user a
		<include refid="workIeaveTemplateUserJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkIeaveTemplateUser" >
		SELECT 
			<include refid="workIeaveTemplateUserColumns"/>
		FROM work_leave_template_user a
		<include refid="workIeaveTemplateUserJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="auditor != null and auditor != ''">
				AND a.auditor = #{auditor}
			</if>
			<if test="branchOffice != null and branchOffice != ''">
				AND a.branch_office = #{branchOffice}
			</if>
			<if test="templateId != null and templateId != ''">
				AND a.template_id = #{templateId}
			</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="WorkIeaveTemplateUser" >
		SELECT 
			<include refid="workIeaveTemplateUserColumns"/>
		FROM work_leave_template_user a
		<include refid="workIeaveTemplateUserJoins"/>
		<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_leave_template_user(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			auditor,
			branch_office,
			template_id,
			comment
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeId},
			#{companyId},
			#{auditor},
			#{branchOffice},
			#{templateId},
			#{comment}
		)
	</insert>
	
	<update id="update">
		UPDATE work_leave_template_user SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{officeId},
			company_id = #{companyId},
			auditor = #{auditor},
			branch_office = #{branchOffice},
			template_id = #{templateId},
			comment = #{comment},
			del_flag=#{delFlag}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_leave_template_user
		WHERE id = #{id}
	</update>
	<update id="deleteByTemplateId">
		DELETE FROM work_leave_template_user
		WHERE template_id = #{templateId}
	</update>
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_leave_template_user SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkIeaveTemplateUser" statementType="STATEMENT">
		select * FROM work_leave_template_user  where ${propertyName} = '${value}'
	</select>


	<select id="findListforLeave" resultType="WorkIeaveTemplateUser" >
		SELECT
		<include refid="workIeaveTemplateUserColumns"/>
		FROM work_leave_template_user a
		<include refid="workIeaveTemplateUserJoins"/>
		<where>
			a.del_flag = '0'
			<if test="branchOffice != null and branchOffice != ''">
			  AND a.branch_office = #{branchOffice}
			</if>
		</where>
	</select>
</mapper>