diff --git a/bweb/src/main/java/com/bweb/config/AuthConfig.java b/bweb/src/main/java/com/bweb/config/AuthConfig.java index 8fafbf9..77272d9 100644 --- a/bweb/src/main/java/com/bweb/config/AuthConfig.java +++ b/bweb/src/main/java/com/bweb/config/AuthConfig.java @@ -94,6 +94,7 @@ public class AuthConfig implements WebMvcConfigurer { .excludePathPatterns("/agentMer/*") .excludePathPatterns("/newLinkNotify/*") .excludePathPatterns("/merchantPayConfig/editConfig") + .excludePathPatterns("/test/*") ; } diff --git a/bweb/src/main/java/com/bweb/controller/BsAgentMerAccountController.java b/bweb/src/main/java/com/bweb/controller/BsAgentMerAccountController.java index 08240bc..6f862dc 100644 --- a/bweb/src/main/java/com/bweb/controller/BsAgentMerAccountController.java +++ b/bweb/src/main/java/com/bweb/controller/BsAgentMerAccountController.java @@ -116,11 +116,12 @@ public class BsAgentMerAccountController { } else if (SecUserObjectTypeEnum.type5.getCode() == userSession.getAccount().getObjectType()) { // 查询代理商工作人员 - BsAgentStaff agentStaff = agentStaffService.getStaffDetailById(userSession.getAccount().getId()); + BsAgentStaff agentStaff = agentStaffService.getStaffDetailById(userSession.getAccount().getObjectId()); if (agentStaff == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, ""); } param.put("agentId", agentStaff.getAgentId()); + param.put("agentStaffId", agentStaff.getId()); } else { throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, ""); } diff --git a/bweb/src/main/java/com/bweb/controller/TestController.java b/bweb/src/main/java/com/bweb/controller/TestController.java new file mode 100644 index 0000000..160c8e3 --- /dev/null +++ b/bweb/src/main/java/com/bweb/controller/TestController.java @@ -0,0 +1,75 @@ +package com.bweb.controller; + +import com.alibaba.fastjson.JSONObject; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.hfkj.channel.gas.shell.CqShellPetroleumRequestService; +import com.hfkj.common.exception.ErrorCode; +import com.hfkj.common.exception.ErrorHelp; +import com.hfkj.common.exception.SysCode; +import com.hfkj.common.utils.HttpUtils; +import com.hfkj.common.utils.HttpsUtils; +import com.hfkj.common.utils.MD5Util; +import com.hfkj.common.utils.ResponseMsgUtil; +import com.hfkj.entity.BsGasOrder; +import com.hfkj.entity.SecUser; +import com.hfkj.model.ResponseData; +import com.hfkj.service.sec.SecUserLoginLogService; +import com.hfkj.service.sec.SecUserService; +import com.hfkj.sysenum.SecUserObjectTypeEnum; +import com.hfkj.sysenum.SecUserStatusEnum; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; + +@Controller +@RequestMapping(value="/test") +@Api(value="测试") +public class TestController { + Logger log = LoggerFactory.getLogger(TestController.class); + + @Resource + private SecUserService secUserService; + @Resource + private SecUserLoginLogService secUserLoginLogService; + + @RequestMapping(value="/editUser",method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "编辑用户") + public ResponseData editUser() { + try { + + JSONObject response = HttpsUtils.doGet("https://oil.dctpay.com/crest/gasOrder/queryOilOrder?orderNo=240804110524374697"); + + BsGasOrder gasOrder = JSONObject.parseObject(response.getJSONObject("return_data").toJSONString(), BsGasOrder.class); + + // 推送加好油【重庆壳牌】 + JSONObject object = CqShellPetroleumRequestService.gasSyncPayment(gasOrder.getOrderNo(), + gasOrder.getMerNo(), + gasOrder.getPayTime(), + gasOrder.getGasRefuelPrice(), + gasOrder.getGasOilNo(), + gasOrder.getGasGunNo(), + gasOrder.getPayablePrice(), + gasOrder.getTotalDeductionPrice() + ); + + return ResponseMsgUtil.success(object); + + } catch (Exception e) { + log.error("error!",e); + return ResponseMsgUtil.exception(e); + } + } + + + +} diff --git a/schedule/src/main/java/com/hfkj/schedule/OrderSchedule.java b/schedule/src/main/java/com/hfkj/schedule/OrderSchedule.java new file mode 100644 index 0000000..3d3b126 --- /dev/null +++ b/schedule/src/main/java/com/hfkj/schedule/OrderSchedule.java @@ -0,0 +1,36 @@ +package com.hfkj.schedule; + +import com.hfkj.service.order.BsOrderService; +import org.apache.commons.collections4.MapUtils; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +/** + * 订单结算业务 + * @className: OrderSettleService + * @author: HuRui + * @date: 2024/6/25 + **/ +@Component +public class OrderSchedule { + @Resource + private BsOrderService orderService; + + @Scheduled(cron="0 0/1 * * * ?") //每分钟执行一次 + public void cancel() { + List> orderList = orderService.getNeedCancelOrderList(); + for (Map param : orderList) { + try { + // 取消订单 + orderService.cancel(MapUtils.getString(param, "orderNo")); + + } catch (Exception e) { + System.out.println(MapUtils.getString(param, "orderNo")+",取消订单失败!"); + } + } + } +} diff --git a/service/src/main/java/com/hfkj/dao/BsAgentMerAccountMapperExt.java b/service/src/main/java/com/hfkj/dao/BsAgentMerAccountMapperExt.java index ed73340..6dc11da 100644 --- a/service/src/main/java/com/hfkj/dao/BsAgentMerAccountMapperExt.java +++ b/service/src/main/java/com/hfkj/dao/BsAgentMerAccountMapperExt.java @@ -24,10 +24,11 @@ public interface BsAgentMerAccountMapperExt { " bs_agent_mer_account a" + " LEFT JOIN bs_merchant b ON a.mer_no = b.mer_no" + " where 1 = 1" + + " and mer_id in (select mer_id from bs_agent_mer where agent_staff_id = #{param.agentStaffId} and `status` = 1) " + " and b.province_code = #{param.provinceCode} " + " and b.city_code = #{param.cityCode} " + " and b.area_code = #{param.areaCode} " + - " and b.agent_id = #{param.agentId} " + + " and a.agent_id = #{param.agentId} " + " and b.mer_no like concat('%',#{param.merNo},'%') " + " and b.mer_name like concat('%',#{param.merName},'%') " + " ORDER BY a.amount asc" + diff --git a/service/src/main/java/com/hfkj/dao/BsOrderMapperExt.java b/service/src/main/java/com/hfkj/dao/BsOrderMapperExt.java index 08c462e..ceec9df 100644 --- a/service/src/main/java/com/hfkj/dao/BsOrderMapperExt.java +++ b/service/src/main/java/com/hfkj/dao/BsOrderMapperExt.java @@ -1,7 +1,18 @@ package com.hfkj.dao; +import org.apache.ibatis.annotations.Select; + +import java.util.List; +import java.util.Map; + /** * mapper扩展类 */ public interface BsOrderMapperExt { -} \ No newline at end of file + + @Select(" select a.orderNo,a.timestampdiff " + + " from (select order_no orderNo, TIMESTAMPDIFF(MINUTE,create_time,NOW()) timestampdiff from bs_order where order_status = 1) a " + + " where a.timestampdiff > 10") + List> selectNeedCancelOrderList(); + +} diff --git a/service/src/main/java/com/hfkj/service/agent/BsAgentMerAccountService.java b/service/src/main/java/com/hfkj/service/agent/BsAgentMerAccountService.java index 75c09d7..1e7a96c 100644 --- a/service/src/main/java/com/hfkj/service/agent/BsAgentMerAccountService.java +++ b/service/src/main/java/com/hfkj/service/agent/BsAgentMerAccountService.java @@ -1,6 +1,7 @@ package com.hfkj.service.agent; import com.hfkj.entity.BsAgentMerAccount; +import com.hfkj.entity.BsAgentMerAccountRecord; import com.hfkj.entity.BsMerchantAccount; import com.hfkj.entity.BsOrderRefund; @@ -67,6 +68,15 @@ public interface BsAgentMerAccountService { */ void refund(Long agentId, String merNo, BigDecimal amount, BsOrderRefund orderRefund) throws Exception; + /** + * 是否交易 + * @param agentId + * @param merNo + * @param orderNo + * @return + */ + boolean isTrade(Long agentId, String merNo, String orderNo); + /** * 查询商户列表 * @param param diff --git a/service/src/main/java/com/hfkj/service/agent/impl/BsAgentMerAccountRecordServiceImpl.java b/service/src/main/java/com/hfkj/service/agent/impl/BsAgentMerAccountRecordServiceImpl.java index ff5b4d7..e44008c 100644 --- a/service/src/main/java/com/hfkj/service/agent/impl/BsAgentMerAccountRecordServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/agent/impl/BsAgentMerAccountRecordServiceImpl.java @@ -69,13 +69,17 @@ public class BsAgentMerAccountRecordServiceImpl implements BsAgentMerAccountReco } if (MapUtils.getLong(param, "agentId") != null) { - criteria.andAgentMerAccountIdEqualTo(MapUtils.getLong(param, "agentId")); + criteria.andAgentIdEqualTo(MapUtils.getLong(param, "agentId")); } if (StringUtils.isNotBlank(MapUtils.getString(param, "merNo"))) { criteria.andMerNoEqualTo(MapUtils.getString(param, "merNo")); } + if (StringUtils.isNotBlank(MapUtils.getString(param, "orderNo"))) { + criteria.andSourceOrderNoEqualTo(MapUtils.getString(param, "orderNo")); + } + example.setOrderByClause("create_time desc"); return agentMerAccountRecordMapper.selectByExample(example); } diff --git a/service/src/main/java/com/hfkj/service/agent/impl/BsAgentMerAccountServiceImpl.java b/service/src/main/java/com/hfkj/service/agent/impl/BsAgentMerAccountServiceImpl.java index f2ec9bd..3784b66 100644 --- a/service/src/main/java/com/hfkj/service/agent/impl/BsAgentMerAccountServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/agent/impl/BsAgentMerAccountServiceImpl.java @@ -18,8 +18,10 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * @className: BsAgentMerAccountServiceImpl @@ -178,10 +180,6 @@ public class BsAgentMerAccountServiceImpl implements BsAgentMerAccountService { // 获取锁成功 // 查询账户 BsAgentMerAccount merAccount = getAccount(agentId, merNo); - // 余额 是否小于 消费余额 - if (merAccount.getAmount().compareTo(amount) == -1) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "余额不足,暂时无法交易"); - } // 变更前金额 BigDecimal beforeAmount = merAccount.getAmount(); // 计算消费后金额 @@ -266,6 +264,38 @@ public class BsAgentMerAccountServiceImpl implements BsAgentMerAccountService { } } + @Override + public boolean isTrade(Long agentId, String merNo, String orderNo) { + Map param = new HashMap<>(); + param.put("agentId", agentId); + param.put("merNo", merNo); + param.put("orderNo", orderNo); + // 查询订单扣款记录 + List list = agentMerAccountRecordService.getList(param); + + // 结果 + boolean result = false; + if (!list.isEmpty()) { + // 是否有交易 + List trade = list + .stream().filter(o -> o.getType().equals(MerchantAccountRecordTypeEnum.type2.getType())) + .collect(Collectors.toList()); + if (!trade.isEmpty()) { + result = true; + + // 是否有退款给商户 + List refund = list + .stream().filter(o -> o.getType().equals(MerchantAccountRecordTypeEnum.type1.getType())) + .collect(Collectors.toList()); + if (!refund.isEmpty()) { + // 有退款,为false 不可重复退款给商户 + result = false; + } + } + } + return result; + } + @Override public List> getMerList(Map param) { return agentMerAccountMapper.selectMerList(param); diff --git a/service/src/main/java/com/hfkj/service/order/BsOrderService.java b/service/src/main/java/com/hfkj/service/order/BsOrderService.java index 2a43cb7..ed50b72 100644 --- a/service/src/main/java/com/hfkj/service/order/BsOrderService.java +++ b/service/src/main/java/com/hfkj/service/order/BsOrderService.java @@ -81,5 +81,10 @@ public interface BsOrderService { */ List getOrderList(Map param); + /** + * 获取需求取消的订单列表 + * @return + */ + List> getNeedCancelOrderList(); } diff --git a/service/src/main/java/com/hfkj/service/order/OrderCancelService.java b/service/src/main/java/com/hfkj/service/order/OrderCancelService.java index efe6545..02034c1 100644 --- a/service/src/main/java/com/hfkj/service/order/OrderCancelService.java +++ b/service/src/main/java/com/hfkj/service/order/OrderCancelService.java @@ -74,21 +74,23 @@ public class OrderCancelService { if (gasOrder.getAgentId() != null) { try { - // 家辉代理商商户金额 - Map param = new HashMap<>(); - param.put("sourceType", MerchantAccountRecordSourceTypeEnum.type4.getType()); - param.put("sourceOrderNo", gasOrder.getOrderNo()); - param.put("sourceOrderContent", "取消加油订单:"+gasOrder.getOrderNo()+",金额:¥" + gasOrder.getGasRefuelPrice()); - agentMerAccountService.orderCancel(gasOrder.getAgentId(), gasOrder.getMerNo(), gasOrder.getGasRefuelPrice(), param); + // 是否可以退款 + boolean isTrade = agentMerAccountService.isTrade(gasOrder.getAgentId(), gasOrder.getMerNo(), gasOrder.getOrderNo()); + if (isTrade) { + // 代理商商户金额 + Map param = new HashMap<>(); + param.put("sourceType", MerchantAccountRecordSourceTypeEnum.type4.getType()); + param.put("sourceOrderNo", gasOrder.getOrderNo()); + param.put("sourceOrderContent", "取消加油订单:"+gasOrder.getOrderNo()+",金额:¥" + gasOrder.getGasRefuelPrice()); + agentMerAccountService.orderCancel(gasOrder.getAgentId(), gasOrder.getMerNo(), gasOrder.getGasRefuelPrice(), param); + } } catch (BaseException e) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getErrorMsg()); } catch (Exception e) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "扣款失败,请稍后重新提交订单"); } - } } } - } diff --git a/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java b/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java index 70b8e7d..bf18b48 100644 --- a/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java @@ -12,6 +12,7 @@ import com.hfkj.model.order.OrderChildModel; import com.hfkj.model.order.OrderModel; import com.hfkj.mqtopic.OrderTopic; import com.hfkj.service.order.*; +import com.hfkj.sysenum.merchant.MerchantAccountRecordTypeEnum; import com.hfkj.sysenum.order.OrderChildProductTypeEnum; import com.hfkj.sysenum.order.OrderChildStatusEnum; import com.hfkj.sysenum.order.OrderStatusEnum; @@ -66,6 +67,9 @@ public class BsOrderServiceImpl implements BsOrderService { private OrderPaySuccessService orderPaySuccessService; @Resource private OrderCancelService orderCancelService; + @Resource + private RedisTemplate redisTemplate; + public BsOrder editData(BsOrder order) { order.setUpdateTime(new Date()); if (order.getId() == null) { @@ -261,23 +265,29 @@ public class BsOrderServiceImpl implements BsOrderService { @Override @Transactional(propagation= Propagation.REQUIRES_NEW, rollbackFor= {RuntimeException.class}, timeout = 10) public OrderModel cancel(String orderNo) { - // 查询订单 - OrderModel order = getDetail(orderNo); - if (order == null) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的订单"); - } - if (!order.getOrderStatus().equals(OrderStatusEnum.status1.getCode())) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法取消,订单不处于待支付状态"); - } - order.setOrderStatus(OrderStatusEnum.status5.getCode()); - order.setCancelTime(new Date()); - editData(order); - - for (BsOrderChild orderChild : order.getOrderChildList()) { - orderChild.setStatus(OrderChildStatusEnum.status5.getCode()); - orderChildService.editData(orderChild); - } + // 锁编号 + String lockKey = "ORDER_CANCEL_"+ orderNo; + // 获取锁 + Boolean lock = redisTemplate.opsForValue().setIfAbsent(lockKey, ""); + if (Boolean.TRUE.equals(lock)) { + try { + // 获取锁成功 + // 查询订单 + OrderModel order = getDetail(orderNo); + if (order == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的订单"); + } + if (!order.getOrderStatus().equals(OrderStatusEnum.status1.getCode())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法取消,订单不处于待支付状态"); + } + order.setOrderStatus(OrderStatusEnum.status5.getCode()); + order.setCancelTime(new Date()); + editData(order); + for (BsOrderChild orderChild : order.getOrderChildList()) { + orderChild.setStatus(OrderChildStatusEnum.status5.getCode()); + orderChildService.editData(orderChild); + } /* // 退回优惠券 if (order.getDeduction().getUserCouponDiscountId() != null) { // 查询用户优惠券 @@ -317,13 +327,29 @@ public class BsOrderServiceImpl implements BsOrderService { source ); }*/ + // 取消订单业务 + orderCancelService.orderBusHandle(order); - // 取消订单业务 - orderCancelService.orderBusHandle(order); + // 释放锁 + redisTemplate.delete(lockKey); - // 更新缓存 - cache(order); - return order; + // 更新缓存 + cache(order); + return order; + + } catch (Exception e) { + // 释放锁 + redisTemplate.delete(lockKey); + + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "取消失败!未知原因"); + } + } else { + try { + Thread.sleep(100); + } catch (Exception e) {} + cancel(orderNo); + } + return null; } @Override @@ -464,4 +490,9 @@ public class BsOrderServiceImpl implements BsOrderService { return orderMapper.selectByExample(example); } + @Override + public List> getNeedCancelOrderList() { + return orderMapper.selectNeedCancelOrderList(); + } + }