<?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.hzs.system.sys.mapper.SysApprovalMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.sys.SysApproval">
        <id column="PK_ID" property="pkId"/>
        <result column="APPROVAL_TYPE" property="approvalType"/>
        <result column="APPROVAL_BUSINESS" property="approvalBusiness"/>
        <result column="EFFECTIVE_DATE" property="effectiveDate"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
    </resultMap>

    <!-- 审批配置+配置项 -->
    <resultMap id="approvalAndItemMap" type="com.hzs.common.domain.system.sys.ext.SysApprovalExt"
               extends="BaseResultMap">
        <collection property="itemList" ofType="com.hzs.common.domain.system.sys.ext.SysApprovalItemExt">
            <result column="PK_APPROVAL" property="pkApproval"/>
            <result column="APPROVAL_LEVEL" property="approvalLevel"/>
            <result column="PK_USER" property="pkUser"/>
            <result column="NICK_NAME" property="nickName"/>
            <result column="DEPT_NAME" property="deptName"/>
        </collection>
    </resultMap>

    <!-- 审批列表 -->
    <select id="approvalList" resultType="com.hzs.system.sys.vo.ApprovalVO">
        select sa.pk_id,
        sa.approval_business,
        tmp.process,
        to_char(tmp.send) send,
        sa.effective_date
        from (select sa.pk_id,
        max(sa.process) process,
        wm_concat(sus.nick_name) send
        from (select sa.pk_id,
        to_char(replace(wm_concat(su.nick_name), ',', ' - ')) process
        from sys_approval sa
        left join sys_approval_item sai
        on sai.pk_approval = sa.pk_id
        left join sys_user su
        on su.user_id = sai.pk_user
        where sa.del_flag = 0
        and sa.pk_country = #{pkCountry}
        <if test="null != param.approvalBusiness">
            and sa.approval_business = #{param.approvalBusiness}
        </if>
        group by sa.pk_id) sa
        left join sys_approval_send sas
        on sas.pk_approval = sa.pk_id
        left join sys_user sus
        on sus.user_id = sas.pk_user
        group by sa.pk_id) tmp
        inner join sys_approval sa
        on sa.pk_id = tmp.pk_id
    </select>

    <!-- 根据审批业务类型相关最新的审批 -->
    <select id="lastApproval" resultMap="BaseResultMap">
        select sa.*
        from SYS_APPROVAL sa
        where (sa.approval_business, effective_date) in
        (select sa.approval_business, max(sa.effective_date) effective_date
        from SYS_APPROVAL sa
        where sa.del_flag = 0
        and sysdate >= sa.effective_date
        and sa.pk_country = #{pkCountry}
        and sa.APPROVAL_BUSINESS in
        <foreach collection="approvalBusinessList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by sa.approval_business)
        order by sa.EFFECTIVE_DATE desc, sa.CREATION_TIME desc
    </select>

    <!-- 根据审批业务相关最新的审批以及审批项 -->
    <select id="lastApprovalAndItem" resultMap="approvalAndItemMap">
        select sa.*, sai.*, su.nick_name, sd.dept_name
        from (select sa.*
        from SYS_APPROVAL sa
        where sa.del_flag = 0
        and (sa.approval_business, effective_date) in
        (select sa.approval_business,
        max(sa.effective_date) effective_date
        from SYS_APPROVAL sa
        where sa.del_flag = 0
        and sysdate >= sa.effective_date
        and sa.pk_country = #{pkCountry}
        and sa.APPROVAL_BUSINESS in
        <foreach collection="approvalBusinessList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by sa.approval_business)) sa
        left join SYS_APPROVAL_ITEM sai
        on sai.pk_approval = sa.pk_id
        left join SYS_USER su
        on su.user_id = sai.pk_user
        left join SYS_DEPT sd
        on sd.dept_id = su.pk_dept
        order by sa.APPROVAL_BUSINESS, sai.approval_level
    </select>

</mapper>
