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.LuggageSystemPlcBufferBatteryLifecycle;
import org.dromara.ems.info.mapper.LuggageSystemPlcBufferBatteryLifecycleMapper;
import org.dromara.ems.info.service.ILuggageSystemPlcBufferBatteryLifecycleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 行李系统PLC缓冲电池生命周期Service业务层处理
 * 
 * @author Yinq
 * @date 2025-06-05
 */
@Service
@RequiredArgsConstructor
public class LuggageSystemPlcBufferBatteryLifecycleServiceImpl implements ILuggageSystemPlcBufferBatteryLifecycleService 
{
    
    private final LuggageSystemPlcBufferBatteryLifecycleMapper luggageSystemPlcBufferBatteryLifecycleMapper;

    /**
     * 查询行李系统PLC缓冲电池生命周期
     * 
     * @param objid 行李系统PLC缓冲电池生命周期主键
     * @return 行李系统PLC缓冲电池生命周期
     */
    @Override
    public LuggageSystemPlcBufferBatteryLifecycle selectLuggageSystemPlcBufferBatteryLifecycleByObjid(Long objid)
    {
        return luggageSystemPlcBufferBatteryLifecycleMapper.selectLuggageSystemPlcBufferBatteryLifecycleByObjid(objid);
    }

    /**
     * 查询行李系统PLC缓冲电池生命周期列表
     * 
     * @param luggageSystemPlcBufferBatteryLifecycle 行李系统PLC缓冲电池生命周期
     * @return 行李系统PLC缓冲电池生命周期
     */
    @Override
    public List<LuggageSystemPlcBufferBatteryLifecycle> selectLuggageSystemPlcBufferBatteryLifecycleList(LuggageSystemPlcBufferBatteryLifecycle luggageSystemPlcBufferBatteryLifecycle)
    {
        return luggageSystemPlcBufferBatteryLifecycleMapper.selectLuggageSystemPlcBufferBatteryLifecycleList(luggageSystemPlcBufferBatteryLifecycle);
    }

    /**
     * 新增行李系统PLC缓冲电池生命周期
     * 
     * @param luggageSystemPlcBufferBatteryLifecycle 行李系统PLC缓冲电池生命周期
     * @return 结果
     */
    @Override
    public int insertLuggageSystemPlcBufferBatteryLifecycle(LuggageSystemPlcBufferBatteryLifecycle luggageSystemPlcBufferBatteryLifecycle)
    {
        return luggageSystemPlcBufferBatteryLifecycleMapper.insertLuggageSystemPlcBufferBatteryLifecycle(luggageSystemPlcBufferBatteryLifecycle);
    }

    /**
     * 修改行李系统PLC缓冲电池生命周期
     * 
     * @param luggageSystemPlcBufferBatteryLifecycle 行李系统PLC缓冲电池生命周期
     * @return 结果
     */
    @Override
    public int updateLuggageSystemPlcBufferBatteryLifecycle(LuggageSystemPlcBufferBatteryLifecycle luggageSystemPlcBufferBatteryLifecycle)
    {
        return luggageSystemPlcBufferBatteryLifecycleMapper.updateLuggageSystemPlcBufferBatteryLifecycle(luggageSystemPlcBufferBatteryLifecycle);
    }

    /**
     * 批量删除行李系统PLC缓冲电池生命周期
     * 
     * @param objids 需要删除的行李系统PLC缓冲电池生命周期主键
     * @return 结果
     */
    @Override
    public int deleteLuggageSystemPlcBufferBatteryLifecycleByObjids(Long[] objids)
    {
        return luggageSystemPlcBufferBatteryLifecycleMapper.deleteLuggageSystemPlcBufferBatteryLifecycleByObjids(objids);
    }

    /**
     * 删除行李系统PLC缓冲电池生命周期信息
     * 
     * @param objid 行李系统PLC缓冲电池生命周期主键
     * @return 结果
     */
    @Override
    public int deleteLuggageSystemPlcBufferBatteryLifecycleByObjid(Long objid)
    {
        return luggageSystemPlcBufferBatteryLifecycleMapper.deleteLuggageSystemPlcBufferBatteryLifecycleByObjid(objid);
    }

    /**
     * 导入行李系统PLC缓冲电池生命周期数据
     * 
     * @param dataList 行李系统PLC缓冲电池生命周期数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importLuggageSystemPlcBufferBatteryLifecycle(List<LuggageSystemPlcBufferBatteryLifecycle> dataList, Boolean updateSupport, String operName)
    {
        if (ObjectUtil.isEmpty(dataList) || dataList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (LuggageSystemPlcBufferBatteryLifecycle data : dataList)
        {
            try
            {
                // 验证是否存在这个数据
                LuggageSystemPlcBufferBatteryLifecycle u = luggageSystemPlcBufferBatteryLifecycleMapper.selectLuggageSystemPlcBufferBatteryLifecycleByObjid(data.getObjid());
                if (ObjectUtil.isNull(u))
                {
                    this.insertLuggageSystemPlcBufferBatteryLifecycle(data);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、" + data.getType() + " 导入成功");
                }
                else if (updateSupport)
                {
                    this.updateLuggageSystemPlcBufferBatteryLifecycle(data);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、" + data.getType() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、" + data.getType() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、" + data.getType() + " 导入失败：";
                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();
    }
}
