123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- 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<GisSurveyLayerApplyPoint> findAddPointByProjId(String projId) throws InterruptedException {
- //点集合
- List<GisSurveyLayerApplyPoint> points = new ArrayList<>();
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
- , String.format(
- "开始根据项目id查询新增点,开启事务和游标 项目ID:%s"
- , projId
- )
- );
- long begin = System.currentTimeMillis();
- //获取游标
- try (Cursor<GisSurveyLayerApplyPoint> 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<GisSurveyLayerApplyLine> findAddLineByProjId(String projId) throws InterruptedException {
- //点集合
- List<GisSurveyLayerApplyLine> lines = new ArrayList<>();
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
- , String.format(
- "开始根据项目id查询新增线,开启事务和游标 项目ID:%s"
- , projId
- )
- );
- long begin = System.currentTimeMillis();
- //获取游标
- try (Cursor<GisSurveyLayerApplyLine> 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<GisSurveyLayerApplyPoint> findAddPointByJobId(String jobId) throws InterruptedException {
- //点集合
- List<GisSurveyLayerApplyPoint> points = new ArrayList<>();
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
- , String.format(
- "开始根据任务id查询新增点,开启事务和游标 任务ID:%s"
- , jobId
- )
- );
- long begin = System.currentTimeMillis();
- //获取游标
- try (Cursor<GisSurveyLayerApplyPoint> 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<GisSurveyLayerApplyLine> findAddLineByJobId(String jobId) throws InterruptedException {
- //点集合
- List<GisSurveyLayerApplyLine> lines = new ArrayList<>();
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
- , String.format(
- "开始根据任务id查询新增线,开启事务和游标 任务ID:%s"
- , jobId
- )
- );
- long begin = System.currentTimeMillis();
- //获取游标
- try (Cursor<GisSurveyLayerApplyLine> 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<GisSurveyLayerApply> findAllByJobIdAndKind(String jobId, String kind) throws InterruptedException {
- //数据集合
- List<GisSurveyLayerApply> 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<GisSurveyLayerApply> 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;
- }
- }
- }
- }
|