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.PowerEnergySupervisionChecklist;
import org.dromara.ems.info.mapper.PowerEnergySupervisionChecklistMapper;
import org.dromara.ems.info.service.IPowerEnergySupervisionChecklistService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 动力能源部监督检查清单Service业务层处理
 * 
 * @author Yinq
 * @date 2025-06-05
 */
@Service
@RequiredArgsConstructor
public class PowerEnergySupervisionChecklistServiceImpl implements IPowerEnergySupervisionChecklistService 
{

    private final PowerEnergySupervisionChecklistMapper powerEnergySupervisionChecklistMapper;

    /**
     * 查询动力能源部监督检查清单
     * 
     * @param id 动力能源部监督检查清单主键
     * @return 动力能源部监督检查清单
     */
    @Override
    public PowerEnergySupervisionChecklist selectPowerEnergySupervisionChecklistById(Long id)
    {
        return powerEnergySupervisionChecklistMapper.selectPowerEnergySupervisionChecklistById(id);
    }

    /**
     * 查询动力能源部监督检查清单列表
     * 
     * @param powerEnergySupervisionChecklist 动力能源部监督检查清单
     * @return 动力能源部监督检查清单
     */
    @Override
    public List<PowerEnergySupervisionChecklist> selectPowerEnergySupervisionChecklistList(PowerEnergySupervisionChecklist powerEnergySupervisionChecklist)
    {
        return powerEnergySupervisionChecklistMapper.selectPowerEnergySupervisionChecklistList(powerEnergySupervisionChecklist);
    }

    /**
     * 新增动力能源部监督检查清单
     * 
     * @param powerEnergySupervisionChecklist 动力能源部监督检查清单
     * @return 结果
     */
    @Override
    public int insertPowerEnergySupervisionChecklist(PowerEnergySupervisionChecklist powerEnergySupervisionChecklist)
    {
        return powerEnergySupervisionChecklistMapper.insertPowerEnergySupervisionChecklist(powerEnergySupervisionChecklist);
    }

    /**
     * 修改动力能源部监督检查清单
     * 
     * @param powerEnergySupervisionChecklist 动力能源部监督检查清单
     * @return 结果
     */
    @Override
    public int updatePowerEnergySupervisionChecklist(PowerEnergySupervisionChecklist powerEnergySupervisionChecklist)
    {
        return powerEnergySupervisionChecklistMapper.updatePowerEnergySupervisionChecklist(powerEnergySupervisionChecklist);
    }

    /**
     * 批量删除动力能源部监督检查清单
     * 
     * @param ids 需要删除的动力能源部监督检查清单主键
     * @return 结果
     */
    @Override
    public int deletePowerEnergySupervisionChecklistByIds(Long[] ids)
    {
        return powerEnergySupervisionChecklistMapper.deletePowerEnergySupervisionChecklistByIds(ids);
    }

    /**
     * 删除动力能源部监督检查清单信息
     * 
     * @param id 动力能源部监督检查清单主键
     * @return 结果
     */
    @Override
    public int deletePowerEnergySupervisionChecklistById(Long id)
    {
        return powerEnergySupervisionChecklistMapper.deletePowerEnergySupervisionChecklistById(id);
    }

    /**
     * 导入动力能源部监督检查清单数据
     *
     * @param recordList 记录数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importPowerEnergySupervisionChecklist(List<PowerEnergySupervisionChecklist> 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 (PowerEnergySupervisionChecklist record : recordList)
        {
            try
            {
                // 设置操作人
                
                // 验证是否存在这个记录
                PowerEnergySupervisionChecklist existRecord = null;
                if (ObjectUtil.isNotEmpty(record.getDate()) && ObjectUtil.isNotEmpty(record.getJobName()))
                {
                    PowerEnergySupervisionChecklist queryRecord = new PowerEnergySupervisionChecklist();
                    queryRecord.setDate(record.getDate());
                    queryRecord.setJobName(record.getJobName());
                    List<PowerEnergySupervisionChecklist> existRecords = powerEnergySupervisionChecklistMapper.selectPowerEnergySupervisionChecklistList(queryRecord);
                    if (existRecords.size() > 0)
                    {
                        existRecord = existRecords.get(0);
                    }
                }
                
                if (ObjectUtil.isNull(existRecord))
                {
                    this.insertPowerEnergySupervisionChecklist(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、检查记录 " + record.getJobName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    record.setId(existRecord.getId());
                    this.updatePowerEnergySupervisionChecklist(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、检查记录 " + record.getJobName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、检查记录 " + record.getJobName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、检查记录 " + record.getJobName() + " 导入失败：";
                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();
    }
}
