package com.cirs.biz.service.impl;

import java.util.List;

import com.cirs.biz.domain.ElectronComponent;
import com.cirs.biz.domain.TVerificationTask;
import com.cirs.biz.mapper.TVerificationTaskMapper;
import com.cirs.common.exception.ServiceException;
import com.cirs.common.utils.DateUtils;
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.TElectronComponentMapper;
import com.cirs.biz.domain.TElectronComponent;
import com.cirs.biz.service.ITElectronComponentService;

import javax.validation.Validator;

/**
 * 元器件Service业务层处理
 * 
 * @author allen
 * @date 2023-11-03
 */
@Service
public class TElectronComponentServiceImpl implements ITElectronComponentService 
{
    @Autowired
    private TElectronComponentMapper tElectronComponentMapper;

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

    @Autowired
    protected Validator validator;
    /**
     * 查询元器件
     * 
     * @param id 元器件主键
     * @return 元器件
     */
    @Override
    public TElectronComponent selectTElectronComponentById(Long id)
    {
        return tElectronComponentMapper.selectTElectronComponentById(id);
    }

    /**
     * 查询元器件列表
     * 
     * @param tElectronComponent 元器件
     * @return 元器件
     */
    @Override
    public List<TElectronComponent> selectTElectronComponentList(TElectronComponent tElectronComponent)
    {
        return tElectronComponentMapper.selectTElectronComponentList(tElectronComponent);
    }

    /**
     * 新增元器件
     * 
     * @param tElectronComponent 元器件
     * @return 结果
     */
    @Override
    public int insertTElectronComponent(TElectronComponent tElectronComponent)
    {
        tElectronComponent.setCreateTime(DateUtils.getNowDate());
        return tElectronComponentMapper.insertTElectronComponent(tElectronComponent);
    }

    /**
     * 修改元器件
     * 
     * @param tElectronComponent 元器件
     * @return 结果
     */
    @Override
    public int updateTElectronComponent(TElectronComponent tElectronComponent)
    {
        tElectronComponent.setUpdateTime(DateUtils.getNowDate());
        return tElectronComponentMapper.updateTElectronComponent(tElectronComponent);
    }

    /**
     * 批量删除元器件
     * 
     * @param ids 需要删除的元器件主键
     * @return 结果
     */
    @Override
    public int deleteTElectronComponentByIds(Long[] ids)
    {
        return tElectronComponentMapper.deleteTElectronComponentByIds(ids);
    }

    /**
     * 删除元器件信息
     * 
     * @param id 元器件主键
     * @return 结果
     */
    @Override
    public int deleteTElectronComponentById(Long id)
    {
        return tElectronComponentMapper.deleteTElectronComponentById(id);
    }

    @Override
    public List<TElectronComponent> getAll() {
        return tElectronComponentMapper.getAll();
    }

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

        for (TElectronComponent component : componentList)
        {
            try
            {

                BeanValidators.validateWithException(validator, component);
                component.setCreateBy(operName);
                tElectronComponentMapper.insertTElectronComponent(component);
                successNum++;
                successMsg.append("<br/>" + successNum + "、元器件 " + component.getComponentName() + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、元器件 " + component.getComponentName() + " 导入失败：";
                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 String getDetailById(Long id) {
        ElectronComponent detail = tElectronComponentMapper.getDetailById(id);
        return detail.getComponent_name()+"-"+detail.getComponent_model()+"-"+detail.getQuality_grade();
    }
}
