package com.haha.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haha.entity.InventoryLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 库存变动日志Mapper
 */
@Mapper
public interface InventoryLogMapper extends BaseMapper<InventoryLog> {

    /**
     * 查询设备库存变动统计
     */
    @Select("SELECT change_type, SUM(ABS(change_quantity)) as total_quantity, COUNT(*) as count " +
            "FROM t_inventory_log " +
            "WHERE device_id = #{deviceId} " +
            "AND create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY change_type")
    List<Map<String, Object>> selectChangeStatistics(@Param("deviceId") String deviceId,
                                                      @Param("startTime") String startTime,
                                                      @Param("endTime") String endTime);

    /**
     * 查询商品变动记录（带详情）
     */
    @Select("SELECT il.*, d.name as device_name " +
            "FROM t_inventory_log il " +
            "LEFT JOIN t_device d ON il.device_id = d.device_id " +
            "WHERE il.product_code = #{productCode} " +
            "ORDER BY il.create_time DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> selectByProductCode(@Param("productCode") String productCode, @Param("limit") int limit);
}
