-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de5446d
commit f1a4924
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/com/alipay/shop/designer/dutyChain/controller/UserDutyChainController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.alipay.shop.designer.dutyChain.controller; | ||
|
||
import com.alipay.shop.designer.dutyChain.service.UserDutyChainService; | ||
import com.alipay.shop.model.BusinessLaunch; | ||
import java.util.List; | ||
import javax.annotation.Resource; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author hyy | ||
* @Description | ||
* @create 2024-01-06 12:35 | ||
*/ | ||
@RestController | ||
@RequestMapping("/dutyChain") | ||
public class UserDutyChainController { | ||
|
||
@Resource | ||
private UserDutyChainService userDutyChainService; | ||
|
||
@PostMapping("/business/launch") | ||
public List<BusinessLaunch> filterBusinessLaunch(@RequestParam("city") String city, @RequestParam("sex") String sex, | ||
@RequestParam("product") String product) { | ||
return userDutyChainService.filterBusinessLaunch(city, sex, product); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/alipay/shop/designer/dutyChain/enums/HandlerEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.alipay.shop.designer.dutyChain.enums; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* File Description. | ||
* | ||
* @author arron | ||
* @date crated at 2024/1/6 12:01 | ||
* @see com.alipay.shop.designer.dutyChain.enums | ||
*/ | ||
@Getter | ||
public enum HandlerEnum { | ||
city("com.alipay.shop.designer.dutyChain.CityHandler"), | ||
sex("com.alipay.shop.designer.dutyChain.SexHandler"), | ||
product("com.alipay.shop.designer.dutyChain.ProductHandler"); | ||
|
||
String value=""; | ||
HandlerEnum(String value) { | ||
this.value = value; | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/com/alipay/shop/designer/dutyChain/service/UserDutyChainService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.alipay.shop.designer.dutyChain.service; | ||
|
||
import com.alipay.shop.designer.dutyChain.AbstractBusinessHandler; | ||
import com.alipay.shop.designer.dutyChain.CityHandler; | ||
import com.alipay.shop.designer.dutyChain.enums.HandlerEnum; | ||
import com.alipay.shop.model.BusinessLaunch; | ||
import com.alipay.shop.repo.mapper.BusinessLaunchMapper; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.annotation.Resource; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author hyy | ||
* @Description | ||
* @create 2024-01-06 11:59 | ||
*/ | ||
@Service | ||
public class UserDutyChainService { | ||
|
||
@Resource | ||
private BusinessLaunchMapper businessLaunchRepo; | ||
|
||
//获取Apollo配置 | ||
@Value("${duty.chain}") | ||
private String handlerType; | ||
|
||
private String currentHandlerType; | ||
|
||
private AbstractBusinessHandler currentHandler; | ||
|
||
public List<BusinessLaunch> filterBusinessLaunch(String city, String sex, String product) { | ||
List<BusinessLaunch> launchList = businessLaunchRepo.findAll(); | ||
return buildChain().processHandler(launchList, city, sex, product); | ||
} | ||
|
||
//组装责任链条并返回责任链条首节点 | ||
private AbstractBusinessHandler buildChain() { | ||
|
||
//如果没有配置,直接返回null | ||
if (Objects.isNull(this.handlerType)) { | ||
return null; | ||
} | ||
|
||
if (Objects.isNull(this.currentHandlerType)) { | ||
this.currentHandlerType = this.handlerType; | ||
} | ||
|
||
if (this.currentHandlerType.equals(this.handlerType) && Objects.nonNull(this.currentHandler)) { | ||
return this.currentHandler; | ||
} else { | ||
System.out.println("config modified or first init, start to assemble duty chain."); | ||
synchronized (this) { | ||
try { | ||
//伪头节点 | ||
AbstractBusinessHandler dummyHeadHandler = new CityHandler(); | ||
AbstractBusinessHandler preHandler = dummyHeadHandler; | ||
|
||
String[] handlerTypeList = this.handlerType.split(","); | ||
for (String handlerType : handlerTypeList) { | ||
AbstractBusinessHandler handler = (AbstractBusinessHandler) | ||
Class.forName(HandlerEnum.valueOf(handlerType).getValue()).newInstance(); | ||
preHandler.nextHandler = handler; | ||
preHandler = handler; | ||
} | ||
|
||
this.currentHandler = dummyHeadHandler.nextHandler; | ||
this.currentHandlerType = this.handlerType; | ||
|
||
return currentHandler; | ||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { | ||
throw new UnsupportedOperationException(e); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters