package com.mirs.biz.service.impl;

import java.util.List;

import com.mirs.biz.domain.BrdfData;
import com.mirs.biz.dto.IndexSearchDTO;
import com.mirs.common.exception.ServiceException;
import com.mirs.common.utils.DateUtils;
import com.mirs.common.utils.StringUtils;
import com.mirs.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mirs.biz.mapper.EmiMeasurementMapper;
import com.mirs.biz.domain.EmiMeasurement;
import com.mirs.biz.service.IEmiMeasurementService;

import javax.validation.Validator;

/**
 * 测量数据Service业务层处理
 * 
 * @author kk
 * @date 2025-04-24
 */
@Service
public class EmiMeasurementServiceImpl implements IEmiMeasurementService 
{
    private static final Logger log = LoggerFactory.getLogger(EmiMeasurementServiceImpl.class);
    @Autowired
    private EmiMeasurementMapper emiMeasurementMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询测量数据
     * 
     * @param id 测量数据主键
     * @return 测量数据
     */
    @Override
    public EmiMeasurement selectEmiMeasurementById(Long id)
    {
        return emiMeasurementMapper.selectEmiMeasurementById(id);
    }

    /**
     * 查询测量数据列表
     * 
     * @param emiMeasurement 测量数据
     * @return 测量数据
     */
    @Override
    public List<EmiMeasurement> selectEmiMeasurementList(EmiMeasurement emiMeasurement)
    {
        return emiMeasurementMapper.selectEmiMeasurementList(emiMeasurement);
    }

    /**
     * 新增测量数据
     * 
     * @param emiMeasurement 测量数据
     * @return 结果
     */
    @Override
    public int insertEmiMeasurement(EmiMeasurement emiMeasurement)
    {
        emiMeasurement.setCreateTime(DateUtils.getNowDate());
        return emiMeasurementMapper.insertEmiMeasurement(emiMeasurement);
    }

    /**
     * 修改测量数据
     * 
     * @param emiMeasurement 测量数据
     * @return 结果
     */
    @Override
    public int updateEmiMeasurement(EmiMeasurement emiMeasurement)
    {
        emiMeasurement.setUpdateTime(DateUtils.getNowDate());
        return emiMeasurementMapper.updateEmiMeasurement(emiMeasurement);
    }

    /**
     * 批量删除测量数据
     * 
     * @param ids 需要删除的测量数据主键
     * @return 结果
     */
    @Override
    public int deleteEmiMeasurementByIds(Long[] ids)
    {
        return emiMeasurementMapper.deleteEmiMeasurementByIds(ids);
    }

    /**
     * 删除测量数据信息
     * 
     * @param id 测量数据主键
     * @return 结果
     */
    @Override
    public int deleteEmiMeasurementById(Long id)
    {
        return emiMeasurementMapper.deleteEmiMeasurementById(id);
    }

    @Override
    public String importEmiMeasurement(List<EmiMeasurement> emiMeasurementList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(emiMeasurementList) || emiMeasurementList.isEmpty())
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i = 0;
        for (EmiMeasurement data : emiMeasurementList)
        {
            try
            {

                BeanValidators.validateWithException(validator, data);
                emiMeasurementMapper.insertEmiMeasurement(data);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、第 ").append(i++).append(" 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、第 " + (i++) + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List getTemperatureK() {
        return emiMeasurementMapper.getTemperatureK();
    }
}
