package com.haha.mapper;

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

    /**
     * 查询门店列表（带设备统计）
     */
    @Select("SELECT s.*, " +
            "COUNT(d.id) as device_count, " +
            "SUM(CASE WHEN d.status = 1 THEN 1 ELSE 0 END) as online_count " +
            "FROM t_shop s " +
            "LEFT JOIN t_device d ON s.id = d.shop_id " +
            "WHERE s.status = 1 " +
            "GROUP BY s.id " +
            "ORDER BY s.create_time DESC")
    List<Map<String, Object>> selectShopsWithDeviceStats();

    /**
     * 根据ID查询门店（带设备统计）
     */
    @Select("SELECT s.*, " +
            "COUNT(d.id) as device_count, " +
            "SUM(CASE WHEN d.status = 1 THEN 1 ELSE 0 END) as online_count " +
            "FROM t_shop s " +
            "LEFT JOIN t_device d ON s.id = d.shop_id " +
            "WHERE s.id = #{id} " +
            "GROUP BY s.id")
    Map<String, Object> selectShopWithStats(@Param("id") Long id);

    /**
     * 查询所有启用的门店（用于下拉选择）
     */
    @Select("SELECT id, name, address, contact_name, contact_phone, erp_shop_id " +
            "FROM t_shop WHERE status = 1 ORDER BY name")
    List<Map<String, Object>> selectAllEnabledShops();
}
