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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.modules.epidemic.domain.BizRiskFactorWeight;
import com.ruoyi.web.modules.epidemic.domain.dto.RiskFactorWeightSaveRequest;
import com.ruoyi.web.modules.epidemic.domain.vo.RiskFactorWeightVo;
import com.ruoyi.web.modules.epidemic.mapper.BizRiskFactorWeightMapper;
import com.ruoyi.web.modules.epidemic.service.IBizRiskFactorWeightService;
import com.ruoyi.web.modules.epidemic.support.RiskFactorWeightRules;
import com.ruoyi.web.modules.epidemic.support.RiskFactorWeightValidation;

/**
 * 风险因子权重服务实现（单例 upsert）。
 */
@Service
public class BizRiskFactorWeightServiceImpl implements IBizRiskFactorWeightService
{
    @Autowired
    private BizRiskFactorWeightMapper riskFactorWeightMapper;

    @Override
    public RiskFactorWeightVo getCurrentConfig()
    {
        BizRiskFactorWeight row = riskFactorWeightMapper.selectBizRiskFactorWeightById(RiskFactorWeightRules.SINGLETON_ID);
        return RiskFactorWeightValidation.toVo(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveConfig(RiskFactorWeightSaveRequest request, String operator)
    {
        RiskFactorWeightValidation.validateForSave(request);
        BizRiskFactorWeight row = RiskFactorWeightValidation.toEntity(request);
        BizRiskFactorWeight existing = riskFactorWeightMapper.selectBizRiskFactorWeightById(RiskFactorWeightRules.SINGLETON_ID);
        if (existing == null)
        {
            row.setCreateBy(operator);
            return riskFactorWeightMapper.insertBizRiskFactorWeight(row);
        }
        row.setUpdateBy(operator);
        return riskFactorWeightMapper.updateBizRiskFactorWeight(row);
    }
}
