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.

408 lines
19 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. package com.example.demo.serviceImpl.cash;
  2. import com.example.demo.Util.JWTUtil;
  3. import com.example.demo.config.RabbitMQConfig;
  4. import com.example.demo.domain.entity.*;
  5. import com.example.demo.domain.vo.cash.CashCollection;
  6. import com.example.demo.domain.vo.cash.CashCollectionMessage;
  7. import com.example.demo.domain.vo.coin.GoldUser;
  8. import com.example.demo.domain.vo.coin.Messages;
  9. import com.example.demo.domain.vo.coin.Result;
  10. import com.example.demo.mapper.cash.CashCollectionMapper;
  11. import com.example.demo.mapper.coin.MarketMapper;
  12. import com.example.demo.mapper.coin.UserMapper;
  13. import com.example.demo.service.cash.CashCollectionService;
  14. import com.github.pagehelper.PageHelper;
  15. import com.github.pagehelper.PageInfo;
  16. import jakarta.servlet.http.HttpServletRequest;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.CollectionUtils;
  23. import org.springframework.web.context.request.RequestContextHolder;
  24. import org.springframework.web.context.request.ServletRequestAttributes;
  25. import java.math.BigDecimal;
  26. import java.time.LocalDateTime;
  27. import java.time.ZoneOffset;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. import java.util.UUID;
  31. /**
  32. * @program: gold-java
  33. * @ClassName cashCollectionServiceImpl
  34. * @description: 处理收款相关业务逻辑
  35. * @author: Ethan
  36. * @create: 202509-26 11:23
  37. * @Version 1.0
  38. **/
  39. @Service
  40. @Slf4j
  41. public class CashCollectionServiceImpl implements CashCollectionService {
  42. @Autowired
  43. private CashCollectionMapper cashCollectionMapper;
  44. @Autowired
  45. private UserMapper userMapper;
  46. @Autowired
  47. private MarketMapper marketMapper;
  48. @Autowired
  49. private RabbitTemplate rabbitTemplate;
  50. //新增收款订单
  51. @Override
  52. public String add(CashCollection cashCollection) {
  53. if (cashCollection.getJwcode() == null) {
  54. throw new IllegalArgumentException("精网号不能为空");
  55. }
  56. if (cashCollection.getJwcode() < 10000000 || cashCollection.getJwcode() > 99999999) {
  57. throw new IllegalArgumentException("精网号必须为8位");
  58. }
  59. if (cashCollection.getName() == null || cashCollection.getName().isEmpty()){
  60. throw new IllegalArgumentException("客户姓名不能为空");
  61. }
  62. if (cashCollection.getActivity() == null || cashCollection.getActivity().isEmpty()) {
  63. throw new IllegalArgumentException("活动不能为空");
  64. }
  65. if (cashCollection.getGoodsName() == null|| cashCollection.getGoodsName().isEmpty()) {
  66. throw new IllegalArgumentException("商品名不能为空");
  67. }
  68. if (cashCollection.getGoodsName().equals("金币充值")) {
  69. if (cashCollection.getPermanentGold() == 0 && cashCollection.getFreeGold() == 0) {
  70. throw new IllegalArgumentException("金币数量不能为空");
  71. }
  72. }
  73. if (!cashCollection.getGoodsName().equals("金币充值")) {
  74. if (cashCollection.getGoodNum() == 0) {
  75. throw new IllegalArgumentException("产品数量不能为空");
  76. }
  77. if (cashCollection.getNumUnit() == null|| cashCollection.getNumUnit().isEmpty()) {
  78. throw new IllegalArgumentException("数量单位不能为空");
  79. }
  80. }
  81. if (cashCollection.getPaymentCurrency() == null || cashCollection.getPaymentCurrency().isEmpty()) {
  82. throw new IllegalArgumentException("支付币种不能为空");
  83. }
  84. if (cashCollection.getPaymentAmount() == null || cashCollection.getPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
  85. throw new IllegalArgumentException("支付金额不能为空");
  86. }
  87. if (cashCollection.getPayType() == null|| cashCollection.getPayType().isEmpty()) {
  88. throw new IllegalArgumentException("支付方式不能为空");
  89. }
  90. if (cashCollection.getReceivedMarket() == null||cashCollection.getReceivedMarket().isEmpty()) {
  91. throw new IllegalArgumentException("到账地区不能为空");
  92. }
  93. if (cashCollection.getPayTime() == null) {
  94. throw new IllegalArgumentException("付款时间不能为空");
  95. }
  96. //生成订单号后半部分
  97. String orderNumber = UUID.randomUUID().toString().replaceAll("-", "");
  98. CashRecord cashRecord = new CashRecord();
  99. //构建订单信息
  100. cashRecord.setOrderCode("XJ_" + orderNumber); //订单号
  101. cashRecord.setJwcode(cashCollection.getJwcode()); //精网号
  102. cashRecord.setName(cashCollection.getName()); //客户姓名
  103. cashRecord.setActivity(cashCollection.getActivity()); // 活动
  104. cashRecord.setGoodsName(cashCollection.getGoodsName()); //商品名称
  105. cashRecord.setGoodNum(cashCollection.getGoodNum()); //商品数量
  106. cashRecord.setNumUnit(cashCollection.getNumUnit()); //数量单位
  107. cashRecord.setPermanentGold(cashCollection.getPermanentGold()); //永久金币
  108. cashRecord.setFreeGold(cashCollection.getFreeGold()); //免费金币
  109. cashRecord.setPaymentCurrency(cashCollection.getPaymentCurrency()); //付款币种
  110. cashRecord.setPaymentAmount(cashCollection.getPaymentAmount()); //付款金额
  111. cashRecord.setReceivedMarket(cashCollection.getReceivedMarket()); //到账地区
  112. cashRecord.setPayType(cashCollection.getPayType()); //支付方式
  113. cashRecord.setPayTime(cashCollection.getPayTime()); //付款时间
  114. cashRecord.setVoucher(cashCollection.getVoucher()); //转账凭证
  115. cashRecord.setRemark(cashCollection.getRemark()); //备注
  116. cashRecord.setStatus(0); //订单状态:付款线下财务待审核
  117. cashRecord.setSubmitterId(cashCollection.getSubmitterId()); //提交人ID
  118. cashRecord.setSubmitterMarket(cashCollection.getSubmitterMarket());
  119. cashRecord.setOrderType(1); //订单类型:1-收款
  120. cashRecord.setMarket(cashCollection.getMarket());
  121. //地区,根据jwcode插入
  122. //cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(cashRecord.getJwcode()));
  123. //插入新收款订单
  124. cashCollectionMapper.add(cashRecord);
  125. // 发送收款创建消息
  126. Messages message = new Messages();
  127. message.setJwcode(cashRecord.getJwcode());
  128. message.setName(cashRecord.getName());
  129. message.setStatus(cashRecord.getStatus());
  130. message.setDesc(cashRecord.getJwcode()+"用户的现金收款申请待审核,请前往审核");
  131. message.setTitle("现金收款--新增收款");
  132. message.setType(1);
  133. message.setTypeId(cashRecord.getId());
  134. message.setMarket(Integer.valueOf(cashRecord.getMarket()));
  135. rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_COLLECTION_EXCHANGE, "cash.collection.save", message);
  136. return "添加成功";
  137. }
  138. //撤回未审核的订单
  139. @Override
  140. public String cancel(String orderCode) {
  141. CashRecord cashRecord = cashCollectionMapper.selectByOrderCode(orderCode);
  142. if (cashRecord == null) {
  143. throw new IllegalArgumentException("订单不存在");
  144. }
  145. if (cashRecord.getStatus() != 0) {
  146. throw new IllegalArgumentException("订单状态不符合条件");
  147. }
  148. //修改订单状态
  149. int rows = cashCollectionMapper.updateStatus(orderCode, 5);
  150. return rows > 0 ? "撤回成功" : "撤回失败";
  151. }
  152. //编辑并重新提交收款订单
  153. @Override
  154. public String reSubmit(CashRecord cashRecord) {
  155. if (cashRecord.getJwcode() == null) {
  156. throw new IllegalArgumentException("精网号不能为空");
  157. }
  158. if (cashRecord.getJwcode() < 10000000 || cashRecord.getJwcode() > 99999999) {
  159. throw new IllegalArgumentException("精网号必须为8位");
  160. }
  161. if (cashRecord.getName() == null) {
  162. throw new IllegalArgumentException("客户姓名不能为空");
  163. }
  164. if (cashRecord.getActivity() == null) {
  165. throw new IllegalArgumentException("活动不能为空");
  166. }
  167. if (cashRecord.getGoodsName() == null) {
  168. throw new IllegalArgumentException("商品名不能为空");
  169. }
  170. if (cashRecord.getGoodsName().equals("金币充值")) {
  171. if (cashRecord.getPermanentGold() == 0 && cashRecord.getFreeGold() == 0) {
  172. throw new IllegalArgumentException("金币数量不能为空");
  173. }
  174. }
  175. if (!cashRecord.getGoodsName().equals("金币充值")) {
  176. if (cashRecord.getGoodNum() == 0) {
  177. throw new IllegalArgumentException("产品数量不能为空");
  178. }
  179. if (cashRecord.getNumUnit() == null) {
  180. throw new IllegalArgumentException("数量单位不能为空");
  181. }
  182. }
  183. if (cashRecord.getPaymentCurrency() == null) {
  184. throw new IllegalArgumentException("支付币种不能为空");
  185. }
  186. if (cashRecord.getPaymentAmount() == null || cashRecord.getPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
  187. throw new IllegalArgumentException("支付金额不能为空");
  188. }
  189. if (cashRecord.getPayType() == null) {
  190. throw new IllegalArgumentException("支付方式不能为空");
  191. }
  192. if (cashRecord.getReceivedMarket() == null) {
  193. throw new IllegalArgumentException("到账地区不能为空");
  194. }
  195. if (cashRecord.getPayTime() == null) {
  196. throw new IllegalArgumentException("付款时间不能为空");
  197. }
  198. CashRecord status = cashCollectionMapper.selectByOrderCode(cashRecord.getOrderCode());
  199. if (!status.getStatus().equals(5)) {
  200. throw new IllegalArgumentException("只允许编辑已撤回订单");
  201. }
  202. //地区,根据jwcode插入(弃用,插入前调用接口获取地区和姓名,之后前端传入)
  203. //cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(cashRecord.getJwcode()));
  204. int rows = cashCollectionMapper.updateByOrderCode(cashRecord);
  205. if (rows > 0) {
  206. // 发送重新提交消息
  207. CashCollectionMessage message = new CashCollectionMessage();
  208. message.setId(cashRecord.getId());
  209. message.setOrderCode(cashRecord.getOrderCode());
  210. message.setStatus(0); // 重新提交后状态变为待审核
  211. message.setStatusDescription("线下财务待审核");
  212. message.setMessage("收款订单已重新提交");
  213. message.setSubmitterId(cashRecord.getSubmitterId());
  214. message.setTimestamp(LocalDateTime.now());
  215. rabbitTemplate.convertAndSend(
  216. RabbitMQConfig.CASH_COLLECTION_EXCHANGE,
  217. "collection.created",
  218. message
  219. );
  220. }
  221. return rows > 0 ? "重新提交成功" : "重新提交失败";
  222. }
  223. //多条件查询收款订单列表
  224. @Override
  225. public PageInfo<CashCollection> selectCollection(Integer pageNum, Integer pageSize, CashCollection cashCollection) {
  226. /* //将操作人的地区列表改为id
  227. List<String> markets = marketMapper.getMarketIds(cashCollection.getMarkets());
  228. if (markets.contains("9") || markets.contains("9999")) {
  229. markets = null;
  230. }*/
  231. // cashCollection.setReceivedMarket(marketMapper.getMarketId(cashCollection.getReceivedMarket()));
  232. if (cashCollection.getCashRoleId() == 2) {
  233. //角色是总部时,若不特地传状态,传1346,sql处理为(1,3,4,6)筛选,
  234. if (cashCollection.getStatus() == null) {
  235. cashCollection.setStatus(1346);
  236. }
  237. cashCollection.setSubmitterId(null);
  238. cashCollection.setReceivedMarket(null);
  239. cashCollection.setSubmitterMarket(null);
  240. }
  241. if (cashCollection.getCashRoleId() == 1) {
  242. //角色是地方财务,提交人置空不设筛选条件,仅按收款地区、提交人地区筛选()
  243. if (cashCollection.getStatus() == null) {
  244. cashCollection.setStatus(123460);
  245. }
  246. //状态为待审核和已驳回时按照提交人地区筛选
  247. if (cashCollection.getStatus() == 0 || cashCollection.getStatus() == 2) {
  248. cashCollection.setReceivedMarket(null);
  249. }
  250. //状态为已通过和Link通过时,满足收款地区或提交人地区即可
  251. /* if (cashCollection.getStatus() == 13) {
  252. cashCollection.setSubmitterId(null);
  253. }*/
  254. //状态为13 或46,已通过或已完成和已退款,满足收款地区或提交人地区即可,
  255. cashCollection.setSubmitterId(null);
  256. }
  257. if (cashCollection.getCashRoleId() == 0) {
  258. //角色是地方财务,提交人置空不设筛选条件---仅当角色是0 地方客服时,按提交人筛选
  259. if (cashCollection.getStatus() == null) {
  260. cashCollection.setStatus(1234560);
  261. }
  262. cashCollection.setSubmitterId(cashCollection.getSubmitterId());
  263. cashCollection.setReceivedMarket(null);
  264. }
  265. // cashCollection.setMarkets(markets);
  266. PageHelper.startPage(pageNum, pageSize);
  267. List<CashCollection> cashCollections = cashCollectionMapper.selectCollection1(pageNum, pageSize, cashCollection);
  268. return new PageInfo<>(cashCollections);
  269. }
  270. //补全手续费等内容
  271. @Override
  272. public String complete(CashRecord cashRecord) {
  273. int rows = cashCollectionMapper.complete(cashRecord);
  274. return rows > 0 ? "编辑成功" : "编辑失败";
  275. }
  276. //根据精网号查询姓名和地区
  277. @Override
  278. public User getNameAndMarket(Integer jwcode) {
  279. try {
  280. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  281. String token = request.getHeader("token");
  282. Admin admin = (Admin) JWTUtil.getUserDetailsList(String.valueOf(token), Admin.class);
  283. if (admin != null) {
  284. List<String> list = Arrays.asList(admin.getMarkets().split(","));
  285. List<String> markets = marketMapper.getMarketIds(list);
  286. if (markets.contains("9") || markets.contains("9999")) {
  287. markets = null;
  288. }
  289. GoldUser gUser = userMapper.selectUserCard(jwcode.toString(), markets);
  290. if (gUser != null) {
  291. User user = new User();
  292. user.setMarket(cashCollectionMapper.getMarketByJwcode(jwcode));
  293. user.setName(cashCollectionMapper.getNameByJwcode(jwcode));
  294. user.setMarketName(cashCollectionMapper.getMarketNameByJwcode(jwcode));
  295. return user;
  296. }
  297. }
  298. } catch (Exception e) {
  299. e.printStackTrace();
  300. }
  301. // 如果没有返回有效用户信息,则抛出异常
  302. throw new RuntimeException("无法获取用户信息");
  303. }
  304. //获取收款活动列表
  305. @Override
  306. public List<RechargeActivity> getActivityList() {
  307. return cashCollectionMapper.getActivityList();
  308. }
  309. //同步g_order订单到cash_record表
  310. @Override
  311. @Transactional(rollbackFor = Exception.class)
  312. public Object syncToCashRecord() {
  313. while (true) {
  314. List<GOrder> gOrders = cashCollectionMapper.getUnSync(100);
  315. if (CollectionUtils.isEmpty(gOrders)) {
  316. break;
  317. }
  318. for (GOrder gOrder : gOrders) {
  319. CashRecord cashRecord = new CashRecord();
  320. cashRecord.setOrderType(1);
  321. cashRecord.setJwcode(gOrder.getJwcode());
  322. cashRecord.setName(cashCollectionMapper.getNameByJwcode(gOrder.getJwcode()));
  323. cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(gOrder.getJwcode()));
  324. cashRecord.setActivity("Link日常充值");
  325. cashRecord.setOrderCode(gOrder.getOrderNo());
  326. if (gOrder != null) {
  327. switch (gOrder.getPayStyle()) {
  328. case 3:
  329. cashRecord.setPayType("IOS内购");
  330. cashRecord.setBankCode(gOrder.getIosTransactionId());
  331. cashRecord.setReceivedMarket("3");
  332. break;
  333. case 5:
  334. cashRecord.setPayType("Stripe-链接收款");
  335. cashRecord.setReceivedMarket("13");
  336. break;
  337. case 6:
  338. cashRecord.setPayType("PaymentAsia-链接收款");
  339. cashRecord.setReceivedMarket("13");
  340. break;
  341. case 7:
  342. cashRecord.setPayType("Ipay88-链接收款");
  343. cashRecord.setReceivedMarket("5");
  344. break;
  345. case 9:
  346. cashRecord.setPayType("FistData");
  347. cashRecord.setReceivedMarket("4");
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. cashRecord.setGoodsName("Link充值金币");
  354. cashRecord.setGoodNum(0);
  355. cashRecord.setPermanentGold(gOrder.getCount());
  356. cashRecord.setFreeGold(0);
  357. cashRecord.setPaymentCurrency("");
  358. cashRecord.setPaymentAmount(BigDecimal.valueOf(0));
  359. cashRecord.setPayTime(LocalDateTime.ofEpochSecond(gOrder.getSuccessTime(), 0, ZoneOffset.UTC));
  360. cashRecord.setStatus(3);
  361. cashRecord.setSubmitterId(99999);
  362. cashRecord.setRemark("Link充值金币");
  363. //存入现金库
  364. cashCollectionMapper.add(cashRecord);
  365. cashCollectionMapper.markSynced(gOrder.getId());
  366. }
  367. log.info("同步完成一批,数量 {}", gOrders.size());
  368. if (gOrders.size() < 100) {
  369. break; // 最后一批
  370. }
  371. }
  372. return "同步完毕";
  373. }
  374. @Override
  375. public CashCollection selectById(CashCollection cashCollection) {
  376. return cashCollectionMapper.selectById(cashCollection.getId());
  377. }
  378. //根据goldcoin订单号查询收款订单
  379. @Override
  380. public CashCollection selectByGoldCoinOrderCode(String orderNo) {
  381. return cashCollectionMapper.selectByGoldCoinOrderCode(orderNo);
  382. }
  383. }