CompanyTitleController.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package com.malk.qiwang.Controller;
  2. import com.malk.qiwang.Service.ICompanyTitleService;
  3. import com.malk.qiwang.entity.CompanyTitle;
  4. import com.malk.qiwang.mapper.CompanyTitleMapper;
  5. import com.malk.server.aliwork.YDConf;
  6. import com.malk.server.aliwork.YDParam;
  7. import com.malk.server.common.McR;
  8. import com.malk.service.aliwork.YDClient;
  9. import com.malk.service.dingtalk.DDClient;
  10. import com.malk.service.dingtalk.DDClient_Workflow;
  11. import com.malk.utils.UtilMap;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.checkerframework.checker.units.qual.A;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.RequestBody;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.stereotype.Controller;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import java.time.LocalDateTime;
  22. import java.time.ZoneId;
  23. import java.util.*;
  24. /**
  25. * <p>
  26. * 公司抬头库表 前端控制器
  27. * </p>
  28. *
  29. * @author LQY
  30. * @since 2026-04-24
  31. */
  32. @RestController
  33. @RequestMapping("/qw1")
  34. @Slf4j
  35. public class CompanyTitleController {
  36. @Autowired
  37. private ICompanyTitleService companyTitleService;
  38. @Autowired
  39. private YDClient ydClient;
  40. @Autowired
  41. private DDClient_Workflow ddClientWorkflow ;
  42. @Autowired
  43. private DDClient ddClient;
  44. @Autowired
  45. private CompanyTitleMapper companyTitleMapper;
  46. @PostMapping("/companyTitle")
  47. public McR insertTransfer2(@RequestBody(required = false) Map<String, Object> map) {
  48. if (map == null) {
  49. map = new HashMap<>();
  50. log.info("请求体为空,使用默认参数");
  51. }
  52. log.info("接收到的参数: {}", map);
  53. // 获取参数
  54. String processCode = "PROC-187CB1F8-5BF0-403A-9D01-94EEF987DD77";
  55. // 设置时间范围(默认查询最近30天)
  56. Long startTime = System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000;
  57. Long endTime = System.currentTimeMillis();
  58. // 获取钉钉accessToken
  59. String accessToken = ddClient.getAccessToken();
  60. // 递归获取所有审批实例ID
  61. List<String> allInstanceIds = ddClientWorkflow.getInstanceIds_all(accessToken, processCode, startTime, endTime, null);
  62. log.info("共查询到 {} 条审批实例", allInstanceIds.size());
  63. // 批量处理审批实例
  64. int successCount = 0;
  65. int failCount = 0;
  66. // 用于记录处理的税号,避免重复处理同一个税号(可选,根据业务需求)
  67. Set<String> processedTaxIds = new HashSet<>();
  68. for (String instanceId : allInstanceIds) {
  69. try {
  70. // 获取审批实例详情
  71. Map<String, Object> processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, instanceId);
  72. if (processInstance != null) {
  73. // 提取并保存公司抬头数据
  74. boolean result = saveCompanyTitleFromInstance(processInstance, processedTaxIds);
  75. if (result) {
  76. successCount++;
  77. } else {
  78. failCount++;
  79. }
  80. } else {
  81. failCount++;
  82. }
  83. } catch (Exception e) {
  84. log.error("处理审批实例失败: instanceId={}", instanceId, e);
  85. failCount++;
  86. }
  87. }
  88. log.info("处理完成: 成功={}, 失败={}", successCount, failCount);
  89. return McR.success();
  90. }
  91. private boolean saveCompanyTitleFromInstance(Map<String, Object> processInstance, Set<String> processedTaxIds) {
  92. List<Map<String, Object>> formComponentValues =
  93. (List<Map<String, Object>>) processInstance.get("formComponentValues");
  94. if (formComponentValues == null || formComponentValues.isEmpty()) {
  95. log.warn("表单数据为空");
  96. return false;
  97. }
  98. String tt = "", sh = "", dz = "", dh = "", yh = "", zh = "", zt = "";
  99. // 修复:安全获取审批时间,避免类型转换异常
  100. Long finishTime = null;
  101. Object finishTimeObj = processInstance.get("finishTime");
  102. if (finishTimeObj != null) {
  103. if (finishTimeObj instanceof Long) {
  104. finishTime = (Long) finishTimeObj;
  105. } else if (finishTimeObj instanceof String) {
  106. try {
  107. finishTime = Long.parseLong((String) finishTimeObj);
  108. } catch (NumberFormatException e) {
  109. log.warn("finishTime格式转换失败: {}", finishTimeObj);
  110. }
  111. } else if (finishTimeObj instanceof Integer) {
  112. finishTime = ((Integer) finishTimeObj).longValue();
  113. }
  114. }
  115. for (Map<String, Object> formComponentValue : formComponentValues) {
  116. String id = String.valueOf(formComponentValue.get("id"));
  117. String value = formComponentValue.get("value") != null ?
  118. String.valueOf(formComponentValue.get("value")) : "";
  119. switch (id) {
  120. case "TextField-K2AD4O5B":
  121. tt = value;
  122. break;
  123. case "TextField_1JEUFYV5F4E80":
  124. sh = value;
  125. break;
  126. case "TextField_EJZY527VJXC0":
  127. dz = value;
  128. break;
  129. case "TextField_12YU12T3PZC0":
  130. dh = value;
  131. break;
  132. case "TextField_1Z4P9MQFOW3K0":
  133. yh = value;
  134. break;
  135. case "TextField_NUL1SS1PJQ80":
  136. zh = value;
  137. break;
  138. case "DDSelectField_528A36XIP9C0":
  139. zt = value;
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. if (StringUtils.isBlank(sh)) {
  146. log.warn("税号为空,跳过保存");
  147. return false;
  148. }
  149. // 可选:避免重复处理同一个税号
  150. if (processedTaxIds.contains(sh)) {
  151. log.debug("税号 {} 已处理过,跳过", sh);
  152. return false;
  153. }
  154. // 查询数据库中该税号的最新记录
  155. CompanyTitle existing = companyTitleMapper.selectByTaxId(sh);
  156. if (existing != null) {
  157. // 比较审批时间,判断是否为最新数据
  158. if (finishTime != null && existing.getUpdatedAt() != null) {
  159. long existingTime = existing.getUpdatedAt().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  160. if (finishTime <= existingTime) {
  161. log.debug("当前审批数据不是最新,跳过更新: taxId={}, 审批时间={}, 数据库更新时间={}",
  162. sh, finishTime, existingTime);
  163. return false;
  164. }
  165. }
  166. // 更新
  167. existing.setCompanyName(tt);
  168. existing.setAddress(dz);
  169. existing.setPhone(dh);
  170. existing.setBankName(yh);
  171. existing.setBankAccount(zh);
  172. existing.setStatus("启用".equals(zt) ? (byte) 0 : (byte) 1);
  173. existing.setUpdatedAt(LocalDateTime.now());
  174. companyTitleMapper.updateById(existing);
  175. log.info("公司抬头更新成功: taxId={}, 审批时间={}", sh, finishTime);
  176. } else {
  177. // 新增
  178. CompanyTitle companyTitle = new CompanyTitle();
  179. companyTitle.setCompanyName(tt);
  180. companyTitle.setTaxId(sh);
  181. companyTitle.setAddress(dz);
  182. companyTitle.setPhone(dh);
  183. companyTitle.setBankName(yh);
  184. companyTitle.setBankAccount(zh);
  185. companyTitle.setStatus("启用".equals(zt) ? (byte) 0 : (byte) 1);
  186. companyTitle.setCreatedAt(LocalDateTime.now());
  187. companyTitle.setUpdatedAt(LocalDateTime.now());
  188. companyTitleMapper.insert(companyTitle);
  189. log.info("公司抬头新增成功: taxId={}", sh);
  190. }
  191. processedTaxIds.add(sh);
  192. return true;
  193. }
  194. }