package com.ruoyi.web.modules.trading.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.trading.domain.BizIsolationSetting;
import com.ruoyi.web.modules.trading.mapper.BizIsolationSettingMapper;
import com.ruoyi.web.modules.trading.service.IBizIsolationSettingService;
import com.ruoyi.web.modules.trading.support.IsolationPenRules;
import com.ruoyi.web.modules.trading.support.IsolationSettingValidation;

/**
 * 默认隔离天数配置服务实现（单例 upsert）。
 */
@Service
public class BizIsolationSettingServiceImpl implements IBizIsolationSettingService
{
    @Autowired
    private BizIsolationSettingMapper bizIsolationSettingMapper;

    @Override
    public BizIsolationSetting getCurrentSetting()
    {
        return bizIsolationSettingMapper.selectBizIsolationSettingById(IsolationPenRules.SETTING_SINGLETON_ID);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveSetting(BizIsolationSetting row, String operator)
    {
        IsolationSettingValidation.validateRowForSave(row);
        BizIsolationSetting existing = bizIsolationSettingMapper.selectBizIsolationSettingById(
                IsolationPenRules.SETTING_SINGLETON_ID);
        if (existing == null)
        {
            row.setCreateBy(operator);
            return bizIsolationSettingMapper.insertBizIsolationSetting(row);
        }
        row.setUpdateBy(operator);
        return bizIsolationSettingMapper.updateBizIsolationSetting(row);
    }
}
