package com.erp.supplier.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.supplier.entity.Supplier;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface SupplierRepository extends BaseMapper<Supplier> {

    IPage<Supplier> selectSupplierPage(Page<Supplier> page,
                                      @Param("status") String status,
                                      @Param("grade") String grade,
                                      @Param("supplierType") String supplierType,
                                      @Param("industry") String industry,
                                      @Param("keyword") String keyword,
                                      @Param("startDate") LocalDateTime startDate,
                                      @Param("endDate") LocalDateTime endDate);

    @Select("SELECT COUNT(*) FROM suppliers WHERE status = 'active'")
    Long countActiveSuppliers();

    @Select("SELECT grade, COUNT(*) as count FROM suppliers WHERE deleted = 0 GROUP BY grade")
    List<GradeCount> countByGrade();

    @Select("SELECT supplier_type, COUNT(*) as count FROM suppliers WHERE deleted = 0 GROUP BY supplier_type")
    List<TypeCount> countByType();

    interface GradeCount {
        String getGrade();
        Long getCount();
    }

    interface TypeCount {
        String getSupplierType();
        Long getCount();
    }
}
