package org.dromara.web.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.dromara.web.domain.ProductInOutRecord;
import org.dromara.web.domain.vo.FoodStockBalanceVo;
import org.dromara.web.domain.vo.ProductInOutRecordVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 产品出入库记录Mapper接口
 *
 * @author Lion Li
 * @date 2025-08-20
 */
@Repository
public interface ProductInOutRecordMapper extends BaseMapperPlus<ProductInOutRecord, ProductInOutRecordVo> {
    @Select(" <script> SELECT  pr.product_id AS productId, pr.category_id AS categoryId, pr.location_id AS locationId,pr.serial_no AS serialNo,pr.product_name AS productName, pr.product_type as productType, " +
        "pr.category_name AS categoryName,pr.location_name AS locationName,pr.product_no AS productNo,SUM( CASE WHEN pr.type = '0' THEN pr.num ELSE - pr.num END ) AS stockNum," +
        "pr.product_batch_no as productBatchNo,pr.unit, ANY_VALUE(pr.specs_name) as specsName, ANY_VALUE(pr.line_no) as lineNo,ANY_VALUE(pr.invoice_no) as invoiceNo, pr.price, " +
        "ANY_VALUE(pr.warning_month) as warningMonth,ANY_VALUE(pr.manufacture_date) as manufactureDate,ANY_VALUE(pr.expiration_date) as expirationDate  " +
        "FROM product_in_out_record pr " +
        "WHERE 1 = 1" +
        "<if test='searchValue != null and searchValue != \"\"'> and (pr.product_name like concat('%', #{searchValue}, '%')) </if>" +
        "GROUP BY pr.product_id,pr.category_id,pr.location_id,pr.serial_no,pr.product_name,pr.category_name,pr.location_name,pr.product_no,pr.product_batch_no,pr.unit,pr.product_type,pr.price  " +
        "<if test='offset != null and rows != null'>LIMIT #{offset}, #{rows}</if>" +
        "</script>" )
    List<ProductInOutRecordVo> queryStockBalance( @Param("searchValue") String searchValue, @Param("offset") Integer offset, @Param("rows") Integer rows);

    @Select(" <script> SELECT COUNT(*)  FROM ( SELECT 1  FROM product_in_out_record pr WHERE 1 = 1 " +
        "    <if test='searchValue != null and searchValue != \"\"'> " +
        "        AND (pr.product_name LIKE CONCAT('%', #{searchValue}, '%') ) </if> " +
        "    GROUP BY pr.product_id,pr.category_id,pr.location_id,pr.serial_no,pr.product_name,pr.category_name,pr.location_name,pr.product_no,pr.product_batch_no,pr.unit " +
        ") AS grouped_data " +
        "</script>" )
    Integer queryStockBalanceCount( @Param("searchValue") String searchValue);
}
