OrderStrategyFactory.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.fdkankan.ucenter.pay.strategy;
  2. import com.fdkankan.ucenter.vo.response.OrderItemVo;
  3. import com.fdkankan.ucenter.common.SpringUtil;
  4. import java.util.Map;
  5. import java.util.concurrent.ConcurrentHashMap;
  6. import lombok.extern.log4j.Log4j2;
  7. import org.springframework.beans.factory.InitializingBean;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Component;
  10. @Log4j2
  11. @Component
  12. public class OrderStrategyFactory implements InitializingBean {
  13. @Autowired
  14. private SpringUtil applicationContextHelper;
  15. private Map<String, OrderStrategy> orderHandleMap = new ConcurrentHashMap<>();
  16. private Map<String, OrderItemStrategy> orderItemHandleMap = new ConcurrentHashMap<>();
  17. @Override
  18. public void afterPropertiesSet() throws Exception {
  19. Map<String, OrderItemStrategy> orderItemHandles = applicationContextHelper.getBeansOfType(OrderItemStrategy.class);
  20. for (Map.Entry<String, OrderItemStrategy> orderHandle : orderItemHandles.entrySet()) {
  21. String type = orderHandle.getValue().getType();
  22. String[] types = type.split(",");
  23. if (types.length > 1){
  24. for (String str : types){
  25. orderItemHandleMap
  26. .put(str, orderHandle.getValue());
  27. }
  28. }
  29. else{
  30. orderItemHandleMap
  31. .put(orderHandle.getValue().getType(), orderHandle.getValue());
  32. }
  33. }
  34. Map<String, OrderStrategy> orderHandles = applicationContextHelper.getBeansOfType(OrderStrategy.class);
  35. for (Map.Entry<String, OrderStrategy> orderHandle : orderHandles.entrySet()) {
  36. orderHandleMap.put(orderHandle.getValue().getType(), orderHandle.getValue());
  37. }
  38. }
  39. public void doHandler(Long userId, OrderItemVo t) throws Exception{
  40. log.warn("sku:"+t.getSkuSn());
  41. orderItemHandleMap.get(t.getSkuSn()).handleOrderItem(userId, t);
  42. }
  43. public void doHandler(String orderSn, String tradeNo, String openId, String type, int paymentTypeName) throws Exception{
  44. Long spaceId = null;
  45. log.info("进入doHandler");
  46. if(orderSn.split("_").length == 2){
  47. spaceId = Long.valueOf(orderSn.split("_")[1]);
  48. orderSn = orderSn.split("_")[0];
  49. }
  50. if(type.matches("^-?[0-9]+")){
  51. type = "expansion";
  52. }
  53. log.info("type:" + type);
  54. log.info("orderSn:" + orderSn);
  55. orderHandleMap.get(type).handleOrder(orderSn, tradeNo, openId, paymentTypeName, spaceId);
  56. }
  57. }