package com.cirs.biz.service.impl;

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

import com.cirs.biz.domain.TVerificationScoretask;
import com.cirs.biz.domain.TVerificationTask;
import com.cirs.biz.domain.TVerificationTaskDetail;
import com.cirs.biz.mapper.TVerificationScoretaskMapper;
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.TVerificationScoretaskDetailMapper;
import com.cirs.biz.domain.TVerificationScoretaskDetail;
import com.cirs.biz.service.ITVerificationScoretaskDetailService;

import javax.validation.Validator;

/**
 * 打分算法验证详情Service业务层处理
 * 
 * @author allen
 * @date 2023-12-10
 */
@Service
public class TVerificationScoretaskDetailServiceImpl implements ITVerificationScoretaskDetailService 
{
    private static final Logger log = LoggerFactory.getLogger(TVerificationScoretaskDetailServiceImpl.class);
    @Autowired
    private TVerificationScoretaskDetailMapper tVerificationScoretaskDetailMapper;

    @Autowired
    private TVerificationScoretaskMapper tVerificationScoretaskMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询打分算法验证详情
     * 
     * @param id 打分算法验证详情主键
     * @return 打分算法验证详情
     */
    @Override
    public TVerificationScoretaskDetail selectTVerificationScoretaskDetailById(Long id)
    {
        return tVerificationScoretaskDetailMapper.selectTVerificationScoretaskDetailById(id);
    }

    /**
     * 查询打分算法验证详情列表
     * 
     * @param tVerificationScoretaskDetail 打分算法验证详情
     * @return 打分算法验证详情
     */
    @Override
    public List<TVerificationScoretaskDetail> selectTVerificationScoretaskDetailList(TVerificationScoretaskDetail tVerificationScoretaskDetail)
    {
        return tVerificationScoretaskDetailMapper.selectTVerificationScoretaskDetailList(tVerificationScoretaskDetail);
    }

    /**
     * 新增打分算法验证详情
     * 
     * @param tVerificationScoretaskDetail 打分算法验证详情
     * @return 结果
     */
    @Override
    public int insertTVerificationScoretaskDetail(TVerificationScoretaskDetail tVerificationScoretaskDetail)
    {
        return tVerificationScoretaskDetailMapper.insertTVerificationScoretaskDetail(tVerificationScoretaskDetail);
    }

    /**
     * 修改打分算法验证详情
     * 
     * @param tVerificationScoretaskDetail 打分算法验证详情
     * @return 结果
     */
    @Override
    public int updateTVerificationScoretaskDetail(TVerificationScoretaskDetail tVerificationScoretaskDetail)
    {
        return tVerificationScoretaskDetailMapper.updateTVerificationScoretaskDetail(tVerificationScoretaskDetail);
    }

    /**
     * 批量删除打分算法验证详情
     * 
     * @param ids 需要删除的打分算法验证详情主键
     * @return 结果
     */
    @Override
    public int deleteTVerificationScoretaskDetailByIds(Long[] ids)
    {
        return tVerificationScoretaskDetailMapper.deleteTVerificationScoretaskDetailByIds(ids);
    }

    /**
     * 删除打分算法验证详情信息
     * 
     * @param id 打分算法验证详情主键
     * @return 结果
     */
    @Override
    public int deleteTVerificationScoretaskDetailById(Long id)
    {
        return tVerificationScoretaskDetailMapper.deleteTVerificationScoretaskDetailById(id);
    }

    @Override
    public String importTaskDetail(List<TVerificationScoretaskDetail> 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();

        TVerificationScoretask task = new TVerificationScoretask();

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

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

        tVerificationScoretaskMapper.insertTVerificationScoretask(task);

        for (TVerificationScoretaskDetail taskDetail : taskDetailList)
        {
            try
            {

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

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

                tVerificationScoretaskDetailMapper.insertTVerificationScoretaskDetail(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);
        tVerificationScoretaskMapper.updateTVerificationScoretask(task);

        return successMsg.toString();
    }

    @Override
    public List<TVerificationScoretaskDetail> getByscoretaskid(Long taskid) {
        return tVerificationScoretaskDetailMapper.getByscoretaskid(taskid);
    }

    @Override
    public List<TVerificationScoretaskDetail> selectTVerificationScoreaskDetailByTaskIds(List<Long> taskids) {
        return tVerificationScoretaskDetailMapper.selectTVerificationScoreaskDetailByTaskIds(taskids);
    }
}
