<?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.ruoyi.approvalFlow.mapper.AuditItemsMapper">

    <resultMap type="AuditItems" id="AuditItemsResult">
        <result property="id" column="id"/>
        <result property="billNo" column="bill_no"/>
        <result property="billId" column="bill_id"/>
        <result property="actId" column="act_id"/>
        <result property="pathId" column="path_id"/>
        <result property="levelId" column="level_id"/>
        <result property="refno1" column="refno1"/>
        <result property="iffinalItem" column="iffinal_item"/>
        <result property="refno2" column="refno2"/>
        <result property="refno3" column="refno3"/>
        <result property="branchId" column="branch_id"/>
        <result property="sendUserId" column="send_user_id"/>
        <result property="sendName" column="send_name"/>
        <result property="sendTime" column="send_time"/>
        <result property="sendMsg" column="send_msg"/>
        <result property="auditUserId" column="audit_user_id"/>
        <result property="auditItem" column="audit_item"/>
        <result property="auditOpTime" column="audit_op_time"/>
        <result property="auditMsg" column="audit_msg"/>
        <result property="auditStatus" column="audit_status"/>
    </resultMap>

    <sql id="selectAuditItemsVo">
        select id, bill_no, bill_id, act_id, path_id, level_id, refno1, iffinal_item, refno2, refno3, branch_id, send_user_id, send_name, send_time, send_msg, audit_user_id, audit_item,audit_op_time, audit_msg, audit_status from audit_items
    </sql>

    <select id="selectAuditItemsList" parameterType="AuditItems" resultMap="AuditItemsResult">
        <include refid="selectAuditItemsVo"/>
        <where>
            <if test="billNo != null  and billNo != ''">and bill_no = #{billNo}</if>
            <if test="billId != null ">and bill_id = #{billId}</if>
            <if test="actId != null ">and act_id = #{actId}</if>
            <if test="pathId != null ">and path_id = #{pathId}</if>
            <if test="levelId != null ">and level_id = #{levelId}</if>
            <if test="refno1 != null  and refno1 != ''">and refno1 = #{refno1}</if>
            <if test="iffinalItem != null  and iffinalItem != ''">and iffinal_item = #{iffinalItem}</if>
            <if test="refno2 != null  and refno2 != ''">and refno2 = #{refno2}</if>
            <if test="refno3 != null  and refno3 != ''">and refno3 = #{refno3}</if>
            <if test="branchId != null ">and branch_id = #{branchId}</if>
            <if test="sendUserId != null ">and send_user_id = #{sendUserId}</if>
            <if test="sendName != null  and sendName != ''">and send_name like concat('%', #{sendName}, '%')</if>
            <if test="sendTime != null ">and send_time = #{sendTime}</if>
            <if test="sendMsg != null  and sendMsg != ''">and send_msg = #{sendMsg}</if>
            <if test="auditUserId != null ">and audit_user_id = #{auditUserId}</if>
            <if test="auditItem != null ">and audit_item = #{auditItem}</if>
            <if test="auditOpTime != null ">and audit_op_time = #{auditOpTime}</if>
            <if test="auditMsg != null  and auditMsg != ''">and audit_msg = #{auditMsg}</if>
            <if test="auditStatus != null  and auditStatus != ''">and audit_status = #{auditStatus}</if>
        </where>
    </select>

    <select id="selectAuditItemsById" parameterType="Long" resultMap="AuditItemsResult">
        <include refid="selectAuditItemsVo"/>
        where id = #{id}
    </select>

    <insert id="insertAuditItems" parameterType="AuditItems" useGeneratedKeys="true" keyProperty="id">
        insert into audit_items
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="billNo != null">bill_no,</if>
            <if test="billId != null">bill_id,</if>
            <if test="actId != null">act_id,</if>
            <if test="pathId != null">path_id,</if>
            <if test="levelId != null">level_id,</if>
            <if test="refno1 != null">refno1,</if>
            <if test="iffinalItem != null">iffinal_item,</if>
            <if test="refno2 != null">refno2,</if>
            <if test="refno3 != null">refno3,</if>
            <if test="branchId != null">branch_id,</if>
            <if test="sendUserId != null">send_user_id,</if>
            <if test="sendName != null">send_name,</if>
            <if test="sendTime != null">send_time,</if>
            <if test="sendMsg != null">send_msg,</if>
            <if test="auditUserId != null">audit_user_id,</if>
            <if test="auditItem != null">audit_item,</if>
            <if test="auditOpTime != null ">audit_op_time,</if>
            <if test="auditMsg != null">audit_msg,</if>
            <if test="auditStatus != null">audit_status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="billNo != null">#{billNo},</if>
            <if test="billId != null">#{billId},</if>
            <if test="actId != null">#{actId},</if>
            <if test="pathId != null">#{pathId},</if>
            <if test="levelId != null">#{levelId},</if>
            <if test="refno1 != null">#{refno1},</if>
            <if test="iffinalItem != null">#{iffinalItem},</if>
            <if test="refno2 != null">#{refno2},</if>
            <if test="refno3 != null">#{refno3},</if>
            <if test="branchId != null">#{branchId},</if>
            <if test="sendUserId != null">#{sendUserId},</if>
            <if test="sendName != null">#{sendName},</if>
            <if test="sendTime != null">#{sendTime},</if>
            <if test="sendMsg != null">#{sendMsg},</if>
            <if test="auditUserId != null">#{auditUserId},</if>
            <if test="auditItem != null">#{auditItem},</if>
            <if test="auditOpTime != null ">#{auditOpTime},</if>
            <if test="auditMsg != null">#{auditMsg},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
        </trim>
    </insert>

    <update id="updateAuditItems" parameterType="AuditItems">
        update audit_items
        <trim prefix="SET" suffixOverrides=",">
            <if test="billNo != null">bill_no = #{billNo},</if>
            <if test="billId != null">bill_id = #{billId},</if>
            <if test="actId != null">act_id = #{actId},</if>
            <if test="pathId != null">path_id = #{pathId},</if>
            <if test="levelId != null">level_id = #{levelId},</if>
            <if test="refno1 != null">refno1 = #{refno1},</if>
            <if test="iffinalItem != null">iffinal_item = #{iffinalItem},</if>
            <if test="refno2 != null">refno2 = #{refno2},</if>
            <if test="refno3 != null">refno3 = #{refno3},</if>
            <if test="branchId != null">branch_id = #{branchId},</if>
            <if test="sendUserId != null">send_user_id = #{sendUserId},</if>
            <if test="sendName != null">send_name = #{sendName},</if>
            <if test="sendTime != null">send_time = #{sendTime},</if>
            <if test="sendMsg != null">send_msg = #{sendMsg},</if>
            <if test="auditUserId != null">audit_user_id = #{auditUserId},</if>
            <if test="auditItem != null">audit_item = #{auditItem},</if>
            <if test="auditOpTime != null ">audit_op_time = #{auditOpTime},</if>
            <if test="auditMsg != null">audit_msg = #{auditMsg},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAuditItemsById" parameterType="Long">
        delete from audit_items where id = #{id}
    </delete>

    <delete id="deleteAuditItemsByIds" parameterType="String">
        delete from audit_items where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectAuditItems" parameterType="AuditItems" resultType="Map">
        SELECT
            item.id,
            item.act_id AS actId,
            item.bill_id AS billId,
            corp.f_name AS refno1,
            item.refno2,
            item.refno3,
            usr.user_name AS auditUserName,
            usr.nick_name AS nickName,
            item.send_time AS auditItem,
            item.audit_msg AS auditMsg,
            item.audit_status AS auditStatus
        FROM
            audit_items item
            LEFT JOIN t_corps corp ON corp.f_id = item.refno1
            LEFT JOIN audit_items_users us ON us.pid = item.id
            LEFT JOIN sys_user usr ON usr.user_id = item.audit_user_id
        where
            us.audit_status = 'S'
            AND item.audit_status = 'S'
            AND us.user_id = #{auditUserId}
        ORDER BY
            item.send_time DESC
    </select>

    <delete id="deleteUpLevelId" parameterType="com.ruoyi.approvalFlow.domain.AuditItems">
        DELETE
            item.*,
            userItem.*
        FROM
            audit_items item
            LEFT JOIN audit_items_users userItem ON userItem.pid = item.id
        WHERE
            item.id &gt; #{id}
            AND item.bill_id = #{billId}
            AND item.level_id &gt; #{levelId}
    </delete>

    <select id="selectWarehouseApprover" parameterType="com.ruoyi.approvalFlow.domain.AuditItems" resultType="Map">
        SELECT
            aud.id,
            us.user_name AS userName,
            us.nick_name AS nickName,
            aud.audit_status AS auditStatus,
            aud.audit_msg AS auditMsg,
            aud.audit_item AS auditItem,
            aud.audit_op_time AS auditOpTime
        FROM
            audit_items aud
            LEFT JOIN sys_user us ON us.user_id = aud.audit_user_id
        WHERE
            aud.act_id = #{actId}
            AND aud.bill_id = #{id}
    </select>

    <delete id="deletePurchaseApproval" parameterType="com.ruoyi.approvalFlow.domain.AuditItems">
        DELETE item, userItem
        FROM
            audit_items item
            LEFT JOIN audit_items_users userItem ON userItem.pid = item.id
        WHERE
            item.act_id = #{actId}
        AND item.level_id &gt;= 0
        AND item.bill_id = #{billId}
    </delete>

    <update id="updateAuditStatus" parameterType="com.ruoyi.approvalFlow.domain.AuditItems">
        UPDATE audit_items it
        LEFT JOIN audit_items_users us ON us.pid = it.id
        SET it.audit_status = 'S', us.audit_status = 'S'
        WHERE
        it.level_id = #{levelId}
        AND it.bill_no = #{billNo}
        AND it.bill_id = #{billId}
        AND it.act_id = #{actId}
        AND it.audit_status = "N"
    </update>

</mapper>