package com.cirs.biz.service.impl;

import java.util.List;

import com.cirs.biz.domain.FactoryScore;
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.TMarkingrecordMapper;
import com.cirs.biz.domain.TMarkingrecord;
import com.cirs.biz.service.ITMarkingrecordService;

import javax.validation.Validator;

/**
 * 打分记录Service业务层处理
 * 
 * @author allen
 * @date 2023-12-18
 */
@Service
public class TMarkingrecordServiceImpl implements ITMarkingrecordService 
{
    @Autowired
    private TMarkingrecordMapper tMarkingrecordMapper;
    private static final Logger log = LoggerFactory.getLogger(TMarkingrecordServiceImpl.class);

    @Autowired
    protected Validator validator;

    /**
     * 查询打分记录
     * 
     * @param id 打分记录主键
     * @return 打分记录
     */
    @Override
    public TMarkingrecord selectTMarkingrecordById(Long id)
    {
        return tMarkingrecordMapper.selectTMarkingrecordById(id);
    }

    /**
     * 查询打分记录列表
     * 
     * @param tMarkingrecord 打分记录
     * @return 打分记录
     */
    @Override
    public List<TMarkingrecord> selectTMarkingrecordList(TMarkingrecord tMarkingrecord)
    {
        return tMarkingrecordMapper.selectTMarkingrecordList(tMarkingrecord);
    }

    /**
     * 新增打分记录
     * 
     * @param tMarkingrecord 打分记录
     * @return 结果
     */
    @Override
    public int insertTMarkingrecord(TMarkingrecord tMarkingrecord)
    {
        return tMarkingrecordMapper.insertTMarkingrecord(tMarkingrecord);
    }

    /**
     * 修改打分记录
     * 
     * @param tMarkingrecord 打分记录
     * @return 结果
     */
    @Override
    public int updateTMarkingrecord(TMarkingrecord tMarkingrecord)
    {
        return tMarkingrecordMapper.updateTMarkingrecord(tMarkingrecord);
    }

    /**
     * 批量删除打分记录
     * 
     * @param ids 需要删除的打分记录主键
     * @return 结果
     */
    @Override
    public int deleteTMarkingrecordByIds(Long[] ids)
    {
        return tMarkingrecordMapper.deleteTMarkingrecordByIds(ids);
    }

    /**
     * 删除打分记录信息
     * 
     * @param id 打分记录主键
     * @return 结果
     */
    @Override
    public int deleteTMarkingrecordById(Long id)
    {
        return tMarkingrecordMapper.deleteTMarkingrecordById(id);
    }

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

        for (TMarkingrecord markingrecord : markingrecordList)
        {
            try
            {

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

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