package com.cirs.biz.service.impl;

import java.util.List;

import com.cirs.common.core.domain.entity.SysUser;
import com.cirs.common.exception.ServiceException;
import com.cirs.common.utils.DateUtils;
import com.cirs.common.utils.SecurityUtils;
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.TVerificationTaskMapper;
import com.cirs.biz.domain.TVerificationTask;
import com.cirs.biz.service.ITVerificationTaskService;

import javax.validation.Validator;

/**
 * 验证任务Service业务层处理
 * 
 * @author allen
 * @date 2023-11-03
 */
@Service
public class TVerificationTaskServiceImpl implements ITVerificationTaskService 
{
    private static final Logger log = LoggerFactory.getLogger(TVerificationTaskServiceImpl.class);
    @Autowired
    private TVerificationTaskMapper tVerificationTaskMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询验证任务
     * 
     * @param id 验证任务主键
     * @return 验证任务
     */
    @Override
    public TVerificationTask selectTVerificationTaskById(Long id)
    {
        return tVerificationTaskMapper.selectTVerificationTaskById(id);
    }

    /**
     * 查询验证任务列表
     * 
     * @param tVerificationTask 验证任务
     * @return 验证任务
     */
    @Override
    public List<TVerificationTask> selectTVerificationTaskList(TVerificationTask tVerificationTask)
    {
        return tVerificationTaskMapper.selectTVerificationTaskList(tVerificationTask);
    }

    /**
     * 新增验证任务
     * 
     * @param tVerificationTask 验证任务
     * @return 结果
     */
    @Override
    public int insertTVerificationTask(TVerificationTask tVerificationTask)
    {
        tVerificationTask.setCreateTime(DateUtils.getNowDate());
        return tVerificationTaskMapper.insertTVerificationTask(tVerificationTask);
    }

    /**
     * 修改验证任务
     * 
     * @param tVerificationTask 验证任务
     * @return 结果
     */
    @Override
    public int updateTVerificationTask(TVerificationTask tVerificationTask)
    {
        tVerificationTask.setUpdateTime(DateUtils.getNowDate());
        return tVerificationTaskMapper.updateTVerificationTask(tVerificationTask);
    }

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

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

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

        for (TVerificationTask task : taskList)
        {
            try
            {

                BeanValidators.validateWithException(validator, task);
                task.setCreateBy(operName);
                tVerificationTaskMapper.insertTVerificationTask(task);
                successNum++;
                successMsg.append("<br/>" + successNum + "、任务 " + task.getTaskName() + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、任务 " + task.getTaskName() + " 导入失败：";
                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 TVerificationTask getLastTask() {
        return tVerificationTaskMapper.getLastTask();
    }
}
