DataArcherService.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.galaxis.manatee.service;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.core.type.TypeReference;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.galaxis.manatee.dao.ChuanyunSelfWorkHourDao;
  7. import com.galaxis.manatee.dao.ChuanyunUserCompanyDao;
  8. import com.galaxis.manatee.entity.chuanyun.data.object.ChuanyunMemberHourDO;
  9. import com.galaxis.manatee.entity.chuanyun.data.object.ChuanyunSelfWorkHourDO;
  10. import com.galaxis.manatee.entity.chuanyun.data.object.ChuanyunUserCompanyDO;
  11. import com.galaxis.manatee.entity.chuanyun.dto.ChuanyunSaveDTO;
  12. import com.galaxis.manatee.entity.chuanyun.dto.ChuanyunSelfWorkHourDTO;
  13. import com.galaxis.manatee.entity.chuanyun.dto.Filter;
  14. import com.galaxis.manatee.exception.BigSizeException;
  15. import com.galaxis.manatee.manager.ChuanYunManager;
  16. import lombok.Data;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang.RandomStringUtils;
  19. import org.springframework.beans.BeanUtils;
  20. import org.springframework.data.domain.PageRequest;
  21. import org.springframework.scheduling.annotation.Async;
  22. import org.springframework.stereotype.Service;
  23. import java.time.Instant;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. /**
  27. * 快速更新数据方法
  28. * @author zcj
  29. * @version 0.1
  30. * @date 2021/3/6 5:55 上午
  31. */
  32. @Slf4j
  33. @Service
  34. public class DataArcherService {
  35. private final ChuanYunManager chuanYunManager;
  36. private final ChuanyunSelfWorkHourDao chuanyunSelfWorkHourDao;
  37. private final ChuanyunUserCompanyDao chuanyunUserCompanyDao;
  38. public DataArcherService(ChuanYunManager chuanYunManager, ChuanyunSelfWorkHourDao chuanyunSelfWorkHourDao, ChuanyunUserCompanyDao chuanyunUserCompanyDao) {
  39. this.chuanYunManager = chuanYunManager;
  40. this.chuanyunSelfWorkHourDao = chuanyunSelfWorkHourDao;
  41. this.chuanyunUserCompanyDao = chuanyunUserCompanyDao;
  42. }
  43. /**
  44. * 更新所有日工时
  45. */
  46. public void updateAllChuanyunSelfWorkHour(){
  47. long startTime = Instant.now().getEpochSecond();
  48. var pageSize = 20;
  49. var page = 0;
  50. while (true){
  51. var pageable = PageRequest.of(page, pageSize);
  52. var updateList = chuanyunSelfWorkHourDao.findAll(pageable);
  53. if (page <= updateList.getTotalPages()) {
  54. page += 1;
  55. this.updateChuanyunSelfWorkHourList(updateList.toList());
  56. }else{
  57. break;
  58. }
  59. }
  60. log.info("更新所有每日工时花费"+(Instant.now().getEpochSecond()-startTime)+"秒");
  61. }
  62. /**
  63. * 更新最近一个月的标准每日工时
  64. */
  65. public void updateRecentChuanyunSelfWorkHour(){
  66. long startTime = Instant.now().getEpochSecond();
  67. var pageSize = 20;
  68. var page = 0;
  69. while (true){
  70. var pageable = PageRequest.of(page, pageSize);
  71. var updateList = chuanyunSelfWorkHourDao.getRecentlyDayHour(pageable);
  72. if (page <= updateList.getTotalPages()) {
  73. page += 1;
  74. this.updateChuanyunSelfWorkHourList(updateList.toList());
  75. }else{
  76. break;
  77. }
  78. }
  79. log.info("更新最近一个月每日工时花费"+(Instant.now().getEpochSecond()-startTime)+"秒");
  80. }
  81. /**
  82. * 根据用户Id更新氚云每日工时列表
  83. * @param userId 用户ID
  84. */
  85. public void updateChuanyunSelfWorkHourListByUserId(String userId){
  86. long startTime = Instant.now().getEpochSecond();
  87. List<ChuanyunSelfWorkHourDO> updateList=chuanyunSelfWorkHourDao.findByUserId(userId);
  88. this.updateChuanyunSelfWorkHourList(updateList);
  89. log.info("用户工时花费"+(Instant.now().getEpochSecond()-startTime)+"秒");
  90. }
  91. /**
  92. * 根据标准日工时列表更新
  93. * @param updateList 更新列表
  94. */
  95. public void updateChuanyunSelfWorkHourList(List<ChuanyunSelfWorkHourDO> updateList){
  96. updateList.forEach(chuanyunSelfWorkHourDO -> {
  97. try {
  98. //
  99. ChuanyunSelfWorkHourDTO chuanyunSelfWorkHourDTO = new ChuanyunSelfWorkHourDTO();
  100. BeanUtils.copyProperties(chuanyunSelfWorkHourDO, chuanyunSelfWorkHourDTO);
  101. ChuanyunUserCompanyDO chuanyunUserCompanyDO = chuanyunUserCompanyDao.findByUserId(chuanyunSelfWorkHourDO.getUserId());
  102. if (chuanyunUserCompanyDO != null) {
  103. chuanyunSelfWorkHourDTO.setUserName(chuanyunUserCompanyDO.getUserName());
  104. chuanyunSelfWorkHourDTO.setBg(chuanyunUserCompanyDO.getBg());
  105. //由于人员所在部门信息可能会表,所以每次更新的时候需要获取人员最新的部门信息
  106. chuanyunSelfWorkHourDTO.setDepartmentId(chuanyunUserCompanyDO.getDepartmentId());
  107. } else {
  108. chuanyunSelfWorkHourDTO.setUserName(chuanyunSelfWorkHourDTO.getUserId());
  109. chuanyunSelfWorkHourDTO.setBg("IBG");
  110. chuanyunSelfWorkHourDTO.setDepartmentId(chuanyunSelfWorkHourDO.getDepartmentId());
  111. }
  112. chuanyunSelfWorkHourDTO.setDepartmentName(chuanyunSelfWorkHourDO.getDepartmentName());
  113. //更新到氚云
  114. updateSelfWorkHour(chuanyunSelfWorkHourDTO);
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. log.warn("更新标准工时异常");
  118. }
  119. });
  120. }
  121. /**
  122. * 异步更新每日工时
  123. */
  124. @Async
  125. protected void updateSelfWorkHour(ChuanyunSelfWorkHourDTO chuanyunSelfWorkHourDTO) {
  126. ObjectMapper objectMapper = new ObjectMapper();
  127. try {
  128. List<String> matchers = new ArrayList<>();
  129. matchers.add("F0000001_2," + chuanyunSelfWorkHourDTO.getProjectId());
  130. matchers.add("F0000002_2," + chuanyunSelfWorkHourDTO.getUserId());
  131. matchers.add("F0000003_2," + chuanyunSelfWorkHourDTO.getDayLogDate());
  132. matchers.add("F0000005_2," + chuanyunSelfWorkHourDTO.getProjectType());
  133. var filter = Filter.instance(0, 1, true, "And", matchers);
  134. var chuanyunFindAllResponse = chuanYunManager.findAll(ChuanyunSelfWorkHourDTO.SCHEMA_CODE, filter);
  135. var selfMonthString = objectMapper.writeValueAsString(chuanyunSelfWorkHourDTO);
  136. ChuanyunSaveDTO chuanyunSaveDTO;
  137. if (chuanyunFindAllResponse.getReturnData() != null) {
  138. List<ChuanyunMemberHourDO> result = objectMapper.convertValue(chuanyunFindAllResponse.getReturnData().getBizObjectArray(), new TypeReference<>() {
  139. });
  140. ChuanyunMemberHourDO firstResult = result.get(0);
  141. //增加工时不相等再更新每日工时数据的
  142. if (0 != firstResult.getStandardWorkHour().compareTo(chuanyunSelfWorkHourDTO.getStandardWorkHour())) {
  143. chuanyunSaveDTO = chuanYunManager.update(ChuanyunMemberHourDO.SCHEMA_CODE, firstResult.getObjectId(), selfMonthString);
  144. if (!chuanyunSaveDTO.getSuccessful()) {
  145. log.warn("更新工时标准化失败");
  146. }
  147. }
  148. } else {
  149. chuanyunSaveDTO = chuanYunManager.save(ChuanyunMemberHourDO.SCHEMA_CODE, selfMonthString, true);
  150. if (!chuanyunSaveDTO.getSuccessful()) {
  151. log.warn("新增工时标准化失败");
  152. }
  153. }
  154. } catch (Exception e) {
  155. e.printStackTrace();
  156. log.warn("更新标准工时异常");
  157. }
  158. }
  159. @Async
  160. public void loose() throws JsonProcessingException, BigSizeException {
  161. @Data
  162. class ApiTestDTO {
  163. /**
  164. * 表名
  165. */
  166. public static final String SCHEMA_CODE = "D001789test_api";
  167. @JsonProperty(value = "F0000001")
  168. private String a;
  169. @JsonProperty(value = "F0000002")
  170. private String b;
  171. @JsonProperty(value = "F0000003")
  172. private String c;
  173. @JsonProperty(value = "F0000004")
  174. private String d;
  175. @JsonProperty(value = "F0000005")
  176. private String e;
  177. @JsonProperty(value = "F0000006")
  178. private String f;
  179. public ApiTestDTO(String a, String b, String c, String d, String e, String f) {
  180. this.a = a;
  181. this.b = b;
  182. this.c = c;
  183. this.d = d;
  184. this.e = e;
  185. this.f = f;
  186. }
  187. }
  188. long tmpStart = System.currentTimeMillis();
  189. ApiTestDTO apiTestDTO = new ApiTestDTO(RandomStringUtils.random(5, true, true),
  190. RandomStringUtils.random(5, true, true),
  191. RandomStringUtils.random(5, true, true),
  192. RandomStringUtils.random(5, true, true),
  193. RandomStringUtils.random(5, true, true),
  194. RandomStringUtils.random(5, true, true));
  195. ObjectMapper objectMapper = new ObjectMapper();
  196. String data = objectMapper.writeValueAsString(apiTestDTO);
  197. chuanYunManager.save(ApiTestDTO.SCHEMA_CODE, data, true);
  198. log.info((System.currentTimeMillis() - tmpStart) + "s");
  199. }
  200. }