<?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.SysApprovalBusinessMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.sys.SysApprovalBusiness">
        <id column="PK_ID" property="pkId"/>
        <result column="APPROVAL_CODE" property="approvalCode"/>
        <result column="PK_APPROVAL" property="pkApproval"/>
        <result column="APPROVAL_TYPE" property="approvalType"/>
        <result column="APPROVAL_BUSINESS" property="approvalBusiness"/>
        <result column="APPROVAL_BUSINESS_VAL" property="approvalBusinessVal"/>
        <result column="BUSINESS_CODE" property="businessCode"/>
        <result column="APPROVAL_STATUS" property="approvalStatus"/>
        <result column="END_TIME" property="endTime"/>
        <result column="PK_USER" property="pkUser"/>
        <result column="SIGN_TYPE" property="signType"/>
        <result column="REMARK" property="remark"/>
        <result column="PK_DEPT" property="pkDept"/>
        <result column="DEPT_NAME" property="deptName"/>
        <result column="USER_NAME" property="userName"/>
        <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"/>
        <result column="SIGN_SOURCE" property="signSource"/>
        <result column="APPROVAL_OPERATION" property="approvalOperation"/>
    </resultMap>

    <!-- 业务审批+详情映射 -->
    <resultMap id="BaseResultExtMap" type="com.hzs.common.domain.system.sys.ext.SysApprovalBusinessExt" extends="BaseResultMap">
        <result column="user_name" property="userName"/>
        <collection property="detailList" ofType="com.hzs.common.domain.system.sys.SysApprovalBusinessDetail">
            <result column="APPROVAL_LEVEL" property="approvalLevel"/>
            <result column="APPROVER_PK_USER" property="pkUser"/>
            <result column="APPROVER_PK_DEPT" property="pkDept"/>
            <result column="APPROVER" property="approver"/>
            <result column="APPROVER_DEPT" property="approverDept"/>
            <result column="APPROVER_TIME" property="approverTime"/>
            <result column="APPROVER_STATUS" property="status"/>
            <result column="APPROVER_REMARK" property="remark"/>
        </collection>
    </resultMap>

    <!-- 业务审批是否进行中 -->
    <select id="checkBusinessExist" resultType="int">
        select count(sab.pk_id)
        from SYS_APPROVAL_BUSINESS sab
        where sab.del_flag = 0
        and sab.pk_country = #{pkCountry}
        and sab.approval_business in
        <foreach collection="approvalBusinessList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and sab.business_code in
        <foreach collection="businessCodeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and sab.APPROVAL_STATUS in (2, 3)
    </select>

    <!-- 查询业务审批+详情 -->
    <select id="queryBusinessAndDetail" resultMap="BaseResultExtMap">
        select sab.*,
        sabd.APPROVAL_LEVEL,
        sabd.PK_USER APPROVER_PK_USER,
        sabd.PK_DEPT APPROVER_PK_DEPT,
        sabd.APPROVER,
        sabd.APPROVER_DEPT,
        sabd.APPROVER_TIME,
        sabd.STATUS APPROVER_STATUS,
        sabd.REMARK APPROVER_REMARK
        from sys_approval_business sab
        left join sys_approval_business_detail sabd
        on sabd.pk_approval_business = sab.pk_id
        where sab.del_flag = 0
        and sab.pk_country = #{pkCountry}
        and sab.approval_business = #{approvalBusiness}
        and sab.business_code in
        <foreach collection="businessCodeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <!-- 根据审批单号获取审批以及明细 -->
    <select id="queryByApprovalCode" resultMap="BaseResultExtMap">
        select sab.*,
               sabd.APPROVAL_LEVEL,
               sabd.PK_USER APPROVER_PK_USER,
               sabd.PK_DEPT APPROVER_PK_DEPT,
               case
                   when sabd.APPROVER is not null then
                       sabd.APPROVER
                   else
                       (select su.nick_name
                        from sys_user su
                        where su.user_id = sabd.PK_USER)
                   end      APPROVER,
               case
                   when sabd.APPROVER_DEPT is not null then
                       sabd.APPROVER_DEPT
                   else
                       (select sd.dept_name
                        from sys_user su
                                 inner join sys_dept sd
                                            on sd.dept_id = su.pk_dept
                        where su.user_id = sabd.PK_USER)
                   end      APPROVER_DEPT,
               sabd.APPROVER_TIME,
               sabd.STATUS  APPROVER_STATUS,
               sabd.REMARK  APPROVER_REMARK
        from SYS_APPROVAL_BUSINESS sab
                 left join SYS_APPROVAL_BUSINESS_DETAIL sabd
                           on sab.pk_id = sabd.pk_approval_business
        where sab.DEL_FLAG = 0
          and sab.APPROVAL_CODE = #{approvalCode}
        order by sabd.APPROVAL_LEVEL
    </select>

    <!-- 业务审批列表 -->
    <select id="queryApprovalList" resultMap="BaseResultMap">
        select sab.*
        from SYS_APPROVAL_BUSINESS sab
        where sab.pk_id in (select sab.pk_id
        from SYS_APPROVAL_BUSINESS sab
        left join SYS_APPROVAL_BUSINESS_DETAIL sabd on sabd.pk_approval_business = sab.pk_id
        where sab.del_flag = 0
        and sab.pk_country = #{pkCountry}
        and sabd.pk_user = #{userId}
        <if test="param.approvalBusiness != null">
            and sab.APPROVAL_BUSINESS = #{param.approvalBusiness}
        </if>
        <if test="param.businessCode != null and param.businessCode != ''">
            and sab.BUSINESS_CODE = #{param.businessCode}
        </if>
        <if test="param.approveStatus != null">
            and sab.STATUS = #{param.approveStatus}
        </if>
        <if test="param.endTime != null">
            and sab.END_TIME >= #{param.endTime[0]}
            and #{param.endTime[1]} >= sab.END_TIME
        </if>
        <if test="param.approvalCode != null and param.approvalCode != ''">
            and sab.APPROVAL_CODE = #{param.approvalCode}
        </if>
        group by sab.pk_id)
        order by sab.modified_time desc
    </select>

    <!-- 待审批业务数 -->
    <select id="waitApprovalNum" resultType="int">
        select count(distinct sab.pk_id)
        from SYS_APPROVAL_BUSINESS sab
        where sab.del_flag = 0
        and sab.pk_country = #{pkCountry}
        and sab.APPROVAL_STATUS in (2, 3)
        <if test="userId != 100000000">
            <!-- 如果是超级管理员不需要做操作人过滤，其它用户需要处理 -->
            and exists (select 1
            from SYS_APPROVAL_BUSINESS_DETAIL sabd
            where sabd.pk_approval_business = sab.pk_id
            and sabd.pk_user = #{userId})
        </if>
        <!--
        select count(1) wait_num
        from SYS_APPROVAL_BUSINESS sab
                 left join SYS_APPROVAL_BUSINESS_DETAIL sabd
                           on sabd.pk_approval_business = sab.pk_id
        where sab.del_flag = 0
          and sab.pk_country = #{pkCountry}
          and sab.pk_user = #{userId}
          and sab.APPROVAL_STATUS in (2, 3)
          -->
    </select>

    <!-- 待审批业务数据 -->
    <select id="waitApproval" resultMap="BaseResultExtMap">
        select sab.*
        from SYS_APPROVAL_BUSINESS sab
        where sab.pk_id in
        (select distinct sab.pk_id
        from SYS_APPROVAL_BUSINESS sab
        where sab.del_flag = 0
        and sab.pk_country = #{pkCountry}
        and sab.APPROVAL_STATUS in (2, 3)
        <if test="userId != 100000000">
            <!-- 如果是超级管理员不需要做操作人过滤，其它用户需要处理 -->
            and exists (select 1
            from SYS_APPROVAL_BUSINESS_DETAIL sabd
            where sabd.pk_approval_business = sab.pk_id
            and sabd.pk_user = #{userId})
        </if>
        <if test="null != param.approvalCode">
            and sab.approval_code like '%' || #{param.approvalCode} || '%'
        </if>
        <if test="null != param.approvalBusiness">
            and sab.approval_business = #{param.approvalBusiness}
        </if>
        <if test="null != param.approvalBusinessVal and '' != param.approvalBusinessVal">
            and sab.approval_business_val like '%' || #{param.approvalBusinessVal} || '%'
        </if>
        <if test="null != param.remark and '' != param.remark">
            and sab.remark like '%' || #{param.remark} || '%'
        </if>
        <if test="null != param.creationTime">
            and sab.CREATION_TIME >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= sab.CREATION_TIME
        </if>
        )
        order by sab.creation_time
        <!--
                select sab.*,
                       sabd.APPROVAL_LEVEL,
                       sabd.APPROVER_TIME,
                       su.NICK_NAME approver,
                       sd.DEPT_NAME approver_dept
                from SYS_APPROVAL_BUSINESS sab
                         left join SYS_APPROVAL_BUSINESS_DETAIL sabd
                                   on sabd.pk_approval_business = sab.pk_id
                         inner join sys_user su
                                    on su.user_id = sabd.pk_user
                         inner join sys_dept sd
                                    on sd.dept_id = su.pk_dept
                where sab.pk_id in (
                    select distinct sab.pk_id
                    from SYS_APPROVAL_BUSINESS sab
                    where sab.del_flag = 0
                      and sab.pk_country = #{pkCountry}
                      and sab.APPROVAL_STATUS in (2, 3)
                      and exists(select 1
                                 from SYS_APPROVAL_BUSINESS_DETAIL sabd
                                 where sabd.pk_approval_business = sab.pk_id
                                   and sabd.pk_user = #{userId})
                )
                order by sab.creation_time, sabd.approval_level
        -->
    </select>

    <!-- 已完成审批业务数据 -->
    <select id="completeApproval" resultMap="BaseResultExtMap">
        select sab.*
        from SYS_APPROVAL_BUSINESS sab
        where sab.pk_id in (select sab.pk_id
        from SYS_APPROVAL_BUSINESS sab
        left join SYS_APPROVAL_BUSINESS_DETAIL sabd
        on sabd.pk_approval_business = sab.pk_id
        where sab.del_flag = 0
        and sab.pk_country = #{pkCountry}
        <if test="userId != 100000000">
            <!-- 如果是超级管理员不需要做操作人过滤，其它用户需要处理 -->
            and (
            sab.pk_creator = #{userId} or
            (sab.APPROVAL_STATUS in (4, 5) and sabd.pk_user = #{userId})
            )
        </if>
        <if test="null != param.approvalCode">
            and sab.approval_code like '%' || #{param.approvalCode} || '%'
        </if>
        <if test="null != param.signSource">
            and sab.sign_source = #{param.signSource}
        </if>
        <if test="null != param.approvalBusiness">
            and sab.approval_business = #{param.approvalBusiness}
        </if>
        <if test="null != param.approvalBusinessVal and '' != param.approvalBusinessVal">
            and sab.approval_business_val like '%' || #{param.approvalBusinessVal} || '%'
        </if>
        <if test="null != param.approveOperation">
            and sab.APPROVAL_OPERATION = #{param.approveOperation}
        </if>
        <if test="null != param.approveStatus">
            and sab.APPROVAL_STATUS = #{param.approveStatus}
        </if>
        <if test="null != param.remark and '' != param.remark">
            and sab.remark like '%' || #{param.remark} || '%'
        </if>
        <if test="null != param.creationTime">
            and sab.CREATION_TIME >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= sab.CREATION_TIME
        </if>
        <if test="null != param.endTime">
            and sab.END_TIME >= #{param.endTime[0]}
            and #{param.endTime[1]} >= sab.END_TIME
        </if>
        group by sab.pk_id)
        order by sab.creation_time desc
    </select>

    <!-- 查询最新的编号 -->
    <select id="queryLastCode" resultType="string">
        select max(sab.approval_code) approval_code
        from sys_approval_business sab
    </select>

    <!-- 自定义签呈列表 -->
    <select id="customList" resultMap="BaseResultMap">
        select *
        from SYS_APPROVAL_BUSINESS sab
        where sab.del_flag = 0
        and sab.approval_business = 0
        and sab.pk_country = #{pkCountry}
        and sab.pk_creator = #{userId}
        <if test="param.approvalBusinessVal != null and param.approvalBusinessVal != ''">
            and sab.approval_business_val like #{param.approvalBusinessVal} || '%'
        </if>
        <if test="param.approveStatus != null">
            and sab.approval_status = #{param.approveStatus}
        </if>
        <if test="null != param.creationTime">
            and sab.CREATION_TIME >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= sab.CREATION_TIME
        </if>
        <if test="null != param.endTime">
            and sab.END_TIME >= #{param.endTime[0]}
            and #{param.endTime[1]} >= sab.END_TIME
        </if>
        order by sab.creation_time desc
    </select>

</mapper>
