package com.ruoyi.web.modules.industryservice.service.impl;

import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.web.modules.industryservice.domain.BizGrassland;
import com.ruoyi.web.modules.industryservice.domain.dto.GrassLivestockBalanceCalculateRequest;
import com.ruoyi.web.modules.industryservice.domain.vo.GrassLivestockBalanceDefaultsVo;
import com.ruoyi.web.modules.industryservice.domain.vo.GrassLivestockBalanceResultVo;
import com.ruoyi.web.modules.industryservice.mapper.BizGrasslandMapper;
import com.ruoyi.web.modules.industryservice.service.IGrassLivestockBalanceService;
import com.ruoyi.web.modules.industryservice.support.GrassLivestockBalanceCalculator;
import com.ruoyi.web.modules.industryservice.support.GrassLivestockBalanceRules;
import com.ruoyi.web.modules.industryservice.support.GrassLivestockBalanceValidation;
import com.ruoyi.web.modules.industryservice.support.GrasslandRules;

@Service
public class GrassLivestockBalanceServiceImpl implements IGrassLivestockBalanceService
{
    @Autowired
    private BizGrasslandMapper bizGrasslandMapper;

    @Autowired
    private ISysConfigService sysConfigService;

    @Override
    public GrassLivestockBalanceResultVo calculate(GrassLivestockBalanceCalculateRequest request)
    {
        GrassLivestockBalanceValidation.validateCalculateRequest(request);
        BizGrassland grassland = requireActiveGrassland(request.getGrasslandId());
        return GrassLivestockBalanceCalculator.calculate(request, grassland.getGrasslandName());
    }

    @Override
    public GrassLivestockBalanceDefaultsVo getDefaults()
    {
        GrassLivestockBalanceDefaultsVo vo = new GrassLivestockBalanceDefaultsVo();
        vo.setSheepUnitAnnualForageKg(resolveConfigDecimal(
                GrassLivestockBalanceRules.CONFIG_SHEEP_UNIT_ANNUAL_FORAGE_KG,
                GrassLivestockBalanceRules.DEFAULT_SHEEP_UNIT_ANNUAL_FORAGE_KG));
        vo.setSheepUnitConvertCoeff(resolveConfigDecimal(
                GrassLivestockBalanceRules.CONFIG_SHEEP_UNIT_CONVERT_COEFF,
                GrassLivestockBalanceRules.DEFAULT_SHEEP_UNIT_CONVERT_COEFF));
        return vo;
    }

    private BizGrassland requireActiveGrassland(Long grasslandId)
    {
        BizGrassland grassland = bizGrasslandMapper.selectBizGrasslandById(grasslandId);
        if (grassland == null || GrasslandRules.DEL_FLAG_DELETED.equals(grassland.getDelFlag()))
        {
            throw new ServiceException("草场不存在或已失效");
        }
        return grassland;
    }

    private BigDecimal resolveConfigDecimal(String key, BigDecimal defaultValue)
    {
        String raw = sysConfigService.selectConfigByKey(key);
        if (StringUtils.isEmpty(raw))
        {
            return defaultValue;
        }
        try
        {
            return new BigDecimal(raw.trim());
        }
        catch (NumberFormatException ex)
        {
            return defaultValue;
        }
    }
}
