package com.haha.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haha.entity.StockRecord;
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 StockRecordMapper extends BaseMapper<StockRecord> {

    /**
     * 查询上货记录（带设备名称）
     */
    @Select("SELECT sr.*, d.name as device_name " +
            "FROM t_stock_record sr " +
            "LEFT JOIN t_device d ON sr.device_id = d.device_id " +
            "WHERE sr.id = #{id}")
    Map<String, Object> selectWithDeviceName(@Param("id") Long id);

    /**
     * 统计上货员工作数据
     */
    @Select("SELECT stocker_id, stocker_name, " +
            "COUNT(*) as total_activities, " +
            "SUM(total_quantity) as total_quantity, " +
            "SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as completed_count " +
            "FROM t_stock_record " +
            "WHERE stocker_id = #{stockerId} " +
            "AND create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY stocker_id, stocker_name")
    Map<String, Object> selectStockerStatistics(@Param("stockerId") Long stockerId,
                                                 @Param("startTime") String startTime,
                                                 @Param("endTime") String endTime);
}
