package com.cirs.biz.service.impl;

import java.util.List;


import com.cirs.biz.domain.TrainReturn;
import com.cirs.biz.domain.VerificationData;
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.SysTrainMapper;
import com.cirs.biz.domain.SysTrain;
import com.cirs.biz.service.ISysTrainService;

import javax.validation.Validator;

/**
 * 训练集数据列Service业务层处理
 * 
 * @author allen
 * @date 2023-11-28
 */
@Service
public class SysTrainServiceImpl implements ISysTrainService 
{
    @Autowired
    private SysTrainMapper sysTrainMapper;

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

    @Autowired
    protected Validator validator;

    /**
     * 查询训练集数据列
     * 
     * @param id 训练集数据列主键
     * @return 训练集数据列
     */
    @Override
    public SysTrain selectSysTrainById(Long id)
    {
        return sysTrainMapper.selectSysTrainById(id);
    }

    /**
     * 查询训练集数据列列表
     * 
     * @param sysTrain 训练集数据列
     * @return 训练集数据列
     */
    @Override
    public List<SysTrain> selectSysTrainList(SysTrain sysTrain)
    {

        return sysTrainMapper.selectSysTrainList(sysTrain);
    }

    /**
     * 新增训练集数据列
     * 
     * @param sysTrain 训练集数据列
     * @return 结果
     */
    @Override
    public int insertSysTrain(SysTrain sysTrain)
    {
        return sysTrainMapper.insertSysTrain(sysTrain);
    }

    /**
     * 修改训练集数据列
     * 
     * @param sysTrain 训练集数据列
     * @return 结果
     */
    @Override
    public int updateSysTrain(SysTrain sysTrain)
    {
        return sysTrainMapper.updateSysTrain(sysTrain);
    }

    /**
     * 批量删除训练集数据列
     * 
     * @param ids 需要删除的训练集数据列主键
     * @return 结果
     */
    @Override
    public int deleteSysTrainByIds(Long[] ids)
    {
        return sysTrainMapper.deleteSysTrainByIds(ids);
    }

    /**
     * 删除训练集数据列信息
     * 
     * @param id 训练集数据列主键
     * @return 结果
     */
    @Override
    public int deleteSysTrainById(Long id)
    {
        return sysTrainMapper.deleteSysTrainById(id);
    }

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

        for (SysTrain train : trainList)
        {
            try
            {

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

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、训练数据 " + train.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();
    }

    @Override
    public Long getComponentId(String result) {//元器件名称-型号规格-质量等级
        String[] attrs = result.split("-");//切割不同属性
        return sysTrainMapper.getComponentId(attrs[0],attrs[1],attrs[2]);
    }

    @Override
    public List<SysTrain> alldata() {
        return sysTrainMapper.getall_data();
    }

    @Override
    public List<TrainReturn> getComponentids() {
        return sysTrainMapper.getComponentids();
    }

    @Override
    public List<VerificationData> gettraindataset() {
        return sysTrainMapper.gettraindataset();
    }


}
