GisSurveyLayerApplyServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package com.shkpr.service.alambizplugin.dbdao.services;
  2. import com.global.base.log.LogLevelFlag;
  3. import com.global.base.log.LogPrintMgr;
  4. import com.shkpr.service.alambizplugin.constants.GisSurveyExcelDefine;
  5. import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
  6. import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyJobInfoMapper;
  7. import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyJobStatusTrackMapper;
  8. import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyLayerApplyMapper;
  9. import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyLayerApplyThirdCopyMapper;
  10. import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyProjectInfoMapper;
  11. import com.shkpr.service.alambizplugin.dbdao.mapper.GisSurveyPropertyValueMapper;
  12. import com.shkpr.service.alambizplugin.dbdao.services.intef.GisSurveyLayerApplyService;
  13. import com.shkpr.service.alambizplugin.dto.GisSurveyJobStatusTrack;
  14. import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApply;
  15. import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApplyLine;
  16. import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApplyPoint;
  17. import org.apache.ibatis.cursor.Cursor;
  18. import org.apache.ibatis.session.SqlSession;
  19. import org.apache.ibatis.session.SqlSessionFactory;
  20. import org.springframework.beans.factory.annotation.Qualifier;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import java.io.IOException;
  24. import java.sql.Connection;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. /**
  28. * 采集元素处理申请service实现
  29. *
  30. * @author 欧阳劲驰
  31. * @since 1.0.0
  32. */
  33. @Service
  34. public class GisSurveyLayerApplyServiceImpl implements GisSurveyLayerApplyService {
  35. /**
  36. * log
  37. */
  38. private final String mStrClassName;
  39. private final String mBizType;
  40. private final SqlSessionFactory sqlSessionFactory;
  41. private final GisSurveyLayerApplyMapper layerApplyMapper;
  42. public GisSurveyLayerApplyServiceImpl(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory
  43. , GisSurveyLayerApplyMapper layerApplyMapper) {
  44. mStrClassName = "GisSurveyLayerApplyServiceImpl";
  45. mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
  46. this.sqlSessionFactory = sqlSessionFactory;
  47. this.layerApplyMapper = layerApplyMapper;
  48. }
  49. /**
  50. * 根据项目id查询新增点
  51. *
  52. * @param projId 项目id
  53. * @return 点集合
  54. */
  55. @Transactional(transactionManager = "mainDbTransactionManager")
  56. public List<GisSurveyLayerApplyPoint> findAddPointByProjId(String projId) throws InterruptedException {
  57. //点集合
  58. List<GisSurveyLayerApplyPoint> points = new ArrayList<>();
  59. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  60. , String.format(
  61. "开始根据项目id查询新增点,开启事务和游标 项目ID:%s"
  62. , projId
  63. )
  64. );
  65. long begin = System.currentTimeMillis();
  66. //获取游标
  67. try (Cursor<GisSurveyLayerApplyPoint> cursor = layerApplyMapper.findAddUpdatePointByProjId(projId)) {
  68. //迭代游标
  69. for (GisSurveyLayerApplyPoint point : cursor) {
  70. //检查线程中断,并响应
  71. if (Thread.interrupted()) throw new InterruptedException();
  72. //检查游标完成
  73. if (cursor.isConsumed()) return points;
  74. points.add(point);
  75. }
  76. long end = System.currentTimeMillis();
  77. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  78. , String.format(
  79. "结束根据项目id查询新增点,关闭事务和游标 项目ID:%s 用时(毫秒):%d"
  80. , projId
  81. , (end - begin)
  82. )
  83. );
  84. } catch (IOException ioException) {
  85. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  86. , String.format(
  87. "关闭游标失败 项目ID:%s msg:%s"
  88. , projId
  89. , ioException.getMessage()
  90. )
  91. );
  92. }
  93. return points;
  94. }
  95. /**
  96. * 根据项目id查询新增线
  97. *
  98. * @param projId 项目id
  99. * @return 线集合
  100. */
  101. @Transactional(transactionManager = "mainDbTransactionManager")
  102. public List<GisSurveyLayerApplyLine> findAddLineByProjId(String projId) throws InterruptedException {
  103. //点集合
  104. List<GisSurveyLayerApplyLine> lines = new ArrayList<>();
  105. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  106. , String.format(
  107. "开始根据项目id查询新增线,开启事务和游标 项目ID:%s"
  108. , projId
  109. )
  110. );
  111. long begin = System.currentTimeMillis();
  112. //获取游标
  113. try (Cursor<GisSurveyLayerApplyLine> cursor = layerApplyMapper.findAddUpdateLineByProjId(projId)) {
  114. //迭代游标
  115. for (GisSurveyLayerApplyLine point : cursor) {
  116. //检查线程中断,并响应
  117. if (Thread.interrupted()) throw new InterruptedException();
  118. //检查游标完成
  119. if (cursor.isConsumed()) return lines;
  120. lines.add(point);
  121. }
  122. long end = System.currentTimeMillis();
  123. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  124. , String.format(
  125. "结束根据项目id查询新增线,关闭事务和游标 项目ID:%s 用时(毫秒):%d"
  126. , projId
  127. , (end - begin)
  128. )
  129. );
  130. } catch (IOException ioException) {
  131. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  132. , String.format(
  133. "关闭游标失败 项目ID:%s msg:%s"
  134. , projId
  135. , ioException.getMessage()
  136. )
  137. );
  138. }
  139. return lines;
  140. }
  141. /**
  142. * 根据任务id查询新增点
  143. *
  144. * @param jobId 任务id
  145. * @return 点集合
  146. */
  147. @Transactional(transactionManager = "mainDbTransactionManager")
  148. public List<GisSurveyLayerApplyPoint> findAddPointByJobId(String jobId) throws InterruptedException {
  149. //点集合
  150. List<GisSurveyLayerApplyPoint> points = new ArrayList<>();
  151. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  152. , String.format(
  153. "开始根据任务id查询新增点,开启事务和游标 任务ID:%s"
  154. , jobId
  155. )
  156. );
  157. long begin = System.currentTimeMillis();
  158. //获取游标
  159. try (Cursor<GisSurveyLayerApplyPoint> cursor = layerApplyMapper.findAddUpdatePointByJobId(jobId)) {
  160. //迭代游标
  161. for (GisSurveyLayerApplyPoint gisSurveyLayerApplyPoint : cursor) {
  162. //检查线程中断,并响应
  163. if (Thread.interrupted()) throw new InterruptedException();
  164. //检查游标完成
  165. if (cursor.isConsumed()) return points;
  166. points.add(gisSurveyLayerApplyPoint);
  167. }
  168. long end = System.currentTimeMillis();
  169. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  170. , String.format(
  171. "结束根据任务id查询新增点,关闭事务和游标 任务ID:%s 用时(毫秒):%d"
  172. , jobId
  173. , (end - begin)
  174. )
  175. );
  176. } catch (IOException ioException) {
  177. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  178. , String.format(
  179. "关闭游标失败 任务ID:%s msg:%s"
  180. , jobId
  181. , ioException.getMessage()
  182. )
  183. );
  184. }
  185. return points;
  186. }
  187. /**
  188. * 根据任务id查询新增线
  189. *
  190. * @param jobId 任务id
  191. * @return 线集合
  192. */
  193. @Transactional(transactionManager = "mainDbTransactionManager")
  194. public List<GisSurveyLayerApplyLine> findAddLineByJobId(String jobId) throws InterruptedException {
  195. //点集合
  196. List<GisSurveyLayerApplyLine> lines = new ArrayList<>();
  197. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  198. , String.format(
  199. "开始根据任务id查询新增线,开启事务和游标 任务ID:%s"
  200. , jobId
  201. )
  202. );
  203. long begin = System.currentTimeMillis();
  204. //获取游标
  205. try (Cursor<GisSurveyLayerApplyLine> cursor = layerApplyMapper.findAddUpdateLineByJobId(jobId)) {
  206. //迭代游标
  207. for (GisSurveyLayerApplyLine line : cursor) {
  208. //检查线程中断,并响应
  209. if (Thread.interrupted()) throw new InterruptedException();
  210. //检查游标完成
  211. if (cursor.isConsumed()) return lines;
  212. lines.add(line);
  213. }
  214. long end = System.currentTimeMillis();
  215. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  216. , String.format(
  217. "结束根据任务id查询新增线,关闭事务和游标 任务ID:%s 用时(毫秒):%d"
  218. , jobId
  219. , (end - begin)
  220. )
  221. );
  222. } catch (IOException ioException) {
  223. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  224. , String.format(
  225. "关闭游标失败 任务ID:%s msg:%s"
  226. , jobId
  227. , ioException.getMessage()
  228. )
  229. );
  230. }
  231. return lines;
  232. }
  233. /**
  234. * 根据任务id和类型查询
  235. *
  236. * @param jobId 任务id
  237. * @param kind 类型
  238. * @return 采集集合
  239. * @throws InterruptedException 中断异常
  240. */
  241. @Transactional(transactionManager = "mainDbTransactionManager")
  242. public List<GisSurveyLayerApply> findAllByJobIdAndKind(String jobId, String kind) throws InterruptedException {
  243. //数据集合
  244. List<GisSurveyLayerApply> layerApplies = new ArrayList<>();
  245. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  246. , String.format(
  247. "开始根据任务id和类型查询采集元素,开启事务和游标 任务ID:%s 类型:%s"
  248. , jobId
  249. , kind
  250. )
  251. );
  252. long begin = System.currentTimeMillis();
  253. //获取游标
  254. try (Cursor<GisSurveyLayerApply> cursor = layerApplyMapper.findAllByJobIdAndKind(jobId, kind)) {
  255. //迭代游标
  256. for (GisSurveyLayerApply layerApply : cursor) {
  257. //检查线程中断,并响应
  258. if (Thread.interrupted()) throw new InterruptedException();
  259. //检查游标完成
  260. if (cursor.isConsumed()) return layerApplies;
  261. layerApplies.add(layerApply);
  262. }
  263. long end = System.currentTimeMillis();
  264. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  265. , String.format(
  266. "结束根据任务id和类型查询采集元素,关闭事务和游标 任务ID:%s 类型:%s 用时(毫秒):%d"
  267. , jobId
  268. , kind
  269. , (end - begin)
  270. )
  271. );
  272. } catch (IOException ioException) {
  273. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  274. , String.format(
  275. "关闭游标失败 任务ID:%s msg:%s"
  276. , jobId
  277. , ioException.getMessage()
  278. )
  279. );
  280. }
  281. return layerApplies;
  282. }
  283. /**
  284. * 合并副表
  285. *
  286. * @param jobId 任务id
  287. * @param operator 操作人
  288. * @return 合并结果
  289. */
  290. public Boolean mergeCopy(String jobId, String operator) {
  291. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  292. , String.format(
  293. "开始合并副表,开启session和事务 任务ID:%s"
  294. , jobId
  295. )
  296. );
  297. long begin = System.currentTimeMillis();
  298. //开启session
  299. try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
  300. try {
  301. //设置手动提交
  302. Connection conn = sqlSession.getConnection();
  303. conn.setAutoCommit(false);
  304. //获取需要的mapper
  305. GisSurveyLayerApplyMapper layerApplyMapper = sqlSession.getMapper(GisSurveyLayerApplyMapper.class);
  306. GisSurveyLayerApplyThirdCopyMapper layerApplyThirdCopyMapper = sqlSession.getMapper(GisSurveyLayerApplyThirdCopyMapper.class);
  307. GisSurveyPropertyValueMapper propertyValueMapper = sqlSession.getMapper(GisSurveyPropertyValueMapper.class);
  308. GisSurveyProjectInfoMapper projectInfoMapper = sqlSession.getMapper(GisSurveyProjectInfoMapper.class);
  309. GisSurveyJobInfoMapper jobInfoMapper = sqlSession.getMapper(GisSurveyJobInfoMapper.class);
  310. GisSurveyJobStatusTrackMapper jobStatusTrackMapper = sqlSession.getMapper(GisSurveyJobStatusTrackMapper.class);
  311. //删除原本job
  312. layerApplyMapper.deleteByJobId(jobId);
  313. propertyValueMapper.deleteByJobId(jobId);
  314. //需要合并的数量
  315. long mergeCount = layerApplyThirdCopyMapper.countByJobId(jobId);
  316. //合并属性
  317. propertyValueMapper.mergeCopyByJobId(jobId);
  318. //合并元素
  319. int layerApply = layerApplyMapper.mergeCopyByJobId(jobId);
  320. //检查合并数量
  321. if (mergeCount != layerApply) {
  322. sqlSession.rollback();
  323. conn.rollback();
  324. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  325. , String.format(
  326. "合并副表条数错误 开始回滚操作 任务id:%s"
  327. , jobId
  328. )
  329. );
  330. return false;
  331. }
  332. //更新刷新时间
  333. long timestamp = System.currentTimeMillis();
  334. int projectUpdated = projectInfoMapper.updateRefreshTimeByJobId(jobId, timestamp);
  335. int jobUpdated = jobInfoMapper.updateRefreshTimeActionStatusByUid(jobId, timestamp, timestamp,
  336. GisSurveyExcelDefine.DEFAULT_VALUE.ACTION, GisSurveyExcelDefine.DEFAULT_VALUE.STATUS);
  337. //写入任务状态
  338. int jobStatusTrackSave = jobStatusTrackMapper.save(new GisSurveyJobStatusTrack(
  339. jobId, operator,
  340. GisSurveyExcelDefine.DEFAULT_VALUE.ACTION, GisSurveyExcelDefine.DEFAULT_VALUE.STATUS,
  341. operator, timestamp));
  342. //检查更新和写入
  343. if (projectUpdated <= 0 || jobUpdated <= 0 || jobStatusTrackSave <= 0) {
  344. sqlSession.rollback();
  345. conn.rollback();
  346. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  347. , String.format(
  348. "合并副表更新任务/项目状态失败 开始回滚操作 任务id:%s"
  349. , jobId
  350. )
  351. );
  352. return false;
  353. }
  354. //提交
  355. sqlSession.commit();
  356. conn.commit();
  357. long end = System.currentTimeMillis();
  358. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  359. , String.format(
  360. "合并副表成功,提交事务 任务ID:%s 用时(毫秒):%d"
  361. , jobId
  362. , (end - begin)
  363. )
  364. );
  365. return true;
  366. } catch (Exception e) {
  367. //回滚
  368. sqlSession.rollback();
  369. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  370. , String.format(
  371. "合并副表失败 开始回滚操作 任务id:%s error:%s"
  372. , jobId
  373. , e
  374. )
  375. );
  376. return false;
  377. }
  378. }
  379. }
  380. }