<?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.zg.project.wisdom.mapper.RkInfoMapper">

    <!-- ========================= resultMap ========================= -->
    <resultMap type="RkInfo" id="RkInfoResult">
        <result property="id"            column="id"/>
        <result property="operationType" column="operation_type"/>
        <result property="operationTypeName" column="operation_type_name"/>
        <result property="bizType"       column="biz_type"/>
        <result property="wlType"        column="wl_type"/>
        <result property="wlTypeName"    column="wl_type_name"/>
        <result property="cangku"        column="cangku"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="parentWarehouseCode" column="parent_warehouse_code"/>
        <result property="parentWarehouseName" column="parent_warehouse_name"/>
        <result property="operationTime" column="operation_time"/>
        <result property="stockAge"      column="stock_age"/>
        <result property="operator"      column="operator"/>
        <result property="operatorName"  column="operator_name"/>
        <result property="isChuku"       column="is_chuku"/>
        <result property="status"        column="status"/>
        <result property="execStatus"    column="exec_status"/>
        <result property="remark"        column="remark"/>
        <result property="billNo"        column="bill_no"/>
        <result property="xj"            column="xj"/>
        <result property="xmNo"          column="xm_no"/>
        <result property="xmMs"          column="xm_ms"/>
        <result property="xmNoCk"        column="xm_no_ck"/>
        <result property="xmMsCk"        column="xm_ms_ck"/>
        <result property="wlNo"          column="wl_no"/>
        <result property="wlMs"          column="wl_ms"/>
        <result property="gysNo"         column="gys_no"/>
        <result property="gysMc"         column="gys_mc"/>
        <result property="jhAmt"         column="jh_amt"/>
        <result property="htDj"          column="ht_dj"/>
        <result property="sapNo"         column="sap_no"/>
        <result property="xh"            column="xh"/>
        <result property="jhQty"         column="jh_qty"/>
        <result property="htQty"         column="ht_qty"/>
        <result property="dw"            column="dw"/>
        <result property="realQty"       column="real_qty"/>
        <result property="pcode"         column="pcode"/>
        <result property="pcodeId"       column="pcode_id"/>
        <result property="trayCode"      column="tray_code"/>
        <result property="entityId"      column="entity_id"/>
        <result property="teamCode"      column="team_code"/>
        <result property="borrowTime"    column="borrow_time"/>
        <result property="returnTime"    column="return_time"/>
        <result property="hasMoved"      column="has_moved"/>
        <result property="isBorrowed"    column="is_borrowed"/>
        <result property="createBy"      column="create_by"/>
        <result property="createTime"    column="create_time"/>
        <result property="updateBy"      column="update_by"/>
        <result property="updateTime"    column="update_time"/>
        <result property="isDelete"      column="is_delete"/>
        <result property="gysJhId"       column="gys_jh_id"/>
        <result property="rdid"          column="rdid"/>
        <result property="rdidCk"        column="rdid_ck"/>
        <result property="sid"           column="sid"/>
        <result property="isDelivery"    column="is_delivery"/>
        <result property="fycde1"        column="fycde_1"/>
        <result property="fycde2"        column="fycde_2"/>
        <result property="isUpdate"      column="is_update"/>
    </resultMap>

    <!-- ========================= 公共查询 SQL（联表完整版） ========================= -->
    <sql id="selectRkInfoVo">
        SELECT
            ri.*,

            /* 出入库类型名称 */
            COALESCE(sit.type_name, sot.type_name) AS operation_type_name,

            /* 物资类型名称 */
            mt.type_name AS wl_type_name,

            /* 仓库信息 */
            wh.warehouse_name,
            wh.parent_warehouse_code,
            wh.parent_warehouse_name,

            /* 理货员 */
            su.nick_name AS operator_name,

            /* 库龄 */
            DATEDIFF(CURRENT_DATE, ri.operation_time) AS stock_age

        FROM rk_info ri

                 LEFT JOIN stock_in_type  sit ON ri.operation_type = sit.type_code
                 LEFT JOIN stock_out_type sot ON ri.operation_type = sot.type_code
                 LEFT JOIN material_type  mt  ON ri.wl_type        = mt.type_code
                 LEFT JOIN warehouse_info wh  ON ri.cangku         = wh.warehouse_code
                 LEFT JOIN sys_user       su  ON ri.operator       = su.user_id
    </sql>

    <!-- ========================= 查询列表 ========================= -->
    <select id="selectRkInfoList" parameterType="RkInfo" resultMap="RkInfoResult">
        <include refid="selectRkInfoVo"/>
        <where>
            ri.exec_status = 1
            AND ri.is_chuku = 0
            AND ri.is_delete = 0
            <if test="operationType != null and operationType != ''">
                AND ri.operation_type LIKE CONCAT('%', #{operationType}, '%')
            </if>
            <if test="sapNo != null and sapNo != ''">
                AND ri.sap_no LIKE CONCAT('%', #{sapNo}, '%')
            </if>
            <if test="xmNo != null and xmNo != ''">
                AND ri.xm_no LIKE CONCAT('%', #{xmNo}, '%')
            </if>
            <if test="xmMs != null and xmMs != ''">
                AND ri.xm_ms LIKE CONCAT('%', #{xmMs}, '%')
            </if>
            <if test="wlNo != null and wlNo != ''">
                AND ri.wl_no LIKE CONCAT('%', #{wlNo}, '%')
            </if>
            <if test="wlMs != null and wlMs != ''">
                AND ri.wl_ms LIKE CONCAT('%', #{wlMs}, '%')
            </if>
            <if test="gysMc != null and gysMc != ''">
                AND ri.gys_mc LIKE CONCAT('%', #{gysMc}, '%')
            </if>
            <if test="pcode != null and pcode != ''">
                AND ri.pcode LIKE CONCAT('%', #{pcode}, '%')
            </if>
            <if test="bizType != null and bizType != ''">
                AND ri.biz_type LIKE CONCAT('%', #{bizType}, '%')
            </if>
            <if test="wlType != null and wlType != ''">
                AND ri.wl_type LIKE CONCAT('%', #{wlType}, '%')
            </if>
            <if test="cangku != null and cangku != ''">
                AND ri.cangku LIKE CONCAT('%', #{cangku}, '%')
            </if>
            <if test="billNo != null and billNo != ''">
                AND ri.bill_no LIKE CONCAT('%', #{billNo}, '%')
            </if>
            <if test="isDelete != null and isDelete != ''">
                AND ri.is_delete = #{isDelete}
            </if>
            <!-- 出入库时间范围 -->
            <if test="startDate != null">
                AND ri.operation_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND ri.operation_time &lt;= #{endDate}
            </if>
        </where>
        ORDER BY ri.operation_time DESC
    </select>

    <!-- ========================= 按 ID 查询 ========================= -->
    <select id="selectRkInfoById" parameterType="Long" resultMap="RkInfoResult">
        <include refid="selectRkInfoVo"/>
        WHERE ri.id = #{id}
    </select>

    <select id="countByBillNo" resultType="int">
        SELECT COUNT(1)
        FROM rk_info
        WHERE bill_no = #{billNo}
    </select>

    <select id="countNotPreByIds" resultType="int">
        SELECT COUNT(1)
        FROM rk_info
        WHERE id IN
        <foreach collection="rkInfoIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND exec_status != '0'
    </select>

    <select id="selectRkInfoByIds"
            parameterType="java.util.List"
            resultMap="RkInfoResult">
        SELECT
        ri.*
        FROM rk_info ri
        WHERE ri.id IN
        <foreach collection="rkInfoIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectStockStatisticByCondition"
            parameterType="com.zg.project.wisdom.domain.RkInfo"
            resultType="com.zg.project.wisdom.domain.vo.StockStatisticVO">

        SELECT
        IFNULL(SUM(ri.real_qty * ri.ht_dj), 0)  AS total_amount,
        COUNT(DISTINCT ri.pcode)                AS location_count,
        IFNULL(SUM(ri.real_qty), 0)             AS total_quantity
        FROM rk_info ri
        <where>
            ri.exec_status = 1
            AND ri.is_chuku = 0
            AND ri.is_delete = 0

            <if test="operationType != null and operationType != ''">
                AND ri.operation_type LIKE CONCAT('%', #{operationType}, '%')
            </if>
            <if test="sapNo != null and sapNo != ''">
                AND ri.sap_no LIKE CONCAT('%', #{sapNo}, '%')
            </if>
            <if test="xmNo != null and xmNo != ''">
                AND ri.xm_no LIKE CONCAT('%', #{xmNo}, '%')
            </if>
            <if test="xmMs != null and xmMs != ''">
                AND ri.xm_ms LIKE CONCAT('%', #{xmMs}, '%')
            </if>
            <if test="wlNo != null and wlNo != ''">
                AND ri.wl_no LIKE CONCAT('%', #{wlNo}, '%')
            </if>
            <if test="wlMs != null and wlMs != ''">
                AND ri.wl_ms LIKE CONCAT('%', #{wlMs}, '%')
            </if>
            <if test="gysMc != null and gysMc != ''">
                AND ri.gys_mc LIKE CONCAT('%', #{gysMc}, '%')
            </if>
            <if test="pcode != null and pcode != ''">
                AND ri.pcode LIKE CONCAT('%', #{pcode}, '%')
            </if>
            <if test="bizType != null and bizType != ''">
                AND ri.biz_type LIKE CONCAT('%', #{bizType}, '%')
            </if>
            <if test="wlType != null and wlType != ''">
                AND ri.wl_type LIKE CONCAT('%', #{wlType}, '%')
            </if>
            <if test="cangku != null and cangku != ''">
                AND ri.cangku LIKE CONCAT('%', #{cangku}, '%')
            </if>
            <if test="billNo != null and billNo != ''">
                AND ri.bill_no LIKE CONCAT('%', #{billNo}, '%')
            </if>
            <if test="startDate != null">
                AND ri.operation_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND ri.operation_time &lt;= #{endDate}
            </if>
        </where>
    </select>

    <!-- ========================= 删除 ========================= -->
    <delete id="deleteRkInfoById" parameterType="Long">
        DELETE FROM rk_info WHERE id = #{id}
    </delete>

    <delete id="deleteRkInfoByIds">
        DELETE FROM rk_info
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- ========================= 插入 ========================= -->
    <insert id="insertRkInfo" parameterType="RkInfo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO rk_info (
            operation_type, biz_type, wl_type, cangku, operation_time, operator,
            is_chuku, status, exec_status, remark, bill_no,
            xj, xm_no, xm_ms, xm_no_ck, xm_ms_ck,
            wl_no, wl_ms,
            gys_no, gys_mc,
            jh_amt, ht_dj, sap_no, xh,
            jh_qty, ht_qty, dw, real_qty,
            pcode, pcode_id, tray_code, entity_id,
            team_code, borrow_time, return_time,
            has_moved, is_borrowed,
            gys_jh_id,
            rdid, rdid_ck, sid,
            is_delivery,
            fycde_1, fycde_2,
            is_update,
            create_by, create_time, is_delete
        )
        VALUES (
                   #{operationType}, #{bizType}, #{wlType}, #{cangku}, #{operationTime}, #{operator},
                   #{isChuku}, #{status}, #{execStatus}, #{remark}, #{billNo},
                   #{xj}, #{xmNo}, #{xmMs}, #{xmNoCk}, #{xmMsCk},
                   #{wlNo}, #{wlMs},
                   #{gysNo}, #{gysMc},
                   #{jhAmt}, #{htDj}, #{sapNo}, #{xh},
                   #{jhQty}, #{htQty}, #{dw}, #{realQty},
                   #{pcode}, #{pcodeId}, #{trayCode}, #{entityId},
                   #{teamCode}, #{borrowTime}, #{returnTime},
                   #{hasMoved}, #{isBorrowed},
                   #{gysJhId},
                   #{rdid}, #{rdidCk}, #{sid},
                   #{isDelivery},
                   #{fycde1}, #{fycde2},
                   #{isUpdate},
                   #{createBy}, #{createTime}, #{isDelete}
               )
    </insert>

    <!-- ========================= 更新 ========================= -->
    <update id="updateRkInfo" parameterType="RkInfo">
        UPDATE rk_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="operationType != null">operation_type = #{operationType},</if>
            <if test="bizType != null">biz_type = #{bizType},</if>
            <if test="wlType != null">wl_type = #{wlType},</if>
            <if test="cangku != null">cangku = #{cangku},</if>
            <if test="operationTime != null">operation_time = #{operationTime},</if>
            <if test="operator != null">operator = #{operator},</if>
            <if test="isChuku != null">is_chuku = #{isChuku},</if>
            <if test="status != null">status = #{status},</if>
            <if test="execStatus != null">exec_status = #{execStatus},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="billNo != null">bill_no = #{billNo},</if>
            <if test="xj != null">xj = #{xj},</if>
            <if test="xmNo != null">xm_no = #{xmNo},</if>
            <if test="xmMs != null">xm_ms = #{xmMs},</if>
            <if test="xmNoCk != null">xm_no_ck = #{xmNoCk},</if>
            <if test="xmMsCk != null">xm_ms_ck = #{xmMsCk},</if>
            <if test="wlNo != null">wl_no = #{wlNo},</if>
            <if test="wlMs != null">wl_ms = #{wlMs},</if>
            <if test="gysNo != null">gys_no = #{gysNo},</if>
            <if test="gysMc != null">gys_mc = #{gysMc},</if>
            <if test="jhAmt != null">jh_amt = #{jhAmt},</if>
            <if test="htDj != null">ht_dj = #{htDj},</if>
            <if test="sapNo != null">sap_no = #{sapNo},</if>
            <if test="xh != null">xh = #{xh},</if>
            <if test="jhQty != null">jh_qty = #{jhQty},</if>
            <if test="htQty != null">ht_qty = #{htQty},</if>
            <if test="dw != null">dw = #{dw},</if>
            <if test="realQty != null">real_qty = #{realQty},</if>
            <if test="pcode != null">pcode = #{pcode},</if>
            <if test="pcodeId != null">pcode_id = #{pcodeId},</if>
            <if test="trayCode != null">tray_code = #{trayCode},</if>
            <if test="entityId != null">entity_id = #{entityId},</if>
            <if test="teamCode != null">team_code = #{teamCode},</if>
            <if test="borrowTime != null">borrow_time = #{borrowTime},</if>
            <if test="returnTime != null">return_time = #{returnTime},</if>
            <if test="hasMoved != null">has_moved = #{hasMoved},</if>
            <if test="isBorrowed != null">is_borrowed = #{isBorrowed},</if>
            <if test="gysJhId != null">gys_jh_id = #{gysJhId},</if>
            <if test="rdid != null">rdid = #{rdid},</if>
            <if test="rdidCk != null">rdid_ck = #{rdidCk},</if>
            <if test="sid != null">sid = #{sid},</if>
            <if test="isDelivery != null">is_delivery = #{isDelivery},</if>
            <if test="fycde1 != null">fycde_1 = #{fycde1},</if>
            <if test="fycde2 != null">fycde_2 = #{fycde2},</if>
            <if test="isUpdate != null">is_update = #{isUpdate},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <update id="updateExecStatusByIds">
        UPDATE rk_info
        SET exec_status = #{execStatus}
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateRealQtyById">
        update rk_info
        set real_qty = #{realQty},
            update_time = now()
        where id = #{id}
    </update>

    <update id="updateIsChukuById">
        update rk_info
        set is_chuku = #{isChuku},
            update_time = now()
        where id = #{id}
    </update>

    <update id="updateBorrowReturn">
        UPDATE rk_info
        SET is_borrowed = #{isBorrowed},
            return_time = #{returnTime},
            update_time = NOW()
        WHERE id = #{id}
          AND is_delete = '0'
    </update>


</mapper>
