package com.admin.major.service.impl;

import com.admin.common.utils.uuid.IdUtils;
import com.admin.major.domain.AdjustResult;
import com.admin.major.domain.dto.AdjustResultDTO;
import com.admin.major.domain.dto.SummaryApplyDTO;
import com.admin.major.mapper.AdjustResultMapper;
import com.admin.major.service.IAdjustResultService;
import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.MyBatisExceptionTranslator;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 分流结果Service业务层处理
 * 
 * @author zewei
 * @date 2022-03-28
 */
@Service
public class AdjustResultServiceImpl implements IAdjustResultService 
{
    @Autowired
    private AdjustResultMapper adjustResultMapper;

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;


    /**
     * 查询分流结果
     * 
     * @param studentId 分流结果主键
     * @return 分流结果
     */
    @Override
    public AdjustResult selectAdjustResultByStudentId(String studentId)
    {
        return adjustResultMapper.selectAdjustResultByBatchId(studentId);
    }

    /**
     * 查询分流结果列表
     * 
     * @param adjustResult 分流结果
     * @return 分流结果
     */
    @Override
    public List<AdjustResult> selectAdjustResultList(AdjustResult adjustResult)
    {
        return adjustResultMapper.selectAdjustResultList(adjustResult);
    }



    /**
     * 查询分流结果
     *
     * @param adjustResult 分流结果
     * @return 分流结果
     */
    @Override
    public AdjustResult selectOneAdjustResult(AdjustResult adjustResult)
    {
        return adjustResultMapper.selectAdjustResultList(adjustResult)
                .stream()
                .findFirst()
                .orElse(null);
    }

    /**
     * 新增分流结果
     *
     * @param adjustResult 分流结果
     * @return 结果
     */

    @Override
    public int insertAdjustResult(AdjustResult adjustResult)
    {
        adjustResult.setBatchId(IdUtils.simpleUUID());
        return adjustResultMapper.insertAdjustResult(adjustResult);
    }

    /**
     * 修改分流结果
     * 
     * @param adjustResult 分流结果
     * @return 结果
     */
    @Override
    public int updateAdjustResult(AdjustResult adjustResult)
    {
        return adjustResultMapper.updateAdjustResult(adjustResult);
    }

    /**
     * 批量删除分流结果
     * 
     * @param batchIds 需要删除的分流结果主键
     * @return 结果
     */
    @Override
    public int deleteAdjustResultByBatchIds(String[] batchIds)
    {
        return adjustResultMapper.deleteAdjustResultByBatchIds(batchIds);
    }

    /**
     * 删除分流结果信息
     * 
     * @param batchId 分流结果主键
     * @return 结果
     */
    @Override
    public int deleteAdjustResultByBatchId(String batchId)
    {
        return adjustResultMapper.deleteAdjustResultByBatchId(batchId);
    }

    @Override
    public int deleteAdjustResultByDynamic(AdjustResult adjustResult) {
        return adjustResultMapper.deleteAdjustResultByDynamic(adjustResult);
    }

    @Override
    public boolean saveBatch(List<AdjustResult> adjustResults) {
        SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);//跟上述sql区别

        int batchSize = 300;
        try {
            for (int i = 0; i < adjustResults.size(); i++) {
                adjustResultMapper.insertAdjustResult(adjustResults.get(i));
                if (i % batchSize == 0 || i == adjustResults.size()) {
                    sqlSession.flushStatements();
                }
            }
            sqlSession.commit();
        } catch (Throwable var11) {
            sqlSession.rollback();
            Throwable unwrapped = ExceptionUtil.unwrapThrowable(var11);
            if (unwrapped instanceof RuntimeException) {
                MyBatisExceptionTranslator myBatisExceptionTranslator = new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true);
                throw (DataAccessException) Objects.requireNonNull(myBatisExceptionTranslator.translateExceptionIfPossible((RuntimeException)unwrapped));
            }

            throw new RuntimeException("插入失败");
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    public List<AdjustResultDTO> viewAdjustResult(AdjustResultDTO adjustResult){
        return adjustResultMapper.viewAdjustResult(adjustResult);
    }

}
