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

package com.example.demo.serviceImpl.cash;
import com.example.demo.Util.JWTUtil;
import com.example.demo.config.RabbitMQConfig;
import com.example.demo.domain.entity.*;
import com.example.demo.domain.vo.cash.CashCollection;
import com.example.demo.domain.vo.cash.CashCollectionMessage;
import com.example.demo.domain.vo.coin.GoldUser;
import com.example.demo.domain.vo.coin.Messages;
import com.example.demo.domain.vo.coin.Result;
import com.example.demo.mapper.cash.CashCollectionMapper;
import com.example.demo.mapper.coin.MarketMapper;
import com.example.demo.mapper.coin.UserMapper;
import com.example.demo.service.cash.CashCollectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
/**
* @program: gold-java
* @ClassName cashCollectionServiceImpl
* @description: 处理收款相关业务逻辑
* @author: Ethan
* @create: 2025−09-26 11:23
* @Version 1.0
**/
@Service
@Slf4j
public class CashCollectionServiceImpl implements CashCollectionService {
@Autowired
private CashCollectionMapper cashCollectionMapper;
@Autowired
private UserMapper userMapper;
@Autowired
private MarketMapper marketMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
//新增收款订单
@Override
public String add(CashCollection cashCollection) {
if (cashCollection.getJwcode() == null) {
throw new IllegalArgumentException("精网号不能为空");
}
if (cashCollection.getJwcode() < 10000000 || cashCollection.getJwcode() > 99999999) {
throw new IllegalArgumentException("精网号必须为8位");
}
if (cashCollection.getName() == null || cashCollection.getName().isEmpty()){
throw new IllegalArgumentException("客户姓名不能为空");
}
if (cashCollection.getActivity() == null || cashCollection.getActivity().isEmpty()) {
throw new IllegalArgumentException("活动不能为空");
}
if (cashCollection.getGoodsName() == null|| cashCollection.getGoodsName().isEmpty()) {
throw new IllegalArgumentException("商品名不能为空");
}
if (cashCollection.getGoodsName().equals("金币充值")) {
if (cashCollection.getPermanentGold() == 0 && cashCollection.getFreeGold() == 0) {
throw new IllegalArgumentException("金币数量不能为空");
}
}
if (!cashCollection.getGoodsName().equals("金币充值")) {
if (cashCollection.getGoodNum() == 0) {
throw new IllegalArgumentException("产品数量不能为空");
}
if (cashCollection.getNumUnit() == null|| cashCollection.getNumUnit().isEmpty()) {
throw new IllegalArgumentException("数量单位不能为空");
}
}
if (cashCollection.getPaymentCurrency() == null || cashCollection.getPaymentCurrency().isEmpty()) {
throw new IllegalArgumentException("支付币种不能为空");
}
if (cashCollection.getPaymentAmount() == null || cashCollection.getPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
throw new IllegalArgumentException("支付金额不能为空");
}
if (cashCollection.getPayType() == null|| cashCollection.getPayType().isEmpty()) {
throw new IllegalArgumentException("支付方式不能为空");
}
if (cashCollection.getReceivedMarket() == null||cashCollection.getReceivedMarket().isEmpty()) {
throw new IllegalArgumentException("到账地区不能为空");
}
if (cashCollection.getPayTime() == null) {
throw new IllegalArgumentException("付款时间不能为空");
}
//生成订单号后半部分
String orderNumber = UUID.randomUUID().toString().replaceAll("-", "");
CashRecord cashRecord = new CashRecord();
//构建订单信息
cashRecord.setOrderCode("XJ_" + orderNumber); //订单号
cashRecord.setJwcode(cashCollection.getJwcode()); //精网号
cashRecord.setName(cashCollection.getName()); //客户姓名
cashRecord.setActivity(cashCollection.getActivity()); // 活动
cashRecord.setGoodsName(cashCollection.getGoodsName()); //商品名称
cashRecord.setGoodNum(cashCollection.getGoodNum()); //商品数量
cashRecord.setNumUnit(cashCollection.getNumUnit()); //数量单位
cashRecord.setPermanentGold(cashCollection.getPermanentGold()); //永久金币
cashRecord.setFreeGold(cashCollection.getFreeGold()); //免费金币
cashRecord.setPaymentCurrency(cashCollection.getPaymentCurrency()); //付款币种
cashRecord.setPaymentAmount(cashCollection.getPaymentAmount()); //付款金额
cashRecord.setReceivedMarket(cashCollection.getReceivedMarket()); //到账地区
cashRecord.setPayType(cashCollection.getPayType()); //支付方式
cashRecord.setPayTime(cashCollection.getPayTime()); //付款时间
cashRecord.setVoucher(cashCollection.getVoucher()); //转账凭证
cashRecord.setRemark(cashCollection.getRemark()); //备注
cashRecord.setStatus(0); //订单状态:付款线下财务待审核
cashRecord.setSubmitterId(cashCollection.getSubmitterId()); //提交人ID
cashRecord.setSubmitterMarket(cashCollection.getSubmitterMarket());
cashRecord.setOrderType(1); //订单类型:1-收款
cashRecord.setMarket(cashCollection.getMarket());
//地区,根据jwcode插入
//cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(cashRecord.getJwcode()));
//插入新收款订单
cashCollectionMapper.add(cashRecord);
// 发送收款创建消息
Messages message = new Messages();
message.setJwcode(cashRecord.getJwcode());
message.setName(cashRecord.getName());
message.setStatus(cashRecord.getStatus());
message.setDesc(cashRecord.getJwcode()+"用户的现金收款申请待审核,请前往审核");
message.setTitle("现金收款--新增收款");
message.setType(1);
message.setTypeId(cashRecord.getId());
message.setMarket(Integer.valueOf(cashRecord.getMarket()));
rabbitTemplate.convertAndSend(RabbitMQConfig.CASH_COLLECTION_EXCHANGE, "cash.collection.save", message);
return "添加成功";
}
//撤回未审核的订单
@Override
public String cancel(String orderCode) {
CashRecord cashRecord = cashCollectionMapper.selectByOrderCode(orderCode);
if (cashRecord == null) {
throw new IllegalArgumentException("订单不存在");
}
if (cashRecord.getStatus() != 0) {
throw new IllegalArgumentException("订单状态不符合条件");
}
//修改订单状态
int rows = cashCollectionMapper.updateStatus(orderCode, 5);
return rows > 0 ? "撤回成功" : "撤回失败";
}
//编辑并重新提交收款订单
@Override
public String reSubmit(CashRecord cashRecord) {
if (cashRecord.getJwcode() == null) {
throw new IllegalArgumentException("精网号不能为空");
}
if (cashRecord.getJwcode() < 10000000 || cashRecord.getJwcode() > 99999999) {
throw new IllegalArgumentException("精网号必须为8位");
}
if (cashRecord.getName() == null) {
throw new IllegalArgumentException("客户姓名不能为空");
}
if (cashRecord.getActivity() == null) {
throw new IllegalArgumentException("活动不能为空");
}
if (cashRecord.getGoodsName() == null) {
throw new IllegalArgumentException("商品名不能为空");
}
if (cashRecord.getGoodsName().equals("金币充值")) {
if (cashRecord.getPermanentGold() == 0 && cashRecord.getFreeGold() == 0) {
throw new IllegalArgumentException("金币数量不能为空");
}
}
if (!cashRecord.getGoodsName().equals("金币充值")) {
if (cashRecord.getGoodNum() == 0) {
throw new IllegalArgumentException("产品数量不能为空");
}
if (cashRecord.getNumUnit() == null) {
throw new IllegalArgumentException("数量单位不能为空");
}
}
if (cashRecord.getPaymentCurrency() == null) {
throw new IllegalArgumentException("支付币种不能为空");
}
if (cashRecord.getPaymentAmount() == null || cashRecord.getPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
throw new IllegalArgumentException("支付金额不能为空");
}
if (cashRecord.getPayType() == null) {
throw new IllegalArgumentException("支付方式不能为空");
}
if (cashRecord.getReceivedMarket() == null) {
throw new IllegalArgumentException("到账地区不能为空");
}
if (cashRecord.getPayTime() == null) {
throw new IllegalArgumentException("付款时间不能为空");
}
CashRecord status = cashCollectionMapper.selectByOrderCode(cashRecord.getOrderCode());
if (!status.getStatus().equals(5)) {
throw new IllegalArgumentException("只允许编辑已撤回订单");
}
//地区,根据jwcode插入(弃用,插入前调用接口获取地区和姓名,之后前端传入)
//cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(cashRecord.getJwcode()));
int rows = cashCollectionMapper.updateByOrderCode(cashRecord);
if (rows > 0) {
// 发送重新提交消息
CashCollectionMessage message = new CashCollectionMessage();
message.setId(cashRecord.getId());
message.setOrderCode(cashRecord.getOrderCode());
message.setStatus(0); // 重新提交后状态变为待审核
message.setStatusDescription("线下财务待审核");
message.setMessage("收款订单已重新提交");
message.setSubmitterId(cashRecord.getSubmitterId());
message.setTimestamp(LocalDateTime.now());
rabbitTemplate.convertAndSend(
RabbitMQConfig.CASH_COLLECTION_EXCHANGE,
"collection.created",
message
);
}
return rows > 0 ? "重新提交成功" : "重新提交失败";
}
//多条件查询收款订单列表
@Override
public PageInfo<CashCollection> selectCollection(Integer pageNum, Integer pageSize, CashCollection cashCollection) {
/* //将操作人的地区列表改为id
List<String> markets = marketMapper.getMarketIds(cashCollection.getMarkets());
if (markets.contains("9") || markets.contains("9999")) {
markets = null;
}*/
// cashCollection.setReceivedMarket(marketMapper.getMarketId(cashCollection.getReceivedMarket()));
if (cashCollection.getCashRoleId() == 2) {
//角色是总部时,若不特地传状态,传1346,sql处理为(1,3,4,6)筛选,
if (cashCollection.getStatus() == null) {
cashCollection.setStatus(1346);
}
cashCollection.setSubmitterId(null);
cashCollection.setReceivedMarket(null);
cashCollection.setSubmitterMarket(null);
}
if (cashCollection.getCashRoleId() == 1) {
//角色是地方财务,提交人置空不设筛选条件,仅按收款地区、提交人地区筛选()
if (cashCollection.getStatus() == null) {
cashCollection.setStatus(123460);
}
//状态为待审核和已驳回时按照提交人地区筛选
if (cashCollection.getStatus() == 0 || cashCollection.getStatus() == 2) {
cashCollection.setReceivedMarket(null);
}
//状态为已通过和Link通过时,满足收款地区或提交人地区即可
/* if (cashCollection.getStatus() == 13) {
cashCollection.setSubmitterId(null);
}*/
//状态为13 或46,已通过或已完成和已退款,满足收款地区或提交人地区即可,
cashCollection.setSubmitterId(null);
}
if (cashCollection.getCashRoleId() == 0) {
//角色是地方财务,提交人置空不设筛选条件---仅当角色是0 地方客服时,按提交人筛选
if (cashCollection.getStatus() == null) {
cashCollection.setStatus(1234560);
}
cashCollection.setSubmitterId(cashCollection.getSubmitterId());
cashCollection.setReceivedMarket(null);
}
// cashCollection.setMarkets(markets);
PageHelper.startPage(pageNum, pageSize);
List<CashCollection> cashCollections = cashCollectionMapper.selectCollection1(pageNum, pageSize, cashCollection);
return new PageInfo<>(cashCollections);
}
//补全手续费等内容
@Override
public String complete(CashRecord cashRecord) {
int rows = cashCollectionMapper.complete(cashRecord);
return rows > 0 ? "编辑成功" : "编辑失败";
}
//根据精网号查询姓名和地区
@Override
public User getNameAndMarket(Integer jwcode) {
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("token");
Admin admin = (Admin) JWTUtil.getUserDetailsList(String.valueOf(token), Admin.class);
if (admin != null) {
List<String> list = Arrays.asList(admin.getMarkets().split(","));
List<String> markets = marketMapper.getMarketIds(list);
if (markets.contains("9") || markets.contains("9999")) {
markets = null;
}
GoldUser gUser = userMapper.selectUserCard(jwcode.toString(), markets);
if (gUser != null) {
User user = new User();
user.setMarket(cashCollectionMapper.getMarketByJwcode(jwcode));
user.setName(cashCollectionMapper.getNameByJwcode(jwcode));
user.setMarketName(cashCollectionMapper.getMarketNameByJwcode(jwcode));
return user;
}
}
} catch (Exception e) {
e.printStackTrace();
}
// 如果没有返回有效用户信息,则抛出异常
throw new RuntimeException("无法获取用户信息");
}
//获取收款活动列表
@Override
public List<RechargeActivity> getActivityList() {
return cashCollectionMapper.getActivityList();
}
//同步g_order订单到cash_record表
@Override
@Transactional(rollbackFor = Exception.class)
public Object syncToCashRecord() {
while (true) {
List<GOrder> gOrders = cashCollectionMapper.getUnSync(100);
if (CollectionUtils.isEmpty(gOrders)) {
break;
}
for (GOrder gOrder : gOrders) {
CashRecord cashRecord = new CashRecord();
cashRecord.setOrderType(1);
cashRecord.setJwcode(gOrder.getJwcode());
cashRecord.setName(cashCollectionMapper.getNameByJwcode(gOrder.getJwcode()));
cashRecord.setMarket(cashCollectionMapper.getMarketByJwcode(gOrder.getJwcode()));
cashRecord.setActivity("Link日常充值");
cashRecord.setOrderCode(gOrder.getOrderNo());
if (gOrder != null) {
switch (gOrder.getPayStyle()) {
case 3:
cashRecord.setPayType("IOS内购");
cashRecord.setBankCode(gOrder.getIosTransactionId());
cashRecord.setReceivedMarket("3");
break;
case 5:
cashRecord.setPayType("Stripe-链接收款");
cashRecord.setReceivedMarket("13");
break;
case 6:
cashRecord.setPayType("PaymentAsia-链接收款");
cashRecord.setReceivedMarket("13");
break;
case 7:
cashRecord.setPayType("Ipay88-链接收款");
cashRecord.setReceivedMarket("5");
break;
case 9:
cashRecord.setPayType("FistData");
cashRecord.setReceivedMarket("4");
break;
default:
break;
}
}
cashRecord.setGoodsName("Link充值金币");
cashRecord.setGoodNum(0);
cashRecord.setPermanentGold(gOrder.getCount());
cashRecord.setFreeGold(0);
cashRecord.setPaymentCurrency("");
cashRecord.setPaymentAmount(BigDecimal.valueOf(0));
cashRecord.setPayTime(LocalDateTime.ofEpochSecond(gOrder.getSuccessTime(), 0, ZoneOffset.UTC));
cashRecord.setStatus(3);
cashRecord.setSubmitterId(99999);
cashRecord.setRemark("Link充值金币");
//存入现金库
cashCollectionMapper.add(cashRecord);
cashCollectionMapper.markSynced(gOrder.getId());
}
log.info("同步完成一批,数量 {}", gOrders.size());
if (gOrders.size() < 100) {
break; // 最后一批
}
}
return "同步完毕";
}
@Override
public CashCollection selectById(CashCollection cashCollection) {
return cashCollectionMapper.selectById(cashCollection.getId());
}
//根据goldcoin订单号查询收款订单
@Override
public CashCollection selectByGoldCoinOrderCode(String orderNo) {
return cashCollectionMapper.selectByGoldCoinOrderCode(orderNo);
}
}