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.

953 lines
44 KiB

6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
5 months ago
6 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
  1. package com.example.demo.serviceImpl.cash;
  2. import com.example.demo.Util.LanguageTranslationUtil;
  3. import com.example.demo.Util.SimpleIdGenerator;
  4. import com.example.demo.domain.entity.*;
  5. import com.example.demo.Util.BusinessException;
  6. import com.example.demo.Util.GoldTistV2;
  7. import com.example.demo.config.RabbitMQConfig;
  8. import com.example.demo.domain.vo.bean.Region;
  9. import com.example.demo.domain.vo.cash.*;
  10. import com.example.demo.domain.vo.coin.Messages;
  11. import com.example.demo.domain.vo.coin.Result;
  12. import com.example.demo.exception.SystemException;
  13. import com.example.demo.mapper.cash.CashCollectionMapper;
  14. import com.example.demo.mapper.cash.CashRefundMapper;
  15. import com.example.demo.mapper.coin.*;
  16. import com.example.demo.service.Wallet.WalletService;
  17. import com.example.demo.service.cash.RefundService;
  18. import com.example.demo.service.coin.TranslationService;
  19. import com.github.pagehelper.PageHelper;
  20. import com.github.pagehelper.PageInfo;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import org.springframework.web.bind.annotation.RequestHeader;
  27. import com.example.demo.domain.DTO.Currency;
  28. import java.math.BigDecimal;
  29. import java.math.RoundingMode;
  30. import java.time.LocalDate;
  31. import java.util.*;
  32. import java.util.function.Function;
  33. import java.util.stream.Collectors;
  34. import static org.apache.commons.lang3.StringUtils.substring;
  35. /**
  36. * @program: GOLD
  37. * @ClassName CashRefundServiceImpl
  38. * @description:
  39. * @author: huangqizhen
  40. * @create: 202509-28 15:02
  41. * @Version 1.0
  42. **/
  43. @Service
  44. @Slf4j
  45. public class CashRefundServiceImpl implements RefundService {
  46. @Autowired
  47. private CashRefundMapper cashRefundMapper;
  48. @Autowired
  49. private RefundMapper refundMapper;
  50. @Autowired
  51. private AuditMapper auditMapper;
  52. @Autowired
  53. private MarketMapper marketMapper;
  54. @Autowired
  55. private RabbitTemplate rabbitTemplate;
  56. @Autowired
  57. private OperationLogMapper operationLogMapper;
  58. @Autowired
  59. private CashCollectionMapper cashCollectionMapper;
  60. @Autowired
  61. private LanguageTranslationUtil languageTranslationUtil;
  62. @Autowired
  63. private WalletService walletService;
  64. @Autowired
  65. private TranslationService translationService;
  66. @Autowired
  67. private WalletMapper walletMapper;
  68. @Override
  69. public PageInfo<CashRecordDTO> select(Integer pageNum, Integer pageSize, CashRecordDTO cashRecordDTO) {
  70. PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper
  71. List<CashRecordDTO> list = cashRefundMapper.select(cashRecordDTO);
  72. if (list.isEmpty()) {
  73. return new PageInfo<>(list);
  74. }
  75. // 批量收集ID
  76. Set<Integer> relatedIds = new HashSet<>();
  77. Set<Integer> marketIds = new HashSet<>();
  78. Set<Integer> submitterIds = new HashSet<>();
  79. Set<Integer> auditIds = new HashSet<>();
  80. Set<Integer> executorIds = new HashSet<>();
  81. list.forEach(item -> {
  82. if (item.getRelatedId() != null) relatedIds.add(item.getRelatedId());
  83. if (item.getMarket() != null) marketIds.add(item.getMarket());
  84. if (item.getSubmitterId() != null) submitterIds.add(item.getSubmitterId());
  85. if (item.getAuditId() != null) auditIds.add(item.getAuditId());
  86. if (item.getExecutor() != null) executorIds.add(item.getExecutor());
  87. });
  88. // 批量查询
  89. Map<Integer, CashCollection> cashCollectionMap = cashCollectionMapper.selectBatchIds(relatedIds)
  90. .stream().collect(Collectors.toMap(CashCollection::getId, Function.identity()));
  91. Map<Integer, String> marketNameMap = marketMapper.getMarketByIds(marketIds)
  92. .stream().collect(Collectors.toMap(Market::getId, Market::getName));
  93. Map<Integer, String> submitterNameMap = auditMapper.getNamesByIds(submitterIds)
  94. .stream().collect(Collectors.toMap(Admin::getId, Admin::getAdminName));
  95. Map<Integer, LhlAudit> auditMap = cashRefundMapper.getAuditBatch(auditIds)
  96. .stream().collect(Collectors.toMap(LhlAudit::getId, Function.identity()));
  97. Map<String, String> executorNameMap = auditMapper.getNamesByJwcodes(executorIds)
  98. .stream().collect(Collectors.toMap(Admin::getAccount, Admin::getAdminName));
  99. // 处理数据
  100. list.forEach(item -> {
  101. CashCollection cashCollection = cashCollectionMap.get(item.getRelatedId());
  102. if (cashCollection != null) {
  103. processCashCollection(item, cashCollection);
  104. }
  105. String marketName = marketNameMap.get(item.getMarket());
  106. String submitter = submitterNameMap.get(item.getSubmitterId());
  107. LhlAudit lhlAudit = auditMap.get(item.getAuditId());
  108. String executorName = executorNameMap.get(String.valueOf(item.getExecutor()));
  109. item.setMarketName(marketName != null ? marketName : "");
  110. item.setSubmitter(submitter != null ? submitter : "");
  111. item.setExecutorName(executorName != null ? executorName : "");
  112. if (lhlAudit != null) {
  113. item.setAreaServise(lhlAudit.getAreaServise());
  114. item.setAreaFinance(lhlAudit.getAreaFinance());
  115. item.setAreaCharge(lhlAudit.getAreaCharge());
  116. item.setHeadFinance(lhlAudit.getHeadFinance());
  117. }
  118. });
  119. return new PageInfo<>(list);
  120. }
  121. private void processCashCollection(CashRecordDTO item, CashCollection cashCollection) {
  122. // 设置默认值
  123. Integer freeGold = cashCollection.getFreeGold() != null ? cashCollection.getFreeGold() : 0;
  124. Integer permanentGold = cashCollection.getPermanentGold() != null ? cashCollection.getPermanentGold() : 0;
  125. BigDecimal free = new BigDecimal(freeGold).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
  126. BigDecimal permanent = new BigDecimal(permanentGold).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
  127. item.setGold(permanent);
  128. item.setFree(free);
  129. item.setActivity(cashCollection.getActivity());
  130. item.setPaymentCurrency(cashCollection.getPaymentCurrency());
  131. if (cashCollection.getPaymentCurrency() != null) {
  132. item.setPaymentAmount(cashCollection.getPaymentAmount().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
  133. } else item.setPaymentAmount(cashCollection.getPaymentAmount());
  134. item.setReceivedCurrency(cashCollection.getReceivedCurrency());
  135. if (cashCollection.getReceivedCurrency() != null) {
  136. item.setReceivedAmount(cashCollection.getReceivedAmount().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
  137. } else item.setReceivedAmount(cashCollection.getReceivedAmount());
  138. item.setPayType(cashCollection.getPayType());
  139. item.setPayTime(cashCollection.getPayTime());
  140. item.setPayBankCode(cashCollection.getBankCode());
  141. item.setPaySubmitter(cashCollection.getSubmitterName());
  142. item.setAudit(cashCollection.getAuditName());
  143. item.setReceivedTime(cashCollection.getReceivedTime());
  144. item.setPayVoucher(cashCollection.getVoucher());
  145. item.setPayRemark(cashCollection.getRemark());
  146. item.setHandlingCharge(cashCollection.getHandlingCharge().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
  147. // 处理金币金额
  148. if (item.getPermanentGold() != null) {
  149. item.setPermanentGold(item.getPermanentGold().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
  150. }
  151. if (item.getFreeGold() != null) {
  152. item.setFreeGold(item.getFreeGold().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
  153. }
  154. }
  155. @Override
  156. public int add(CashRecordRefund cashRecordRefund, @RequestHeader(defaultValue = "zh_CN") String lang) throws Exception {
  157. try {
  158. if (cashRecordRefund.getJwcode() == null) {
  159. throw new Exception("未输入精网号");
  160. }
  161. if (cashRecordRefund.getRefundModel() == null) {
  162. throw new Exception("请填充退款类型");
  163. }
  164. if (cashRecordRefund.getRefundReason() == null) {
  165. throw new Exception("请填写退款理由");
  166. }
  167. if (cashRecordRefund.getHandlingCharge() == null) {
  168. throw new Exception("请先填写手续费");
  169. }
  170. if (cashRecordRefund.getMarket() == null || cashRecordRefund.getMarket().trim().isEmpty()) {
  171. throw new Exception("请选择所属地区");
  172. }
  173. String payType = cashRecordRefund.getPayType();
  174. Integer wallet = null;
  175. if (payType == null || payType.trim().isEmpty()) {
  176. throw new SystemException("未穿输支付方式");
  177. }
  178. if (payType.equals("Stripe")||payType.equals("PaymentAsia"))
  179. {
  180. wallet = 2;
  181. }
  182. if (payType.equals("FirstData")||payType.equals("Grabpay")||payType.equals("Nets")||payType.equals("PayPal")||payType.equals("IOS"))
  183. {
  184. wallet = 5;
  185. }
  186. if (payType.equals("Stripe2"))
  187. {
  188. wallet = 3;
  189. }
  190. if (payType.equals("iPay88"))
  191. {
  192. wallet = 4;
  193. }
  194. if (payType.equals("E-Transfer"))
  195. {
  196. wallet = 6;
  197. }
  198. if (payType.equals("paysolution"))
  199. {
  200. wallet = 8;
  201. }
  202. UserRegionWallet userRegionWallet = walletMapper.selectWallet(cashRecordRefund.getJwcode(), wallet);
  203. if (userRegionWallet == null) {
  204. //初始化钱包
  205. walletMapper.insert(new UserRegionWallet(null, cashRecordRefund.getJwcode(), wallet, BigDecimal.ZERO, new Date(), new Date()));
  206. log.warn("用户钱包不存在,已初始化钱包");
  207. }
  208. if (userRegionWallet.getCurrentPermanentGold().compareTo(BigDecimal.valueOf(cashRecordRefund.getPermanentGold())) < 0) {
  209. throw new BusinessException("用户钱包金币不足");
  210. }
  211. CashRecordDone cashRecordDonetwo = new CashRecordDone();
  212. cashRecordDonetwo.setAreaServise(cashRecordRefund.getAreaServise());
  213. cashRefundMapper.addAudit(cashRecordDonetwo);
  214. cashRecordRefund.setAuditId(cashRecordDonetwo.getId());
  215. cashRecordRefund.setStatus(10);
  216. //生成订单号后半部分
  217. String orderNumber = cashRecordRefund.getOrderCode();
  218. //构建订单信息
  219. cashRecordRefund.setOrderCode("TK" + orderNumber); //订单号
  220. // 查询市场 ID,增加空值检查和多语言支持
  221. String marketName = cashRecordRefund.getMarket();
  222. String marketId = marketMapper.getMarketId(marketName);
  223. // 如果直接查询失败,尝试将英文名称转换为中文后再次查询
  224. if (marketId == null || marketId.trim().isEmpty()) {
  225. String chineseMarketName = translationService.findChineseSimplifiedByTranslation(marketName, "en");
  226. if (chineseMarketName != null && !chineseMarketName.equals(marketName)) {
  227. marketId = marketMapper.getMarketId(chineseMarketName);
  228. }
  229. }
  230. if (marketId == null || marketId.trim().isEmpty()) {
  231. throw new Exception("无效的所属地区:" + cashRecordRefund.getMarket());
  232. }
  233. cashRecordRefund.setMarket(String.valueOf(Integer.valueOf(marketId)));
  234. cashRefundMapper.insert(cashRecordRefund);
  235. CashRecordDone cashRecordDone1 = new CashRecordDone();
  236. cashRecordDone1.setId(cashRecordRefund.getOriginalOrderId());
  237. cashRecordDone1.setStatus(6);
  238. if (cashRecordDone1.getId() != null || cashRecordDone1.getOrderCode() != null) {
  239. cashRefundMapper.updateStatus(cashRecordDone1);
  240. } else {
  241. return Result.error("提交失败").getCode();
  242. }
  243. // 发送退款创建消息
  244. Messages message = new Messages();
  245. message.setJwcode(cashRecordRefund.getJwcode());
  246. message.setName(cashRecordRefund.getName());
  247. message.setStatus(cashRecordRefund.getStatus());
  248. message.setDesc("的客服退款申请待审核,前往处理");
  249. message.setTitle("现金退款--新增退款");
  250. message.setType(0);
  251. message.setTypeId(cashRecordRefund.getId());
  252. message.setMarket(Integer.valueOf(cashRecordRefund.getMarket()));
  253. String marketName2 = marketMapper.getMarketNameById(String.valueOf(message.getMarket()));
  254. message.setMarketName(marketName2);
  255. message.setQueryId(103);
  256. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_REFUND_EXCHANGE, "cash.refund.save", message);
  257. return Result.success("提交成功").getCode();
  258. } catch (Exception e) {
  259. log.error("add error", e);
  260. throw e;
  261. }
  262. }
  263. @Override
  264. public int update(CashRecordDone cashRecordDone) throws Exception {
  265. if (cashRecordDone.getJwcode() == null) {
  266. throw new RuntimeException("未输入精网号");
  267. }
  268. if (cashRecordDone.getPaymentAmount() == null) {
  269. throw new RuntimeException("未输入付款金额");
  270. }
  271. if (cashRecordDone.getPaymentCurrency() == null) {
  272. throw new RuntimeException("未输入付款币种");
  273. }
  274. if (cashRecordDone.getRefundModel() == null) {
  275. throw new RuntimeException("请填写退款类型");
  276. }
  277. if (cashRecordDone.getRefundReason() == null) {
  278. throw new RuntimeException("请填写退款理由");
  279. }
  280. if (cashRecordDone.getNewRefundGold() == null) {
  281. cashRecordDone.setNewRefundGold(BigDecimal.valueOf(0));
  282. }
  283. if (cashRecordDone.getNewRefundFree() == null) {
  284. cashRecordDone.setNewRefundFree(BigDecimal.valueOf(0));
  285. }
  286. int result = cashRefundMapper.update(cashRecordDone);
  287. CashRecordDTO cashRecordDTO = cashRefundMapper.selectById(cashRecordDone.getId());
  288. if (result > 0) {
  289. // 发送审核消息
  290. Messages message = new Messages();
  291. message.setJwcode(cashRecordDTO.getJwcode());
  292. message.setName(cashRecordDTO.getName());
  293. message.setStatus(cashRecordDTO.getStatus());
  294. message.setDesc("的退款申请待审核,前往处理");
  295. message.setTitle("现金管理--退款审批");
  296. message.setType(1);
  297. message.setTypeId(cashRecordDTO.getId());
  298. message.setMarket(cashRecordDTO.getMarket());
  299. String marketName = marketMapper.getMarketNameById(String.valueOf(message.getMarket()));
  300. message.setMarketName(marketName);
  301. message.setQueryId(103);
  302. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_REFUND_EXCHANGE, "cash.refund.save", message);
  303. }
  304. return (result > 0 ? Result.success("提交成功") : Result.error("提交失败")).getCode();
  305. }
  306. @Override
  307. public int withdraw(CashRecordDone cashRecordDone) {
  308. // 撤回订单
  309. int result = cashRefundMapper.withdraw(cashRecordDone.getId());
  310. if (result > 0) {
  311. // 更新与该订单关联的消息记录的flag字段为1
  312. cashRefundMapper.updateMessageFlagByOrderId(cashRecordDone.getId());
  313. }
  314. return result;
  315. }
  316. @Override
  317. public int review(CashRecordDone cashRecordDone, @RequestHeader(defaultValue = "zh_CN") String lang) throws Exception {
  318. if (cashRecordDone.getStatus() == 12 || cashRecordDone.getStatus() == 22) {
  319. if (cashRecordDone.getOrderCode() == null) {
  320. throw new RuntimeException("未输入订单号");
  321. }
  322. CashRecordDone cashRecordDone1 = new CashRecordDone();
  323. cashRecordDone1.setId(cashRecordDone.getRelatedId());
  324. cashRecordDone1.setOrderCode(cashRecordDone.getOrderCode().substring(2));
  325. cashRecordDone1.setStatus(4);
  326. if (cashRecordDone1.getId() != null || cashRecordDone1.getOrderCode() != null) {
  327. cashRefundMapper.updateStatus(cashRecordDone1);
  328. }
  329. }
  330. cashRefundMapper.updateAudit(cashRecordDone);
  331. int result = cashRefundMapper.review(cashRecordDone);
  332. CashRecordDTO cashRecordDTO = cashRefundMapper.selectById(cashRecordDone.getId());
  333. if (result > 0) {
  334. // 发送审核消息
  335. Messages message = new Messages();
  336. message.setJwcode(cashRecordDTO.getJwcode());
  337. message.setName(cashRecordDTO.getName());
  338. message.setStatus(cashRecordDTO.getStatus());
  339. if (cashRecordDTO.getStatus()==20) {
  340. message.setDesc("的现金退款申请待审批,前往处理");
  341. message.setTitle("现金管理--退款审批(负责人)");
  342. message.setQueryId(107);
  343. } else if (cashRecordDTO.getStatus()==30) {
  344. message.setDesc("的现金退款申请待审核,前往处理");
  345. message.setTitle("现金管理--退款审批(总部财务)");
  346. message.setQueryId(111);
  347. } else {
  348. message.setDesc("的现金退款申请已被驳回,前往查看详情");
  349. message.setTitle("现金管理--退款提交");
  350. message.setQueryId(99);
  351. }
  352. message.setType(1);
  353. message.setTypeId(cashRecordDTO.getId());
  354. message.setMarket(cashRecordDTO.getMarket());
  355. String marketName = marketMapper.getMarketNameById(String.valueOf(message.getMarket()));
  356. message.setMarketName(marketName);
  357. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_REFUND_EXCHANGE, "cash.refund.save", message);
  358. }
  359. return (result > 0 ? Result.success("提交成功") : Result.error("提交失败")).getCode();
  360. }
  361. @Override
  362. public int executor(CashRecordDone cashRecordDone) throws Exception {
  363. if (cashRecordDone.getRefundVoucher() == null) {
  364. throw new RuntimeException("未输入退款凭证");
  365. }
  366. if (cashRecordDone.getRefundTime() == null) {
  367. throw new RuntimeException("未输入退款时间");
  368. }
  369. if (cashRecordDone.getRefundRemark() == null) {
  370. throw new RuntimeException("未输入退款备注");
  371. }
  372. if (cashRecordDone.getRefundChannels() == null) {
  373. throw new RuntimeException("未输入退款途径");
  374. }
  375. if (cashRecordDone.getRefundCurrency() == null) {
  376. throw new RuntimeException("未输入退款币种");
  377. }
  378. if (cashRecordDone.getRefundAmount() == null) {
  379. throw new RuntimeException("未输入退款金额");
  380. }
  381. int result = cashRefundMapper.executor(cashRecordDone);
  382. return (result > 0 ? Result.success("提交成功") : Result.error("提交失败")).getCode();
  383. }
  384. @Override
  385. public int updateStatus(CashRecordDone cashRecordDone) {
  386. return cashRefundMapper.updateStatus(cashRecordDone);
  387. }
  388. @Transactional(rollbackFor = Exception.class)
  389. @Override
  390. public int finalreview(CashRecordDone cashRecordDone, @RequestHeader(defaultValue = "zh_CN") String lang) {
  391. if (cashRecordDone.getPermanentGold() == null) {
  392. cashRecordDone.setPermanentGold(0);
  393. }
  394. if (cashRecordDone.getFreeGold() == null) {
  395. cashRecordDone.setFreeGold(0);
  396. }
  397. if (cashRecordDone.getStatus() == 32) {
  398. CashRecordDone cashRecordDone1 = new CashRecordDone();
  399. cashRecordDone1.setOrderCode(cashRecordDone.getOrderCode().substring(2));
  400. cashRecordDone1.setStatus(4);
  401. if (cashRecordDone1.getId() != null || cashRecordDone1.getOrderCode() != null) {
  402. cashRefundMapper.updateStatus(cashRecordDone1);
  403. }
  404. }
  405. if (cashRecordDone.getGoodsName() != null && cashRecordDone.getStatus() == 40 &&
  406. (cashRecordDone.getGoodsName().equals(languageTranslationUtil.translate("金币充值", lang)) ||
  407. cashRecordDone.getGoodsName().contains(languageTranslationUtil.translate("金币充值", lang)))) {
  408. UserGoldRecord userGoldRecord = new UserGoldRecord();
  409. userGoldRecord.setOrderCode(cashRecordDone.getOrderCode());
  410. String orderCode = cashRecordDone.getOrderCode();
  411. if (orderCode != null && orderCode.length() > 4 && orderCode.startsWith("TKXJ")) {
  412. orderCode = "XJCZ" + orderCode.substring(4);
  413. }
  414. userGoldRecord.setType((byte) 2);
  415. userGoldRecord.setIsRefund((byte) 1);
  416. userGoldRecord.setRefundType("金币退款");
  417. if (cashRecordDone.getRefundModel() == 1) {
  418. userGoldRecord.setRefundModel(Byte.valueOf("1"));
  419. } else if (cashRecordDone.getRefundModel() == 0) {
  420. userGoldRecord.setRefundModel(Byte.valueOf("0"));
  421. }
  422. userGoldRecord.setJwcode(cashRecordDone.getJwcode());
  423. userGoldRecord.setSumGold(cashRecordDone.getPermanentGold() + cashRecordDone.getFreeGold());
  424. userGoldRecord.setPermanentGold(cashRecordDone.getPermanentGold());
  425. int currentMonth = LocalDate.now().getMonthValue();
  426. if (currentMonth >= 1 && currentMonth <= 6) {
  427. // 1-6月:设置12月额度,6月保持默认值
  428. userGoldRecord.setFreeJune(0);
  429. userGoldRecord.setFreeDecember(cashRecordDone.getFreeGold());
  430. } else {
  431. // 7-12月:设置6月额度,12月保持默认值
  432. userGoldRecord.setFreeJune(cashRecordDone.getFreeGold());
  433. userGoldRecord.setFreeDecember(0);
  434. }
  435. userGoldRecord.setGoodsName("金币充值");
  436. userGoldRecord.setPayPlatform("金币系统");
  437. userGoldRecord.setRemark(cashRecordDone.getRemark());
  438. userGoldRecord.setAdminId(cashRecordDone.getAdminId());
  439. userGoldRecord.setAuditStatus(1);
  440. userGoldRecord.setTaskGold(0);
  441. userGoldRecord.setCreateTime(new Date());
  442. userGoldRecord.setUpdateTime(new Date());
  443. String auditName = auditMapper.getName(cashRecordDone.getAuditId());
  444. refundMapper.add(userGoldRecord);
  445. cashRefundMapper.updategold(orderCode);
  446. User user = new User();
  447. user.setJwcode(userGoldRecord.getJwcode());
  448. user.setCurrentPermanentGold(BigDecimal.valueOf(-userGoldRecord.getPermanentGold())); //当前永久金币
  449. user.setCurrentFreeJune(BigDecimal.valueOf(-userGoldRecord.getFreeJune())); //当前六月免费金币
  450. user.setCurrentFreeDecember(BigDecimal.valueOf(-userGoldRecord.getFreeDecember())); //当前十二月免费金币
  451. auditMapper.updateUserGold(user);
  452. //钱包更新
  453. String orderCodeA = "XJ" + orderCode.substring(4);
  454. List<UserWalletRecord> userWalletList = walletService.selectUserWalletRecord(userGoldRecord.getJwcode(), orderCodeA);
  455. UserRegionWallet userRegionWallet = new UserRegionWallet();
  456. for (UserWalletRecord userWalletRecord : userWalletList){
  457. userRegionWallet.setJwcode(userWalletRecord.getJwcode());
  458. userRegionWallet.setWalletId(userWalletRecord.getWalletId());
  459. userRegionWallet.setCurrentPermanentGold(BigDecimal.valueOf(-(userGoldRecord.getFreeDecember()+userGoldRecord.getFreeJune()+userGoldRecord.getPermanentGold())));
  460. walletService.updateUserGoldRecord(userRegionWallet);
  461. walletService.updateUserWalletRecord(userWalletRecord.getId());
  462. UserWalletRecord userWalletRecord1 = new UserWalletRecord();
  463. userWalletRecord1.setType(2);
  464. userWalletRecord1.setJwcode(userWalletRecord.getJwcode());
  465. userWalletRecord1.setWalletId(userWalletRecord.getWalletId());
  466. userWalletRecord1.setAmount(-userWalletRecord.getAmount());
  467. userWalletRecord1.setOrderCode("TK"+orderCodeA);
  468. userWalletRecord1.setDescription(userWalletRecord.getDescription()+"退款");
  469. walletService.addUserWalletRecord(userWalletRecord1);
  470. }
  471. GoldTistV2.addCoinNew(userGoldRecord.getJwcode().toString(), 58, //退款免费+永久金币-充值
  472. (double) (userGoldRecord.getFreeDecember() + userGoldRecord.getFreeJune() + userGoldRecord.getPermanentGold()) / 100, SimpleIdGenerator.generateId(),
  473. userGoldRecord.getRemark(), (double) userGoldRecord.getPermanentGold() / 100, auditName, "退款金币充值");
  474. }
  475. cashRefundMapper.updateAudit(cashRecordDone);
  476. int result = cashRefundMapper.review(cashRecordDone);
  477. CashRecordDTO cashRecordDTO = cashRefundMapper.selectById(cashRecordDone.getId());
  478. if (result > 0) {
  479. // 发送审核消息
  480. Messages message = new Messages();
  481. message.setJwcode(cashRecordDTO.getJwcode());
  482. message.setName(cashRecordDTO.getName());
  483. message.setStatus(cashRecordDTO.getStatus());
  484. message.setDesc(cashRecordDTO.getStatus() != 32 ? "的退款记录需填写执行明细,前往查看处理" : "的现金退款申请已被驳回,前往查看详情");
  485. message.setTitle(cashRecordDTO.getStatus() != 32 ? "现金管理--执行明细填写":"现金管理--退款提交");
  486. message.setType(1);
  487. message.setTypeId(cashRecordDTO.getId());
  488. message.setMarket(cashRecordDTO.getMarket());
  489. String marketName = marketMapper.getMarketNameById(String.valueOf(message.getMarket()));
  490. message.setMarketName(marketName);
  491. message.setQueryId(cashRecordDTO.getStatus() != 32 ? 115:99);
  492. if (cashRecordDTO.getStatus() != 32) {
  493. log.info("Setting executor to: {}", cashRecordDTO.getExecutor());
  494. message.setExecutor(cashRecordDTO.getExecutor());
  495. log.info("Executor after setting: {}", message.getExecutor());
  496. }
  497. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_REFUND_EXCHANGE, "cash.refund.save", message);
  498. }
  499. return (result > 0 ? Result.success("提交成功") : Result.error("提交失败")).getCode();
  500. }
  501. @Override
  502. public PageInfo<CashRecordDTO> financeSelect(Integer pageNum, Integer pageSize, CashRecordDTO cashRecordDTO) {
  503. PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper
  504. // System.out.println(goldDetail.getMarkets());
  505. List<CashRecordDTO> list = cashRefundMapper.financeSelect(cashRecordDTO);
  506. if (list.isEmpty()) {
  507. return new PageInfo<>(list);
  508. }
  509. // 批量收集ID
  510. Set<Integer> relatedIds = new HashSet<>();
  511. Set<Integer> marketIds = new HashSet<>();
  512. Set<Integer> submitterIds = new HashSet<>();
  513. Set<Integer> auditIds = new HashSet<>();
  514. Set<Integer> executorIds = new HashSet<>();
  515. list.forEach(item -> {
  516. if (item.getRelatedId() != null) relatedIds.add(item.getRelatedId());
  517. if (item.getMarket() != null) marketIds.add(item.getMarket());
  518. if (item.getSubmitterId() != null) submitterIds.add(item.getSubmitterId());
  519. if (item.getAuditId() != null) auditIds.add(item.getAuditId());
  520. if (item.getExecutor() != null) executorIds.add(item.getExecutor());
  521. });
  522. // 批量查询
  523. Map<Integer, CashCollection> cashCollectionMap = cashCollectionMapper.selectBatchIds(relatedIds)
  524. .stream().collect(Collectors.toMap(CashCollection::getId, Function.identity()));
  525. Map<Integer, String> marketNameMap = marketMapper.getMarketByIds(marketIds)
  526. .stream().collect(Collectors.toMap(Market::getId, Market::getName));
  527. Map<Integer, String> submitterNameMap = auditMapper.getNamesByIds(submitterIds)
  528. .stream().collect(Collectors.toMap(Admin::getId, Admin::getAdminName));
  529. Map<Integer, LhlAudit> auditMap = cashRefundMapper.getAuditBatch(auditIds)
  530. .stream().collect(Collectors.toMap(LhlAudit::getId, Function.identity()));
  531. Map<String, String> executorNameMap = auditMapper.getNamesByJwcodes(executorIds)
  532. .stream().collect(Collectors.toMap(Admin::getAccount, Admin::getAdminName));
  533. // 处理数据
  534. list.forEach(item -> {
  535. CashCollection cashCollection = cashCollectionMap.get(item.getRelatedId());
  536. if (cashCollection != null) {
  537. processCashCollection(item, cashCollection);
  538. }
  539. String marketName = marketNameMap.get(item.getMarket());
  540. String submitter = submitterNameMap.get(item.getSubmitterId());
  541. LhlAudit lhlAudit = auditMap.get(item.getAuditId());
  542. String executorName = executorNameMap.get(String.valueOf(item.getExecutor()));
  543. item.setMarketName(marketName != null ? marketName : "");
  544. item.setSubmitter(submitter != null ? submitter : "");
  545. item.setExecutorName(executorName != null ? executorName : "");
  546. if (lhlAudit != null) {
  547. item.setAreaServise(lhlAudit.getAreaServise());
  548. item.setAreaFinance(lhlAudit.getAreaFinance());
  549. item.setAreaCharge(lhlAudit.getAreaCharge());
  550. item.setHeadFinance(lhlAudit.getHeadFinance());
  551. }
  552. });
  553. return new PageInfo<>(list);
  554. }
  555. @Override
  556. public PageInfo<CashRecordDTO> financeSelect2(Integer pageNum, Integer pageSize, CashRecordDTO cashRecordDTO) {
  557. PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper
  558. // System.out.println(goldDetail.getMarkets());
  559. List<CashRecordDTO> list = cashRefundMapper.financeSelect(cashRecordDTO);
  560. if (list.isEmpty()) {
  561. return new PageInfo<>(list);
  562. }
  563. // 批量收集ID
  564. Set<Integer> relatedIds = new HashSet<>();
  565. Set<Integer> marketIds = new HashSet<>();
  566. Set<Integer> submitterIds = new HashSet<>();
  567. Set<Integer> auditIds = new HashSet<>();
  568. Set<Integer> executorIds = new HashSet<>();
  569. list.forEach(item -> {
  570. if (item.getRelatedId() != null) relatedIds.add(item.getRelatedId());
  571. if (item.getMarket() != null) marketIds.add(item.getMarket());
  572. if (item.getSubmitterId() != null) submitterIds.add(item.getSubmitterId());
  573. if (item.getAuditId() != null) auditIds.add(item.getAuditId());
  574. if (item.getExecutor() != null) executorIds.add(item.getExecutor());
  575. });
  576. // 批量查询
  577. Map<Integer, CashCollection> cashCollectionMap = cashCollectionMapper.selectBatchIds(relatedIds)
  578. .stream().collect(Collectors.toMap(CashCollection::getId, Function.identity()));
  579. Map<Integer, String> marketNameMap = marketMapper.getMarketByIds(marketIds)
  580. .stream().collect(Collectors.toMap(Market::getId, Market::getName));
  581. Map<Integer, String> submitterNameMap = auditMapper.getNamesByIds(submitterIds)
  582. .stream().collect(Collectors.toMap(Admin::getId, Admin::getAdminName));
  583. Map<Integer, LhlAudit> auditMap = cashRefundMapper.getAuditBatch(auditIds)
  584. .stream().collect(Collectors.toMap(LhlAudit::getId, Function.identity()));
  585. Map<String, String> executorNameMap = auditMapper.getNamesByJwcodes(executorIds)
  586. .stream().collect(Collectors.toMap(Admin::getAccount, Admin::getAdminName));
  587. // 处理数据
  588. list.forEach(item -> {
  589. CashCollection cashCollection = cashCollectionMap.get(item.getRelatedId());
  590. if (cashCollection != null) {
  591. processCashCollection(item, cashCollection);
  592. }
  593. String marketName = marketNameMap.get(item.getMarket());
  594. String submitter = submitterNameMap.get(item.getSubmitterId());
  595. LhlAudit lhlAudit = auditMap.get(item.getAuditId());
  596. String executorName = executorNameMap.get(String.valueOf(item.getExecutor()));
  597. item.setMarketName(marketName != null ? marketName : "");
  598. item.setSubmitter(submitter != null ? submitter : "");
  599. item.setExecutorName(executorName != null ? executorName : "");
  600. if (lhlAudit != null) {
  601. item.setAreaServise(lhlAudit.getAreaServise());
  602. item.setAreaFinance(lhlAudit.getAreaFinance());
  603. item.setAreaCharge(lhlAudit.getAreaCharge());
  604. item.setHeadFinance(lhlAudit.getHeadFinance());
  605. }
  606. });
  607. return new PageInfo<>(list);
  608. }
  609. @Override
  610. public PageInfo<CashRecordDTO> exSelect(Integer pageNum, Integer pageSize, CashRecordDTO cashRecordDTO) {
  611. PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper
  612. // System.out.println(goldDetail.getMarkets());
  613. List<CashRecordDTO> list = cashRefundMapper.exSelect(cashRecordDTO);
  614. System.out.println(list);
  615. if (list.isEmpty()) {
  616. return new PageInfo<>(list);
  617. }
  618. // 批量收集ID
  619. Set<Integer> relatedIds = new HashSet<>();
  620. Set<Integer> marketIds = new HashSet<>();
  621. Set<Integer> submitterIds = new HashSet<>();
  622. Set<Integer> auditIds = new HashSet<>();
  623. Set<Integer> executorIds = new HashSet<>();
  624. list.forEach(item -> {
  625. if (item.getRelatedId() != null) relatedIds.add(item.getRelatedId());
  626. if (item.getMarket() != null) marketIds.add(item.getMarket());
  627. if (item.getSubmitterId() != null) submitterIds.add(item.getSubmitterId());
  628. if (item.getAuditId() != null) auditIds.add(item.getAuditId());
  629. if (item.getExecutor() != null) executorIds.add(item.getExecutor());
  630. });
  631. // 批量查询
  632. Map<Integer, CashCollection> cashCollectionMap = cashCollectionMapper.selectBatchIds(relatedIds)
  633. .stream().collect(Collectors.toMap(CashCollection::getId, Function.identity()));
  634. Map<Integer, String> marketNameMap = marketMapper.getMarketByIds(marketIds)
  635. .stream().collect(Collectors.toMap(Market::getId, Market::getName));
  636. Map<Integer, String> submitterNameMap = auditMapper.getNamesByIds(submitterIds)
  637. .stream().collect(Collectors.toMap(Admin::getId, Admin::getAdminName));
  638. Map<Integer, LhlAudit> auditMap = cashRefundMapper.getAuditBatch(auditIds)
  639. .stream().collect(Collectors.toMap(LhlAudit::getId, Function.identity()));
  640. Map<String, String> executorNameMap = auditMapper.getNamesByJwcodes(executorIds)
  641. .stream().collect(Collectors.toMap(Admin::getAccount, Admin::getAdminName));
  642. // 处理数据
  643. list.forEach(item -> {
  644. CashCollection cashCollection = cashCollectionMap.get(item.getRelatedId());
  645. if (cashCollection != null) {
  646. processCashCollection(item, cashCollection);
  647. }
  648. String marketName = marketNameMap.get(item.getMarket());
  649. String submitter = submitterNameMap.get(item.getSubmitterId());
  650. LhlAudit lhlAudit = auditMap.get(item.getAuditId());
  651. String executorName = executorNameMap.get(String.valueOf(item.getExecutor()));
  652. item.setMarketName(marketName != null ? marketName : "");
  653. item.setSubmitter(submitter != null ? submitter : "");
  654. item.setExecutorName(executorName != null ? executorName : "");
  655. if (lhlAudit != null) {
  656. item.setAreaServise(lhlAudit.getAreaServise());
  657. item.setAreaFinance(lhlAudit.getAreaFinance());
  658. item.setAreaCharge(lhlAudit.getAreaCharge());
  659. item.setHeadFinance(lhlAudit.getHeadFinance());
  660. }
  661. });
  662. return new PageInfo<>(list);
  663. }
  664. @Override
  665. @Transactional(rollbackFor = Exception.class)
  666. public void addOnline(CashRecordRefund cashRecordRefund,String lang) {
  667. try {
  668. if (cashRecordRefund.getJwcode() == null) {
  669. throw new BusinessException("未输入精网号");
  670. }
  671. if (cashRecordRefund.getRefundModel() == null) {
  672. throw new BusinessException("请填充退款类型");
  673. }
  674. if (cashRecordRefund.getRefundReason() == null) {
  675. throw new BusinessException("请填写退款理由");
  676. }
  677. if (cashRecordRefund.getMarket() == null || cashRecordRefund.getMarket().trim().isEmpty()) {
  678. throw new BusinessException("请选择所属地区");
  679. }
  680. String payType = cashRecordRefund.getPayType();
  681. Integer wallet = null;
  682. if (payType == null || payType.trim().isEmpty()) {
  683. throw new SystemException("未穿输支付方式");
  684. }
  685. if (payType.equals("Stripe")||payType.equals("PaymentAsia"))
  686. {
  687. wallet = 2;
  688. }
  689. if (payType.equals("FirstData")||payType.equals("Grabpay")||payType.equals("Nets")||payType.equals("PayPal")||payType.equals("IOS"))
  690. {
  691. wallet = 5;
  692. }
  693. if (payType.equals("Stripe2"))
  694. {
  695. wallet = 3;
  696. }
  697. if (payType.equals("iPay88"))
  698. {
  699. wallet = 4;
  700. }
  701. if (payType.equals("E-Transfer"))
  702. {
  703. wallet = 6;
  704. }
  705. if (payType.equals("paysolution"))
  706. {
  707. wallet = 8;
  708. }
  709. UserRegionWallet userRegionWallet = walletMapper.selectWallet(cashRecordRefund.getJwcode(), wallet);
  710. if (userRegionWallet == null) {
  711. //初始化钱包
  712. walletMapper.insert(new UserRegionWallet(null, cashRecordRefund.getJwcode(), wallet, BigDecimal.ZERO, new Date(), new Date()));
  713. log.warn("用户钱包不存在,已初始化钱包");
  714. }
  715. if (userRegionWallet.getCurrentPermanentGold().compareTo(BigDecimal.valueOf(cashRecordRefund.getPermanentGold())) < 0) {
  716. throw new BusinessException("用户钱包金币不足");
  717. }
  718. CashRecordDone cashRecordDonetwo = new CashRecordDone();
  719. cashRecordDonetwo.setAreaServise(cashRecordRefund.getAreaServise());
  720. cashRefundMapper.addAudit(cashRecordDonetwo);
  721. cashRecordRefund.setAuditId(cashRecordDonetwo.getId());
  722. cashRecordRefund.setStatus(20);
  723. //生成订单号后半部分
  724. String orderNumber = cashRecordRefund.getOrderCode();
  725. //构建订单信息
  726. cashRecordRefund.setOrderCode("TK" + orderNumber); //订单号
  727. // 查询市场 ID,增加空值检查
  728. String marketName = cashRecordRefund.getMarket();
  729. // 如果传入的是英文名称,需要转换为中文名称再查询
  730. String marketId = marketMapper.getMarketId(marketName);
  731. if (marketId == null || marketId.trim().isEmpty()) {
  732. // 尝试将英文名称转换为中文后再次查询
  733. String chineseMarketName = translationService.findChineseSimplifiedByTranslation(marketName, "en");
  734. if (chineseMarketName != null && !chineseMarketName.equals(marketName)) {
  735. marketId = marketMapper.getMarketId(chineseMarketName);
  736. }
  737. }
  738. if (marketId == null || marketId.trim().isEmpty()) {
  739. throw new BusinessException("无效的所属地区:" + cashRecordRefund.getMarket());
  740. }
  741. cashRecordRefund.setMarket(String.valueOf(Integer.valueOf(marketId)));
  742. cashRefundMapper.insert(cashRecordRefund);
  743. CashRecordDone cashRecordDone1 = new CashRecordDone();
  744. cashRecordDone1.setId(cashRecordRefund.getOriginalOrderId());
  745. cashRecordDone1.setStatus(6);
  746. if (cashRecordDone1.getId() != null || cashRecordDone1.getOrderCode() != null) {
  747. cashRefundMapper.updateStatus(cashRecordDone1);
  748. } else {
  749. throw new SystemException("提交失败");
  750. }
  751. } catch (BusinessException | SystemException e) {
  752. throw e;
  753. } catch (Exception e) {
  754. log.error("addOnline error", e);
  755. throw new SystemException("提交失败:" + e.getMessage());
  756. }
  757. }
  758. @Override
  759. public PageInfo<FundsDTO> funds(Integer pageNum, Integer pageSize, FundsDTO fundsDTO) {
  760. // 1. 分页查询主数据
  761. PageHelper.startPage(pageNum, pageSize);
  762. if (fundsDTO.getStatuses() == null || fundsDTO.getStatuses().isEmpty()) {
  763. fundsDTO.setStatuses(Arrays.asList(4, 6));
  764. }
  765. List<FundsDTO> list = cashRefundMapper.selectfunds(fundsDTO);
  766. // 2. 收集 status == 6 的记录 ID
  767. List<Integer> needQueryIds = new ArrayList<>();
  768. for (FundsDTO dto : list) {
  769. if (dto.getStatus() != null && dto.getStatus() == 6) {
  770. needQueryIds.add(dto.getId());
  771. }
  772. }
  773. // 3. 批量查询 refundDetail 信息(用于负数处理)
  774. if (!needQueryIds.isEmpty()) {
  775. List<FundsDTO> detailList = cashRefundMapper.selectRefundCount(needQueryIds);
  776. Map<Integer, FundsDTO> detailMap = new HashMap<>();
  777. for (FundsDTO detail : detailList) {
  778. // 假设 detail.getRelatedId() 对应主表的 id
  779. detailMap.put(detail.getRelatedId(), detail);
  780. }
  781. // 回填 refundAmount(取负)和 refundCurrency
  782. for (FundsDTO dto : list) {
  783. if (dto.getStatus() != null && dto.getStatus() == 6) {
  784. FundsDTO detail = detailMap.get(dto.getId());
  785. if (detail != null) {
  786. BigDecimal amount = detail.getRefundAmount();
  787. if (amount != null) {
  788. dto.setRefundAmount(amount.negate()); // 转为负数
  789. } else {
  790. dto.setRefundAmount(null); // 或设为 BigDecimal.ZERO
  791. }
  792. dto.setRefundCurrency(detail.getRefundCurrency());
  793. }
  794. }
  795. }
  796. }
  797. // 4. 收集所有需要转换的 regionId 和 currencyId
  798. Set<Integer> regionIds = new HashSet<>();
  799. Set<Integer> currencyIds = new HashSet<>();
  800. Set<Integer> reCurrencyIds = new HashSet<>();
  801. for (FundsDTO dto : list) {
  802. if (dto.getMarket() != null) {
  803. regionIds.add(dto.getMarket());
  804. }
  805. if (dto.getPaymentCurrency() != null) {
  806. currencyIds.add(dto.getPaymentCurrency());
  807. }
  808. if (dto.getReceivedCurrency() != null) {
  809. reCurrencyIds.add(dto.getReceivedCurrency());
  810. }
  811. }
  812. // 5. 批量查询地区字典
  813. Map<Integer, String> regionMap = new HashMap<>();
  814. if (!regionIds.isEmpty()) {
  815. List<Region> regions = refundMapper.selectByIds(new ArrayList<>(regionIds));
  816. for (Region region : regions) {
  817. regionMap.put(region.getId(), region.getName());
  818. }
  819. }
  820. // 6. 批量查询币种字典
  821. Map<Integer, String> currencyMap = new HashMap<>();
  822. if (!currencyIds.isEmpty()) {
  823. List<Currency> currencies = refundMapper.selectByCIds(new ArrayList<>(currencyIds));
  824. for (Currency currency : currencies) {
  825. currencyMap.put(currency.getId(), currency.getName()); // 或 getCode(),按需调整
  826. }
  827. }
  828. Map<Integer, String> reCurrencyMap = new HashMap<>();
  829. if (!reCurrencyIds.isEmpty()) {
  830. List<Currency> reCurrencies = refundMapper.selectByCIds(new ArrayList<>(reCurrencyIds));
  831. for (Currency reCurrency : reCurrencies) {
  832. reCurrencyMap.put(reCurrency.getId(), reCurrency.getName()); // 或 getCode(),按需调整
  833. }
  834. }
  835. // 7. 回填地区名称和币种名称到 DTO
  836. for (FundsDTO dto : list) {
  837. dto.setMarketName(regionMap.get(dto.getMarket()));
  838. dto.setPaymentCurrencyName(currencyMap.get(dto.getPaymentCurrency()));
  839. dto.setReceivedCurrencyName(reCurrencyMap.get(dto.getReceivedCurrency()));
  840. }
  841. // 8. 返回分页结果
  842. return new PageInfo<>(list);
  843. }
  844. }