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.BizMarketStall;
import com.ruoyi.web.modules.trading.mapper.BizMarketStallMapper;
import com.ruoyi.web.modules.trading.service.IBizMarketStallService;
import com.ruoyi.web.modules.trading.support.MarketStallReferenceChecker;
import com.ruoyi.web.modules.trading.support.MarketStallRules;
import com.ruoyi.web.modules.trading.support.MarketStallValidation;
import com.ruoyi.web.modules.trading.support.StallCodeGenerator;

/**
 * 市场栏位服务实现。
 */
@Service
public class BizMarketStallServiceImpl implements IBizMarketStallService
{
    @Autowired
    private BizMarketStallMapper bizMarketStallMapper;

    @Autowired
    private StallCodeGenerator stallCodeGenerator;

    @Autowired
    private MarketStallReferenceChecker referenceChecker;

    @Override
    public BizMarketStall selectBizMarketStallById(Long id)
    {
        return bizMarketStallMapper.selectBizMarketStallById(id);
    }

    @Override
    public List<BizMarketStall> selectBizMarketStallList(BizMarketStall query)
    {
        return bizMarketStallMapper.selectBizMarketStallList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizMarketStall(BizMarketStall row)
    {
        row.setStallCode(null);
        MarketStallValidation.validateRowForSave(row);
        row.setStallCode(stallCodeGenerator.allocateNextCode());
        row.setDelFlag(MarketStallRules.DEL_FLAG_NORMAL);
        return bizMarketStallMapper.insertBizMarketStall(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizMarketStall(BizMarketStall row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        BizMarketStall db = requireActiveRow(row.getId());
        if (StringUtils.isNotEmpty(row.getStallCode()) && !row.getStallCode().equals(db.getStallCode()))
        {
            throw new ServiceException("栏位编号不可修改");
        }
        row.setStallCode(db.getStallCode());
        MarketStallValidation.validateRowForSave(row);
        return bizMarketStallMapper.updateBizMarketStall(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizMarketStallByIds(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)
        {
            BizMarketStall row = bizMarketStallMapper.selectBizMarketStallByIdAny(id);
            if (row == null || MarketStallRules.DEL_FLAG_DELETED.equals(row.getDelFlag()))
            {
                continue;
            }
            if (referenceChecker.hasReference(id))
            {
                throw new ServiceException("存在关联业务，无法删除");
            }
        }
        return bizMarketStallMapper.logicDeleteBizMarketStallByIds(cleaned.toArray(new Long[0]), updateBy);
    }

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