package com.cirs.biz.service.impl;

import java.util.List;

import com.cirs.biz.domain.TElectronComponent;
import com.cirs.biz.domain.UseInfofromRecord;
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.TElecCompRecordMapper;
import com.cirs.biz.domain.TElecCompRecord;
import com.cirs.biz.service.ITElecCompRecordService;

import javax.validation.Validator;

/**
 * 元器件使用经历Service业务层处理
 * 
 * @author allen
 * @date 2023-11-04
 */
@Service
public class TElecCompRecordServiceImpl implements ITElecCompRecordService 
{
    @Autowired
    private TElecCompRecordMapper tElecCompRecordMapper;

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

    @Autowired
    protected Validator validator;
    /**
     * 查询元器件使用经历
     * 
     * @param id 元器件使用经历主键
     * @return 元器件使用经历
     */
    @Override
    public TElecCompRecord selectTElecCompRecordById(Long id)
    {
        return tElecCompRecordMapper.selectTElecCompRecordById(id);
    }

    /**
     * 查询元器件使用经历列表
     * 
     * @param tElecCompRecord 元器件使用经历
     * @return 元器件使用经历
     */
    @Override
    public List<TElecCompRecord> selectTElecCompRecordList(TElecCompRecord tElecCompRecord)
    {
        return tElecCompRecordMapper.selectTElecCompRecordList(tElecCompRecord);
    }

    /**
     * 新增元器件使用经历
     * 
     * @param tElecCompRecord 元器件使用经历
     * @return 结果
     */
    @Override
    public int insertTElecCompRecord(TElecCompRecord tElecCompRecord)
    {
        tElecCompRecord.setCreateTime(DateUtils.getNowDate());
        return tElecCompRecordMapper.insertTElecCompRecord(tElecCompRecord);
    }

    /**
     * 修改元器件使用经历
     * 
     * @param tElecCompRecord 元器件使用经历
     * @return 结果
     */
    @Override
    public int updateTElecCompRecord(TElecCompRecord tElecCompRecord)
    {
        tElecCompRecord.setUpdateTime(DateUtils.getNowDate());
        return tElecCompRecordMapper.updateTElecCompRecord(tElecCompRecord);
    }

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

    /**
     * 删除元器件使用经历信息
     * 
     * @param id 元器件使用经历主键
     * @return 结果
     */
    @Override
    public int deleteTElecCompRecordById(Long id)
    {
        return tElecCompRecordMapper.deleteTElecCompRecordById(id);
    }

    /**
     * 利用元器件编号获取元器件使用场景和使用日期 wangruilin
     */
    @Override
    public List<UseInfofromRecord> getBycomponentId(Long componentId) {
        return tElecCompRecordMapper.getBycomponentId(componentId);
    }

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

        for (TElecCompRecord record : recordList)
        {
            try
            {

                BeanValidators.validateWithException(validator, record);
                record.setCreateBy(operName);
                tElecCompRecordMapper.insertTElecCompRecord(record);
                successNum++;
                successMsg.append("<br/>" + successNum + "、元器件记录 " + record.getId() + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、元器件记录 " + record.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();
    }
}
