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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.sys.SysApprovalBusinessReceive">
        <result column="PK_APPROVAL_BUSINESS" property="pkApprovalBusiness"/>
        <result column="SEND_TIME" property="sendTime"/>
        <result column="PK_RECEIVE" property="pkReceive"/>
        <result column="RECEIVE_STATUS" property="receiveStatus"/>
        <result column="RECEIVE_TIME" property="receiveTime"/>
    </resultMap>

    <!-- 批量插入数据 -->
    <insert id="insertBatch">
        INSERT ALL
        <foreach collection="list" item="item" separator=" ">
            into SYS_APPROVAL_BUSINESS_RECEIVE (PK_APPROVAL_BUSINESS, SEND_TIME, PK_RECEIVE)
            values (#{item.pkApprovalBusiness}, #{item.sendTime}, #{item.pkReceive})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <!-- 查询列表数量 -->
    <select id="listReceiveCount" resultType="int">
        select count(1)
        from sys_approval_business_receive sabr
                 inner join sys_approval_business sab
                            on sab.pk_id = sabr.pk_approval_business
                                and sab.del_flag = 0
                                and sab.approval_status = 5
                                and sab.pk_country = #{pkCountry}
        where sabr.pk_receive = #{userId}
          and sabr.receive_status = 1
    </select>

    <!-- 查询列表 -->
    <select id="listReceive" resultType="com.hzs.common.domain.system.sys.ext.SysApprovalBusinessReceiveExt">
        select sabr.*, sab.pk_id, sab.approval_business, sab.approval_code, sab.user_name, sab.creation_time
        from sys_approval_business_receive sabr
        inner join sys_approval_business sab
        on sab.pk_id = sabr.pk_approval_business
        and sab.del_flag = 0
        and sab.approval_status = 5
        and sab.pk_country = #{pkCountry}
        where sabr.pk_receive = #{userId}
        <if test="param.approvalBusiness != null">
            and sab.approval_business = #{param.approvalBusiness}
        </if>
        <if test="param.receiveStatus != null">
            and sabr.receive_status = #{param.receiveStatus}
        </if>
        <if test="param.userName != null and param.userName != ''">
            and sab.user_name like #{param.userName} || '%'
        </if>
        <if test="param.creationTime != null">
            and sab.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= sab.creation_time
        </if>
        <if test="param.sendTime != null">
            and sabr.send_time >= #{param.sendTime[0]}
            and #{param.sendTime[1]} >= sabr.send_time
        </if>
        order by sabr.send_time desc
    </select>

</mapper>
