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.SparePartsUsageRecord;
import org.dromara.ems.info.mapper.SparePartsUsageRecordMapper;
import org.dromara.ems.info.service.ISparePartsUsageRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 动力能源部行输科备件领用更换记录Service业务层处理
 * 
 * @author Yinq
 * @date 2025-06-05
 */
@Service
@RequiredArgsConstructor
public class SparePartsUsageRecordServiceImpl implements ISparePartsUsageRecordService 
{
    
    private final SparePartsUsageRecordMapper sparePartsUsageRecordMapper;

    /**
     * 查询动力能源部行输科备件领用更换记录
     * 
     * @param id 动力能源部行输科备件领用更换记录主键
     * @return 动力能源部行输科备件领用更换记录
     */
    @Override
    public SparePartsUsageRecord selectSparePartsUsageRecordById(Long id)
    {
        return sparePartsUsageRecordMapper.selectSparePartsUsageRecordById(id);
    }

    /**
     * 查询动力能源部行输科备件领用更换记录列表
     * 
     * @param sparePartsUsageRecord 动力能源部行输科备件领用更换记录
     * @return 动力能源部行输科备件领用更换记录
     */
    @Override
    public List<SparePartsUsageRecord> selectSparePartsUsageRecordList(SparePartsUsageRecord sparePartsUsageRecord)
    {
        return sparePartsUsageRecordMapper.selectSparePartsUsageRecordList(sparePartsUsageRecord);
    }

    /**
     * 新增动力能源部行输科备件领用更换记录
     * 
     * @param sparePartsUsageRecord 动力能源部行输科备件领用更换记录
     * @return 结果
     */
    @Override
    public int insertSparePartsUsageRecord(SparePartsUsageRecord sparePartsUsageRecord)
    {
        return sparePartsUsageRecordMapper.insertSparePartsUsageRecord(sparePartsUsageRecord);
    }

    /**
     * 修改动力能源部行输科备件领用更换记录
     * 
     * @param sparePartsUsageRecord 动力能源部行输科备件领用更换记录
     * @return 结果
     */
    @Override
    public int updateSparePartsUsageRecord(SparePartsUsageRecord sparePartsUsageRecord)
    {
        return sparePartsUsageRecordMapper.updateSparePartsUsageRecord(sparePartsUsageRecord);
    }

    /**
     * 批量删除动力能源部行输科备件领用更换记录
     * 
     * @param ids 需要删除的动力能源部行输科备件领用更换记录主键
     * @return 结果
     */
    @Override
    public int deleteSparePartsUsageRecordByIds(Long[] ids)
    {
        return sparePartsUsageRecordMapper.deleteSparePartsUsageRecordByIds(ids);
    }

    /**
     * 删除动力能源部行输科备件领用更换记录信息
     * 
     * @param id 动力能源部行输科备件领用更换记录主键
     * @return 结果
     */
    @Override
    public int deleteSparePartsUsageRecordById(Long id)
    {
        return sparePartsUsageRecordMapper.deleteSparePartsUsageRecordById(id);
    }

    /**
     * 导入动力能源部行输科备件领用更换记录数据
     *
     * @param recordList 记录数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importSparePartsUsageRecord(List<SparePartsUsageRecord> 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 (SparePartsUsageRecord record : recordList)
        {
            try
            {
                // 设置操作人
                
                // 验证是否存在这个记录
                SparePartsUsageRecord existRecord = null;
                if (ObjectUtil.isNotEmpty(record.getDate()) && ObjectUtil.isNotEmpty(record.getSparePartName()))
                {
                    SparePartsUsageRecord queryRecord = new SparePartsUsageRecord();
                    queryRecord.setDate(record.getDate());
                    queryRecord.setSparePartName(record.getSparePartName());
                    List<SparePartsUsageRecord> existRecords = sparePartsUsageRecordMapper.selectSparePartsUsageRecordList(queryRecord);
                    if (existRecords.size() > 0)
                    {
                        existRecord = existRecords.get(0);
                    }
                }
                
                if (ObjectUtil.isNull(existRecord))
                {
                    this.insertSparePartsUsageRecord(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、备件记录 " + record.getSparePartName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    record.setId(existRecord.getId());
                    this.updateSparePartsUsageRecord(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、备件记录 " + record.getSparePartName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、备件记录 " + record.getSparePartName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、备件记录 " + record.getSparePartName() + " 导入失败：";
                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();
    }
}
