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.LuggageSystemSecurityDoorBatteryLifecycle;
import org.dromara.ems.info.mapper.LuggageSystemSecurityDoorBatteryLifecycleMapper;
import org.dromara.ems.info.service.ILuggageSystemSecurityDoorBatteryLifecycleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 行李系统安全门铅酸电池生命周期Service业务层处理
 * 
 * @author Yinq
 * @date 2025-06-05
 */
@Service
@RequiredArgsConstructor
public class LuggageSystemSecurityDoorBatteryLifecycleServiceImpl implements ILuggageSystemSecurityDoorBatteryLifecycleService 
{
    
    private final LuggageSystemSecurityDoorBatteryLifecycleMapper luggageSystemSecurityDoorBatteryLifecycleMapper;

    /**
     * 查询行李系统安全门铅酸电池生命周期
     * 
     * @param objid 行李系统安全门铅酸电池生命周期主键
     * @return 行李系统安全门铅酸电池生命周期
     */
    @Override
    public LuggageSystemSecurityDoorBatteryLifecycle selectLuggageSystemSecurityDoorBatteryLifecycleByObjid(Long objid)
    {
        return luggageSystemSecurityDoorBatteryLifecycleMapper.selectLuggageSystemSecurityDoorBatteryLifecycleByObjid(objid);
    }

    /**
     * 查询行李系统安全门铅酸电池生命周期列表
     * 
     * @param luggageSystemSecurityDoorBatteryLifecycle 行李系统安全门铅酸电池生命周期
     * @return 行李系统安全门铅酸电池生命周期
     */
    @Override
    public List<LuggageSystemSecurityDoorBatteryLifecycle> selectLuggageSystemSecurityDoorBatteryLifecycleList(LuggageSystemSecurityDoorBatteryLifecycle luggageSystemSecurityDoorBatteryLifecycle)
    {
        return luggageSystemSecurityDoorBatteryLifecycleMapper.selectLuggageSystemSecurityDoorBatteryLifecycleList(luggageSystemSecurityDoorBatteryLifecycle);
    }

    /**
     * 新增行李系统安全门铅酸电池生命周期
     * 
     * @param luggageSystemSecurityDoorBatteryLifecycle 行李系统安全门铅酸电池生命周期
     * @return 结果
     */
    @Override
    public int insertLuggageSystemSecurityDoorBatteryLifecycle(LuggageSystemSecurityDoorBatteryLifecycle luggageSystemSecurityDoorBatteryLifecycle)
    {
        return luggageSystemSecurityDoorBatteryLifecycleMapper.insertLuggageSystemSecurityDoorBatteryLifecycle(luggageSystemSecurityDoorBatteryLifecycle);
    }

    /**
     * 修改行李系统安全门铅酸电池生命周期
     * 
     * @param luggageSystemSecurityDoorBatteryLifecycle 行李系统安全门铅酸电池生命周期
     * @return 结果
     */
    @Override
    public int updateLuggageSystemSecurityDoorBatteryLifecycle(LuggageSystemSecurityDoorBatteryLifecycle luggageSystemSecurityDoorBatteryLifecycle)
    {
        return luggageSystemSecurityDoorBatteryLifecycleMapper.updateLuggageSystemSecurityDoorBatteryLifecycle(luggageSystemSecurityDoorBatteryLifecycle);
    }

    /**
     * 批量删除行李系统安全门铅酸电池生命周期
     * 
     * @param objids 需要删除的行李系统安全门铅酸电池生命周期主键
     * @return 结果
     */
    @Override
    public int deleteLuggageSystemSecurityDoorBatteryLifecycleByObjids(Long[] objids)
    {
        return luggageSystemSecurityDoorBatteryLifecycleMapper.deleteLuggageSystemSecurityDoorBatteryLifecycleByObjids(objids);
    }

    /**
     * 删除行李系统安全门铅酸电池生命周期信息
     * 
     * @param objid 行李系统安全门铅酸电池生命周期主键
     * @return 结果
     */
    @Override
    public int deleteLuggageSystemSecurityDoorBatteryLifecycleByObjid(Long objid)
    {
        return luggageSystemSecurityDoorBatteryLifecycleMapper.deleteLuggageSystemSecurityDoorBatteryLifecycleByObjid(objid);
    }

    /**
     * 导入行李系统安全门铅酸电池生命周期数据
     * 
     * @param dataList 行李系统安全门铅酸电池生命周期数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importLuggageSystemSecurityDoorBatteryLifecycle(List<LuggageSystemSecurityDoorBatteryLifecycle> 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 (LuggageSystemSecurityDoorBatteryLifecycle data : dataList)
        {
            try
            {
                // 验证是否存在这个数据
                LuggageSystemSecurityDoorBatteryLifecycle u = luggageSystemSecurityDoorBatteryLifecycleMapper.selectLuggageSystemSecurityDoorBatteryLifecycleByObjid(data.getObjid());
                if (ObjectUtil.isNull(u))
                {
                    this.insertLuggageSystemSecurityDoorBatteryLifecycle(data);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、" + data.getType() + " 导入成功");
                }
                else if (updateSupport)
                {
                    this.updateLuggageSystemSecurityDoorBatteryLifecycle(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();
    }
}
