<?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.workregularapply.dao.WorkRegularContentDao">
    
	<sql id="workRegularContentColumns">
		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.content AS "content",
		o.name as officeName,
		b.name as "companyName"
	</sql>
	
	<sql id="workRegularContentJoins">
		LEFT JOIN sys_user u on u.id = a.create_by
		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_office b ON b.id = a.branch_office
	</sql>
	
    
	<select id="get" resultType="WorkRegularContent" >
		SELECT 
			<include refid="workRegularContentColumns"/>
		FROM work_regular_content a
		<include refid="workRegularContentJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkRegularContent" >
		SELECT 
			<include refid="workRegularContentColumns"/>
		FROM work_regular_content a
		<include refid="workRegularContentJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="content != null and content != ''">
				AND a.content LIKE 
					<if test="dbName == 'oracle'">'%'||#{content}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{content}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{content},'%')</if>
			</if>
			<if test="branchOffice != null and branchOffice != ''">
				AND a.branch_office=#{branchOffice}
			</if>
			<!--<choose>
				<when test="branchOffice !=null and branchOffice != ''">
					and  a.office_id in (select off.id from sys_office off where off.branch_office=#{branchOffice})
				</when>
				<otherwise>
					and  a.office_id in
					(select off.id from sys_office off where off.parent_ids like concat('%',#{companyId},'%') and off.type=2 and (off.branch_office is null or off.branch_office=''))
				</otherwise>
			</choose>-->
		</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="WorkRegularContent" >
		SELECT 
			<include refid="workRegularContentColumns"/>
		FROM work_regular_content a
		<include refid="workRegularContentJoins"/>
		<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_regular_content(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			content,
			branch_office
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeId},
			#{companyId},
			#{content},
			#{branchOffice}
		)
	</insert>
	
	<update id="update">
		UPDATE work_regular_content SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{officeId},
			company_id = #{companyId},
			content = #{content},
			branch_office = #{branchOffice}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_regular_content
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_regular_content SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkRegularContent" statementType="STATEMENT">
		select * FROM work_regular_content  where ${propertyName} = '${value}'
	</select>
	
</mapper>