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.DailyFaultRecord;
import org.dromara.ems.info.mapper.DailyFaultRecordMapper;
import org.dromara.ems.info.service.IDailyFaultRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 日常故障记录Service业务层处理
 * 
 * @author Yinq
 * @date 2025-06-05
 */
@Service
@RequiredArgsConstructor
public class DailyFaultRecordServiceImpl implements IDailyFaultRecordService 
{

    private final DailyFaultRecordMapper dailyFaultRecordMapper;

    /**
     * 查询日常故障记录
     * 
     * @param id 日常故障记录主键
     * @return 日常故障记录
     */
    @Override
    public DailyFaultRecord selectDailyFaultRecordById(Long id)
    {
        return dailyFaultRecordMapper.selectDailyFaultRecordById(id);
    }

    /**
     * 查询日常故障记录列表
     * 
     * @param dailyFaultRecord 日常故障记录
     * @return 日常故障记录
     */
    @Override
    public List<DailyFaultRecord> selectDailyFaultRecordList(DailyFaultRecord dailyFaultRecord)
    {
        List<DailyFaultRecord> list = dailyFaultRecordMapper.selectDailyFaultRecordList(dailyFaultRecord);
        // 确保格式化字段正确设置（防止数据库查询未正确设置的情况）
        for (DailyFaultRecord record : list) {
            if (record.getHandlingDurationFormatted() == null && record.getHandlingDuration() != null) {
                record.setHandlingDuration(record.getHandlingDuration()); // 触发格式化
            }
        }
        return list;
    }

    /**
     * 新增日常故障记录
     * 
     * @param dailyFaultRecord 日常故障记录
     * @return 结果
     */
    @Override
    public int insertDailyFaultRecord(DailyFaultRecord dailyFaultRecord)
    {
        // 确保格式化字段正确设置
        if (dailyFaultRecord.getHandlingDuration() != null) {
            dailyFaultRecord.setHandlingDuration(dailyFaultRecord.getHandlingDuration());
        }
        return dailyFaultRecordMapper.insertDailyFaultRecord(dailyFaultRecord);
    }

    /**
     * 修改日常故障记录
     * 
     * @param dailyFaultRecord 日常故障记录
     * @return 结果
     */
    @Override
    public int updateDailyFaultRecord(DailyFaultRecord dailyFaultRecord)
    {
        // 确保格式化字段正确设置
        if (dailyFaultRecord.getHandlingDuration() != null) {
            dailyFaultRecord.setHandlingDuration(dailyFaultRecord.getHandlingDuration());
        }
        return dailyFaultRecordMapper.updateDailyFaultRecord(dailyFaultRecord);
    }

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

    /**
     * 删除日常故障记录信息
     * 
     * @param id 日常故障记录主键
     * @return 结果
     */
    @Override
    public int deleteDailyFaultRecordById(Long id)
    {
        return dailyFaultRecordMapper.deleteDailyFaultRecordById(id);
    }


    /**
     * 导入日常故障记录数据
     *
     * @param recordList 记录数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importDailyFaultRecord(List<DailyFaultRecord> 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 (DailyFaultRecord record : recordList)
        {
            try
            {
                // 设置操作人
                
                // 确保格式化字段正确设置
                if (record.getHandlingDuration() != null) {
                    record.setHandlingDuration(record.getHandlingDuration());
                }
                
                // 验证是否存在这个记录
                DailyFaultRecord existRecord = null;
                if (ObjectUtil.isNotEmpty(record.getDate()) && ObjectUtil.isNotEmpty(record.getLocation()))
                {
                    DailyFaultRecord queryRecord = new DailyFaultRecord();
                    queryRecord.setDate(record.getDate());
                    queryRecord.setLocation(record.getLocation());
                    List<DailyFaultRecord> existRecords = dailyFaultRecordMapper.selectDailyFaultRecordList(queryRecord);
                    if (existRecords.size() > 0)
                    {
                        existRecord = existRecords.get(0);
                    }
                }
                
                if (ObjectUtil.isNull(existRecord))
                {
                    this.insertDailyFaultRecord(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、日常故障记录 " + record.getLocation() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    record.setId(existRecord.getId());
                    this.updateDailyFaultRecord(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、日常故障记录 " + record.getLocation() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、日常故障记录 " + record.getLocation() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、日常故障记录 " + record.getLocation() + " 导入失败：";
                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();
    }

}
