<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.workflow.dao.TemplateDepartmentDAO">
	<resultMap id="TemplateDepartmentMap" type="com.jpsoft.shinestar.modules.workflow.entity.TemplateDepartment">
		<id property="id" column="id_" />
			<result property="processTemplateId" column="process_template_id" />
			<result property="departmentId" column="department_id" />
			<result property="createBy" column="create_by" />
			<result property="createTime" column="create_time" />
			<result property="updateBy" column="update_by" />
			<result property="updateTime" column="update_time" />
			<result property="delFlag" column="del_flag" />
			</resultMap>
	<insert id="insert" parameterType="com.jpsoft.shinestar.modules.workflow.entity.TemplateDepartment">
	<!--
	<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
		select sys_guid() from dual
	</selectKey>
	-->
	<![CDATA[
		insert into wf_template_department
	    (id_,process_template_id,department_id,create_by,create_time,update_by,update_time,del_flag)
		values
		(
#{id,jdbcType=VARCHAR}
,#{processTemplateId,jdbcType=VARCHAR}
,#{departmentId,jdbcType=VARCHAR}
,#{createBy,jdbcType=VARCHAR}
,#{createTime,jdbcType= TIMESTAMP }
,#{updateBy,jdbcType=VARCHAR}
,#{updateTime,jdbcType= TIMESTAMP }
,#{delFlag,jdbcType= NUMERIC }
		)
	]]>
	</insert>
	<delete id="delete" parameterType="string">
		delete from wf_template_department where id_=#{id,jdbcType=VARCHAR}
	</delete>
	<update id="update" parameterType="com.jpsoft.shinestar.modules.workflow.entity.TemplateDepartment">
		update wf_template_department
		<set>
				<if test="processTemplateId!=null">
		process_template_id=#{processTemplateId,jdbcType=VARCHAR},
		</if>
				<if test="departmentId!=null">
		department_id=#{departmentId,jdbcType=VARCHAR},
		</if>
				<if test="createBy!=null">
		create_by=#{createBy,jdbcType=VARCHAR},
		</if>
				<if test="createTime!=null">
		create_time=#{createTime,jdbcType= TIMESTAMP },
		</if>
				<if test="updateBy!=null">
		update_by=#{updateBy,jdbcType=VARCHAR},
		</if>
				<if test="updateTime!=null">
		update_time=#{updateTime,jdbcType= TIMESTAMP },
		</if>
				<if test="delFlag!=null">
		del_flag=#{delFlag,jdbcType= NUMERIC },
		</if>
		</set>
	where id_=#{id}
	</update>
	<select id="get" parameterType="string" resultMap="TemplateDepartmentMap">
		select
id_,process_template_id,department_id,create_by,create_time,update_by,update_time,del_flag		from wf_template_department where id_=#{0}
	</select>
	<select id="exist" parameterType="string" resultType="int">
		select count(*) from wf_template_department where id_=#{0}
	</select>
	<select id="list" resultMap="TemplateDepartmentMap">
		select * from wf_template_department
	</select>
	<select id="findByTemplateId" resultMap="TemplateDepartmentMap">
		select * from wf_template_department where del_flag=false and process_template_id=#{0}
	</select>

	<select id="search" parameterType="hashmap" resultMap="TemplateDepartmentMap">
		<![CDATA[
			select * from wf_template_department
		]]>
		<where>
			<if test="searchParams.id != null">
				and ID_ like #{searchParams.id}
			</if>
		</where>
		<foreach item="sort" collection="sortList"  open="order by" separator=",">
	        ${sort.name} ${sort.order}
	 	</foreach>
	</select>

	<select id="findByFormIdDepartmentId" resultMap="TemplateDepartmentMap">
		<![CDATA[
		select a.* from wf_template_department a
		left join wf_process_template b on b.id_ = a.process_template_id
		left join wf_process_set c on c.id_ = b.process_set_id
		left join wf_process_activity d on d.process_template_id = b.id_
		where a.del_flag = 0
		and b.del_flag = 0
		and c.del_flag = 0
		and a.department_id = #{companyId}
		and c.form_id = #{formId}
		and d.person_role_id = #{personRoleId}
		limit 1
		]]>
	</select>

	<select id="findByFormId" resultMap="TemplateDepartmentMap">
		<![CDATA[
		select a.* from wf_template_department a
		left join wf_process_template b on b.id_ = a.process_template_id
		left join wf_process_set c on c.id_ = b.process_set_id
		where a.del_flag = 0
		and b.del_flag = 0
		and c.del_flag = 0
		and c.form_id = #{formId}
		limit 1
		]]>
	</select>
</mapper>