设计模式-策略模式
策略模式:
属于行为模式,包含一个策略接口Strategy,四个以下(大于四个可以使用混合模式)的策略实现类,
一个使用该策略的类Context
实例:两个角色 厅店和能人 都有一个酬金明细,一个是分页查询厅店发放的酬金明细,一个是分页查询能人提现的酬金明细
1.一个策略接口Strategy
/** * 抽象酬金明细接口 */ public interface MoneyDetailListPageStrategy { /**分页数据接口*/ ResultJson getMoneyDetailListPage(MoneyDetailListPageVo pageVo, CpUserInfoDto userInfo); }
2。策略实现类
2.1 能人角色的提现酬金明细
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.inphase.demo.handler; 2 3 import cn.hutool.core.util.ObjectUtil; 4 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 5 import com.inphase.ableperson.entity.CpOtherNeedsModel; 6 import com.inphase.ableperson.mapper.CpNeedsReportModelMapper; 7 import com.inphase.ableperson.mapper.CpOtherNeedsModelMapper; 8 import com.inphase.demo.common.domain.ResultJson; 9 import com.inphase.demo.entity.CpUserInfoDto; 10 import com.inphase.demo.entity.MoneyDetailListPage; 11 import com.inphase.demo.entity.MoneyDetailListPageDto; 12 import com.inphase.demo.mapper.user.CpReconciliationStatementMapper; 13 import com.inphase.demo.vo.MoneyDetailListPageVo; 14 import lombok.extern.slf4j.Slf4j; 15 import org.springframework.beans.BeanUtils; 16 import org.springframework.stereotype.Component; 17 18 import javax.annotation.Resource; 19 import java.util.*; 20 import java.util.stream.Collectors; 21 22 /** 23 * 能人分页查询酬金明细 24 */ 25 @Component("AblePersonMoneyDetailListPageStrategy") 26 @Slf4j 27 public class AblePersonMoneyDetailListPageStrategy implements MoneyDetailListPageStrategy{ 28 29 @Resource 30 CpReconciliationStatementMapper cpReconciliationStatementMapper; 31 @Resource 32 CpNeedsReportModelMapper cpNeedsReportModelMapper; 33 @Resource 34 CpOtherNeedsModelMapper cpOtherNeedsModelMapper; 35 36 @Override 37 public ResultJson getMoneyDetailListPage(MoneyDetailListPageVo pageVo, CpUserInfoDto userInfo) { 38 Map<String,Object> resultMap = new HashMap<>(); 39 pageVo.setPageNum((pageVo.getPageNum()-1)*pageVo.getPageSize()); 40 List<MoneyDetailListPageDto> moneyDetailListPageDtos = Optional.ofNullable(cpReconciliationStatementMapper.selectMoneyDetailListPage(userInfo.getMaId(), pageVo)).orElse(new ArrayList<>()); 41 int total = cpReconciliationStatementMapper.selectFountRows(); 42 log.info("查询明细数量:"+moneyDetailListPageDtos.size()); 43 List<MoneyDetailListPage> moneyDetailListPages = null; 44 if (moneyDetailListPageDtos.size()>0) { 45 //1.根据 46 moneyDetailListPages = moneyDetailListPageDtos.stream().map(dto -> { 47 MoneyDetailListPage page = new MoneyDetailListPage(); 48 BeanUtils.copyProperties(dto, page); 49 //参数赋值 50 if (dto.getFlag() == 1) { //收入 51 if (ObjectUtil.isNotEmpty(dto.getAddTime())) { 52 Calendar c = Calendar.getInstance(); 53 54 c.setTimeInMillis(dto.getAddTime() * 1000); 55 page.setAddTime(c.getTime()); 56 } 57 if (ObjectUtil.isNotEmpty(dto.getOrderState())) { 58 String orderStateName = dto.getOrderState() == 1 ? "基本酬金" : "业务提成"; 59 page.setOrderStateName(orderStateName); 60 } 61 62 } 63 if (dto.getFlag() == 2 && ObjectUtil.isNotEmpty(dto.getWithdrawStatus())) { 64 if (dto.getWithdrawStatus() == 3) { //提现完成 65 page.setWithdrawStatus(1); 66 page.setWithdrawStatusName("提现成功"); 67 } else { //其余提现中 68 page.setWithdrawStatus(0); 69 page.setWithdrawStatusName("提现中"); 70 } 71 } 72 return page; 73 }).collect(Collectors.toList()); 74 dealWithOrderVal(moneyDetailListPages); 75 } 76 resultMap.put("total",total); 77 78 resultMap.put("moneyDetailListPages",Optional.ofNullable(moneyDetailListPages).orElse(new ArrayList<>())); 79 80 return ResultJson.success(resultMap); 81 } 82 83 //根据模板类型和模板id 赋值订单需求类型 84 private void dealWithOrderVal(List<MoneyDetailListPage> moneyDetailListPages){ 85 moneyDetailListPages //只有收入才有详情 86 .forEach(moneyDetailListPage -> { 87 System.out.println("moneyDetailListPage:"+moneyDetailListPage); 88 Integer modelType = moneyDetailListPage.getModelType(); 89 if (ObjectUtil.isNotEmpty(modelType) && modelType==1){ 90 Map<String, Object> map = cpNeedsReportModelMapper.commonDemandTem(moneyDetailListPage.getModelId()); 91 moneyDetailListPage.setNeedsTypeName(map.get("nr_type_name")+""); 92 } 93 if (ObjectUtil.isNotEmpty(modelType) && modelType ==2){ 94 QueryWrapper<CpOtherNeedsModel> otherWrapper = new QueryWrapper(); 95 otherWrapper.eq("cp_o_id", moneyDetailListPage.getModelId()); 96 CpOtherNeedsModel cpOtherNeedsModel = cpOtherNeedsModelMapper.selectOne(otherWrapper); 97 moneyDetailListPage.setNeedsTypeName(cpOtherNeedsModel.getNeedsDesc()); 98 } 99 }); 100 } 101 }
2.2 厅店角色的发放酬金明细
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.inphase.demo.handler; 2 3 import cn.hutool.core.date.DateTime; 4 import cn.hutool.core.date.DateUtil; 5 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 6 import com.inphase.ableperson.entity.CpNeedsFormOrder; 7 import com.inphase.ableperson.entity.CpOtherNeedsModel; 8 import com.inphase.ableperson.mapper.CpNeedsFormOrderMapper; 9 import com.inphase.ableperson.mapper.CpNeedsReportModelMapper; 10 import com.inphase.ableperson.mapper.CpOtherNeedsModelMapper; 11 import com.inphase.demo.common.domain.ResultJson; 12 import com.inphase.demo.entity.CpUserInfoDto; 13 import com.inphase.demo.entity.HallMoneyDetailListPage; 14 import com.inphase.demo.entity.HallMoneyDetailListPageDto; 15 import com.inphase.demo.entity.MoneyDetailListPage; 16 import com.inphase.demo.vo.MoneyDetailListPageVo; 17 import lombok.extern.slf4j.Slf4j; 18 import org.springframework.beans.BeanUtils; 19 import org.springframework.stereotype.Component; 20 21 import javax.annotation.Resource; 22 import java.util.*; 23 import java.util.stream.Collectors; 24 25 /** 26 * 厅店分页查询酬金明细 27 */ 28 @Component("HallMoneyDetailListPageStrategy") 29 @Slf4j 30 public class HallMoneyDetailListPageStrategy implements MoneyDetailListPageStrategy{ 31 32 @Resource 33 private CpNeedsFormOrderMapper cpNeedsFormOrderMapper; 34 35 @Resource 36 CpNeedsReportModelMapper cpNeedsReportModelMapper; 37 @Resource 38 CpOtherNeedsModelMapper cpOtherNeedsModelMapper; 39 40 @Override 41 public ResultJson getMoneyDetailListPage(MoneyDetailListPageVo pageVo, CpUserInfoDto userInfo) { 42 pageVo.setSmId(userInfo.getCpStoreMessageDtoList().get(0).getSmId());//TODO 单个厅店登录 临时 43 Map<String,Object> resultMap = new HashMap<>(); 44 pageVo.setPageNum((pageVo.getPageNum()-1)*pageVo.getPageSize()); 45 List<HallMoneyDetailListPageDto> hallMoneyDetailListPageDtos = Optional.ofNullable(cpNeedsFormOrderMapper.getHallMoneyDetailListPage(pageVo)).orElse(new ArrayList<>()); 46 int total = cpNeedsFormOrderMapper.selectFoundRows(); 47 log.info("查询已经发放订单基础信息明细数量:"+hallMoneyDetailListPageDtos.size()); 48 List<HallMoneyDetailListPage> hallMoneyDetailListPages = hallMoneyDetailListPageDtos.stream().map(dto -> { 49 HallMoneyDetailListPage page = new HallMoneyDetailListPage(); 50 BeanUtils.copyProperties(dto, page); 51 page.setAddTime(DateUtil.date(dto.getAddTime()*1000)); 52 page.setFormOrderCheckTime(DateUtil.date(dto.getFormOrderCheckTime()*1000)); 53 //订单状态修改 订单状态1、酬金待审核;2、酬金审核通过;3、酬金审核不通过;4、酬金待提现(稽核后);5、酬金已提现(提现后);6、提成待审核;7、提成审核通过;8、提成审核未通过;9、提成待提现(稽核后);10、提成已提现(提现后)', 54 page.setFormOrderStauts(1); 55 page.setFormOrderStautsName("发放成功"); 56 List<Integer> sendingStatus = Arrays.asList(2, 4, 7, 9); 57 List<Integer> successStatus = Arrays.asList(5,10); 58 Integer orderStauts = dto.getFormOrderStauts(); 59 if (sendingStatus.contains(orderStauts) ){ 60 page.setWithDrawalStateName("发放中"); 61 } 62 if (successStatus.contains(orderStauts)){ 63 page.setWithDrawalStateName("发放成功"); 64 } 65 return page; 66 }).collect(Collectors.toList()); 67 dealWithOrderVal(hallMoneyDetailListPages); 68 resultMap.put("total",total); 69 resultMap.put("moneyDetailListPages",hallMoneyDetailListPages); 70 return ResultJson.success(resultMap); 71 } 72 73 //根据模板类型和模板id 赋值订单需求类型 74 private void dealWithOrderVal(List<HallMoneyDetailListPage> moneyDetailListPages){ 75 moneyDetailListPages 76 .forEach(moneyDetailListPage -> { 77 //需求类型赋值 78 if (moneyDetailListPage.getModelType()==1){ 79 Map<String, Object> map = cpNeedsReportModelMapper.commonDemandTem(moneyDetailListPage.getModelId()); 80 moneyDetailListPage.setNeedsTypeName(map.get("nr_type_name")+""); 81 } 82 if (moneyDetailListPage.getModelType()==2){ 83 QueryWrapper<CpOtherNeedsModel> otherWrapper = new QueryWrapper(); 84 otherWrapper.eq("cp_o_id", moneyDetailListPage.getModelId()); 85 CpOtherNeedsModel cpOtherNeedsModel = cpOtherNeedsModelMapper.selectOne(otherWrapper); 86 moneyDetailListPage.setNeedsTypeName(cpOtherNeedsModel.getNeedsDesc()); 87 } 88 89 }); 90 } 91 92 93 }
3.调用分页查询酬金明细的context类(spring的构造器注入)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.inphase.demo.handler; 2 3 import com.inphase.demo.common.domain.ResultJson; 4 import com.inphase.demo.entity.CpUserInfoDto; 5 import com.inphase.demo.vo.MoneyDetailListPageVo; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Component; 8 9 import java.util.Map; 10 import java.util.concurrent.ConcurrentHashMap; 11 @Component 12 public class MoneyDetailListPageContext { 13 14 @Autowired 15 private Map<String, MoneyDetailListPageStrategy> strategyMap = new ConcurrentHashMap<>(); 16 17 public MoneyDetailListPageContext(Map<String, MoneyDetailListPageStrategy> strategyMap) { 18 this.strategyMap.clear(); 19 strategyMap.forEach((k, v)-> this.strategyMap.put(k, v)); 20 } 21 22 public ResultJson getMoneyDetailListPage(String strategy, MoneyDetailListPageVo listPageVo, CpUserInfoDto userInfo){ 23 return strategyMap.get(strategy).getMoneyDetailListPage(listPageVo,userInfo); 24 } 25 26 }
4.调用不同的策略的Service层,其实也可以Service层作为context
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 用户登录注册 3 */ 4 @Service 5 @Slf4j 6 @Transactional 7 public class CpUserService { 8 @Resource 9 private MoneyDetailListPageContext moneyDetailListPageContext; 10 11 /** 12 * 获取金额明细信息 13 * @param listPageVo 查询条件 14 * @return 查询结果 15 */ 16 public ResultJson getMoneyDetailListPage(MoneyDetailListPageVo listPageVo,CpUserInfoDto userInfo) { 17 log.info("请求参数:MoneyDetailListPageVo==>"+listPageVo); 18 log.info("请求参数:CpUserInfoDto==>"+userInfo); 19 ResultJson resultJson; 20 switch (listPageVo.getCpType()){ 21 case 1: 22 resultJson = moneyDetailListPageContext.getMoneyDetailListPage("AblePersonMoneyDetailListPageStrategy", listPageVo, userInfo); 23 break; 24 case 2: 25 resultJson = moneyDetailListPageContext.getMoneyDetailListPage("HallMoneyDetailListPageStrategy", listPageVo, userInfo); 26 break; 27 default: 28 resultJson = ResultJson.error("请求参数cpType错误:"+listPageVo.getCpType()); 29 } 30 return resultJson; 31 } 32 }