package com.ruoyi.web.modules.hub.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.web.modules.hub.domain.BizDataSendRecord;
import com.ruoyi.web.modules.hub.mapper.BizDataSendRecordMapper;
import com.ruoyi.web.modules.hub.service.IBizDataSendRecordService;
import com.ruoyi.web.modules.hub.support.DataSendRecordRules;
import com.ruoyi.web.modules.hub.support.DataSendRecordValidation;

/**
 * 数据发送记录服务实现。
 */
@Service
public class BizDataSendRecordServiceImpl implements IBizDataSendRecordService
{
    @Autowired
    private BizDataSendRecordMapper bizDataSendRecordMapper;

    @Override
    public BizDataSendRecord selectBizDataSendRecordById(Long id)
    {
        return bizDataSendRecordMapper.selectBizDataSendRecordById(id);
    }

    @Override
    public List<BizDataSendRecord> selectBizDataSendRecordList(BizDataSendRecord query)
    {
        if (query == null)
        {
            query = new BizDataSendRecord();
        }
        DataSendRecordValidation.validateListDateRange(query.getStartDate(), query.getEndDate());
        return bizDataSendRecordMapper.selectBizDataSendRecordList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizDataSendRecord(BizDataSendRecord row)
    {
        DataSendRecordValidation.validateRowForSave(row);
        row.setDelFlag(DataSendRecordRules.DEL_FLAG_NORMAL);
        return bizDataSendRecordMapper.insertBizDataSendRecord(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizDataSendRecord(BizDataSendRecord row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        requireActiveRow(row.getId());
        DataSendRecordValidation.validateRowForSave(row);
        return bizDataSendRecordMapper.updateBizDataSendRecord(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizDataSendRecordByIds(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)
        {
            BizDataSendRecord row = bizDataSendRecordMapper.selectBizDataSendRecordByIdAny(id);
            if (row == null || DataSendRecordRules.DEL_FLAG_DELETED.equals(row.getDelFlag()))
            {
                continue;
            }
        }
        return bizDataSendRecordMapper.logicDeleteBizDataSendRecordByIds(
                cleaned.toArray(new Long[0]), updateBy);
    }

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