<?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="cn.timer.api.dao.spmk.SpmkApproveSummaryMapper">

    <resultMap id="BaseResultMap" type="cn.timer.api.bean.spmk.SpmkApproveSummary" >
                <id column="id" property="id" />
                <result column="org_code" property="orgCode" />
                <result column="emp_num" property="empNum" />
                <result column="title" property="title" />
                <result column="department_name" property="departmentName" />
                <result column="initiator" property="initiator" />
                <result column="create_time" property="createTime" />
                <result column="approve_name" property="approveName" />
                <result column="sts" property="sts" />
                <result column="current_approver" property="currentApprover" />
                <result column="history_approver" property="historyApprover" />
                <result column="asso_type" property="assoType" />
                <result column="digest" property="digest" />
                <result column="update_time" property="updateTime" />
                <result column="end_time" property="endTime" />
                <result column="digest" property="digest" />
                <result column="is_delete" property="isDelete" />
    </resultMap>
    
    <resultMap id="BaseResultMap_As" type="cn.timer.api.bean.spmk.SpmkApproveSummary" >
                <id column="id" property="id" />
                <result column="org_code" property="orgCode" />
                <result column="emp_num" property="empNum" />
                <result column="title" property="title" />
                <result column="department_name" property="departmentName" />
                <result column="initiator" property="initiator" />
                <result column="create_time" property="createTime" />
                <result column="approve_name" property="approveName" />
                <result column="sts" property="sts" />
                <result column="current_approver" property="currentApprover" />
                <result column="history_approver" property="historyApprover" />
                <result column="asso_type" property="assoType" />
                <result column="update_time" property="updateTime" />
                <result column="end_time" property="endTime" />
                <result column="digest" property="digest" />
                <result column="is_delete" property="isDelete" />
                <result column="gw_name" property="gwName" />
                <result column="bm_id" property="bmId" />
                <result column="bm_name" property="bmName" />
    </resultMap>

    <sql id="Base_Column_List">
                id,
                org_code,
                emp_num,
                title,
                department_name,
                initiator,
                create_time,
                approve_name,
                sts,
                current_approver,
                history_approver,
                asso_type,
                update_time,
                end_time,
                digest,
                is_delete
    </sql>
    
    <sql id="Base_Column_List_a">
                a.id,
                a.org_code,
                a.emp_num,
                a.title,
                a.department_name,
                a.initiator,
                a.create_time,
                a.approve_name,
                a.sts,
                a.current_approver,
                a.history_approver,
                a.asso_type,
                a.update_time,
                a.end_time,
                a.digest,
                a.is_delete,
                a.is_urgent
    </sql>
    
    <sql id="Base_Column_List_Alias">
                id SpmkApproveSummary_id,
                org_code SpmkApproveSummary_org_code,
                emp_num SpmkApproveSummary_emp_num,
                title SpmkApproveSummary_title,
                department_name SpmkApproveSummary_department_name,
                initiator SpmkApproveSummary_initiator,
                create_time SpmkApproveSummary_create_time,
                approve_name SpmkApproveSummary_approve_name,
                sts SpmkApproveSummary_sts,
                current_approver SpmkApproveSummary_current_approver,
                history_approver SpmkApproveSummary_history_approver,
                asso_type SpmkApproveSummary_asso_type,
                update_time SpmkApproveSummary_update_time,
                end_time SpmkApproveSummary_end_time,
                digest SpmkApproveSummary_digest
    </sql>
    
    <select id="selectPageByQuery" resultMap="BaseResultMap_As" >
        SELECT 
        	<include refid="Base_Column_List_a" />,
        	(SELECT name FROM zzgl_bmgw_m WHERE id = (SELECT bmgw_id FROM yggl_main_emp WHERE a.emp_num = emp_num and a.org_code=org_code limit 1) limit 1) as gw_name,
			(SELECT id FROM zzgl_bmgw_m WHERE id = (SELECT up_id FROM zzgl_bmgw_m WHERE id = (SELECT bmgw_id FROM yggl_main_emp WHERE a.emp_num = emp_num and a.org_code=org_code limit 1) limit 1) limit 1) as bm_id,
			(SELECT name FROM zzgl_bmgw_m WHERE id = (SELECT up_id FROM zzgl_bmgw_m WHERE id = (SELECT bmgw_id FROM yggl_main_emp WHERE a.emp_num = emp_num and a.org_code=org_code limit 1) limit 1) limit 1) as bm_name
        FROM spmk_approve_summary a
        WHERE a.org_code = #{param.orgCode}  and a.is_delete = 0
        	<if test="param.query != null and param.query != ''">
        		and (
        			a.title like CONCAT('%',#{param.query},'%') or
        			a.initiator like CONCAT('%',#{param.query},'%') or
        			a.id like CONCAT('%',#{param.query},'%') 
        		)
        	</if>
        	<if test="param.sts != null">
        		and a.sts = #{param.sts}
			</if>
        	<if test="param.startTime != null and param.startTime != ''">
        		and a.create_time <![CDATA[ >= ]]> #{param.startTime}
        	</if>
        	<if test="param.endTime != null and param.endTime != ''">
        		and a.create_time <![CDATA[ <= ]]> #{param.endTime}
        	</if>
        	<if test="param.empNums != null and param.empNums.size() > 0">
				and a.emp_num IN 
				(
				<foreach item="it" index="index" collection="param.empNums" separator="," close="" >
		        	#{it}
				</foreach>
				)
        	</if> 
        ORDER BY a.id DESC
    </select>
    
    <select id="selectPageByQueryForEmpNum" resultMap="BaseResultMap_As" >
        SELECT 
        	<include refid="Base_Column_List_a" />,
        	(SELECT name FROM zzgl_bmgw_m WHERE id = (SELECT bmgw_id FROM yggl_main_emp WHERE a.emp_num = emp_num limit 1) limit 1) as gw_name,
			(SELECT id FROM zzgl_bmgw_m WHERE id = (SELECT up_id FROM zzgl_bmgw_m WHERE id = (SELECT bmgw_id FROM yggl_main_emp WHERE a.emp_num = emp_num limit 1) limit 1) limit 1) as bm_id,
			(SELECT name FROM zzgl_bmgw_m WHERE id = (SELECT up_id FROM zzgl_bmgw_m WHERE id = (SELECT bmgw_id FROM yggl_main_emp WHERE a.emp_num = emp_num limit 1) limit 1) limit 1) as bm_name
        FROM spmk_approve_summary a
        WHERE a.org_code = #{param.orgCode}  and  a.is_delete=0
        
        	<if test="param.type != null and param.type >= 1">
        	and a.id in 
        	(SELECT approve_summary_id FROM spmk_approve_execute_record WHERE id IN 
				(SELECT 
					approve_execute_record_id 
				 FROM 
				 	spmk_executor 
				 WHERE 
				 	emp_num = #{param.empNum}
				 	<if test="param.sts == 0">
					 	and sts not in (2,3,4)
					</if>
				 	) 
				 <if test="param.type == 3">
				 	and type in (0,1,2)
				 </if>
				 <if test="param.type != 3">
				 	and type = #{param.type}
				 </if>
			) 
			
			</if>
			
			<if test="param.type != null and param.type == 0">
        	and a.emp_num = #{param.empNum}
			</if>
			
			<if test="param.sts != null">
				<if test="param.sts == 5">
	        		and a.sts in (0,1,2,3)
				</if>
				<if test="param.sts == 4">
	        		and a.sts in (2,3)
				</if>
				<if test="param.sts != 4 and param.sts != 5">
					and a.sts = #{param.sts}
				</if>
			</if>
			

        	<if test="param.query != null and param.query != ''">
        		and (
        			a.title like CONCAT('%',#{param.query},'%') or
        			a.initiator like CONCAT('%',#{param.query},'%') or
        			a.id like CONCAT('%',#{param.query},'%') 
        		)
        	</if>
        	<if test="param.startTime != null and param.startTime != ''">
        		and a.create_time <![CDATA[ >= ]]> #{param.startTime}
        	</if>
        	<if test="param.endTime != null and param.endTime != ''">
        		and a.create_time <![CDATA[ <= ]]> #{param.endTime}
        	</if> 
        ORDER BY a.id DESC
    </select>
    

	<!-- 

    <insert id="insert" useGeneratedKeys="true" keyColumn="id" parameterType="cn.timer.api.bean.spmk.SpmkApproveSummary">
        INSERT INTO spmk_approve_summary
        <trim prefix="(" suffix=")" suffixOverrides=",">
                        <if test ='null != orgCode'>
                        org_code,
                        </if>
                        <if test ='null != empNum'>
                        emp_num,
                        </if>
                        <if test ='null != title'>
                        title,
                        </if>
                        <if test ='null != departmentName'>
                        department_name,
                        </if>
                        <if test ='null != initiator'>
                        initiator,
                        </if>
                        <if test ='null != createTime'>
                        create_time,
                        </if>
                        <if test ='null != approveName'>
                        approve_name,
                        </if>
                        <if test ='null != sts'>
                        sts,
                        </if>
                        <if test ='null != currentApprover'>
                        current_approver,
                        </if>
                        <if test ='null != historyApprover'>
                        history_approver,
                        </if>
                        <if test ='null != assoType'>
                        asso_type,
                        </if>
                        <if test ='null != updateTime'>
                        update_time,
                        </if>
                        <if test ='null != endTime'>
                        end_time,
                        </if>
                        <if test ='null != digest'>
                        digest
                        </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                        <if test ='null != orgCode'>
                        #{orgCode},
                        </if>
                        <if test ='null != empNum'>
                        #{empNum},
                        </if>
                        <if test ='null != title'>
                        #{title},
                        </if>
                        <if test ='null != departmentName'>
                        #{departmentName},
                        </if>
                        <if test ='null != initiator'>
                        #{initiator},
                        </if>
                        <if test ='null != createTime'>
                        #{createTime},
                        </if>
                        <if test ='null != approveName'>
                        #{approveName},
                        </if>
                        <if test ='null != sts'>
                        #{sts},
                        </if>
                        <if test ='null != currentApprover'>
                        #{currentApprover},
                        </if>
                        <if test ='null != historyApprover'>
                        #{historyApprover},
                        </if>
                        <if test ='null != assoType'>
                        #{assoType},
                        </if>
                        <if test ='null != updateTime'>
                        #{updateTime},
                        </if>
                        <if test ='null != endTime'>
                        #{endTime},
                        </if>
                        <if test ='null != digest'>
                        #{digest}
                        </if>
        </trim>
    </insert>

    <delete id="delete" >
        DELETE FROM spmk_approve_summary
        WHERE id = #{id}
    </delete>

    <update id="update" parameterType="cn.timer.api.bean.spmk.SpmkApproveSummary">
        UPDATE spmk_approve_summary
        <set>
                    <if test ='null != orgCode'>org_code = #{orgCode},</if>
                    <if test ='null != empNum'>emp_num = #{empNum},</if>
                    <if test ='null != title'>title = #{title},</if>
                    <if test ='null != departmentName'>department_name = #{departmentName},</if>
                    <if test ='null != initiator'>initiator = #{initiator},</if>
                    <if test ='null != createTime'>create_time = #{createTime},</if>
                    <if test ='null != approveName'>approve_name = #{approveName},</if>
                    <if test ='null != sts'>sts = #{sts},</if>
                    <if test ='null != currentApprover'>current_approver = #{currentApprover},</if>
                    <if test ='null != historyApprover'>history_approver = #{historyApprover},</if>
                    <if test ='null != assoType'>asso_type = #{assoType},</if>
                    <if test ='null != updateTime'>update_time = #{updateTime},</if>
                    <if test ='null != endTime'>end_time = #{endTime},</if>
                    <if test ='null != digest'>digest = #{digest}</if>
        </set>
        WHERE id = #{id}
    </update>


    <select id="load" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM spmk_approve_summary
        WHERE id = #{id}
    </select>

    <select id="pageList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM spmk_approve_summary
        LIMIT #{offset}, #{pageSize}
    </select>

    <select id="pageListCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM spmk_approve_summary
    </select>

	 -->

</mapper>