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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.sys.SysApprovalBusinessDetail">
        <result column="PK_APPROVAL_BUSINESS" property="pkApprovalBusiness"/>
        <result column="APPROVAL_LEVEL" property="approvalLevel"/>
        <result column="PK_USER" property="pkUser"/>
        <result column="PK_DEPT" property="pkDept"/>
        <result column="APPROVER" property="approver"/>
        <result column="APPROVER_TIME" property="approverTime"/>
        <result column="STATUS" property="status"/>
        <result column="REMARK" property="remark"/>
        <result column="SIGNATURE" property="signature"/>
        <result column="APPROVER_DEPT" property="approverDept"/>
    </resultMap>

    <!-- 批量插入数据 -->
    <insert id="insertBatch">
        INSERT ALL
        <foreach collection="list" item="item" separator=" ">
            into SYS_APPROVAL_BUSINESS_DETAIL (PK_APPROVAL_BUSINESS, APPROVAL_LEVEL, PK_USER)
            values (#{item.pkApprovalBusiness}, #{item.approvalLevel}, #{item.pkUser})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <!-- 非当前用户指定部门是否存在进行中的审核 -->
    <select id="detailExistByUserAndDept" resultType="Integer">
        select count(pk_approval_business)
        from (
                 select sabd.pk_approval_business
                 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
                 where sab.del_flag = 0
                   and sab.approval_status in (2, 3)
                   and (sabd.pk_user = #{userId} and su.pk_dept = #{deptId})
                 group by sabd.pk_approval_business
                 having count(sabd.pk_approval_business) > 1
             ) t
    </select>

    <!-- 根据审批业务ID查询审批业务明细 -->
    <select id="approvalDetailList" resultMap="BaseResultMap">
        select sabd.pk_approval_business,
        sabd.approval_level,
        sabd.pk_user,
        NVL(sabd.pk_dept, su.pk_dept) pk_dept,
        NVL(sabd.approver, su.nick_name) approver,
        NVL(sabd.approver_dept, sd.dept_name) approver_dept,
        sabd.approver_time,
        sabd.status,
        sabd.remark
        from SYS_APPROVAL_BUSINESS_DETAIL sabd
        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 sabd.PK_APPROVAL_BUSINESS in
        <foreach collection="pkIdList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by sabd.PK_APPROVAL_BUSINESS desc, sabd.APPROVAL_LEVEL asc
    </select>

    <select id="selectByMaxApprovalBusinessDetailInfo" resultType="com.hzs.common.domain.system.sys.SysApprovalBusinessDetail">
        select abd.*
        from SYS_APPROVAL_BUSINESS_DETAIL abd
        where abd.PK_APPROVAL_BUSINESS = #{pkApprovalBusiness}
          and abd.APPROVAL_LEVEL = (select max(ad.APPROVAL_LEVEL) from SYS_APPROVAL_BUSINESS_DETAIL ad where ad.PK_APPROVAL_BUSINESS = #{pkApprovalBusiness} and ad.status = 1)
    </select>

</mapper>
