package com.ruoyi.traceability.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.security.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.traceability.mapper.BaseAlarminfoMapper;
import com.ruoyi.traceability.domain.BaseAlarminfo;
import com.ruoyi.traceability.service.IBaseAlarminfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 报警信息Service业务层处理
 * 
 * @author wenjy
 * @date 2023-01-01
 */
@Service
public class BaseAlarminfoServiceImpl implements IBaseAlarminfoService 
{
    @Autowired
    private BaseAlarminfoMapper baseAlarminfoMapper;

    /**
     * 查询报警信息
     * 
     * @param objId 报警信息主键
     * @return 报警信息
     */
    @Override
    public BaseAlarminfo selectBaseAlarminfoByObjId(Long objId)
    {
        return baseAlarminfoMapper.selectBaseAlarminfoByObjId(objId);
    }

    /**
     * 查询报警信息列表
     * 
     * @param baseAlarminfo 报警信息
     * @return 报警信息
     */
    @Override
    public List<BaseAlarminfo> selectBaseAlarminfoList(BaseAlarminfo baseAlarminfo)
    {
        return baseAlarminfoMapper.selectBaseAlarminfoList(baseAlarminfo);
    }

    /**
     * 新增报警信息
     * 
     * @param baseAlarminfo 报警信息
     * @return 结果
     */
    @Override
    public int insertBaseAlarminfo(BaseAlarminfo baseAlarminfo)
    {
        return baseAlarminfoMapper.insertBaseAlarminfo(baseAlarminfo);
    }

    /**
     * 修改报警信息
     * 
     * @param baseAlarminfo 报警信息
     * @return 结果
     */
    @Override
    public int updateBaseAlarminfo(BaseAlarminfo baseAlarminfo)
    {
        return baseAlarminfoMapper.updateBaseAlarminfo(baseAlarminfo);
    }

    /**
     * 批量删除报警信息
     * 
     * @param objIds 需要删除的报警信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseAlarminfoByObjIds(String objIds)
    {
        return baseAlarminfoMapper.deleteBaseAlarminfoByObjIds(Convert.toStrArray(objIds));
    }

    /**
     * 删除报警信息信息
     * 
     * @param objId 报警信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseAlarminfoByObjId(Long objId)
    {
        return baseAlarminfoMapper.deleteBaseAlarminfoByObjId(objId);
    }

    @Override
    public String importAlarmInfo(List<BaseAlarminfo> alarminfoList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(alarminfoList) || alarminfoList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseAlarminfo alarminfo : alarminfoList)
        {
            try
            {
                BaseAlarminfo info = baseAlarminfoMapper.selectBaseAlarmInfoByAlarmCode(alarminfo.getAlarmCode());
                if (StringUtils.isNull(info))
                {
                    this.insertBaseAlarminfo(alarminfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号 " + alarminfo.getAlarmCode() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    info.setAlarmMessage(alarminfo.getAlarmMessage());
                    info.setAlarmPropose(alarminfo.getAlarmPropose());
                    this.updateBaseAlarminfo(info);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号 " + info.getAlarmCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、编号 " + info.getAlarmCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编号 " + alarminfo.getAlarmCode() + " 导入失败：";
                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();
    }
}
