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.BizIsolationPatrol;
import com.ruoyi.web.modules.trading.domain.vo.IsolationPenOptionVo;
import com.ruoyi.web.modules.trading.mapper.BizIsolationPatrolMapper;
import com.ruoyi.web.modules.trading.service.IBizIsolationPatrolService;
import com.ruoyi.web.modules.trading.support.EarTagCodec;
import com.ruoyi.web.modules.trading.support.IsolationPatrolRules;
import com.ruoyi.web.modules.trading.support.PatrolValidation;

/**
 * 隔离巡查登记服务实现。
 */
@Service
public class BizIsolationPatrolServiceImpl implements IBizIsolationPatrolService
{
    @Autowired
    private BizIsolationPatrolMapper bizIsolationPatrolMapper;

    @Autowired
    private PatrolValidation patrolValidation;

    @Override
    public BizIsolationPatrol selectBizIsolationPatrolById(Long id)
    {
        BizIsolationPatrol row = bizIsolationPatrolMapper.selectBizIsolationPatrolById(id);
        enrichEarTags(row);
        return row;
    }

    @Override
    public List<BizIsolationPatrol> selectBizIsolationPatrolList(BizIsolationPatrol query)
    {
        patrolValidation.validateListQuery(query);
        List<BizIsolationPatrol> list = bizIsolationPatrolMapper.selectBizIsolationPatrolList(query);
        if (list != null)
        {
            for (BizIsolationPatrol row : list)
            {
                enrichEarTags(row);
            }
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizIsolationPatrol(BizIsolationPatrol row)
    {
        patrolValidation.prepareAndValidateForSave(row);
        row.setDelFlag(IsolationPatrolRules.DEL_FLAG_NORMAL);
        return bizIsolationPatrolMapper.insertBizIsolationPatrol(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizIsolationPatrol(BizIsolationPatrol row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        requireActiveRow(row.getId());
        patrolValidation.prepareAndValidateForSave(row);
        row.setDelFlag(IsolationPatrolRules.DEL_FLAG_NORMAL);
        return bizIsolationPatrolMapper.updateBizIsolationPatrol(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizIsolationPatrolByIds(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)
        {
            BizIsolationPatrol row = bizIsolationPatrolMapper.selectBizIsolationPatrolByIdAny(id);
            if (row == null || IsolationPatrolRules.DEL_FLAG_DELETED.equals(row.getDelFlag()))
            {
                continue;
            }
        }
        return bizIsolationPatrolMapper.logicDeleteBizIsolationPatrolByIds(
                cleaned.toArray(new Long[0]), updateBy);
    }

    @Override
    public List<IsolationPenOptionVo> selectInStockPenOptions()
    {
        List<IsolationPenOptionVo> list = bizIsolationPatrolMapper.selectInStockPenOptions();
        return list != null ? list : new ArrayList<>();
    }

    @Override
    public List<String> selectPenEarTags(Long penId)
    {
        if (penId == null)
        {
            throw new ServiceException(IsolationPatrolRules.MSG_PEN_REQUIRED);
        }
        if (!patrolValidation.isPenInStock(penId))
        {
            throw new ServiceException(IsolationPatrolRules.MSG_PEN_NOT_IN_STOCK);
        }
        return patrolValidation.listInStockEarTags(penId);
    }

    private void requireActiveRow(Long id)
    {
        BizIsolationPatrol row = bizIsolationPatrolMapper.selectBizIsolationPatrolById(id);
        if (row == null)
        {
            throw new ServiceException(IsolationPatrolRules.MSG_NOT_FOUND);
        }
    }

    private static void enrichEarTags(BizIsolationPatrol row)
    {
        if (row == null)
        {
            return;
        }
        if (StringUtils.isNotEmpty(row.getEarTags()))
        {
            row.setEarTagList(EarTagCodec.parse(row.getEarTags()));
        }
        else
        {
            row.setEarTagList(new ArrayList<>());
        }
    }
}
