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

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.trading.domain.BizIsolationPen;
import com.ruoyi.web.modules.trading.mapper.BizIsolationPenMapper;
import com.ruoyi.web.modules.trading.service.IBizIsolationPenService;
import com.ruoyi.web.modules.trading.support.IsolationPenReferenceChecker;
import com.ruoyi.web.modules.trading.support.IsolationPenRules;
import com.ruoyi.web.modules.trading.support.IsolationPenValidation;
import com.ruoyi.web.modules.trading.support.PenCodeGenerator;

/**
 * 隔离圈位服务实现。
 */
@Service
public class BizIsolationPenServiceImpl implements IBizIsolationPenService
{
    @Autowired
    private BizIsolationPenMapper bizIsolationPenMapper;

    @Autowired
    private PenCodeGenerator penCodeGenerator;

    @Autowired
    private IsolationPenReferenceChecker referenceChecker;

    @Override
    public BizIsolationPen selectBizIsolationPenById(Long id)
    {
        return bizIsolationPenMapper.selectBizIsolationPenById(id);
    }

    @Override
    public List<BizIsolationPen> selectBizIsolationPenList(BizIsolationPen query)
    {
        return bizIsolationPenMapper.selectBizIsolationPenList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizIsolationPen(BizIsolationPen row)
    {
        row.setPenCode(null);
        IsolationPenValidation.validateRowForSave(row);
        row.setPenCode(penCodeGenerator.allocateNextCode());
        row.setDelFlag(IsolationPenRules.DEL_FLAG_NORMAL);
        return bizIsolationPenMapper.insertBizIsolationPen(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizIsolationPen(BizIsolationPen row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        BizIsolationPen db = requireActiveRow(row.getId());
        if (StringUtils.isNotEmpty(row.getPenCode()) && !row.getPenCode().equals(db.getPenCode()))
        {
            throw new ServiceException("圈位编号不可修改");
        }
        row.setPenCode(db.getPenCode());
        IsolationPenValidation.validateRowForSave(row);
        return bizIsolationPenMapper.updateBizIsolationPen(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizIsolationPenByIds(Long[] ids, String updateBy)
    {
        if (ids == null || ids.length == 0)
        {
            return 0;
        }
        List<Long> cleaned = new ArrayList<>();
        for (Long id : ids)
        {
            if (id != null)
            {
                cleaned.add(id);
            }
        }
        if (cleaned.isEmpty())
        {
            return 0;
        }
        for (Long id : cleaned)
        {
            BizIsolationPen row = bizIsolationPenMapper.selectBizIsolationPenByIdAny(id);
            if (row == null || IsolationPenRules.DEL_FLAG_DELETED.equals(row.getDelFlag()))
            {
                continue;
            }
            if (referenceChecker.hasReference(id))
            {
                throw new ServiceException("存在关联业务，无法删除");
            }
        }
        return bizIsolationPenMapper.logicDeleteBizIsolationPenByIds(cleaned.toArray(new Long[0]), updateBy);
    }

    private BizIsolationPen requireActiveRow(Long id)
    {
        BizIsolationPen row = bizIsolationPenMapper.selectBizIsolationPenById(id);
        if (row == null)
        {
            throw new ServiceException("记录不存在或已删除");
        }
        return row;
    }
}
