// Package service 业务逻辑层
package service

import (
	"server/config"
	"server/modules/yx/dto"
	"server/modules/yx/entity"
	"server/modules/yx/mapper"

	"github.com/google/uuid"
)

type YxHistoryMajorEnrollService struct {
	mapper *mapper.YxHistoryMajorEnrollMapper
}

// RecommendMajorDTOListSetHistoryInfo 填充历史录取信息
func (s *YxHistoryMajorEnrollService) RecommendMajorDTOListSetHistoryInfo(dtoList *[]dto.SchoolMajorDTO, years []string) {
	if dtoList == nil || len(*dtoList) == 0 {
		return
	}

	schoolCodes := make([]string, 0)
	majorNames := make([]string, 0)
	category := (*dtoList)[0].Category // 假设同一批次查询类别相同
	majorType := (*dtoList)[0].MajorType

	for _, item := range *dtoList {
		schoolCodes = append(schoolCodes, item.SchoolCode)
		majorNames = append(majorNames, item.MajorName)
	}

	historyList, err := s.ListBySchoolCodesAndMajorNames(schoolCodes, majorNames, category, majorType, years)
	if err != nil {
		return // Log error
	}

	// Group by SchoolCode + MajorName
	historyMap := make(map[string][]entity.YxHistoryMajorEnroll)
	for _, h := range historyList {
		key := h.SchoolCode + "_" + h.MajorName
		historyMap[key] = append(historyMap[key], h)
	}

	for i := range *dtoList {
		item := &(*dtoList)[i]
		key := item.SchoolCode + "_" + item.MajorName
		if list, ok := historyMap[key]; ok {
			item.HistoryMajorEnrollList = list
		}
	}
}

// HistoryMajorEnrollService 中的方法
func (s *YxHistoryMajorEnrollService) ListBySchoolCodesAndMajorNames(
	schoolCodes []string,
	majorNames []string,
	category string,
	majorType string,
	years []string,
) ([]entity.YxHistoryMajorEnroll, error) {
	var items []entity.YxHistoryMajorEnroll
	sql := `
		SELECT hme.*
		FROM yx_history_major_enroll hme
		WHERE 1=1 AND hme.rules_enroll_probability is not null AND hme.admission_line > 0
	`
	params := []interface{}{}
	if majorType != "" {
		sql += " AND hme.major_type = ?"
		params = append(params, majorType)
	}
	if category != "" {
		sql += " AND hme.category = ?"
		params = append(params, category)
	}
	if len(schoolCodes) > 0 {
		sql += " AND hme.school_code IN ?"
		params = append(params, schoolCodes)
	}
	if len(majorNames) > 0 {
		sql += " AND hme.major_name IN ?"
		params = append(params, majorNames)
	}
	if len(years) > 0 {
		sql += " AND hme.year IN ?"
		params = append(params, years)
	}
	sql += " ORDER BY hme.year DESC"
	err := config.DB.Raw(sql, params...).Scan(&items).Error
	return items, err
}

func NewYxHistoryMajorEnrollService() *YxHistoryMajorEnrollService {
	return &YxHistoryMajorEnrollService{mapper: mapper.NewYxHistoryMajorEnrollMapper()}
}

func (s *YxHistoryMajorEnrollService) List(page, size int) ([]entity.YxHistoryMajorEnroll, int64, error) {
	return s.mapper.FindAll(page, size)
}

func (s *YxHistoryMajorEnrollService) GetByID(id string) (*entity.YxHistoryMajorEnroll, error) {
	return s.mapper.FindByID(id)
}

func (s *YxHistoryMajorEnrollService) Create(item *entity.YxHistoryMajorEnroll) error {
	item.ID = uuid.New().String()
	return s.mapper.Create(item)
}

func (s *YxHistoryMajorEnrollService) Update(item *entity.YxHistoryMajorEnroll) error {
	return s.mapper.Update(item)
}

func (s *YxHistoryMajorEnrollService) UpdateFields(id string, fields map[string]interface{}) error {
	return s.mapper.UpdateFields(id, fields)
}

func (s *YxHistoryMajorEnrollService) Delete(id string) error {
	return s.mapper.Delete(id)
}

func (s *YxHistoryMajorEnrollService) GetByYear(year string) ([]entity.YxHistoryMajorEnroll, error) {
	return s.mapper.FindByYear(year)
}

func (s *YxHistoryMajorEnrollService) BatchCreate(items []entity.YxHistoryMajorEnroll) error {
	for i := range items {
		items[i].ID = uuid.New().String()
	}
	return s.mapper.BatchCreate(items, 100)
}

func (s *YxHistoryMajorEnrollService) BatchUpdate(items []entity.YxHistoryMajorEnroll) error {
	return s.mapper.BatchUpdate(items)
}

func (s *YxHistoryMajorEnrollService) BatchUpsert(items []entity.YxHistoryMajorEnroll, updateColumns []string) error {
	for i := range items {
		if items[i].ID == "" {
			items[i].ID = uuid.New().String()
		}
	}
	return s.mapper.BatchUpsert(items, updateColumns)
}

func (s *YxHistoryMajorEnrollService) BatchDelete(ids []string) error {
	return s.mapper.BatchDelete(ids)
}
