<?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.workactivitymenu.dao.WorkActivityMenuDao">
    
	<sql id="workActivityMenuColumns">
		a.id AS "id",
		a.parent_id AS "parent.id",
		a.parent_ids AS "parentIds",
		a.name AS "name",
		a.sort AS "sort",
		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.process_key AS "processKey",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.functional_name AS "functionalName",
		a.branch_company_id AS "branchCompanyId",
		a.is_ext AS "isExt",
		a.process_type AS "processType",
		p.name AS "parent.name",
		o.name AS "officeName"
	</sql>

	<sql id="workActivityMenuJoins">
		LEFT JOIN work_activity_menu p ON p.id = a.parent_id
		LEFT JOIN sys_office o ON o.id = a.office_id
	</sql>
    
	<select id="get" resultType="WorkActivityMenu">
		SELECT 
			<include refid="workActivityMenuColumns"/>
		FROM work_activity_menu a
		<include refid="workActivityMenuJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkActivityMenu">
		SELECT 
			<include refid="workActivityMenuColumns"/>
		FROM work_activity_menu a
		<include refid="workActivityMenuJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="parentIds != null and parentIds != ''">
				AND a.parent_ids LIKE
					<if test="dbName == 'oracle'">'%'||#{parentIds}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{parentIds}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{parentIds},'%')</if>
			</if>
			<if test="parent != null and parent.id != null and parent.id != ''">
				AND a.parent_id =#{parent.id}
			</if>
			<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>
			<if test="companyId != null and companyId != ''">
				AND (find_in_set(#{companyId},a.company_id) or a.company_id = '0')
			</if>
			<if test="officeId != null and officeId != ''">
				AND (a.office_id = #{officeId} or a.office_id = '0')
			</if>
			<if test="branchCompanyId != null and branchCompanyId != ''">
				AND (find_in_set(#{branchCompanyId},a.branch_company_id) or a.branch_company_id = '0')
			</if>
		</where>
		ORDER BY a.sort ASC
	</select>

	<select id="findByParentAndOffice" resultType="WorkActivityMenu">
		SELECT
			<include refid="workActivityMenuColumns"/>
		FROM work_activity_menu a
		<include refid="workActivityMenuJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="parent != null and parent.id != null and parent.id != ''">
				AND a.parent_id =#{parent.id}
			</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</if>
		</where>
		ORDER BY a.sort ASC
	</select>

	<select id="findAllList" resultType="WorkActivityMenu">
		SELECT 
			<include refid="workActivityMenuColumns"/>
		FROM work_activity_menu a
		<include refid="workActivityMenuJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
		ORDER BY a.sort ASC
	</select>

	<select id="findByList" resultType="WorkActivityMenu">
		SELECT
			a.id,a.sort
		FROM work_activity_menu a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND (
				a.parent_id = #{parent.id} OR a.id= #{parent.id})
			AND (
				a.office_id = #{officeId} OR a.office_id = '0'
			)
		</where>
		ORDER BY a.sort ASC
	</select>
	<select id="findTreeData" resultType="WorkActivityMenu">
		SELECT
		<include refid="workActivityMenuColumns"/>
		FROM work_activity_menu a
		<include refid="workActivityMenuJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.office_id = #{officeId}
			AND (a.process_key IS NULL or trim(a.process_key)='')
		</where>
		ORDER BY a.sort ASC
	</select>

	<select id="findByParentIdsLike" resultType="WorkActivityMenu">
		SELECT
			a.id,
			a.parent_id AS "parent.id",
			a.parent_ids
		FROM work_activity_menu a
		<include refid="workActivityMenuJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.parent_ids LIKE #{parentIds}
		</where>
		ORDER BY a.sort ASC
	</select>
	
	<insert id="insert">
		INSERT INTO work_activity_menu(
			id,
			parent_id,
			parent_ids,
			name,
			sort,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			process_key,
			functional_name,
			process_type,
			branch_company_id,
			is_ext
		) VALUES (
			#{id},
			#{parentId},
			#{parentIds},
			#{name},
			#{sort},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{processKey},
			#{functionalName},
			#{processType},
			#{branchCompanyId},
			#{isExt}
		)
	</insert>
	
	<update id="update">
		UPDATE work_activity_menu SET 	
			parent_id = #{parentId},
			parent_ids = #{parentIds},
			name = #{name},
			sort = #{sort},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			process_key = #{processKey},
			functional_name = #{functionalName},
			branch_company_id = #{branchCompanyId},
			process_type = #{processType},
			is_ext = #{isExt}
		WHERE id = #{id}
	</update>
	
	<update id="updateParentIds">
		UPDATE work_activity_menu SET 
			parent_id = #{parent.id}, 
			parent_ids = #{parentIds}
		WHERE id = #{id}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_activity_menu
		WHERE id = #{id} OR parent_ids LIKE 
		<if test="dbName == 'oracle'">#{id}||'%'</if>
		<if test="dbName == 'mssql'">#{id}+'%'</if>
		<if test="dbName == 'mysql'">concat(#{id},'%')</if>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_activity_menu SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id} OR parent_ids LIKE 
		<if test="dbName == 'oracle'">#{id}||'%'</if>
		<if test="dbName == 'mssql'">#{id}+'%'</if>
		<if test="dbName == 'mysql'">concat(#{id},'%')</if>
	</update>
	
</mapper>