<?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.workapprovalcopy.dao.ApprovalCopyDao">
    
	<sql id="approvalCopyColumns">
		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.cc_id AS "CCId",
		a.type AS "type",
		a.user_id AS "userId",
		a.approval_id AS "approvalId",
		a.approver_id AS "approverId",
		a.read_flag AS "readFlag"
	</sql>
	
	<sql id="approvalCopyJoins">
	</sql>
	
    
	<select id="get" resultType="ApprovalCopy" >
		SELECT 
			<include refid="approvalCopyColumns"/>
		FROM oa_approval_copy a
		<include refid="approvalCopyJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByApprovalId" resultType="ApprovalCopy">
		SELECT <include refid="approvalCopyColumns"/>
		FROM oa_approval_copy a
		<include refid="approvalCopyJoins"/>
		WHERE a.approval_id = #{approvalId}
	</select>

	<select id="findList" resultType="ApprovalCopy" >
		SELECT 
			<include refid="approvalCopyColumns"/>
		FROM oa_approval_copy a
		<include refid="approvalCopyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="CCId != null and CCId != ''">
                AND a.cc_id  LIKE
                <if test="dbName == 'oracle'">'%'||#{CCID}||'%'</if>
                <if test="dbName == 'mssql'">'%'+#{CCID}+'%'</if>
                <if test="dbName == 'mysql'">concat('%',#{CCId},'%')</if>
            </if>
            <if test="userId != null and userId != ''">
                AND a.user_id = #{userId}
            </if>
            <if test="approvalId != null and approvalId != ''">
                AND a.approval_id = #{approvalId}
            </if>
            <if test="approverId != null and approverId != ''">
                AND a.approver_id = #{approverId}
            </if>
            <if test="readFlag != null and readFlag != ''">
                AND a.read_flag = #{readFlag}
            </if>
            <if test="type != null and type != ''">
            AND a.type = #{type}
            </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="ApprovalCopy" >
		SELECT 
			<include refid="approvalCopyColumns"/>
		FROM oa_approval_copy a
		<include refid="approvalCopyJoins"/>
		<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 oa_approval_copy(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			cc_id,
			type,
			user_id,
			approval_id,
			read_flag,
			approver_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{CCId},
			#{type},
			#{userId},
			#{approvalId},
			#{readFlag},
			#{approverId}
		)
	</insert>
	
	<update id="update">
		UPDATE oa_approval_copy SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			cc_id = #{CCId},
			type = #{type},
			user_id = #{userId},
			approval_id = #{approvalId},
			read_flag = #{readFlag},
			approver_id = #{approverId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM oa_approval_copy
		WHERE id = #{id}
	</update>
	<!--物理删除-->
	<update id="deletes">
		UPDATE oa_approval_copy set cc_id =''
		WHERE approval_id = #{approvalId}
	</update>

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