You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

644 lines
31 KiB

3 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
3 months ago
  1. package com.example.demo.serviceImpl.cash;
  2. import com.example.demo.Util.JWTUtil;
  3. import com.example.demo.Util.LanguageTranslationUtil;
  4. import com.example.demo.config.RabbitMQConfig;
  5. import com.example.demo.domain.DTO.PerformanceDTO;
  6. import com.example.demo.domain.entity.*;
  7. import com.example.demo.domain.vo.cash.CashCollection;
  8. import com.example.demo.domain.vo.cash.CashCollectionMessage;
  9. import com.example.demo.domain.vo.cash.PerformanceVO;
  10. import com.example.demo.domain.vo.coin.GoldUser;
  11. import com.example.demo.domain.vo.coin.Messages;
  12. import com.example.demo.domain.vo.coin.Result;
  13. import com.example.demo.mapper.cash.CashCollectionMapper;
  14. import com.example.demo.mapper.coin.MarketMapper;
  15. import com.example.demo.mapper.coin.UserMapper;
  16. import com.example.demo.service.cash.CashCollectionService;
  17. import com.github.pagehelper.PageHelper;
  18. import com.github.pagehelper.PageInfo;
  19. import jakarta.servlet.http.HttpServletRequest;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import org.springframework.util.CollectionUtils;
  26. import org.springframework.web.bind.annotation.RequestHeader;
  27. import org.springframework.web.context.request.RequestContextHolder;
  28. import org.springframework.web.context.request.ServletRequestAttributes;
  29. import java.math.BigDecimal;
  30. import java.time.LocalDateTime;
  31. import java.time.ZoneOffset;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. import java.util.Objects;
  35. import java.util.UUID;
  36. import java.util.stream.Collectors;
  37. /**
  38. * @program: gold-java
  39. * @ClassName cashCollectionServiceImpl
  40. * @description: 处理收款相关业务逻辑
  41. * @author: Ethan
  42. * @create: 202509-26 11:23
  43. * @Version 1.0
  44. **/
  45. @Service
  46. @Slf4j
  47. public class CashCollectionServiceImpl implements CashCollectionService {
  48. @Autowired
  49. private CashCollectionMapper cashCollectionMapper;
  50. @Autowired
  51. private UserMapper userMapper;
  52. @Autowired
  53. private MarketMapper marketMapper;
  54. @Autowired
  55. private RabbitTemplate rabbitTemplate;
  56. @Autowired
  57. private LanguageTranslationUtil languageTranslationUtil;
  58. //新增收款订单
  59. @Override
  60. public String add(CashCollection cashCollection,@RequestHeader(defaultValue = "zh_CN") String lang) {
  61. if (cashCollection.getJwcode() == null) {
  62. throw new IllegalArgumentException("精网号不能为空");
  63. }
  64. if (cashCollection.getJwcode() < 10000000 || cashCollection.getJwcode() > 99999999) {
  65. throw new IllegalArgumentException("精网号必须为8位");
  66. }
  67. if (cashCollection.getName() == null || cashCollection.getName().isEmpty()){
  68. throw new IllegalArgumentException("客户姓名不能为空");
  69. }
  70. if (cashCollection.getActivity() == null || cashCollection.getActivity().isEmpty()) {
  71. throw new IllegalArgumentException("活动不能为空");
  72. }
  73. if (cashCollection.getGoodsName() == null|| cashCollection.getGoodsName().isEmpty()) {
  74. throw new IllegalArgumentException("产品名称不能为空");
  75. }
  76. if (cashCollection.getGoodsName().equals("金币充值")) {
  77. if (cashCollection.getPermanentGold() == 0 && cashCollection.getFreeGold() == 0) {
  78. throw new IllegalArgumentException("金币数量不能为空");
  79. }
  80. if (cashCollection.getWalletId() == null || cashCollection.getWalletId() < 0 || cashCollection.getWalletId() > 10) {
  81. throw new IllegalArgumentException("钱包ID不能为空");
  82. }
  83. }
  84. if (!cashCollection.getGoodsName().equals("金币充值")) {
  85. if (cashCollection.getGoodNum() == 0) {
  86. throw new IllegalArgumentException("产品数量不能为空");
  87. }
  88. if (cashCollection.getNumUnit() == null|| cashCollection.getNumUnit().isEmpty()) {
  89. throw new IllegalArgumentException("数量单位不能为空");
  90. }
  91. }
  92. if (cashCollection.getPaymentCurrency() == null || cashCollection.getPaymentCurrency().isEmpty()) {
  93. throw new IllegalArgumentException("支付币种不能为空");
  94. }
  95. if (cashCollection.getPaymentAmount() == null || cashCollection.getPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
  96. throw new IllegalArgumentException("支付金额不能为空");
  97. }
  98. if (cashCollection.getPayType() == null|| cashCollection.getPayType().isEmpty()) {
  99. throw new IllegalArgumentException("支付方式不能为空");
  100. }
  101. if (cashCollection.getReceivedMarket() == null||cashCollection.getReceivedMarket().isEmpty()) {
  102. throw new IllegalArgumentException("到账地区不能为空");
  103. }
  104. if (cashCollection.getPayTime() == null) {
  105. throw new IllegalArgumentException("付款时间不能为空");
  106. }
  107. //生成订单号后半部分
  108. String orderNumber = UUID.randomUUID().toString().replaceAll("-", "");
  109. CashRecord cashRecord = new CashRecord();
  110. //构建订单信息
  111. cashRecord.setOrderCode("XJ_" + orderNumber); //订单号
  112. cashRecord.setJwcode(cashCollection.getJwcode()); //精网号
  113. cashRecord.setName(cashCollection.getName()); //客户姓名
  114. cashRecord.setActivity(cashCollection.getActivity()); // 活动
  115. cashRecord.setGoodsName(cashCollection.getGoodsName()); //商品名称
  116. cashRecord.setGoodNum(cashCollection.getGoodNum()); //商品数量
  117. cashRecord.setNumUnit(cashCollection.getNumUnit()); //数量单位
  118. cashRecord.setPermanentGold(cashCollection.getPermanentGold()); //永久金币
  119. cashRecord.setFreeGold(cashCollection.getFreeGold()); //免费金币
  120. cashRecord.setWalletId(cashCollection.getWalletId()); // 钱包 ID
  121. // 打印钱包 ID
  122. log.info("钱包ID:{}", cashRecord.getWalletId());
  123. cashRecord.setPaymentCurrency(cashCollection.getPaymentCurrency()); //付款币种
  124. cashRecord.setPaymentAmount(cashCollection.getPaymentAmount()); //付款金额
  125. cashRecord.setReceivedMarket(cashCollection.getReceivedMarket()); //到账地区
  126. cashRecord.setPayType(cashCollection.getPayType()); //支付方式
  127. cashRecord.setPayTime(cashCollection.getPayTime()); //付款时间
  128. cashRecord.setVoucher(cashCollection.getVoucher()); //转账凭证
  129. cashRecord.setRemark(cashCollection.getRemark()); //备注
  130. cashRecord.setStatus(0); //订单状态:付款线下财务待审核
  131. cashRecord.setSubmitterId(cashCollection.getSubmitterId()); //提交人 ID
  132. cashRecord.setSubmitterMarket(cashCollection.getSubmitterMarket());
  133. cashRecord.setOrderType(1); //订单类型:1-收款
  134. cashRecord.setMarket(cashCollection.getMarket());
  135. //地区,根据jwcode插入
  136. //cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(cashRecord.getJwcode()));
  137. //插入新收款订单
  138. cashCollectionMapper.add(cashRecord);
  139. // 发送收款创建消息
  140. Messages message = new Messages();
  141. message.setJwcode(cashRecord.getJwcode());
  142. message.setName(cashRecord.getName());
  143. message.setStatus(cashRecord.getStatus());
  144. message.setDesc("的现金收款申请待审核,前往审核");
  145. message.setTitle("现金管理--收款处理");
  146. message.setType(0);
  147. message.setTypeId(cashRecord.getId());
  148. message.setMarket(Integer.valueOf(cashRecord.getMarket()));
  149. String marketName = marketMapper.getMarketNameById(String.valueOf(message.getMarket()));
  150. message.setMarketName(marketName);
  151. message.setQueryId(67);
  152. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_COLLECTION_EXCHANGE, "cash.collection.save", message);
  153. return "添加成功";
  154. }
  155. @Override
  156. public String addFreeCoin(CashCollection cashCollection,@RequestHeader(defaultValue = "zh_CN") String lang) {
  157. if (cashCollection.getJwcode() == null) {
  158. throw new IllegalArgumentException("精网号不能为空");
  159. }
  160. if (cashCollection.getJwcode() < 10000000 || cashCollection.getJwcode() > 99999999) {
  161. throw new IllegalArgumentException("精网号必须为8位");
  162. }
  163. if (cashCollection.getName() == null || cashCollection.getName().isEmpty()){
  164. throw new IllegalArgumentException("客户姓名不能为空");
  165. }
  166. if (cashCollection.getActivity() == null || cashCollection.getActivity().isEmpty()) {
  167. throw new IllegalArgumentException("活动不能为空");
  168. }
  169. if (cashCollection.getFreeGold() == 0) {
  170. throw new IllegalArgumentException("免费金币数量不能为空");
  171. }
  172. //生成订单号后半部分
  173. String orderNumber = UUID.randomUUID().toString().replaceAll("-", "");
  174. CashRecord cashRecord = new CashRecord();
  175. //构建订单信息
  176. cashRecord.setOrderCode("XJ_" + orderNumber); //订单号
  177. cashRecord.setJwcode(cashCollection.getJwcode()); //精网号
  178. cashRecord.setName(cashCollection.getName()); //客户姓名
  179. cashRecord.setActivity(cashCollection.getActivity()); // 活动
  180. cashRecord.setGoodsName("免费金币赠送"); //商品名称
  181. cashRecord.setFreeGold(cashCollection.getFreeGold()); //免费金币
  182. cashRecord.setRemark(cashCollection.getRemark()); //备注
  183. cashRecord.setStatus(0); //订单状态:付款线下财务待审核
  184. cashRecord.setSubmitterId(cashCollection.getSubmitterId()); //提交人ID
  185. cashRecord.setSubmitterMarket(cashCollection.getSubmitterMarket());
  186. cashRecord.setOrderType(1); //订单类型:1-收款
  187. cashRecord.setMarket(cashCollection.getMarket());
  188. //地区,根据jwcode插入
  189. //cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(cashRecord.getJwcode()));
  190. //插入新收款订单
  191. cashCollectionMapper.add(cashRecord);
  192. // 发送收款创建消息
  193. Messages message = new Messages();
  194. message.setJwcode(cashRecord.getJwcode());
  195. message.setName(cashRecord.getName());
  196. message.setStatus(cashRecord.getStatus());
  197. message.setDesc("的现金收款申请待审核,请前往审核");
  198. message.setTitle("现金收款--现金收款");
  199. message.setType(1);
  200. message.setTypeId(cashRecord.getId());
  201. message.setMarket(Integer.valueOf(cashRecord.getMarket()));
  202. String marketName = marketMapper.getMarketNameById(String.valueOf(message.getMarket()));
  203. message.setMarketName(marketName);
  204. message.setQueryId(67);
  205. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_COLLECTION_EXCHANGE, "cash.collection.save", message);
  206. return "添加成功";
  207. }
  208. //撤回未审核的订单
  209. @Override
  210. public String cancel(String orderCode) {
  211. // 查询订单是否存在
  212. CashRecord cashRecord = cashCollectionMapper.selectByOrderCode(orderCode);
  213. if (cashRecord == null) {
  214. throw new IllegalArgumentException("订单不存在");
  215. }
  216. if (cashRecord.getStatus() != 0) {
  217. throw new IllegalArgumentException("订单状态不符合条件");
  218. }
  219. // 更新订单状态为撤回状态(5)
  220. int rows = cashCollectionMapper.updateStatus(orderCode, 5);
  221. // 更新与该订单关联的消息记录的 flag 字段为 1
  222. try {
  223. // 根据订单ID查找关联的消息记录
  224. Messages message = new Messages();
  225. message.setTypeId(cashRecord.getId()); // 订单ID作为消息的 typeId
  226. message.setType(0); // 消息类型为收款
  227. // 更新消息的 flag 字段为 1
  228. int messageRows = cashCollectionMapper.updateMessageFlagByTypeIdAndType(
  229. message.getTypeId(), message.getType(), 1);
  230. if (messageRows <= 0) {
  231. log.warn("未找到与订单 {} 关联的消息记录", orderCode);
  232. }
  233. } catch (Exception e) {
  234. log.error("更新消息状态失败,订单号:{}", orderCode, e);
  235. // 可选择抛出异常或记录日志后继续执行
  236. }
  237. return rows > 0 ? "撤回成功" : "撤回失败";
  238. }
  239. //编辑并重新提交收款订单
  240. @Override
  241. public String reSubmit(CashRecord cashRecord,@RequestHeader(defaultValue = "zh_CN") String lang) {
  242. if (cashRecord.getJwcode() == null) {
  243. throw new IllegalArgumentException("精网号不能为空");
  244. }
  245. if (cashRecord.getJwcode() < 10000000 || cashRecord.getJwcode() > 99999999) {
  246. throw new IllegalArgumentException("精网号必须为8位");
  247. }
  248. if (cashRecord.getName() == null) {
  249. throw new IllegalArgumentException("客户姓名不能为空");
  250. }
  251. if (cashRecord.getActivity() == null) {
  252. throw new IllegalArgumentException("活动不能为空");
  253. }
  254. if (cashRecord.getGoodsName() == null) {
  255. throw new IllegalArgumentException("商品名不能为空");
  256. }
  257. if (cashRecord.getGoodsName().equals("金币充值")) {
  258. if (cashRecord.getPermanentGold() == 0 && cashRecord.getFreeGold() == 0) {
  259. throw new IllegalArgumentException("金币数量不能为空");
  260. }if (cashRecord.getPermanentGold() == 0){
  261. throw new IllegalArgumentException("永久金币数量不能为空");
  262. }
  263. if (cashRecord.getWalletId() == null) {
  264. throw new IllegalArgumentException("钱包ID不能为空");
  265. }
  266. }
  267. if (!cashRecord.getGoodsName().equals("金币充值")) {
  268. if (cashRecord.getGoodNum() == 0) {
  269. throw new IllegalArgumentException("产品数量不能为空");
  270. }
  271. if (cashRecord.getNumUnit() == null) {
  272. throw new IllegalArgumentException("数量单位不能为空");
  273. }
  274. }
  275. if (cashRecord.getPaymentCurrency() == null) {
  276. throw new IllegalArgumentException("支付币种不能为空");
  277. }
  278. if (cashRecord.getPaymentAmount() == null || cashRecord.getPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
  279. throw new IllegalArgumentException("支付金额不能为空");
  280. }
  281. if (cashRecord.getPayType() == null) {
  282. throw new IllegalArgumentException("支付方式不能为空");
  283. }
  284. if (cashRecord.getReceivedMarket() == null) {
  285. throw new IllegalArgumentException("到账地区不能为空");
  286. }
  287. if (cashRecord.getPayTime() == null) {
  288. throw new IllegalArgumentException("付款时间不能为空");
  289. }
  290. CashRecord status = cashCollectionMapper.selectByOrderCode(cashRecord.getOrderCode());
  291. if (!status.getStatus().equals(5)) {
  292. throw new IllegalArgumentException("只允许编辑已撤回订单");
  293. }
  294. //地区,根据jwcode插入(弃用,插入前调用接口获取地区和姓名,之后前端传入)
  295. //cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(cashRecord.getJwcode()));
  296. int rows = cashCollectionMapper.updateByOrderCode(cashRecord);
  297. if (rows > 0) {
  298. // 发送收款创建消息
  299. Messages message = new Messages();
  300. message.setJwcode(cashRecord.getJwcode());
  301. message.setName(cashRecord.getName());
  302. message.setStatus(cashRecord.getStatus());
  303. message.setDesc("的现金收款申请待审核,前往审核");
  304. message.setTitle("现金管理--收款处理");
  305. message.setType(0);
  306. message.setTypeId(cashRecord.getId());
  307. message.setMarket(Integer.valueOf(cashRecord.getMarket()));
  308. String marketName = marketMapper.getMarketNameById(String.valueOf(message.getMarket()));
  309. message.setMarketName(marketName);
  310. message.setQueryId(67);
  311. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_COLLECTION_EXCHANGE, "cash.collection.save", message);
  312. }
  313. return rows > 0 ? "重新提交成功" : "重新提交失败";
  314. }
  315. //多条件查询收款订单列表
  316. @Override
  317. public PageInfo<CashCollection> selectCollection(Integer pageNum, Integer pageSize, CashCollection cashCollection) {
  318. /* //将操作人的地区列表改为id
  319. List<String> markets = marketMapper.getMarketIds(cashCollection.getMarkets());
  320. if (markets.contains("9") || markets.contains("9999")) {
  321. markets = null;
  322. }*/
  323. // cashCollection.setReceivedMarket(marketMapper.getMarketId(cashCollection.getReceivedMarket()));
  324. if (cashCollection.getCashRoleId() == 2) {
  325. //角色是总部时,若不特地传状态,传1346,sql处理为(1,3,4,6)筛选,
  326. if (cashCollection.getStatus() == null) {
  327. cashCollection.setStatus(1346);
  328. }
  329. cashCollection.setSubmitterId(null);
  330. cashCollection.setReceivedMarket(null);
  331. cashCollection.setSubmitterMarket(null);
  332. }
  333. if (cashCollection.getCashRoleId() == 1) {
  334. //角色是地方财务,提交人置空不设筛选条件,仅按收款地区、提交人地区筛选()
  335. if (cashCollection.getStatus() == null) {
  336. cashCollection.setStatus(123460);
  337. }
  338. //状态为待审核和已驳回时按照提交人地区筛选
  339. if (cashCollection.getStatus() == 0 || cashCollection.getStatus() == 2) {
  340. cashCollection.setReceivedMarket(null);
  341. }
  342. //状态为已通过和Link通过时,满足收款地区或提交人地区即可
  343. /* if (cashCollection.getStatus() == 13) {
  344. cashCollection.setSubmitterId(null);
  345. }*/
  346. //状态为13 或46,已通过或已完成和已退款,满足收款地区或提交人地区即可,
  347. cashCollection.setSubmitterId(null);
  348. }
  349. if (cashCollection.getCashRoleId() == 0) {
  350. //角色是地方财务,提交人置空不设筛选条件---仅当角色是0 地方客服时,按提交人筛选
  351. if (cashCollection.getStatus() == null) {
  352. cashCollection.setStatus(1234560);
  353. }
  354. cashCollection.setSubmitterId(cashCollection.getSubmitterId());
  355. cashCollection.setReceivedMarket(null);
  356. }
  357. // cashCollection.setMarkets(markets);
  358. PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper
  359. List<CashCollection> cashCollections = cashCollectionMapper.selectCollection1(pageNum, pageSize, cashCollection);
  360. return new PageInfo<>(cashCollections);
  361. }
  362. //补全手续费等内容
  363. @Override
  364. @Transactional(rollbackFor = Exception.class)
  365. public String complete(CashRecord cashRecord) {
  366. if (!Objects.equals(cashRecord.getPaymentCurrency(), cashRecord.getReceivedCurrency())
  367. && ("Stripe".equals(cashRecord.getPayType()) || "Paypal".equals(cashRecord.getPayType()))) {
  368. return "支付币种与收款币种不一致";
  369. }
  370. int rows = cashCollectionMapper.complete(cashRecord);
  371. String goodsName = cashCollectionMapper.selectGoodsNameByCode(cashRecord.getOrderCode());
  372. if (goodsName != null && goodsName.equals("金币充值")) {
  373. // 先从数据库中获取订单的 walletId , permanentGold 和 jwcode
  374. CashRecord dbRecord = cashCollectionMapper.selectByOrderCode(cashRecord.getOrderCode());
  375. Integer walletId = dbRecord != null ? dbRecord.getWalletId() : null;
  376. Integer permanentGold = dbRecord != null ? dbRecord.getPermanentGold() : null;
  377. Integer jwcode = dbRecord != null ? dbRecord.getJwcode() : null;
  378. cashRecord.setOrderCode(cashRecord.getOrderCode().replace("XJ_", "XJCZ_"));
  379. //修改金币订单
  380. cashCollectionMapper.updateGoldOrder(cashRecord);
  381. // 充值成功,增加用户钱包金币
  382. if (walletId != null && permanentGold > 0) {
  383. try {
  384. // 尝试更新用户钱包余额
  385. int updateRows = cashCollectionMapper.addUserWalletPermanentGold(
  386. jwcode,
  387. walletId,
  388. permanentGold
  389. );
  390. // 如果没有更新任何行,说明记录不存在,需要插入新记录
  391. if (updateRows == 0) {
  392. UserRegionWallet wallet = new UserRegionWallet();
  393. wallet.setJwcode(jwcode);
  394. wallet.setWalletId(walletId);
  395. wallet.setCurrentPermanentGold(new BigDecimal(permanentGold));
  396. cashCollectionMapper.insertUserWallet(wallet);
  397. log.info("创建新的钱包记录:jwcode={}, walletId={}, permanentGold={}",
  398. jwcode, walletId, permanentGold);
  399. } else {
  400. log.info("充值成功:jwcode={}, walletId={}, 增加金币={}",
  401. jwcode, walletId, permanentGold);
  402. }
  403. } catch (Exception e) {
  404. log.error("充值钱包失败:jwcode={}, walletId={}, error={}",
  405. jwcode, walletId, e.getMessage(), e);
  406. throw new RuntimeException("充值钱包失败:" + e.getMessage(), e);
  407. }
  408. }
  409. }
  410. return rows > 0 ? "编辑成功" : "编辑失败";
  411. }
  412. //根据精网号查询姓名和地区
  413. @Override
  414. public Result getNameAndMarket(Integer jwcode,@RequestHeader(defaultValue = "zh_CN") String lang) {
  415. try {
  416. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  417. String token = request.getHeader("token");
  418. Admin admin = (Admin) JWTUtil.getUserDetailsList(String.valueOf(token), Admin.class);
  419. if (admin != null) {
  420. List<String> list = Arrays.asList(admin.getMarkets().split(","));
  421. List<String> markets = marketMapper.getMarketIds(list);
  422. // 检查用户是否存在
  423. int userCount = cashCollectionMapper.checkUserExists(jwcode);
  424. if (userCount == 0) {
  425. String message = languageTranslationUtil.translate("精网号有误!请检查", lang);
  426. return Result.error(message);
  427. }
  428. if (markets.contains("9") || markets.contains("9999")) {
  429. // 有特殊权限,可访问所有用户
  430. User user = new User();
  431. user.setMarket(cashCollectionMapper.getMarketByJwcode(jwcode));
  432. user.setName(cashCollectionMapper.getNameByJwcode(jwcode));
  433. user.setMarketName(cashCollectionMapper.getMarketNameByJwcode(jwcode));
  434. return Result.success(user);
  435. } else {
  436. // 检查用户所在市场是否在管理员权限范围内
  437. String userMarket = cashCollectionMapper.getMarketByJwcode(jwcode);
  438. if (userMarket != null && markets.contains(userMarket)) {
  439. // 有权限访问,返回用户信息
  440. User user = new User();
  441. user.setMarket(userMarket);
  442. user.setName(cashCollectionMapper.getNameByJwcode(jwcode));
  443. user.setMarketName(cashCollectionMapper.getMarketNameByJwcode(jwcode));
  444. return Result.success(user);
  445. } else {
  446. // 无权限,返回用户所属地区
  447. String userMarketName = cashCollectionMapper.getMarketNameByJwcode(jwcode);
  448. String translatedMarketName = languageTranslationUtil.translate(userMarketName, lang);
  449. String message = languageTranslationUtil.translate("无权限访问", lang);
  450. return Result.errorWithData(message, java.util.Map.of("market", translatedMarketName));
  451. }
  452. }
  453. } else {
  454. // admin 为 null,即权限验证失败
  455. String userMarketName = cashCollectionMapper.getMarketNameByJwcode(jwcode);
  456. String translatedMarketName = languageTranslationUtil.translate(userMarketName, lang);
  457. String message = languageTranslationUtil.translate("无权限访问", lang);
  458. return Result.errorWithData(message, java.util.Map.of("market", translatedMarketName));
  459. }
  460. } catch (Exception e) {
  461. e.printStackTrace();
  462. String errorMessage = languageTranslationUtil.translate("精网号有误!请检查", lang);
  463. return Result.error(errorMessage);
  464. }
  465. }
  466. //获取收款活动列表
  467. @Override
  468. public List<RechargeActivity> getActivityList() {
  469. LocalDateTime now = LocalDateTime.now();
  470. return cashCollectionMapper.getActivityList(now);
  471. }
  472. //同步g_order订单到cash_record表
  473. @Override
  474. public Object syncToCashRecord() {
  475. int total = 0;
  476. while (true) {
  477. List<GOrder> gOrders = cashCollectionMapper.getUnSync(50);
  478. if (CollectionUtils.isEmpty(gOrders)) {
  479. break;
  480. }
  481. try {
  482. // 每批独立事务
  483. processBatch(gOrders);
  484. total += gOrders.size();
  485. log.info("✅ 同步完成一批,数量: {}, 累计: {}", gOrders.size(), total);
  486. } catch (Exception e) {
  487. List<Integer> failedIds = gOrders.stream().map(GOrder::getId).collect(Collectors.toList());
  488. log.error("失败订单ID: {}", failedIds);
  489. // 可存入 error_log 表,供人工处理
  490. // 这里选择继续下一批(容忍部分失败)
  491. }
  492. if (gOrders.size() < 50) {
  493. break; // 最后一批
  494. }
  495. }
  496. return "同步完毕,成功处理 " + total + " 条";
  497. }
  498. @Override
  499. @Transactional(rollbackFor = Exception.class)
  500. public void processBatch(List<GOrder> gOrders) {
  501. for (GOrder gOrder : gOrders) {
  502. CashRecord cashRecord = new CashRecord();
  503. //构建基础信息
  504. cashRecord.setOrderType(1);
  505. cashRecord.setJwcode(gOrder.getJwcode());
  506. String name=cashCollectionMapper.getNameByJwcode(gOrder.getJwcode());
  507. if (name!=null){
  508. cashRecord.setName(name);
  509. }else cashRecord.setName("未知");
  510. cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(gOrder.getJwcode()));
  511. if (gOrder.getType().equals("gold")){ //充金豆
  512. cashRecord.setActivity("99");
  513. cashRecord.setGoodsName("Link充值金豆");
  514. cashRecord.setRemark("Link充值金豆");
  515. cashRecord.setNumUnit("个");
  516. cashRecord.setPermanentGold(0);
  517. cashRecord.setGoodNum(gOrder.getCount());
  518. }
  519. if (gOrder.getType().equals("gold_coin")){//充金币
  520. cashRecord.setActivity("98");
  521. cashRecord.setGoodsName("Link充值金币");
  522. cashRecord.setRemark("Link充值金币");
  523. cashRecord.setPermanentGold(gOrder.getCount()*100);
  524. cashRecord.setGoodNum(0);
  525. }
  526. cashRecord.setOrderCode(gOrder.getOrderNo());
  527. if (gOrder != null) {
  528. switch (gOrder.getPayStyle()) {
  529. case 3:
  530. cashRecord.setPayType("IOS内购");
  531. cashRecord.setBankCode(gOrder.getIosTransactionId());
  532. cashRecord.setReceivedMarket("4");
  533. cashRecord.setPayload("IOS");
  534. break;
  535. case 5:
  536. cashRecord.setPayType("Stripe");
  537. cashRecord.setReceivedMarket("13");
  538. cashRecord.setPayload("Stripe");
  539. break;
  540. case 6:
  541. cashRecord.setPayType("PaymentAsia");
  542. cashRecord.setReceivedMarket("13");
  543. cashRecord.setPayload("PaymentAsia");
  544. break;
  545. case 7:
  546. cashRecord.setPayType("Ipay88");
  547. cashRecord.setReceivedMarket("5");
  548. cashRecord.setPayload("Ipay88");
  549. break;
  550. case 9:
  551. cashRecord.setPayType("FirstData");
  552. cashRecord.setReceivedMarket("4");
  553. cashRecord.setBankCode(gOrder.getFirstdataIpgTransactionId());
  554. cashRecord.setPayload("FirstData");
  555. break;
  556. case 10:
  557. cashRecord.setPayType("PaySolutions");
  558. cashRecord.setReceivedMarket("24018");
  559. cashRecord.setPayload("PaySolutions");
  560. break;
  561. case 15:
  562. cashRecord.setPayType("Stripe2");
  563. cashRecord.setReceivedMarket("4");
  564. cashRecord.setPayload("Stripe2");
  565. default:
  566. break;
  567. }
  568. }
  569. cashRecord.setFreeGold(0);
  570. cashRecord.setPaymentCurrency("");
  571. cashRecord.setPaymentAmount(BigDecimal.valueOf(0));
  572. //转换时间戳,加上时区偏移
  573. cashRecord.setPayTime(LocalDateTime.ofEpochSecond(gOrder.getSuccessTime(), 0, ZoneOffset.of("+08:00")));
  574. cashRecord.setAuditTime(LocalDateTime.ofEpochSecond(gOrder.getSuccessTime(), 0, ZoneOffset.of("+08:00")));
  575. cashRecord.setStatus(3);
  576. cashRecord.setSubmitterId(99999);
  577. //存入现金库
  578. cashCollectionMapper.add(cashRecord);
  579. cashCollectionMapper.markSynced(gOrder.getId());
  580. }
  581. }
  582. @Override
  583. public CashCollection selectById(CashCollection cashCollection) {
  584. return cashCollectionMapper.selectById(cashCollection.getId());
  585. }
  586. //根据goldcoin订单号查询收款订单
  587. @Override
  588. public CashCollection selectByGoldCoinOrderCode(String orderNo) {
  589. return cashCollectionMapper.selectByGoldCoinOrderCode(orderNo);
  590. }
  591. //多条件查询收款订单列表
  592. @Override
  593. public PageInfo<PerformanceVO> performanceSelect(Integer pageNum, Integer pageSize, PerformanceDTO performanceDTO) {
  594. PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper
  595. List<PerformanceVO> performanceVOs = cashCollectionMapper.performanceSelect(performanceDTO);
  596. return new PageInfo<>(performanceVOs);
  597. }
  598. }