package com.cirs.biz.service.impl;

import java.util.Date;
import java.util.List;

import com.cirs.biz.domain.TVerificationTask;
import com.cirs.biz.domain.VerificationData;
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.TVerificationTaskDetailMapper;
import com.cirs.biz.domain.TVerificationTaskDetail;
import com.cirs.biz.service.ITVerificationTaskDetailService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;

/**
 * 验证任务详情Service业务层处理
 * 
 * @author allen
 * @date 2023-11-03
 */
@Service
public class TVerificationTaskDetailServiceImpl implements ITVerificationTaskDetailService 
{
    private static final Logger log = LoggerFactory.getLogger(TVerificationTaskDetailServiceImpl.class);

    @Autowired
    private TVerificationTaskDetailMapper tVerificationTaskDetailMapper;

    @Autowired
    private TVerificationTaskMapper tVerificationTaskMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询验证任务详情
     * 
     * @param id 验证任务详情主键
     * @return 验证任务详情
     */
    @Override
    public TVerificationTaskDetail selectTVerificationTaskDetailById(Long id)
    {
        return tVerificationTaskDetailMapper.selectTVerificationTaskDetailById(id);
    }

    /**
     * 查询验证任务详情列表
     * 
     * @param tVerificationTaskDetail 验证任务详情
     * @return 验证任务详情
     */
    @Override
    public List<TVerificationTaskDetail> selectTVerificationTaskDetailList(TVerificationTaskDetail tVerificationTaskDetail)
    {
        return tVerificationTaskDetailMapper.selectTVerificationTaskDetailList(tVerificationTaskDetail);
    }

    /**
     * 新增验证任务详情
     * 
     * @param tVerificationTaskDetail 验证任务详情
     * @return 结果
     */
    @Override
    public int insertTVerificationTaskDetail(TVerificationTaskDetail tVerificationTaskDetail)
    {
        tVerificationTaskDetail.setCreateTime(DateUtils.getNowDate());
        return tVerificationTaskDetailMapper.insertTVerificationTaskDetail(tVerificationTaskDetail);
    }

    /**
     * 修改验证任务详情
     * 
     * @param tVerificationTaskDetail 验证任务详情
     * @return 结果
     */
    @Override
    public int updateTVerificationTaskDetail(TVerificationTaskDetail tVerificationTaskDetail)
    {
        tVerificationTaskDetail.setUpdateTime(DateUtils.getNowDate());
        return tVerificationTaskDetailMapper.updateTVerificationTaskDetail(tVerificationTaskDetail);
    }

    /**
     * 批量删除验证任务详情
     * 
     * @param ids 需要删除的验证任务详情主键
     * @return 结果
     */
    @Override
    public int deleteTVerificationTaskDetailByIds(Long[] ids)
    {
        return tVerificationTaskDetailMapper.deleteTVerificationTaskDetailByIds(ids);
    }

    /**
     * 删除验证任务详情信息
     * 
     * @param id 验证任务详情主键
     * @return 结果
     */
    @Override
    public int deleteTVerificationTaskDetailById(Long id)
    {
        return tVerificationTaskDetailMapper.deleteTVerificationTaskDetailById(id);
    }

    @Override
    @Transactional
    public String importTaskDetail(List<TVerificationTaskDetail> taskDetailList, boolean updateSupport, String operName, String fileName) {
        if (StringUtils.isNull(taskDetailList) || taskDetailList.size() == 0)
        {
            throw new ServiceException("导入任务数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        TVerificationTask task = new TVerificationTask();

        task.setCreateBy(operName);//setCreateBy(operName)
        task.setTaskStatus("未运行");

        task.setTaskName(fileName);
        task.setCreateTime(new Date());

        tVerificationTaskMapper.insertTVerificationTask(task);

        for (TVerificationTaskDetail taskDetail : taskDetailList)
        {
            try
            {

                BeanValidators.validateWithException(validator, taskDetail);
                taskDetail.setCreateBy(operName);

                taskDetail.setTaskId(task.getId());//任务标识

                tVerificationTaskDetailMapper.insertTVerificationTaskDetail(taskDetail);
                successNum++;
                successMsg.append("<br/>" + successNum + "、任务 " + taskDetail.getTaskId() + "详情导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、任务 " + taskDetail.getTaskId() + " 详情导入失败：";
                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 + " 条，数据如下：");
        }
        task.setSubtaskNum((long) successNum);
        tVerificationTaskMapper.updateTVerificationTask(task);

        return successMsg.toString();
    }

    @Override
    public List<TVerificationTaskDetail> selectTVerificationTaskDetailByTaskIds(List<Long> taskids) {
        return tVerificationTaskDetailMapper.selectTVerificationTaskDetailByTaskIds(taskids);

    }

    @Override
    public List<TVerificationTaskDetail> alldata(Long taskid) {
        return tVerificationTaskDetailMapper.alldata(taskid);
    }

    @Override
    public Long setComponentId(String result) {
        String[] attrs = result.split("-");//切割不同属性
        return tVerificationTaskDetailMapper.getComponentId(attrs[0],attrs[1],attrs[2]);
    }

    @Override
    public List<VerificationData> getComponentids(Long taskid) {
        return tVerificationTaskDetailMapper.getComponentids(taskid);
    }
}
