package com.shkpr.service.alambizplugin.dbdao.services; import com.global.base.log.LogLevelFlag; import com.global.base.log.LogPrintMgr; import com.shkpr.service.alambizplugin.constants.GisSurveyExcelDefine; import com.shkpr.service.alambizplugin.constants.LogFlagBusiType; import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyJobInfoMapper; import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyJobStatusTrackMapper; import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyLayerApplyMapper; import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyLayerApplyThirdCopyMapper; import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyProjectInfoMapper; import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyPropertyValueMapper; import com.shkpr.service.alambizplugin.dbdao.services.intef.GisSurveyLayerApplyService; import com.shkpr.service.alambizplugin.dto.GisSurveyJobStatusTrack; import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApply; import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApplyLine; import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApplyPoint; import org.apache.ibatis.cursor.Cursor; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.sql.Connection; import java.util.ArrayList; import java.util.List; /** * 采集元素处理申请service实现 * * @author 欧阳劲驰 * @since 1.0.0 */ @Service public class GisSurveyLayerApplyServiceImpl implements GisSurveyLayerApplyService { /** * log */ private final String mStrClassName; private final String mBizType; private final SqlSessionFactory sqlSessionFactory; private final GisSurveyLayerApplyMapper layerApplyMapper; public GisSurveyLayerApplyServiceImpl(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory , GisSurveyLayerApplyMapper layerApplyMapper) { mStrClassName = "GisSurveyLayerApplyServiceImpl"; mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue(); this.sqlSessionFactory = sqlSessionFactory; this.layerApplyMapper = layerApplyMapper; } /** * 根据项目id查询新增点 * * @param projId 项目id * @return 点集合 */ @Transactional(transactionManager = "mainDbTransactionManager") public List findAddPointByProjId(String projId) throws InterruptedException { //点集合 List points = new ArrayList<>(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "开始根据项目id查询新增点,开启事务和游标 项目ID:%s" , projId ) ); long begin = System.currentTimeMillis(); //获取游标 try (Cursor cursor = layerApplyMapper.findAddUpdatePointByProjId(projId)) { //迭代游标 for (GisSurveyLayerApplyPoint point : cursor) { //检查线程中断,并响应 if (Thread.interrupted()) throw new InterruptedException(); //检查游标完成 if (cursor.isConsumed()) return points; points.add(point); } long end = System.currentTimeMillis(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "结束根据项目id查询新增点,关闭事务和游标 项目ID:%s 用时(毫秒):%d" , projId , (end - begin) ) ); } catch (IOException ioException) { LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName , String.format( "关闭游标失败 项目ID:%s msg:%s" , projId , ioException.getMessage() ) ); } return points; } /** * 根据项目id查询新增线 * * @param projId 项目id * @return 线集合 */ @Transactional(transactionManager = "mainDbTransactionManager") public List findAddLineByProjId(String projId) throws InterruptedException { //点集合 List lines = new ArrayList<>(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "开始根据项目id查询新增线,开启事务和游标 项目ID:%s" , projId ) ); long begin = System.currentTimeMillis(); //获取游标 try (Cursor cursor = layerApplyMapper.findAddUpdateLineByProjId(projId)) { //迭代游标 for (GisSurveyLayerApplyLine point : cursor) { //检查线程中断,并响应 if (Thread.interrupted()) throw new InterruptedException(); //检查游标完成 if (cursor.isConsumed()) return lines; lines.add(point); } long end = System.currentTimeMillis(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "结束根据项目id查询新增线,关闭事务和游标 项目ID:%s 用时(毫秒):%d" , projId , (end - begin) ) ); } catch (IOException ioException) { LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName , String.format( "关闭游标失败 项目ID:%s msg:%s" , projId , ioException.getMessage() ) ); } return lines; } /** * 根据任务id查询新增点 * * @param jobId 任务id * @return 点集合 */ @Transactional(transactionManager = "mainDbTransactionManager") public List findAddPointByJobId(String jobId) throws InterruptedException { //点集合 List points = new ArrayList<>(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "开始根据任务id查询新增点,开启事务和游标 任务ID:%s" , jobId ) ); long begin = System.currentTimeMillis(); //获取游标 try (Cursor cursor = layerApplyMapper.findAddUpdatePointByJobId(jobId)) { //迭代游标 for (GisSurveyLayerApplyPoint gisSurveyLayerApplyPoint : cursor) { //检查线程中断,并响应 if (Thread.interrupted()) throw new InterruptedException(); //检查游标完成 if (cursor.isConsumed()) return points; points.add(gisSurveyLayerApplyPoint); } long end = System.currentTimeMillis(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "结束根据任务id查询新增点,关闭事务和游标 任务ID:%s 用时(毫秒):%d" , jobId , (end - begin) ) ); } catch (IOException ioException) { LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName , String.format( "关闭游标失败 任务ID:%s msg:%s" , jobId , ioException.getMessage() ) ); } return points; } /** * 根据任务id查询新增线 * * @param jobId 任务id * @return 线集合 */ @Transactional(transactionManager = "mainDbTransactionManager") public List findAddLineByJobId(String jobId) throws InterruptedException { //点集合 List lines = new ArrayList<>(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "开始根据任务id查询新增线,开启事务和游标 任务ID:%s" , jobId ) ); long begin = System.currentTimeMillis(); //获取游标 try (Cursor cursor = layerApplyMapper.findAddUpdateLineByJobId(jobId)) { //迭代游标 for (GisSurveyLayerApplyLine line : cursor) { //检查线程中断,并响应 if (Thread.interrupted()) throw new InterruptedException(); //检查游标完成 if (cursor.isConsumed()) return lines; lines.add(line); } long end = System.currentTimeMillis(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "结束根据任务id查询新增线,关闭事务和游标 任务ID:%s 用时(毫秒):%d" , jobId , (end - begin) ) ); } catch (IOException ioException) { LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName , String.format( "关闭游标失败 任务ID:%s msg:%s" , jobId , ioException.getMessage() ) ); } return lines; } /** * 根据任务id和类型查询 * * @param jobId 任务id * @param kind 类型 * @return 采集集合 * @throws InterruptedException 中断异常 */ @Transactional(transactionManager = "mainDbTransactionManager") public List findAllByJobIdAndKind(String jobId, String kind) throws InterruptedException { //数据集合 List layerApplies = new ArrayList<>(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "开始根据任务id和类型查询采集元素,开启事务和游标 任务ID:%s 类型:%s" , jobId , kind ) ); long begin = System.currentTimeMillis(); //获取游标 try (Cursor cursor = layerApplyMapper.findAllByJobIdAndKind(jobId, kind)) { //迭代游标 for (GisSurveyLayerApply layerApply : cursor) { //检查线程中断,并响应 if (Thread.interrupted()) throw new InterruptedException(); //检查游标完成 if (cursor.isConsumed()) return layerApplies; layerApplies.add(layerApply); } long end = System.currentTimeMillis(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "结束根据任务id和类型查询采集元素,关闭事务和游标 任务ID:%s 类型:%s 用时(毫秒):%d" , jobId , kind , (end - begin) ) ); } catch (IOException ioException) { LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName , String.format( "关闭游标失败 任务ID:%s msg:%s" , jobId , ioException.getMessage() ) ); } return layerApplies; } /** * 合并副表 * * @param jobId 任务id * @param operator 操作人 * @return 合并结果 */ public Boolean mergeCopy(String jobId, String operator) { LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "开始合并副表,开启session和事务 任务ID:%s" , jobId ) ); long begin = System.currentTimeMillis(); //开启session try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) { try { //设置手动提交 Connection conn = sqlSession.getConnection(); conn.setAutoCommit(false); //获取需要的mapper GisSurveyLayerApplyMapper layerApplyMapper = sqlSession.getMapper(GisSurveyLayerApplyMapper.class); GisSurveyLayerApplyThirdCopyMapper layerApplyThirdCopyMapper = sqlSession.getMapper(GisSurveyLayerApplyThirdCopyMapper.class); GisSurveyPropertyValueMapper propertyValueMapper = sqlSession.getMapper(GisSurveyPropertyValueMapper.class); GisSurveyProjectInfoMapper projectInfoMapper = sqlSession.getMapper(GisSurveyProjectInfoMapper.class); GisSurveyJobInfoMapper jobInfoMapper = sqlSession.getMapper(GisSurveyJobInfoMapper.class); GisSurveyJobStatusTrackMapper jobStatusTrackMapper = sqlSession.getMapper(GisSurveyJobStatusTrackMapper.class); //删除原本job layerApplyMapper.deleteByJobId(jobId); propertyValueMapper.deleteByJobId(jobId); //需要合并的数量 long mergeCount = layerApplyThirdCopyMapper.countByJobId(jobId); //合并属性 propertyValueMapper.mergeCopyByJobId(jobId); //合并元素 int layerApply = layerApplyMapper.mergeCopyByJobId(jobId); //检查合并数量 if (mergeCount != layerApply) { sqlSession.rollback(); conn.rollback(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName , String.format( "合并副表条数错误 开始回滚操作 任务id:%s" , jobId ) ); return false; } //更新刷新时间 long timestamp = System.currentTimeMillis(); int projectUpdated = projectInfoMapper.updateRefreshTimeByJobId(jobId, timestamp); int jobUpdated = jobInfoMapper.updateRefreshTimeActionStatusByUid(jobId, timestamp, timestamp, GisSurveyExcelDefine.DEFAULT_VALUE.ACTION, GisSurveyExcelDefine.DEFAULT_VALUE.STATUS); //写入任务状态 int jobStatusTrackSave = jobStatusTrackMapper.save(new GisSurveyJobStatusTrack( jobId, operator, GisSurveyExcelDefine.DEFAULT_VALUE.ACTION, GisSurveyExcelDefine.DEFAULT_VALUE.STATUS, operator, timestamp)); //检查更新和写入 if (projectUpdated <= 0 || jobUpdated <= 0 || jobStatusTrackSave <= 0) { sqlSession.rollback(); conn.rollback(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName , String.format( "合并副表更新任务/项目状态失败 开始回滚操作 任务id:%s" , jobId ) ); return false; } //提交 sqlSession.commit(); conn.commit(); long end = System.currentTimeMillis(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName , String.format( "合并副表成功,提交事务 任务ID:%s 用时(毫秒):%d" , jobId , (end - begin) ) ); return true; } catch (Exception e) { //回滚 sqlSession.rollback(); LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName , String.format( "合并副表失败 开始回滚操作 任务id:%s error:%s" , jobId , e ) ); return false; } } } }