package com.example.cattletends.service.impl;

import com.example.cattletends.entity.ProvinceDailyPrice;
import com.example.cattletends.repository.ProvinceDailyPriceRepository;
import com.example.cattletends.service.ProvinceDailyPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * 省份每日均价服务实现类
 */
@Service
public class ProvinceDailyPriceServiceImpl implements ProvinceDailyPriceService {
    
    @Autowired
    private ProvinceDailyPriceRepository provinceDailyPriceRepository;
    
    @Override
    @Transactional
    public ProvinceDailyPrice saveOrUpdate(String province, java.math.BigDecimal price, LocalDate priceDate) {
        if (province == null || province.trim().isEmpty() || price == null) {
            System.out.println("保存省份每日均价失败: 参数不完整 - 省份: " + province + ", 价格: " + price);
            return null;
        }
        
        String trimmedProvince = province.trim();
        LocalDate date = priceDate != null ? priceDate : LocalDate.now();
        
        // 检查是否已存在（同一天同一省份）
        java.util.Optional<ProvinceDailyPrice> existing = provinceDailyPriceRepository.findByProvinceAndPriceDate(trimmedProvince, date);
        
        if (existing.isPresent()) {
            // 已存在，更新价格
            ProvinceDailyPrice dailyPrice = existing.get();
            java.math.BigDecimal oldPrice = dailyPrice.getPrice();
            dailyPrice.setPrice(price);
            ProvinceDailyPrice updated = provinceDailyPriceRepository.save(dailyPrice);
            System.out.println("✓ 更新省份每日均价: " + trimmedProvince + ", 日期: " + date + ", 旧价格: " + oldPrice + " -> 新价格: " + price + " (ID: " + updated.getId() + ")");
            return updated;
        } else {
            // 不存在则创建新记录
            ProvinceDailyPrice dailyPrice = new ProvinceDailyPrice();
            dailyPrice.setProvince(trimmedProvince);
            dailyPrice.setPrice(price);
            dailyPrice.setPriceDate(date);
            
            ProvinceDailyPrice saved = provinceDailyPriceRepository.save(dailyPrice);
            System.out.println("✓ 保存省份每日均价: " + trimmedProvince + ", 日期: " + date + ", 价格: " + price + " (ID: " + saved.getId() + ")");
            return saved;
        }
    }
    
    @Override
    public List<ProvinceDailyPrice> getByProvince(String province) {
        if (province == null || province.trim().isEmpty()) {
            return provinceDailyPriceRepository.findAllByOrderByPriceDateDesc();
        }
        return provinceDailyPriceRepository.findByProvinceOrderByPriceDateDesc(province.trim());
    }
    
    @Override
    public List<ProvinceDailyPrice> getAll() {
        return provinceDailyPriceRepository.findAllByOrderByPriceDateDesc();
    }
    
    @Override
    public List<ProvinceDailyPrice> getByProvinceAndDateRange(String province, LocalDate startDate, LocalDate endDate) {
        if (province == null || province.trim().isEmpty()) {
            return getAll();
        }
        return provinceDailyPriceRepository.findByProvinceAndDateRange(province.trim(), startDate, endDate);
    }
    
    @Override
    public List<ProvinceDailyPrice> getByPriceDate(LocalDate priceDate) {
        if (priceDate == null) {
            return getAll();
        }
        return provinceDailyPriceRepository.findByPriceDateOrderByProvinceAsc(priceDate);
    }
    
    @Override
    public java.util.Optional<ProvinceDailyPrice> getByProvinceAndPriceDate(String province, LocalDate priceDate) {
        if (province == null || province.trim().isEmpty() || priceDate == null) {
            return java.util.Optional.empty();
        }
        return provinceDailyPriceRepository.findByProvinceAndPriceDate(province.trim(), priceDate);
    }
}

