<?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.officeintroduce.dao.OfficeintroduceDao">
    
	<sql id="officeintroduceColumns">
		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.introduce AS "introduce",
		wa.attachment_name AS "uName",
		office.name AS "office.name",
		a.company_id AS "companyId",
		wa.url AS "url",
		a.upload_date AS "uploadDate",
		a.company_name AS "companyName",
		a.num AS "num",
		a.sysoffice_id AS "office.id"
	</sql>
	
	<sql id="officeintroduceJoins">
		LEFT JOIN sys_office office ON office.id = a.sysoffice_id
		LEFT join work_attachment wa on wa.attachment_id = a.id
	</sql>
	
    
	<select id="get" resultType="Officeintroduce" >
		SELECT 
			<include refid="officeintroduceColumns"/>
		FROM office_introduce a
		<include refid="officeintroduceJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="Officeintroduce" >
		SELECT 
			<include refid="officeintroduceColumns"/>
		FROM office_introduce a
		<include refid="officeintroduceJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="(companyId == null or companyId == '') and office != null and office.id != null and office.id != ''">
				AND a.sysoffice_id = #{office.id}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="companyName != null and companyName != ''">
				AND a.company_name LIKE
				<if test="dbName == 'oracle'">'%'||#{companyName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{companyName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{companyName},'%')</if>
			</if>
			<if test="introduce != null and introduce != ''">
				AND a.introduce LIKE
				<if test="dbName == 'oracle'">'%'||#{introduce}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{introduce}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{introduce},'%')</if>
			</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="Officeintroduce" >
		SELECT 
			<include refid="officeintroduceColumns"/>
		FROM office_introduce a
		<include refid="officeintroduceJoins"/>
		<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 office_introduce(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			introduce,
			upload_date,
			company_name,
			num,
			company_id,
			sysoffice_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{introduce},
			#{uploadDate},
			#{companyName},
			#{num},
			#{companyId},
			#{office.id}
		)
	</insert>
	
	<update id="update">
		UPDATE office_introduce SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			introduce = #{introduce},
			upload_date= #{uploadDate},
			company_name = #{companyName},
			num = #{num},
			company_id = #{companyId},
			sysoffice_id = #{office.id}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM office_introduce
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE office_introduce SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Officeintroduce" statementType="STATEMENT">
		select * FROM office_introduce  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByoffice" resultType="Office">
		SELECT 
			*
		FROM sys_office a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE 
				<if test="dbName == 'oracle'">#{name}||'%'</if>
				<if test="dbName == 'mssql'">#{name}+'%'</if>
				<if test="dbName == 'mysql'">concat(#{name},'%')</if>
			</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>
</mapper>