package org.dromara.ems.info.service.impl;

import org.dromara.common.core.exception.ServiceException;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.ems.info.domain.FaultHandlingRecord;
import org.dromara.ems.info.mapper.FaultHandlingRecordMapper;
import org.dromara.ems.info.service.IFaultHandlingRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 故障处置记录Service业务层处理
 * 
 * @author Yinq
 * @date 2025-06-05
 */
@Service
@RequiredArgsConstructor
public class FaultHandlingRecordServiceImpl implements IFaultHandlingRecordService 
{
    
    private final FaultHandlingRecordMapper faultHandlingRecordMapper;

    /**
     * 查询故障处置记录
     * 
     * @param id 故障处置记录主键
     * @return 故障处置记录
     */
    @Override
    public FaultHandlingRecord selectFaultHandlingRecordById(Long id)
    {
        return faultHandlingRecordMapper.selectFaultHandlingRecordById(id);
    }

    /**
     * 查询故障处置记录列表
     * 
     * @param faultHandlingRecord 故障处置记录
     * @return 故障处置记录
     */
    @Override
    public List<FaultHandlingRecord> selectFaultHandlingRecordList(FaultHandlingRecord faultHandlingRecord)
    {
        return faultHandlingRecordMapper.selectFaultHandlingRecordList(faultHandlingRecord);
    }

    /**
     * 新增故障处置记录
     * 
     * @param faultHandlingRecord 故障处置记录
     * @return 结果
     */
    @Override
    public int insertFaultHandlingRecord(FaultHandlingRecord faultHandlingRecord)
    {
        return faultHandlingRecordMapper.insertFaultHandlingRecord(faultHandlingRecord);
    }

    /**
     * 修改故障处置记录
     * 
     * @param faultHandlingRecord 故障处置记录
     * @return 结果
     */
    @Override
    public int updateFaultHandlingRecord(FaultHandlingRecord faultHandlingRecord)
    {
        return faultHandlingRecordMapper.updateFaultHandlingRecord(faultHandlingRecord);
    }

    /**
     * 批量删除故障处置记录
     * 
     * @param ids 需要删除的故障处置记录主键
     * @return 结果
     */
    @Override
    public int deleteFaultHandlingRecordByIds(Long[] ids)
    {
        return faultHandlingRecordMapper.deleteFaultHandlingRecordByIds(ids);
    }

    /**
     * 删除故障处置记录信息
     * 
     * @param id 故障处置记录主键
     * @return 结果
     */
    @Override
    public int deleteFaultHandlingRecordById(Long id)
    {
        return faultHandlingRecordMapper.deleteFaultHandlingRecordById(id);
    }

    /**
     * 导入故障处置记录数据
     *
     * @param recordList 记录数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importFaultHandlingRecord(List<FaultHandlingRecord> recordList, Boolean isUpdateSupport, String operName)
    {
        if (ObjectUtil.isNull(recordList) || recordList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FaultHandlingRecord record : recordList)
        {
            try
            {
                // 设置操作人
                
                // 验证是否存在这个记录
                FaultHandlingRecord existRecord = null;
                if (ObjectUtil.isNotEmpty(record.getFaultDate()) && ObjectUtil.isNotEmpty(record.getFaultLocation()))
                {
                    FaultHandlingRecord queryRecord = new FaultHandlingRecord();
                    queryRecord.setFaultDate(record.getFaultDate());
                    queryRecord.setFaultLocation(record.getFaultLocation());
                    List<FaultHandlingRecord> existRecords = faultHandlingRecordMapper.selectFaultHandlingRecordList(queryRecord);
                    if (existRecords.size() > 0)
                    {
                        existRecord = existRecords.get(0);
                    }
                }
                
                if (ObjectUtil.isNull(existRecord))
                {
                    this.insertFaultHandlingRecord(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、故障处置记录 " + record.getFaultLocation() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    record.setId(existRecord.getId());
                    this.updateFaultHandlingRecord(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、故障处置记录 " + record.getFaultLocation() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、故障处置记录 " + record.getFaultLocation() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、故障处置记录 " + record.getFaultLocation() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
