package com.mirs.biz.service.impl;

import java.util.List;

import com.mirs.biz.domain.EmiMeasurement;
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.IntegratedDataMapper;
import com.mirs.biz.domain.IntegratedData;
import com.mirs.biz.service.IIntegratedDataService;

import javax.validation.Validator;

/**
 * 积分数据Service业务层处理
 * 
 * @author kk
 * @date 2025-04-24
 */
@Service
public class IntegratedDataServiceImpl implements IIntegratedDataService 
{
    private static final Logger log = LoggerFactory.getLogger(BrdfDataServiceImpl.class);
    @Autowired
    private IntegratedDataMapper integratedDataMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询积分数据
     * 
     * @param id 积分数据主键
     * @return 积分数据
     */
    @Override
    public IntegratedData selectIntegratedDataById(Long id)
    {
        return integratedDataMapper.selectIntegratedDataById(id);
    }

    /**
     * 查询积分数据列表
     * 
     * @param integratedData 积分数据
     * @return 积分数据
     */
    @Override
    public List<IntegratedData> selectIntegratedDataList(IntegratedData integratedData)
    {
        return integratedDataMapper.selectIntegratedDataList(integratedData);
    }

    /**
     * 新增积分数据
     * 
     * @param integratedData 积分数据
     * @return 结果
     */
    @Override
    public int insertIntegratedData(IntegratedData integratedData)
    {
        integratedData.setCreateTime(DateUtils.getNowDate());
        return integratedDataMapper.insertIntegratedData(integratedData);
    }

    /**
     * 修改积分数据
     * 
     * @param integratedData 积分数据
     * @return 结果
     */
    @Override
    public int updateIntegratedData(IntegratedData integratedData)
    {
        integratedData.setUpdateTime(DateUtils.getNowDate());
        return integratedDataMapper.updateIntegratedData(integratedData);
    }

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

    /**
     * 删除积分数据信息
     * 
     * @param id 积分数据主键
     * @return 结果
     */
    @Override
    public int deleteIntegratedDataById(Long id)
    {
        return integratedDataMapper.deleteIntegratedDataById(id);
    }

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

                BeanValidators.validateWithException(validator, data);
                integratedDataMapper.insertIntegratedData(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();
    }
}
