<?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.wansenai.mappers.product.ProductStockMapper">
    <resultMap id="productResultMap" type="com.wansenai.vo.product.ProductStockKeepUnitVO">
        <id property="id" column="id" />
        <result property="productId" column="productId" />
        <result property="productCategoryId" column="product_category_id" />
        <result property="barCode" column="product_bar_code" />
        <result property="productName" column="product_name" />
        <result property="productCategoryName" column="category_name" />
        <result property="productStandard" column="product_standard" />
        <result property="productModel" column="product_model" />
        <result property="productColor" column="product_color" />
        <result property="productUnit" column="product_unit" />
        <result property="multiAttribute" column="multi_attribute" />
        <result property="stock" column="current_stock_quantity" />
        <result property="extendInfo" column="product_manufacturer" />
        <result property="retailPrice" column="retail_price" />
        <result property="salePrice" column="sale_price" />
        <result property="purchasePrice" column="purchase_price" />
    </resultMap>

    <select id="getProductSkuList" resultMap="productResultMap" parameterType="com.baomidou.mybatisplus.core.metadata.IPage">
        SELECT ps.id,
        p.id AS productId,
        p.product_category_id,
        p.product_name,
        p.product_model,
        p.product_standard,
        p.product_color,
        sku.product_bar_code,
        sku.multi_attribute,
        sku.product_unit,
        sku.retail_price,
        sku.sale_price,
        sku.purchase_price,
        p.product_manufacturer,
        pc.category_name,
        ps.current_stock_quantity,
        w.warehouse_name,
        ps.warehouse_id
        FROM product_stock AS ps
        LEFT JOIN warehouse AS w ON ps.warehouse_id = w.id
        LEFT JOIN product_sku AS sku ON sku.id = ps.product_sku_id
        LEFT JOIN product AS p ON sku.product_id = p.id
        LEFT JOIN product_category AS pc ON p.product_category_id = pc.id
        <where>
            <if test="queryProductStockKeepUnitDTO.productCategoryId != null">
                AND p.product_category_id = #{queryProductStockKeepUnitDTO.productCategoryId}
            </if>
            <if test="queryProductStockKeepUnitDTO.warehouseId != null">
                AND ps.warehouse_id = #{queryProductStockKeepUnitDTO.warehouseId}
            </if>
            <if test="queryProductStockKeepUnitDTO.productName != null">
                AND p.product_name = #{queryProductStockKeepUnitDTO.productName}
            </if>
            <if test="queryProductStockKeepUnitDTO.enableSerialNumber != null">
                AND p.enable_serial_number = #{queryProductStockKeepUnitDTO.enableSerialNumber}
            </if>
            <if test="queryProductStockKeepUnitDTO.enableBatchNumber != null">
                AND p.enable_batch_number = #{queryProductStockKeepUnitDTO.enableBatchNumber}
            </if>
            <if test="queryProductStockKeepUnitDTO.keywords != null">
                AND (
                p.product_name LIKE CONCAT('%', #{queryProductStockKeepUnitDTO.keywords}, '%')
                OR p.product_standard LIKE CONCAT('%', #{queryProductStockKeepUnitDTO.keywords}, '%')
                OR p.product_model LIKE CONCAT('%', #{queryProductStockKeepUnitDTO.keywords}, '%')
                OR p.product_color LIKE CONCAT('%', #{queryProductStockKeepUnitDTO.keywords}, '%')
                )
            </if>
        </where>
    </select>

    <select id="getProductSkuByBarCode" resultMap="productResultMap">
        SELECT sku.id, p.id AS productId, p.product_category_id, p.product_name, p.product_model, p.product_standard, p.product_color,
               sku.product_bar_code, sku.multi_attribute, sku.product_unit, sku.retail_price, sku.sale_price, sku.purchase_price, p.product_manufacturer,
               pc.category_name, ps.current_stock_quantity, ps.warehouse_id
        FROM product_stock AS ps
                 LEFT JOIN product_sku AS sku ON sku.id = ps.product_sku_id
                 LEFT JOIN product AS p ON sku.product_id = p.id
                 LEFT JOIN product_category AS pc ON p.product_category_id = pc.id
        WHERE sku.product_bar_code = #{barCode} AND ps.warehouse_id = #{warehouseId}
    </select>

    <select id="getProductSkuDetail" resultMap="productResultMap">
        SELECT sku.id, p.id AS productId, p.product_category_id, p.product_name, p.product_model, p.product_standard, p.product_color,
               sku.product_bar_code, sku.multi_attribute, sku.product_unit, sku.retail_price, sku.sale_price, sku.purchase_price, p.product_manufacturer,
               pc.category_name, ps.current_stock_quantity, ps.warehouse_id
        FROM product_stock AS ps
                 LEFT JOIN product_sku AS sku ON sku.id = ps.product_sku_id
                 LEFT JOIN product AS p ON sku.product_id = p.id
                 LEFT JOIN product_category AS pc ON p.product_category_id = pc.id
        WHERE p.id= #{productId} AND ps.warehouse_id = #{warehouseId} AND sku.product_bar_code = #{barCode}
    </select>

    <select id="getProductStock" resultType="com.wansenai.vo.report.ProductStockSkuVO" parameterType="com.baomidou.mybatisplus.extension.plugins.pagination.Page">
        SELECT ps.id, p.product_name, p.id AS productId, p.product_model, p.product_standard, p.product_color,sku.product_bar_code, p.product_weight, p.warehouse_shelves,
        sku.product_unit, sku.retail_price, sku.sale_price, sku.purchase_price, p.product_manufacturer,pc.category_name AS product_category_name,
        ps.current_stock_quantity AS current_stock, ps.init_stock_quantity AS initial_stock, w.warehouse_name, w.id AS warehouseId,
        ROUND((ps.current_stock_quantity * sku.retail_price), 2) AS stock_amount
        FROM product_stock AS ps
        LEFT JOIN warehouse as w ON ps.warehouse_id = w.id
        LEFT JOIN product_sku AS sku ON sku.id = ps.product_sku_id
        LEFT JOIN product AS p ON sku.product_id = p.id
        LEFT JOIN product_category AS pc ON p.product_category_id = pc.id
        <where>
            <if test="queryProductStockDTO.productCategoryId != null">
                AND p.product_category_id = #{queryProductStockDTO.productCategoryId}
            </if>
            <if test="queryProductStockDTO.warehouseId != null">
                AND ps.warehouse_id = #{queryProductStockDTO.warehouseId}
            </if>
            <if test="queryProductStockDTO.warehouseShelves != null">
                AND p.warehouse_shelves = #{queryProductStockDTO.warehouseShelves}
            </if>
            <if test="queryProductStockDTO.productInfo != null">
                AND p.product_name LIKE CONCAT('%', #{queryProductStockDTO.productInfo}, '%')
            </if>
        </where>
    </select>

    <select id="getProductStockListByTerms" resultType="com.wansenai.vo.report.ProductStockSkuVO" parameterType="com.wansenai.dto.report.QueryProductStockDTO">
        SELECT ps.id, p.product_name, p.id AS productId, p.product_model, p.product_standard, p.product_color,sku.product_bar_code, p.product_weight, p.warehouse_shelves,
        sku.product_unit, sku.retail_price, sku.sale_price, sku.purchase_price, p.product_manufacturer,pc.category_name AS product_category_name,
        ps.current_stock_quantity AS current_stock, ps.init_stock_quantity AS initial_stock, w.warehouse_name, w.id AS warehouseId,
        ROUND((ps.current_stock_quantity * sku.retail_price), 2) AS stock_amount
        FROM product_stock AS ps
        INNER JOIN warehouse as w ON ps.warehouse_id = w.id
        INNER JOIN product_sku AS sku ON sku.id = ps.product_sku_id
        INNER JOIN product AS p ON sku.product_id = p.id
        LEFT JOIN product_category AS pc ON p.product_category_id = pc.id
        <where>
            <if test="productCategoryId != null">
                AND p.product_category_id = #{productCategoryId}
            </if>
            <if test="warehouseId != null">
                AND ps.warehouse_id = #{warehouseId}
            </if>
            <if test="warehouseShelves != null">
                AND p.warehouse_shelves = #{warehouseShelves}
            </if>
            <if test="productInfo != null">
                AND p.product_name LIKE CONCAT('%', #{productInfo}, '%')
            </if>
        </where>
    </select>

    <select id="getProductStockList" resultType="com.wansenai.vo.report.ProductStockSkuVO">
        SELECT ps.id, p.product_name, p.id AS productId, p.product_model, p.product_standard, p.product_color,sku.product_bar_code, p.product_weight, p.warehouse_shelves,
               sku.product_unit, sku.retail_price, sku.sale_price, sku.purchase_price, p.product_manufacturer,pc.category_name AS product_category_name,
               ps.current_stock_quantity AS current_stock, ps.init_stock_quantity AS initial_stock, w.warehouse_name, w.id AS warehouseId,
               ROUND((ps.current_stock_quantity * sku.retail_price), 2) AS stock_amount
        FROM product_stock AS ps
                 INNER JOIN warehouse as w ON ps.warehouse_id = w.id
                 INNER JOIN product_sku AS sku ON sku.id = ps.product_sku_id
                 INNER JOIN product AS p ON sku.product_id = p.id
                 LEFT JOIN product_category AS pc ON p.product_category_id = pc.id
    </select>

    <select id="productStockExist" resultType="boolean" parameterType="map">
        SELECT COUNT(1) > 0
        FROM product_stock
        WHERE product_sku_id = #{productSkuId}
          AND warehouse_id = #{warehouseId}
    </select>

    <insert id="saveBatch" parameterType="java.util.List">
        INSERT INTO product_stock (id, tenant_id, product_sku_id, warehouse_id,
        init_stock_quantity, high_stock_quantity, current_stock_quantity, create_time, update_time, create_by, update_by, delete_flag)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id},
            #{item.tenantId},
            #{item.productSkuId},
            #{item.warehouseId},
            #{item.initStockQuantity},
            #{item.highStockQuantity},
            #{item.currentStockQuantity},
            #{item.createTime},
            #{item.updateTime},
            #{item.createBy},
            #{item.updateBy},
            #{item.deleteFlag}
            )
        </foreach>
    </insert>
</mapper>
