ClientHylServiceImpl.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.malk.hengyilong.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.malk.hengyilong.service.ClientHylService;
  7. import com.malk.server.aliwork.YDConf;
  8. import com.malk.server.aliwork.YDParam;
  9. import com.malk.server.aliwork.YDSearch;
  10. import com.malk.server.common.McR;
  11. import com.malk.server.dingtalk.DDR_New;
  12. import com.malk.service.aliwork.YDClient;
  13. import com.malk.utils.UtilMap;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.util.*;
  18. /**
  19. * 功能:
  20. * 作者:hanxue
  21. * 日期:2026/2/2 10:48
  22. */
  23. @Service
  24. @Slf4j
  25. public class ClientHylServiceImpl implements ClientHylService {
  26. @Autowired
  27. private YDClient ydClient;
  28. @Override
  29. public McR getClient(Map param) {
  30. DDR_New ddrNew;
  31. int pageNumber = 1;
  32. int pageSize = 100;
  33. JSONArray clientList = new JSONArray();
  34. do {
  35. ddrNew = ydClient.queryData(YDParam.builder()
  36. .formUuid("FORM-GMC66781QZP16UF1NPSLG6C8ZC9G38R5V6IJM1")
  37. .searchFieldJson(JSONObject.toJSONString(Arrays.asList(
  38. new YDSearch("selectField_mkm0qu7p", "锁定", "selectField_mkm0qu7p", YDSearch.Type.TEXT_FIELD, YDSearch.Operator.EQ),
  39. new YDSearch("employeeField_mkm0ce8w", param.get("userId"), "employeeField_mkm0ce8w",YDSearch.Type.EMPLOYEE_FIELD, YDSearch.Operator.CONTAINS)
  40. )))
  41. .pageNumber(pageNumber)
  42. .pageSize(pageSize)
  43. .build(), YDConf.FORM_QUERY.retrieve_list_all);
  44. if (ddrNew != null && ddrNew.getData() != null) {
  45. JSONArray currentPageData = (JSONArray) ddrNew.getData();
  46. clientList.addAll(currentPageData);
  47. System.out.println("已处理第 " + pageNumber + " 页数据,当前页记录数: " + currentPageData.size());
  48. if (currentPageData.size() < pageSize) {
  49. break;
  50. }
  51. }
  52. pageNumber++; // 准备查询下一页
  53. } while (ddrNew != null && ddrNew.getTotalCount() > (pageNumber - 1) * pageSize);
  54. List clientNameList = new ArrayList();
  55. for(int i=0;i<clientList.size();i++) {
  56. Map client = (Map) clientList.get(i);
  57. Map formData = (Map) client.get("formData");
  58. String clientName = UtilMap.getString(formData,"textfield_uB2fZYNg");
  59. clientNameList.add(clientName);
  60. }
  61. log.info("clientList:{}",clientNameList);
  62. return McR.success(clientNameList);
  63. }
  64. @Override
  65. public McR syncClient(Map map) {
  66. List clientdetails = (List) ydClient.queryData(YDParam.builder()
  67. .formUuid("FORM-GMC66781QZP16UF1NPSLG6C8ZC9G38R5V6IJM1")
  68. .searchFieldJson(JSONObject.toJSONString(Arrays.asList(
  69. new YDSearch("textfield_uB2fZYNg", UtilMap.getString(map, "clientName"), "textfield_uB2fZYNg", YDSearch.Type.TEXT_FIELD, YDSearch.Operator.EQ)
  70. )))
  71. .build(), YDConf.FORM_QUERY.retrieve_list_all).getData();
  72. Map clientdetail = (Map) clientdetails.get(0);
  73. log.info("clientList:{}",clientdetail.get("formInstanceId"));
  74. String clientLink = "https://gkwk0b.aliwork.com/APP_QX8IOI1D22HK3GC413L5/formDetail/FORM-GMC66781QZP16UF1NPSLG6C8ZC9G38R5V6IJM1?formInstId="+clientdetail.get("formInstanceId");
  75. return McR.success(clientLink);
  76. }
  77. @Override
  78. public McR getAccountPeriod(Map map) {
  79. String formInstance = UtilMap.getString(map, "formInstance");
  80. // 应收帐日期
  81. String receiveDate = null;
  82. try {
  83. Map formData = ydClient.queryData(YDParam.builder()
  84. .formInstanceId(formInstance)
  85. .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
  86. String settleMethod = UtilMap.getString(formData, "selectField_ml3e44b1");//结算方式
  87. Long invoiceDateTimestamp = UtilMap.getLong(formData, "datefield_DMpwNIpo");//发票日期
  88. int paymentDays = UtilMap.getInt(formData, "selectField_ml3e44b2");//付款天数
  89. int settleDay = UtilMap.getInt(formData, "numberField_mq8yh6p6");//结算日
  90. int paymentDate = UtilMap.getInt(formData, "numberField_mjl1xyz3");//付款日
  91. if (settleMethod == null || settleMethod.trim().isEmpty()) {
  92. log.warn("结算方式缺失,返回异常标记X");
  93. receiveDate = "X";
  94. } else {
  95. Date invoiceDate = null;
  96. if (invoiceDateTimestamp != null && invoiceDateTimestamp > 0) {
  97. try {
  98. invoiceDate = new Date(invoiceDateTimestamp);
  99. } catch (Exception e) {
  100. log.error("发票日期无效: {}", invoiceDateTimestamp, e);
  101. return McR.success(null);
  102. }
  103. } else {
  104. log.error("发票日期为空");
  105. return McR.success(null);
  106. }
  107. if ("货到".equals(settleMethod)) {
  108. Date accountDate = DateUtil.offsetDay(invoiceDate, paymentDays);
  109. receiveDate = DateUtil.formatDate(accountDate);
  110. log.info("货到结算,应到帐时间: {}", receiveDate);
  111. } else if ("预付".equals(settleMethod)) {
  112. receiveDate = DateUtil.formatDate(invoiceDate);
  113. log.info("预付结算,应到帐时间: {}", receiveDate);
  114. } else if ("月结".equals(settleMethod)) {
  115. Calendar cal = Calendar.getInstance();
  116. cal.setTime(invoiceDate);
  117. int lastDayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  118. int adjustDays;
  119. if (settleDay == 0 || settleDay == 30) {
  120. adjustDays = 0;
  121. } else {
  122. adjustDays = lastDayOfMonth - settleDay;
  123. }
  124. Date baseDate = DateUtil.offsetDay(invoiceDate, adjustDays);
  125. int months = paymentDays / 30;
  126. Date dateWithPeriod = DateUtil.offsetMonth(baseDate, months);
  127. Calendar cal2 = Calendar.getInstance();
  128. cal2.setTime(dateWithPeriod);
  129. int lastDayOfResultMonth = cal2.getActualMaximum(Calendar.DAY_OF_MONTH);
  130. cal2.set(Calendar.DAY_OF_MONTH, lastDayOfResultMonth);
  131. Date monthEndDate = cal2.getTime();
  132. Date finalDate;
  133. if (paymentDate == 30) {
  134. finalDate = monthEndDate;
  135. } else {
  136. finalDate = DateUtil.offsetDay(monthEndDate, paymentDate);
  137. }
  138. receiveDate = DateUtil.formatDate(finalDate);
  139. log.info("月结结算,应到帐时间: {}", receiveDate);
  140. } else {
  141. log.warn("未识别的结算方式: {}", settleMethod);
  142. receiveDate = "X";
  143. }
  144. }
  145. } catch (NumberFormatException e) {
  146. log.error("数字格式转换异常", e);
  147. receiveDate = "X";
  148. } catch (Exception e) {
  149. log.error("计算应到帐时间异常", e);
  150. receiveDate = "X";
  151. } finally {
  152. try {
  153. if (!"X".equals(receiveDate) && receiveDate != null) {
  154. Date resultDate = DateUtil.parse(receiveDate);
  155. ydClient.operateData(YDParam.builder()
  156. .formInstanceId(formInstance)
  157. .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("dateField_mpdpzvrv", resultDate.getTime())))
  158. .build(), YDConf.FORM_OPERATION.update);
  159. log.info("已回写应到帐时间到宜搭: {}", receiveDate);
  160. } else {
  161. log.info("结果为X或null,不回写宜搭");
  162. }
  163. return McR.success(receiveDate);
  164. } catch (Exception e) {
  165. log.error("回写宜搭失败", e);
  166. return McR.errorParam( e.getMessage());
  167. }
  168. }
  169. }
  170. }