<?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="actId"    column="act_id"    />
        <result property="billId"    column="bill_id"    />
        <result property="billNo"    column="bill_no"    />
        <result property="refno1"    column="refno1"    />
        <result property="refno2"    column="refno2"    />
        <result property="refno3"    column="refno3"    />
        <result property="pathId"    column="path_id"    />
        <result property="levelId"    column="level_id"    />
        <result property="iffinalItem"    column="iffinal_item"    />
        <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"    />
        <result property="billTime"    column="bill_time"    />
        <result property="fidStatus"    column="fid_status"    />
        <result property="times"    column="times"    />
    </resultMap>

    <resultMap type="com.ruoyi.approvalFlow.domain.vo.AppAuditItemVO" id="AppQueryResult">
        <result property="id"    column="id"    />
        <result property="actId"    column="act_id"    />
        <result property="billId"    column="f_id"    />
        <result property="type"    column="type"    />
        <result property="fMblno"    column="f_mblno"    />
        <result property="fTrademodeid"    column="f_trademodeid"    />
        <result property="fQty"    column="f_qty"    />
        <result property="fBsdate"    column="f_bsdate"    />
        <result property="refno4"    column="f_billtype"    />
        <result property="auditState"    column="audit_status"    />
    </resultMap>

    <sql id="selectAuditItemsVo">
        select id, act_id, bill_id, bill_no, refno1, refno2, refno3, path_id, level_id, iffinal_item, branch_id, send_user_id, send_name,
         send_time, send_msg, audit_user_id, audit_item, audit_op_time, audit_msg, audit_status, bill_time, fid_status, times 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>
            <if test="billTime != null "> and bill_time = #{billTime},</if>
            <if test="fidStatus != null  and fidStatus != ''"> and fid_status = #{fidStatus}</if>
            <if test="times != null "> and times = #{times}</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="actId != null">act_id,</if>
            <if test="billId != null">bill_id,</if>
            <if test="billNo != null">bill_no,</if>
            <if test="refno1 != null">refno1,</if>
            <if test="refno2 != null">refno2,</if>
            <if test="refno3 != null">refno3,</if>
            <if test="pathId != null">path_id,</if>
            <if test="levelId != null">level_id,</if>
            <if test="iffinalItem != null">iffinal_item,</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>
            <if test="billTime != null">bill_time,</if>
            <if test="fidStatus != null">fid_status,</if>
            <if test="times != null">times,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="actId != null">#{actId},</if>
            <if test="billId != null">#{billId},</if>
            <if test="billNo != null">#{billNo},</if>
            <if test="refno1 != null">#{refno1},</if>
            <if test="refno2 != null">#{refno2},</if>
            <if test="refno3 != null">#{refno3},</if>
            <if test="pathId != null">#{pathId},</if>
            <if test="levelId != null">#{levelId},</if>
            <if test="iffinalItem != null">#{iffinalItem},</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>
            <if test="billTime != null">#{billTime},</if>
            <if test="fidStatus != null">#{fidStatus},</if>
            <if test="times != null">#{times},</if>
        </trim>
    </insert>

    <update id="updateAuditItems" parameterType="AuditItems">
        update audit_items
        <trim prefix="SET" suffixOverrides=",">
            <if test="actId != null">act_id = #{actId},</if>
            <if test="billId != null">bill_id = #{billId},</if>
            <if test="billNo != null">bill_no = #{billNo},</if>
            <if test="refno1 != null">refno1 = #{refno1},</if>
            <if test="refno2 != null">refno2 = #{refno2},</if>
            <if test="refno3 != null">refno3 = #{refno3},</if>
            <if test="pathId != null">path_id = #{pathId},</if>
            <if test="levelId != null">level_id = #{levelId},</if>
            <if test="iffinalItem != null">iffinal_item = #{iffinalItem},</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>
            <if test="billTime != null">bill_time = #{billTime},</if>
            <if test="fidStatus != null">fid_status = #{fidStatus},</if>
            <if test="times != null">times = #{times},</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="selectCountAuditItems" resultType="java.lang.Integer">
        SELECT count(*)
        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'
            <if test="billNo != null  and billNo != ''"> and item.bill_no = #{billNo}</if>
            <if test="actId != null  and actId != ''"> and item.act_id = #{actId}</if>
            <if test="refno2 != null  and refno2 != ''"> and item.refno2 = #{refno2}</if>
            <if test="billId != null  and billId != ''"> and item.bill_id like concat('%', #{billId}, '%')</if>
            <if test="refno1 != null  and refno1 != ''"> and corp.f_name like concat('%', #{refno1}, '%')</if>
            <if test="sendUserId != null  and sendUserId != ''"> and item.send_user_id like concat('%', #{sendUserId}, '%')</if>
            <if test="billTime != null  and billTime != ''"> and item.bill_time = #{billTime}</if>
            <if test="fidStatus != null  and fidStatus != ''"> and item.fid_status = #{fidStatus}</if>
            <if test="auditUserId != null  and auditUserId != ''"> AND us.user_id = #{auditUserId}</if>
            <if test='sendTime != null and sendTime[0] != null and sendTime[0]!= ""'>
                and item.send_time &gt;= #{sendTime[0]}
            </if>
            <if test='sendTime != null and sendTime[1] != null and sendTime[1]!= ""'>
                and item.send_time &lt;= #{sendTime[1]}
            </if>
            <if test='auditOpTime != null and auditOpTime[0] != null and auditOpTime[0]!= ""'>
                and item.audit_op_time &gt;= #{auditOpTime[0]}
            </if>
            <if test='auditOpTime != null and auditOpTime[1] != null and auditOpTime[1]!= ""'>
                and item.audit_op_time &lt;= #{auditOpTime[1]}
            </if>
        ORDER BY
        item.send_time DESC
    </select>

    <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,
            item.refno2 AS refno4,
            item.bill_no AS billNo,
            us.user_id AS userId,
            item.audit_item,
            item.audit_user_id AS auditUserId,
            CASE
            item.audit_status
            WHEN 'O' THEN
            '提交'
            WHEN 'N' THEN
            '审核中'
            WHEN 'S' THEN
            '待审'
            WHEN 'B' THEN
            '审核退回'
            WHEN 'A' THEN
            '审核通过'
            END audit,
            item.send_user_id AS sendUserId,
            usr.user_name AS auditUserName,
            usr.nick_name AS nickName,
            item.send_time AS sendTime,
            item.audit_op_time AS auditOpTime,
            item.bill_time AS billTime,
            item.fid_status AS fidStatus,
            item.audit_msg AS auditMsg
        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>
            <if test="billNo != null  and billNo != ''"> and item.bill_no = #{billNo}</if>
            <if test="actId != null  and actId != ''"> and item.act_id = #{actId}</if>
            <if test="refno2 != null  and refno2 != ''"> and item.refno2 = #{refno2}</if>
            <if test="refno3 != null  and refno3 != ''">
                and (item.refno3 like concat('%', #{refno3}, '%') or item.bill_no like concat('%', #{refno3}, '%'))
            </if>
            <if test="billId != null  and billId != ''"> and item.bill_id like concat('%', #{billId}, '%')</if>
            <if test="refno1 != null  and refno1 != ''"> and corp.f_name like concat('%', #{refno1}, '%')</if>
            <if test="sendUserId != null  and sendUserId != ''"> and item.send_user_id like concat('%', #{sendUserId}, '%')</if>
            <if test="billTime != null  and billTime != ''"> and item.bill_time = #{billTime}</if>
            <if test="fidStatus != null  and fidStatus != ''"> AND item.fid_status = #{fidStatus}</if>
            <if test="auditStatus != null  and auditStatus != ''"> AND item.audit_status = #{auditStatus} and us.audit_status =  #{auditStatus}</if>
            <if test="auditUserId != null  and auditUserId != ''"> AND us.user_id = #{auditUserId}</if>
            <if test='sendTime != null and sendTime[0] != null and sendTime[0]!= ""'>
                and item.send_time &gt;= #{sendTime[0]}
            </if>
            <if test='sendTime != null and sendTime[1] != null and sendTime[1]!= ""'>
                and item.send_time &lt;= #{sendTime[1]}
            </if>
            <if test='auditOpTime != null and auditOpTime[0] != null and auditOpTime[0]!= ""'>
                and item.audit_op_time &gt;= #{auditOpTime[0]}
            </if>
            <if test='auditOpTime != null and auditOpTime[1] != null and auditOpTime[1]!= ""'>
                and item.audit_op_time &lt;= #{auditOpTime[1]}
            </if>
        </where>
        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}
            AND item.fid_status = #{fidStatus}
    </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,
            aud.bill_time AS billTime
        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}
            AND aud.fid_status = #{fidStatus}
    </select>

    <select id="selectListByRefno1AndActId" resultType="java.lang.String">
        SELECT
            u.user_name
        FROM
            audit_items a LEFT JOIN audit_items_users i ON i.pid = a.id LEFT JOIN sys_user u ON u.user_id = i.user_id
        WHERE
            a.act_id in
            <foreach item="id" collection="actId" open="(" separator="," close=")">
                #{id}
            </foreach>
            AND a.bill_id = #{id}
            AND a.audit_status = 'A'
            AND i.audit_status = 'A'
        ORDER BY
            a.send_time DESC
        limit 0, 2
    </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 = #{id}
        AND item.fid_status = #{fidStatus}
    </delete>
    <delete id="deleteAuditItemsByBillId" parameterType="Long">
        DELETE
	        ai.*,
            au.*
        FROM
            audit_items ai
        LEFT JOIN audit_items_users au
        ON ai.id = au.pid
        WHERE ai.bill_id = #{id}
    </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"
        AND it.fid_status = #{fidStatus}
    </update>

    <select id="selectOrderAuditItems" resultMap="AppQueryResult">
        SELECT
            a.id,
            w.f_bsdate,
            w.f_id,
            w.f_billtype,
            a.act_id,
            a.audit_status,
            '仓库' type,
            w.f_mblno,
            w.f_trademodeid,
            w.f_qty,
            g.f_name goodsName,
            ware.f_name warehouseName
        FROM
            audit_items a
            LEFT JOIN t_warehousebills w ON w.f_id = a.bill_id
            LEFT JOIN t_corps t ON t.f_id = w.f_corpid
            LEFT JOIN t_goods g ON g.f_id = w.f_goodsid
            LEFT JOIN t_warehouse ware ON ware.f_id = IF ( w.f_billtype = 'CKDB', w.f_inwarehouseid, w.f_warehouseid )
        <where>
            w.f_billtype in
            <foreach collection="audit.billTypeList" item="type" index="index" open="(" close=")" separator=",">
                #{type}
            </foreach>
            and a.act_id in
            <foreach collection="audit.actIdList" item="actId" index="index" open="(" close=")" separator=",">
                #{actId}
            </foreach>
            and a.audit_user_id = #{audit.auditUserId}
            <if test='audit.auditStatus != null and audit.auditStatus!= ""'>
                and a.audit_status = #{audit.auditStatus}
            </if>
            <if test='audit.corpId != null and audit.corpId!= ""'>
                and w.f_corpid = #{audit.corpId}
            </if>
            <if test='audit.fShipper != null and audit.fShipper!= ""'>
                and w.f_shipper like concat('%', #{audit.fShipper}, '%')
            </if>
            <if test='audit.sendList != null and audit.sendList[1]!= ""'>
                and a.send_time BETWEEN #{audit.sendList[0]} AND #{audit.sendList[1]}
            </if>
        </where>
            order by a.id desc
    </select>

</mapper>
