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

import java.util.ArrayList;
import java.util.Comparator;
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.web.modules.circulation.domain.BizTradeMarket;
import com.ruoyi.web.modules.circulation.mapper.BizTradeMarketMapper;
import com.ruoyi.web.modules.circulation.support.TradeMarketRules;
import com.ruoyi.web.modules.trading.domain.BizMarketDailyQuote;
import com.ruoyi.web.modules.trading.domain.TradeMarketOption;
import com.ruoyi.web.modules.trading.mapper.BizMarketDailyQuoteMapper;
import com.ruoyi.web.modules.trading.service.IBizMarketDailyQuoteService;
import com.ruoyi.web.modules.trading.support.MarketDailyQuoteLabelResolver;
import com.ruoyi.web.modules.trading.support.MarketDailyQuoteReferenceChecker;
import com.ruoyi.web.modules.trading.support.MarketDailyQuoteRules;
import com.ruoyi.web.modules.trading.support.MarketDailyQuoteValidation;

/**
 * 市场行情登记服务实现。
 */
@Service
public class BizMarketDailyQuoteServiceImpl implements IBizMarketDailyQuoteService
{
    @Autowired
    private BizMarketDailyQuoteMapper bizMarketDailyQuoteMapper;

    @Autowired
    private BizTradeMarketMapper bizTradeMarketMapper;

    @Autowired
    private MarketDailyQuoteValidation marketDailyQuoteValidation;

    @Autowired
    private MarketDailyQuoteReferenceChecker referenceChecker;

    @Override
    public BizMarketDailyQuote selectBizMarketDailyQuoteById(Long id)
    {
        BizMarketDailyQuote row = bizMarketDailyQuoteMapper.selectBizMarketDailyQuoteById(id);
        if (row == null)
        {
            throw new ServiceException("日度行情不存在或已删除");
        }
        MarketDailyQuoteLabelResolver.enrich(row);
        return row;
    }

    @Override
    public List<BizMarketDailyQuote> selectBizMarketDailyQuoteList(BizMarketDailyQuote query)
    {
        marketDailyQuoteValidation.validateListQuery(query);
        List<BizMarketDailyQuote> list = bizMarketDailyQuoteMapper.selectBizMarketDailyQuoteList(query);
        if (list != null)
        {
            for (BizMarketDailyQuote row : list)
            {
                MarketDailyQuoteLabelResolver.enrich(row);
            }
        }
        return list;
    }

    @Override
    public List<TradeMarketOption> selectTradeMarketOptions()
    {
        BizTradeMarket query = new BizTradeMarket();
        query.setOperateStatus(TradeMarketRules.OPERATE_STATUS_NORMAL);
        List<BizTradeMarket> markets = bizTradeMarketMapper.selectBizTradeMarketList(query);
        List<TradeMarketOption> options = new ArrayList<>();
        if (markets != null)
        {
            for (BizTradeMarket m : markets)
            {
                options.add(new TradeMarketOption(m.getId(), m.getMarketName()));
            }
            options.sort(Comparator.comparing(TradeMarketOption::getMarketName,
                    Comparator.nullsLast(String::compareTo)));
        }
        return options;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizMarketDailyQuote(BizMarketDailyQuote row)
    {
        marketDailyQuoteValidation.prepareAndValidateForSave(row, null);
        row.setDelFlag(MarketDailyQuoteRules.DEL_FLAG_NORMAL);
        return bizMarketDailyQuoteMapper.insertBizMarketDailyQuote(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizMarketDailyQuote(BizMarketDailyQuote row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        requireActiveRow(row.getId());
        marketDailyQuoteValidation.prepareAndValidateForSave(row, row.getId());
        row.setDelFlag(MarketDailyQuoteRules.DEL_FLAG_NORMAL);
        return bizMarketDailyQuoteMapper.updateBizMarketDailyQuote(row);
    }

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

    private void requireActiveRow(Long id)
    {
        BizMarketDailyQuote row = bizMarketDailyQuoteMapper.selectBizMarketDailyQuoteById(id);
        if (row == null)
        {
            throw new ServiceException("日度行情不存在或已删除");
        }
    }
}
