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.PowerEnergySparePartsRegistration;
import org.dromara.ems.info.mapper.PowerEnergySparePartsRegistrationMapper;
import org.dromara.ems.info.service.IPowerEnergySparePartsRegistrationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 动力能源部行输科废旧备件登记Service业务层处理
 * 
 * @author Yinq
 * @date 2025-06-06
 */
@Service
@RequiredArgsConstructor
public class PowerEnergySparePartsRegistrationServiceImpl implements IPowerEnergySparePartsRegistrationService 
{
    
    private final PowerEnergySparePartsRegistrationMapper powerEnergySparePartsRegistrationMapper;

    /**
     * 查询动力能源部行输科废旧备件登记
     * 
     * @param objid 动力能源部行输科废旧备件登记主键
     * @return 动力能源部行输科废旧备件登记
     */
    @Override
    public PowerEnergySparePartsRegistration selectPowerEnergySparePartsRegistrationByObjid(Long objid)
    {
        return powerEnergySparePartsRegistrationMapper.selectPowerEnergySparePartsRegistrationByObjid(objid);
    }

    /**
     * 查询动力能源部行输科废旧备件登记列表
     * 
     * @param powerEnergySparePartsRegistration 动力能源部行输科废旧备件登记
     * @return 动力能源部行输科废旧备件登记
     */
    @Override
    public List<PowerEnergySparePartsRegistration> selectPowerEnergySparePartsRegistrationList(PowerEnergySparePartsRegistration powerEnergySparePartsRegistration)
    {
        return powerEnergySparePartsRegistrationMapper.selectPowerEnergySparePartsRegistrationList(powerEnergySparePartsRegistration);
    }

    /**
     * 新增动力能源部行输科废旧备件登记
     * 
     * @param powerEnergySparePartsRegistration 动力能源部行输科废旧备件登记
     * @return 结果
     */
    @Override
    public int insertPowerEnergySparePartsRegistration(PowerEnergySparePartsRegistration powerEnergySparePartsRegistration)
    {
        return powerEnergySparePartsRegistrationMapper.insertPowerEnergySparePartsRegistration(powerEnergySparePartsRegistration);
    }

    /**
     * 修改动力能源部行输科废旧备件登记
     * 
     * @param powerEnergySparePartsRegistration 动力能源部行输科废旧备件登记
     * @return 结果
     */
    @Override
    public int updatePowerEnergySparePartsRegistration(PowerEnergySparePartsRegistration powerEnergySparePartsRegistration)
    {
        return powerEnergySparePartsRegistrationMapper.updatePowerEnergySparePartsRegistration(powerEnergySparePartsRegistration);
    }

    /**
     * 批量删除动力能源部行输科废旧备件登记
     * 
     * @param objids 需要删除的动力能源部行输科废旧备件登记主键
     * @return 结果
     */
    @Override
    public int deletePowerEnergySparePartsRegistrationByObjids(Long[] objids)
    {
        return powerEnergySparePartsRegistrationMapper.deletePowerEnergySparePartsRegistrationByObjids(objids);
    }

    /**
     * 删除动力能源部行输科废旧备件登记信息
     * 
     * @param objid 动力能源部行输科废旧备件登记主键
     * @return 结果
     */
    @Override
    public int deletePowerEnergySparePartsRegistrationByObjid(Long objid)
    {
        return powerEnergySparePartsRegistrationMapper.deletePowerEnergySparePartsRegistrationByObjid(objid);
    }

    /**
     * 导入动力能源部行输科废旧备件登记数据
     *
     * @param registrationList 登记数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importPowerEnergySparePartsRegistration(List<PowerEnergySparePartsRegistration> registrationList, Boolean isUpdateSupport, String operName)
    {
        if (ObjectUtil.isNull(registrationList) || registrationList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PowerEnergySparePartsRegistration registration : registrationList)
        {
            try
            {
                // 设置操作人
                
                // 验证是否存在这个记录（使用日期+备件名称+存放位置作为唯一性检查）
                PowerEnergySparePartsRegistration existRegistration = null;
                if (ObjectUtil.isNotEmpty(registration.getDate()) && ObjectUtil.isNotEmpty(registration.getSparePartsName()) && ObjectUtil.isNotEmpty(registration.getStorageLocation()))
                {
                    PowerEnergySparePartsRegistration queryRegistration = new PowerEnergySparePartsRegistration();
                    queryRegistration.setDate(registration.getDate());
                    queryRegistration.setSparePartsName(registration.getSparePartsName());
                    queryRegistration.setStorageLocation(registration.getStorageLocation());
                    List<PowerEnergySparePartsRegistration> existRegistrations = powerEnergySparePartsRegistrationMapper.selectPowerEnergySparePartsRegistrationList(queryRegistration);
                    if (existRegistrations.size() > 0)
                    {
                        existRegistration = existRegistrations.get(0);
                    }
                }
                
                if (ObjectUtil.isNull(existRegistration))
                {
                    this.insertPowerEnergySparePartsRegistration(registration);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、废旧备件登记 " + registration.getSparePartsName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    registration.setObjid(existRegistration.getObjid());
                    this.updatePowerEnergySparePartsRegistration(registration);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、废旧备件登记 " + registration.getSparePartsName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、废旧备件登记 " + registration.getSparePartsName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、废旧备件登记 " + registration.getSparePartsName() + " 导入失败：";
                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();
    }
}
