<?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.workactivity.dao.ActivityDao">
    
	<sql id="activityColumns">
		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.name AS "name",
		a.user_id AS "User.id",
		a.role_enname AS "role.enname",
		a.process_key AS "processKey",
		a.return_back AS "returnBack",
		a.is_approval AS "isApproval",
		a.approval_id AS "approvalId",
		a.count AS "count",
		a.encount AS "encount",
		a.enlist AS "enlist",
		a.activity_task AS "activityTask",
		a.activity_menu_id AS "activityMenuId"
	</sql>
	
	<sql id="activityJoins">
	</sql>
	
    
	<select id="get" resultType="Activity" >
		SELECT 
			<include refid="activityColumns"/>
		FROM work_activity a
		<include refid="activityJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="Activity" >
		SELECT 
			<include refid="activityColumns"/>
		FROM work_activity a
		<include refid="activityJoins"/>
		<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>
	<select id="findListByMenu" resultType="Activity" >
		SELECT
			<include refid="activityColumns"/>,
		am.name AS "activityName"
		FROM work_activity a
		<include refid="activityJoins"/>
		LEFT JOIN work_activity_menu m ON a.activity_menu_id = m.id
		LEFT JOIN work_activity_menu am ON am.id = m.parent_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.role_enname in (#{role.enname},#{role.name})
			AND (m.company_id = #{companyId} OR m.company_id = '0')
		</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="findByActivityMenuId" resultType="Activity" >
		SELECT
			<include refid="activityColumns"/>
		FROM work_activity a
		WHERE a.activity_menu_id = #{activityMenuId} and a.del_flag = '0'
		ORDER BY a.count
	</select>

	<select id="groupByActivityMenu" resultType="Activity" >
		SELECT
			a.process_key
		FROM work_activity a
		WHERE a.activity_menu_id in (SELECT id FROM work_activity_menu m where m.parent_id = #{parentId} and FIND_IN_SET(#{companyId},m.company_id))
			GROUP BY a.process_key
	</select>

	<select id="getByProcessInstanceId" resultType="Activity" >
		SELECT <include refid="activityColumns"/> from work_activity a
		where a.process_key in
		(select k.process_key from work_activity_process k where k.process_instance_id = #{processInstanceId} GROUP BY k.process_key) ORDER BY a.count ASC
	</select>

	<select id="findAllList" resultType="Activity" >
		SELECT 
			<include refid="activityColumns"/>
		FROM work_activity a
		<include refid="activityJoins"/>
		<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_activity(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			name,
			user_id,
			role_enname,
			process_key,
			return_back,
			is_approval,
			approval_id,
			count,
			encount,
			enlist,
			activity_task,
			activity_menu_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{name},
			#{User.id},
			#{role.enname},
			#{processKey},
			#{returnBack},
			#{isApproval},
			#{approvalId},
			#{count},
			#{encount},
			#{enlist},
			#{activityTask},
			#{activityMenuId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_activity SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			name = #{name},
			user_id = #{User.id},
			role_enname = #{role.enname},
			process_key = #{processKey},
			return_back = #{returnBack},
			is_approval = #{isApproval},
			approval_id = #{approvalId},
			count = #{count},
			encount = #{encount},
			enlist = #{enlist},
			activity_task = #{activitytask},
			activity_menu_id = #{activityMenuId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_activity
		WHERE id = #{id}
	</update>

	<!--物理删除-->
	<update id="deleteToDelflag">
		UPDATE work_activity SET
		del_flag = 1
		WHERE activity_menu_id = #{activityMenuId}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_activity SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="Activity" statementType="STATEMENT">
		select * FROM work_activity  where ${propertyName} = '${value}'
	</select>
	
</mapper>