package com.example.cattletends.service.impl;

import com.example.cattletends.entity.CattleNational;
import com.example.cattletends.entity.CattleProvince;
import com.example.cattletends.repository.CattleNationalRepository;
import com.example.cattletends.repository.CattleProvinceRepository;
import com.example.cattletends.service.CattleNationalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 全国总量数据服务实现类
 */
@Service
public class CattleNationalServiceImpl implements CattleNationalService {

    private final CattleNationalRepository cattleNationalRepository;
    private final CattleProvinceRepository cattleProvinceRepository;

    /**
     * 构造器注入
     */
    public CattleNationalServiceImpl(CattleNationalRepository cattleNationalRepository,
                                     CattleProvinceRepository cattleProvinceRepository) {
        this.cattleNationalRepository = cattleNationalRepository;
        this.cattleProvinceRepository = cattleProvinceRepository;
    }

    @Override
    @Transactional(readOnly = true)
    public CattleNational getNationalData() {
        CattleNational nationalData = cattleNationalRepository.findFirstByOrderByIdAsc()
                .orElse(null);
        
        // 如果不存在，自动计算并创建
        if (nationalData == null) {
            return calculateAndUpdateNationalData();
        }
        
        return nationalData;
    }

    @Override
    @Transactional
    public CattleNational calculateAndUpdateNationalData() {
        // 从 cattleprovince 表中获取所有省份数据
        List<CattleProvince> allProvinces = cattleProvinceRepository.findAll();
        
        // 初始化全国总量
        int nationalInventory23th = 0;
        int nationalSlaughter23th = 0;
        int nationalInventory24th = 0;
        int nationalSlaughter24th = 0;
        int nationalInventory25th = 0;
        int nationalSlaughter25th = 0;
        
        // 累加所有省份的数据
        System.out.println("========== 开始计算全国总量 ==========");
        System.out.println("省份总数: " + allProvinces.size());
        for (CattleProvince province : allProvinces) {
            System.out.println("省份: " + province.getProvince() + 
                ", 23年存栏: " + province.getInventory23th() + 
                ", 23年出栏: " + province.getSlaughter23th() +
                ", 24年存栏: " + province.getInventory24th() +
                ", 24年出栏: " + province.getSlaughter24th() +
                ", 25年存栏: " + province.getInventory25th() +
                ", 25年出栏: " + province.getSlaughter25th());
            if (province.getInventory23th() != null) {
                nationalInventory23th += province.getInventory23th();
            }
            if (province.getSlaughter23th() != null) {
                nationalSlaughter23th += province.getSlaughter23th();
            }
            if (province.getInventory24th() != null) {
                nationalInventory24th += province.getInventory24th();
            }
            if (province.getSlaughter24th() != null) {
                nationalSlaughter24th += province.getSlaughter24th();
            }
            if (province.getInventory25th() != null) {
                nationalInventory25th += province.getInventory25th();
            }
            if (province.getSlaughter25th() != null) {
                nationalSlaughter25th += province.getSlaughter25th();
            }
        }
        
        System.out.println("计算结果: 23年存栏=" + nationalInventory23th + 
            ", 23年出栏=" + nationalSlaughter23th +
            ", 24年存栏=" + nationalInventory24th +
            ", 24年出栏=" + nationalSlaughter24th +
            ", 25年存栏=" + nationalInventory25th +
            ", 25年出栏=" + nationalSlaughter25th);
        System.out.println("========== 计算完成 ==========");
        
        // 查找是否已存在全国总量记录
        CattleNational nationalData = cattleNationalRepository.findFirstByOrderByIdAsc()
                .orElse(null);
        
        if (nationalData != null) {
            // 更新现有记录
            nationalData.setNationalInventory23th(nationalInventory23th);
            nationalData.setNationalSlaughter23th(nationalSlaughter23th);
            nationalData.setNationalInventory24th(nationalInventory24th);
            nationalData.setNationalSlaughter24th(nationalSlaughter24th);
            nationalData.setNationalInventory25th(nationalInventory25th);
            nationalData.setNationalSlaughter25th(nationalSlaughter25th);
            return cattleNationalRepository.save(nationalData);
        } else {
            // 创建新记录
            nationalData = new CattleNational();
            nationalData.setNationalInventory23th(nationalInventory23th);
            nationalData.setNationalSlaughter23th(nationalSlaughter23th);
            nationalData.setNationalInventory24th(nationalInventory24th);
            nationalData.setNationalSlaughter24th(nationalSlaughter24th);
            nationalData.setNationalInventory25th(nationalInventory25th);
            nationalData.setNationalSlaughter25th(nationalSlaughter25th);
            return cattleNationalRepository.save(nationalData);
        }
    }
}

