package com.cirs.biz.service.impl;

import java.util.List;

import com.cirs.biz.domain.SysTrain;
import com.cirs.common.exception.ServiceException;
import com.cirs.common.utils.StringUtils;
import com.cirs.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.cirs.biz.mapper.FactoryScoreMapper;
import com.cirs.biz.domain.FactoryScore;
import com.cirs.biz.service.IFactoryScoreService;

import javax.validation.Validator;

/**
 * 厂家得分Service业务层处理
 * 
 * @author allen
 * @date 2023-12-10
 */
@Service
public class FactoryScoreServiceImpl implements IFactoryScoreService 
{
    @Autowired
    private FactoryScoreMapper factoryScoreMapper;

    private static final Logger log = LoggerFactory.getLogger(FactoryScoreServiceImpl.class);

    @Autowired
    protected Validator validator;
    /**
     * 查询厂家得分
     * 
     * @param id 厂家得分主键
     * @return 厂家得分
     */
    @Override
    public FactoryScore selectFactoryScoreById(Long id)
    {
        return factoryScoreMapper.selectFactoryScoreById(id);
    }

    /**
     * 查询厂家得分列表
     * 
     * @param factoryScore 厂家得分
     * @return 厂家得分
     */
    @Override
    public List<FactoryScore> selectFactoryScoreList(FactoryScore factoryScore)
    {
        return factoryScoreMapper.selectFactoryScoreList(factoryScore);
    }

    /**
     * 新增厂家得分
     * 
     * @param factoryScore 厂家得分
     * @return 结果
     */
    @Override
    public int insertFactoryScore(FactoryScore factoryScore)
    {
        return factoryScoreMapper.insertFactoryScore(factoryScore);
    }

    /**
     * 修改厂家得分
     * 
     * @param factoryScore 厂家得分
     * @return 结果
     */
    @Override
    public int updateFactoryScore(FactoryScore factoryScore)
    {
        return factoryScoreMapper.updateFactoryScore(factoryScore);
    }

    /**
     * 批量删除厂家得分
     * 
     * @param ids 需要删除的厂家得分主键
     * @return 结果
     */
    @Override
    public int deleteFactoryScoreByIds(Long[] ids)
    {
        return factoryScoreMapper.deleteFactoryScoreByIds(ids);
    }

    /**
     * 删除厂家得分信息
     * 
     * @param id 厂家得分主键
     * @return 结果
     */
    @Override
    public int deleteFactoryScoreById(Long id)
    {
        return factoryScoreMapper.deleteFactoryScoreById(id);
    }

    @Override
    public String importTrain(List<FactoryScore> scoreList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(scoreList) || scoreList.size() == 0)
        {
            throw new ServiceException("导入元器件数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (FactoryScore score : scoreList)
        {
            try
            {

                BeanValidators.validateWithException(validator, score);
                score.setCreateBy(operName);
                factoryScoreMapper.insertFactoryScore(score);
                successNum++;
                successMsg.append("<br/>" + successNum + "、训练数据 " + score.getId() + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、训练数据 " + score.getId() + " 导入失败：";
                failureMsg.append(msg + 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();
    }
}
