<?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.WorkActivityProcessDao">
	<resultMap id="workActivityProcessResult" type="WorkActivityProcess">
		<id property="id" column="id" />
		<result property="createBy.id" column="createBy.id" />
		<result property="enname" column="enname" />
		<result property="createDate" column="createDate" />
		<result property="updateBy.id" column="updateBy.id" />
		<result property="updateDate" column="updateDate" />
		<result property="remarks" column="remarks" />
		<result property="delFlag" column="delFlag" />
		<result property="processKey" column="processKey" />
		<result property="isApproval" column="isApproval" />
		<result property="processInstanceId" column="processInstanceId" />
		<result property="count" column="count" />
		<result property="activityTask" column="activityTask" />
		<result property="activity.id" column="activity.id" />
		<result property="activity.name" column="activity.name" />
		<result property="activity.user.id" column="activity.user.id" />
		<result property="activity.role.enname" column="activity.role.enname" />
		<result property="activity.isApproval" column="activity.isApproval" />
		<result property="activity.name" column="activity.name" />
		<collection property="processUserList" ofType="WorkActivityProcessUser">
			<id property="id" column="processUserList.id" />
			<id property="name" column="processUserList.name" />
		</collection>
	</resultMap>

	<sql id="workActivityProcessColumns">
		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.process_key AS "processKey",
		a.is_approval AS "isApproval",
		a.process_instance_id AS "processInstanceId",
		a.count AS "count",
		a.activity_task AS "activityTask",
		a.activity_id AS "activity.id"
	</sql>
	
	<sql id="workActivityProcessJoins">
	</sql>

    <update id="updateDelFlagAndIsApproval">
		UPDATE work_activity_process
		SET del_flag = #{delFlag},is_approval = #{isApproval}
		WHERE id = #{id}
	</update>
	
	<select id="get" resultType="WorkActivityProcess" >
		SELECT 
			<include refid="workActivityProcessColumns"/>
		FROM work_activity_process a
		<include refid="workActivityProcessJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findByProcInsIdAndIsApproval" resultType="WorkActivityProcess" >
		SELECT
		<include refid="workActivityProcessColumns"/>,
		u.name AS "createBy.name"
		FROM work_activity_process a
		LEFT JOIN sys_user u on a.create_by = u.id
		<include refid="workActivityProcessJoins"/>
		<where>
			a.count != 0
			<if test="processInstanceId != null and processInstanceId != ''">
				and a.process_instance_id = #{processInstanceId}
			</if>
			<if test="isApproval != null and isApproval != ''">
				AND a.is_approval = #{isApproval}
			</if>
		</where>
	</select>

	<select id="findByKeyAndCount" resultType="WorkActivityProcess" >
		SELECT
		<include refid="workActivityProcessColumns"/>
		FROM work_activity_process a
		<include refid="workActivityProcessJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="processKey != null and processKey != ''">
				a.process_key = #{processKey}
			</if>
			<if test="processInstanceId != null and processInstanceId != ''">
				and a.process_instance_id = #{processInstanceId}
			</if>
			<if test="count != null and count != ''">
				AND a.count = #{count}
			</if>
			<if test="isApproval != null and isApproval != ''">
				AND a.is_approval = #{isApproval}
			</if>
		</where>
	</select>
	<select id="findByProcInsId" resultType="WorkActivityProcess" >
		SELECT
		<include refid="workActivityProcessColumns"/>,
		w.name AS "activity.name",
		w.user_id AS "activity.user.id",
		w.role_enname AS "activity.role.enname",
		w.is_approval AS "activity.isApproval"
		FROM work_activity_process a
		LEFT JOIN work_activity w ON w.process_key = a.process_key and w.count = a.count
		<include refid="workActivityProcessJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} and w.del_flag = #{DEL_FLAG_NORMAL}
			<if test="processInstanceId != null and processInstanceId != ''">
				and a.process_instance_id = #{processInstanceId}
			</if>
			<choose>
				<when test="isApproval != null and isApproval != ''">
					AND a.is_approval = 0
					ORDER BY  a.count ASC
				</when>
				<otherwise>
					AND a.is_approval != 0
					ORDER BY a.update_date ASC
				</otherwise>
			</choose>
		</where>
	</select>

	<select id="findByProcInsIdAudit" resultMap="workActivityProcessResult" >
		SELECT
		<include refid="workActivityProcessColumns"/>,
		w.name AS "activity.name",
		w.user_id AS "activity.user.id",
		w.role_enname AS "activity.role.enname",
		w.is_approval AS "activity.isApproval",
		p.user_id AS "processUserList.id",
		u.name AS "processUserList.name"
		FROM work_activity_process a
		LEFT JOIN work_activity w ON w.process_key = a.process_key and w.count = a.count
		LEFT JOIN work_activity_process_user p ON a.process_instance_id = p.process_id and p.count = a.count
		LEFT JOIN sys_user u ON u.id = p.user_id
		<include refid="workActivityProcessJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="processInstanceId != null and processInstanceId != ''">
				and a.process_instance_id = #{processInstanceId}
			</if>
			<if test="count != null and count != ''">
				and a.count = #{count}
			</if>
			<if test="type != null and type != ''">
				and p.type = #{type}
			</if>
			<if test="isApproval != null and isApproval != ''">
				AND a.is_approval = #{isApproval}
			</if>
			ORDER BY  a.count ASC
		</where>
	</select>

	<select id="findList" resultType="WorkActivityProcess" >
		SELECT 
			<include refid="workActivityProcessColumns"/>
		FROM work_activity_process a
		<include refid="workActivityProcessJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="processKey != null and processKey != ''">
				and a.process_key = #{processKey}
			</if>
			<if test="processInstanceId != null and processInstanceId != ''">
				and a.process_instance_id = #{processInstanceId}
			</if>
			<if test="count != null and count != ''">
				AND a.count = #{count}
			</if>
			<if test="isApproval != null and isApproval != ''">
				AND a.is_approval = #{isApproval}
			</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="WorkActivityProcess" >
		SELECT 
			<include refid="workActivityProcessColumns"/>
		FROM work_activity_process a
		<include refid="workActivityProcessJoins"/>
		<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_process(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			process_key,
			is_approval,
			process_instance_id,
			count,
			activity_task,
			activity_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{processKey},
			#{isApproval},
			#{processInstanceId},
			#{count},
			#{activityTask},
			#{activity.id}
		)
	</insert>
	
	<update id="update">
		UPDATE work_activity_process SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			process_key = #{processKey},
			is_approval = #{isApproval},
			count = #{count},
			activity_id = #{activity.id},
			activity_task = #{activityTask},
			process_instance_id = #{processInstanceId}
		WHERE id = #{id}
	</update>
	<update id="updateProcessInstanceId">
		UPDATE work_activity_process SET
			process_instance_id = #{processInstanceId}
		WHERE process_instance_id = #{processId} and del_flag = '0'
	</update>
	<update id="deleteProcessInstanceId">
		DELETE FROM work_activity_process
		WHERE process_instance_id = #{processId} and del_flag = '1'
	</update>
	<update id="deleteProcessInstanceById">
		DELETE FROM work_activity_process
		WHERE process_instance_id = #{processId}
	</update>

	<update id="deleteAudits">
		DELETE FROM work_activity_process_user
		WHERE process_id = #{processId} and count = #{count}
	</update>

	<insert id="insertAudits">
		INSERT INTO work_activity_process_user(process_id,user_id, count,type)
		<foreach collection="processUserList" item="WorkActivityProcessUser" separator=" union all ">
			SELECT #{WorkActivityProcessUser.processId}, #{WorkActivityProcessUser.userId},#{WorkActivityProcessUser.count},#{WorkActivityProcessUser.type} FROM dual
		</foreach>
	</insert>

	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_activity_process
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_activity_process SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	<!--逻辑删除-->
	<update id="delFlag">
		UPDATE work_activity_process a SET
			a.del_flag = #{delFlag}
		<where>
			a.process_instance_id = #{processInstanceId}
			<if test="count != null and count != ''">
				AND a.count &lt; #{count} AND A.COUNT &gt; 0
			</if>
			<if test="isApproval != null and isApproval != ''">
				AND a.is_approval = #{isApproval}
			</if>
		</where>
	</update>
	<!--逻辑删除-->
	<update id="deleteByAudit">
		DELETE FROM work_activity_process
		WHERE
			process_instance_id = #{processInstanceId}
			AND count = #{count}
			AND is_approval = #{isApproval}
	</update>
	<!--逻辑删除-->
	<update id="deleteProcessIdAuditUsers">
		DELETE FROM work_activity_process_user
		WHERE
			process_id = #{processInstanceId}
	</update>

	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkActivityProcess" statementType="STATEMENT">
		select * FROM work_activity_process  where ${propertyName} = '${value}'
	</select>


	<update id="deleteyProcess">
		DELETE FROM work_activity_process
		WHERE process_instance_id = #{id}
	</update>
	<update id="updateType">
	UPDATE work_activity_process a SET
			a.process_key = #{type} where a.process_instance_id=#{processInstanceId}
	</update>
</mapper>