DtsController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package com.kxs.transfer.api.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.aliyun.dts.subscribe.clients.record.DefaultUserRecord;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.kxs.product.api.model.KxsMerchant;
  6. import com.kxs.system.api.model.KxsSchoolStudy;
  7. import com.kxs.transfer.api.annotation.DtsMsgListener;
  8. import com.kxs.transfer.api.model.KxsDtsErrorLog;
  9. import com.kxs.transfer.api.model.KxsDtsLog;
  10. import com.kxs.transfer.api.model.table.DMLData;
  11. import com.kxs.transfer.api.service.KxsDtsErrorLogService;
  12. import com.kxs.transfer.api.service.KxsDtsLogService;
  13. import com.kxs.transfer.api.service.product.*;
  14. import com.kxs.transfer.api.service.store.KxsMachineRecycleService;
  15. import com.kxs.transfer.api.service.store.KxsWarehouseService;
  16. import com.kxs.transfer.api.service.sys.KxsMorningLogService;
  17. import com.kxs.transfer.api.service.sys.KxsMorningService;
  18. import com.kxs.transfer.api.service.sys.KxsSchoolStudyService;
  19. import com.kxs.transfer.api.service.sys.KxsSysMsgService;
  20. import com.kxs.transfer.api.service.user.*;
  21. import com.kxs.user.api.model.KxsPartner;
  22. import lombok.RequiredArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang.exception.ExceptionUtils;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.web.bind.annotation.RestController;
  27. import java.util.List;
  28. /**
  29. * <p>
  30. * DTS控制器
  31. * 在此控制器里做数据分发
  32. * 比如将数据分发到不同的数据源中
  33. * </p>
  34. *
  35. * @author 没秃顶的码农
  36. * @date 2024-01-25
  37. */
  38. @RestController
  39. @Slf4j
  40. @RequiredArgsConstructor
  41. public class DtsController {
  42. private final KxsDtsLogService kxsDtsLogService;
  43. private final KxsDtsErrorLogService kxsDtsErrorLogService;
  44. private final KxsUserService kxsUserService;
  45. private final KxsUserPresetLogService kxsUserPresetLogService;
  46. private final KxsUserAddressService kxsUserAddressService;
  47. private final KxsUserAmountService kxsUserAmountService;
  48. private final KxsUserAdvanceService kxsUserAdvanceService;
  49. private final KxsUserAmountLogService kxsUserAmountLogService;
  50. private final KxsUserWithdrawalService kxsUserWithdrawalService;
  51. private final KxsLeaderService kxsLeaderService;
  52. private final KxsLeaderAmountLogService kxsLeaderAmountLogService;
  53. private final KxsPartnerService kxsPartnerService;
  54. private final KxsMorningService kxsMorningService;
  55. private final KxsSchoolStudyService kxsSchoolStudyService;
  56. private final KxsSysMsgService kxsSysMsgService;
  57. private final KxsMachineService kxsMachineService;
  58. //产品模块
  59. private final KxsShopGoodsService kxsShopGoodsService;
  60. private final KxsMachinePledgeService kxsMachinePledgeService;
  61. private final KxsMachineRatioService kxsMachineRatioService;
  62. private final KxsTicketService kxsTicketService;
  63. private final KxsTicketTransferService kxsTicketTransferService;
  64. private final KxsMerchantService kxsMerchantService;
  65. //仓库模块
  66. private final KxsWarehouseService kxsWarehouseService;
  67. private final KxsMachineRecycleService kxsMachineRecycleService;
  68. @Value("${spring.profiles.active}")
  69. private String active;
  70. @DtsMsgListener
  71. public void dtsListener(Long dataId, DMLData dmlData, DefaultUserRecord record) {
  72. try {
  73. //过滤无效数据
  74. if ("Users".equals(dmlData.getTableName())) {
  75. List<String> changeFieldList = dmlData.getChangeFieldList();
  76. //过滤Users老表记录登陆设备的信息日志
  77. if (changeFieldList.size() == 2 && changeFieldList.contains("DeviceId") && changeFieldList.contains("DeviceType")) {
  78. record.commit(String.valueOf(record.getSourceTimestamp()));
  79. return;
  80. }
  81. if (changeFieldList.size() == 1 && changeFieldList.contains("DeviceId")) {
  82. // 如果changeFieldList的大小为1,且changeFieldList包含DeviceId,则提交记录
  83. record.commit(String.valueOf(record.getSourceTimestamp()));
  84. return;
  85. }
  86. //过滤Users本月交易额字段
  87. if (changeFieldList.size() == 1 && changeFieldList.contains("ThisMonthTrade")) {
  88. record.commit(String.valueOf(record.getSourceTimestamp()));
  89. return;
  90. }
  91. //过滤Users库存更新字段
  92. if (changeFieldList.size() == 1 && changeFieldList.contains("StoreStock")) {
  93. record.commit(String.valueOf(record.getSourceTimestamp()));
  94. return;
  95. }
  96. }
  97. //去重数据
  98. KxsDtsLog dtsLog = kxsDtsLogService.getOne(Wrappers.<KxsDtsLog>lambdaQuery().eq(KxsDtsLog::getDataId, dataId));
  99. if (dtsLog != null) {
  100. log.info("dts的数据重复:{},dataID:{}", dmlData.getValidFieldDataMap(), dataId);
  101. record.commit(String.valueOf(record.getSourceTimestamp()));
  102. return;
  103. }
  104. // if (!"UserMoveInfo".equals(dmlData.getTableName())) {
  105. // log.info("开始消费:{}表的数据,dataID:{}, 表名:{}", dmlData.getTableName(), dataId, dmlData.getDatabaseName());
  106. // }
  107. //用户表
  108. if ("Users".equals(dmlData.getTableName())) {
  109. kxsUserService.changeUser(dmlData);
  110. }
  111. //用户预设职级表
  112. if ("UserRankWhite".equals(dmlData.getTableName())) {
  113. kxsUserPresetLogService.changeUser(dmlData);
  114. }
  115. //密码操作
  116. if ("UserMoveInfo".equals(dmlData.getTableName())) {
  117. kxsUserService.changeUserPwd(dmlData);
  118. //密码操作不存储原始数据
  119. record.commit(String.valueOf(record.getSourceTimestamp()));
  120. return;
  121. }
  122. //用户地址表
  123. if ("UserAddress".equals(dmlData.getTableName())) {
  124. kxsUserAddressService.changeData(dmlData);
  125. }
  126. //用户账户
  127. if ("UserAccount".equals(dmlData.getTableName()) && ("dev".equals(active) || "test".equals(active) ? "KxsMainServer":"KxsProfitServer").equals(dmlData.getDatabaseName())) {
  128. kxsUserAmountService.changeData(dmlData);
  129. }
  130. //用户预扣款表
  131. if ("ToChargeBackRecord".equals(dmlData.getTableName())) {
  132. kxsUserAdvanceService.changeData(dmlData);
  133. }
  134. //用户账户余额日志
  135. if ("UserAccountRecord".equals(dmlData.getTableName())) {
  136. kxsUserAmountLogService.changeData(dmlData);
  137. }
  138. //用户提现申请记录
  139. if ("UserCashRecord".equals(dmlData.getTableName()) && ("dev".equals(active) || "test".equals(active) ? "KxsMainServer":"KxsProfitServer").equals(dmlData.getDatabaseName())) {
  140. kxsUserWithdrawalService.changeData(dmlData);
  141. }
  142. //盟主表
  143. if ("Leaders".equals(dmlData.getTableName())) {
  144. kxsLeaderService.changeData(dmlData);
  145. }
  146. //盟主金额变动记录表
  147. // if ("LeaderReserveRecord".equals(dmlData.getTableName())) {
  148. // kxsLeaderAmountLogService.changeData(dmlData);
  149. // }
  150. //合伙人表
  151. if ("OperatorRankWhite".equals(dmlData.getTableName())) {
  152. kxsPartnerService.changeData(dmlData);
  153. }
  154. //合伙人账户 -> 合伙人表
  155. if ("UserAccount".equals(dmlData.getTableName()) && "KxsOpServer".equals(dmlData.getDatabaseName())) {
  156. kxsPartnerService.changeAccountData(dmlData);
  157. }
  158. //商品表同步
  159. if ("Products".equals(dmlData.getTableName())) {
  160. kxsShopGoodsService.changeData(dmlData);
  161. }
  162. //每日晨会同步
  163. if ("SchoolMorningMeet".equals(dmlData.getTableName())) {
  164. kxsMorningService.changeData(dmlData);
  165. }
  166. if ("SchoolMorningMeetLog".equals(dmlData.getTableName())) {
  167. kxsMorningService.changeLogData(dmlData);
  168. }
  169. //创客学堂
  170. if ("SchoolMakerStudy".equals(dmlData.getTableName())) {
  171. kxsSchoolStudyService.changeData(dmlData);
  172. }
  173. //仓库表
  174. if ("StoreHouse".equals(dmlData.getTableName())) {
  175. kxsWarehouseService.changeData(dmlData);
  176. }
  177. //仓库担保记录
  178. if ("StoreHouseAmountPromiss".equals(dmlData.getTableName())) {
  179. kxsWarehouseService.changeCreditData(dmlData);
  180. }
  181. //仓库额度表
  182. if ("UserAccount".equals(dmlData.getTableName()) && ("dev".equals(active) || "test".equals(active) ? "KxsMainServer":"KxsProfitServer").equals(dmlData.getDatabaseName())) {
  183. kxsWarehouseService.changeAccountData(dmlData);
  184. }
  185. //小仓库额度表
  186. if ("UserAccount".equals(dmlData.getTableName()) && ("dev".equals(active) || "test".equals(active) ? "KxsMainServer":"KxsProfitServer").equals(dmlData.getDatabaseName())) {
  187. kxsWarehouseService.changeAccountMinData(dmlData);
  188. }
  189. //机具表
  190. if ("PosMachinesTwo".equals(dmlData.getTableName())) {
  191. kxsMachineService.changeData(dmlData);
  192. }
  193. //商户表
  194. if ("PosMerchantInfo".equals(dmlData.getTableName())) {
  195. kxsMerchantService.changeData(dmlData);
  196. }
  197. //机具回收表
  198. if ("RecycMachineOrder".equals(dmlData.getTableName())) {
  199. kxsMachineRecycleService.changeData(dmlData);
  200. }
  201. if ("RecycMachineOrderPos".equals(dmlData.getTableName())) {
  202. kxsMachineRecycleService.changeInfoData(dmlData);
  203. }
  204. //机具押金调整记录
  205. if ("MerchantDepositSet".equals(dmlData.getTableName())) {
  206. kxsMachinePledgeService.changeData(dmlData);
  207. }
  208. //机具费率调整记录
  209. if ("PosMachinesFeeChangeRecord".equals(dmlData.getTableName())) {
  210. kxsMachineRatioService.changeData(dmlData);
  211. }
  212. //券码表同步
  213. if ("PosCoupons".equals(dmlData.getTableName())) {
  214. kxsTicketService.changeData(dmlData);
  215. }
  216. //券码划拨表同步
  217. if ("PosCouponOrders".equals(dmlData.getTableName())) {
  218. kxsTicketTransferService.changeData(dmlData);
  219. }
  220. //创客消息表
  221. if ("MsgPersonal".equals(dmlData.getTableName())) {
  222. kxsSysMsgService.changeUserData(dmlData);
  223. }
  224. //系统消息表
  225. if ("MsgPlacard".equals(dmlData.getTableName())) {
  226. kxsSysMsgService.changeSysData(dmlData);
  227. }
  228. //系统弹窗表
  229. if ("MsgAlert".equals(dmlData.getTableName())) {
  230. kxsSysMsgService.changeModalData(dmlData);
  231. }
  232. //存储历史数据
  233. KxsDtsLog kxsDtsLog = new KxsDtsLog();
  234. kxsDtsLog.setDataId(dataId);
  235. // kxsDtsLog.setContent(JSON.toJSONString(dmlData));
  236. kxsDtsLog.setTableName(dmlData.getTableName());
  237. kxsDtsLog.setOperation(dmlData.getOperation().toString());
  238. kxsDtsLogService.save(kxsDtsLog);
  239. //删除错误记录,不管有没有都删除,因为很快
  240. // kxsDtsErrorLogService.removeById(dataId);
  241. record.commit(String.valueOf(record.getSourceTimestamp()));
  242. } catch (Exception e) {
  243. //详细错误信息
  244. String fullStackTrace = ExceptionUtils.getFullStackTrace(e);
  245. //存储错误数据
  246. KxsDtsErrorLog kxsDtsLog = new KxsDtsErrorLog();
  247. kxsDtsLog.setId(dataId);
  248. kxsDtsLog.setContent(JSON.toJSONString(dmlData));
  249. kxsDtsLog.setTableName(dmlData.getTableName());
  250. kxsDtsLog.setOperation(dmlData.getOperation().toString());
  251. kxsDtsLog.setErrorStr(fullStackTrace);
  252. kxsDtsErrorLogService.save(kxsDtsLog);
  253. record.commit(String.valueOf(record.getSourceTimestamp()));
  254. log.error(fullStackTrace);
  255. }
  256. }
  257. }