DownloadServiceImpl.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2019 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * >>
  17. *
  18. */
  19. package edp.davinci.service.impl;
  20. import com.alibaba.druid.util.StringUtils;
  21. import edp.core.exception.UnAuthorizedException;
  22. import edp.davinci.core.enums.ActionEnum;
  23. import edp.davinci.core.enums.DownloadTaskStatus;
  24. import edp.davinci.core.enums.DownloadType;
  25. import edp.davinci.core.enums.LogNameEnum;
  26. import edp.davinci.dao.DownloadRecordMapper;
  27. import edp.davinci.dao.UserMapper;
  28. import edp.davinci.dto.viewDto.DownloadViewExecuteParam;
  29. import edp.davinci.model.DownloadRecord;
  30. import edp.davinci.model.User;
  31. import edp.davinci.service.DownloadService;
  32. import edp.davinci.service.excel.ExecutorUtils;
  33. import edp.davinci.service.excel.MsgWrapper;
  34. import edp.davinci.service.excel.WidgetContext;
  35. import edp.davinci.service.excel.WorkBookContext;
  36. import lombok.extern.slf4j.Slf4j;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.stereotype.Service;
  41. import java.util.Date;
  42. import java.util.List;
  43. /**
  44. * Created by IntelliJ IDEA.
  45. *
  46. * @Author daemon
  47. * @Date 19/5/28 10:04
  48. * To change this template use File | Settings | File Templates.
  49. */
  50. @Service
  51. @Slf4j
  52. public class DownloadServiceImpl extends DownloadCommonService implements DownloadService {
  53. private static final Logger downloadLogger = LoggerFactory.getLogger(LogNameEnum.BUSINESS_DOWNLOAD.getName());
  54. @Autowired
  55. private DownloadRecordMapper downloadRecordMapper;
  56. @Autowired
  57. private UserMapper userMapper;
  58. @Override
  59. public List<DownloadRecord> queryDownloadRecordPage(Long userId) {
  60. return downloadRecordMapper.getDownloadRecordsByUser(userId);
  61. }
  62. @Override
  63. public DownloadRecord downloadById(Long id, String token) throws UnAuthorizedException {
  64. if (StringUtils.isEmpty(token)) {
  65. throw new UnAuthorizedException();
  66. }
  67. String username = tokenUtils.getUsername(token);
  68. if (StringUtils.isEmpty(username)) {
  69. throw new UnAuthorizedException();
  70. }
  71. User user = userMapper.selectByUsername(username);
  72. if (null == user) {
  73. throw new UnAuthorizedException();
  74. }
  75. DownloadRecord record = downloadRecordMapper.getById(id);
  76. if (!record.getUserId().equals(user.getId())) {
  77. throw new UnAuthorizedException();
  78. }
  79. record.setLastDownloadTime(new Date());
  80. record.setStatus(DownloadTaskStatus.DOWNLOADED.getStatus());
  81. downloadRecordMapper.updateById(record);
  82. return record;
  83. }
  84. @Override
  85. public Boolean submit(DownloadType type, Long id, User user, List<DownloadViewExecuteParam> params) {
  86. try {
  87. List<WidgetContext> widgetList = getWidgetContexts(type, id, user, params);
  88. DownloadRecord record = new DownloadRecord();
  89. record.setName(getDownloadFileName(type, id));
  90. record.setUserId(user.getId());
  91. record.setCreateTime(new Date());
  92. record.setStatus(DownloadTaskStatus.PROCESSING.getStatus());
  93. downloadRecordMapper.insert(record);
  94. MsgWrapper wrapper = new MsgWrapper(record, ActionEnum.DOWNLOAD, record.getId());
  95. WorkBookContext workBookContext = WorkBookContext.WorkBookContextBuilder.newBuilder()
  96. .withWrapper(wrapper)
  97. .withWidgets(widgetList)
  98. .withUser(user)
  99. .withResultLimit(resultLimit)
  100. .withTaskKey("DownloadTask_" + id)
  101. .withCustomLogger(downloadLogger)
  102. .build();
  103. ExecutorUtils.submitWorkbookTask(workBookContext, downloadLogger);
  104. log.info("Download task submit:{}", wrapper);
  105. } catch (Exception e) {
  106. log.error("Submit download task error", e);
  107. return false;
  108. }
  109. return true;
  110. }
  111. }