准备物料:
SELECT * FROM dm_item WHERE id = 5;
SELECT * FROM dm_scheduler WHERE itemId = 5;
SELECT * FROM dm_scheduler_seat_price WHERE scheduleId = 12700;
SELECT * FROM dm_scheduler_seat WHERE scheduleId = 12700 AND STATUS = 1;
https://piao.damai.cn/178499.html?spm=a2oeg.home.card_1.ditem_1.26c623e1ckkVsa
(1)根据商品ID查询商品排期接口
(2)根据商品排期查询商品价格接口
思路1:定位dm-item-consumer微服务工程
思路2:入口为商品itemId,出口为商品排期集合Dto (vo中包含两张表:dm_item,dm_scheduler)
思路3:定位dm_scheduler排期库中的dm_scheduler表
思路4:组装VOList(itemSchedulerVoList)
步骤1:
*********dm-item-consumer/ItemDetailService*********
/**
* 根据商品ID查询排期
* @param itemId
* @return
* @throws Exception
*/
public Dto<List<ItemSchedulerVo>> queryItemScheduler(Long itemId)throws Exception;
*********dm-item-consumer/ItemDetailServiceImpl*********
附注:(dm-item-consumer\src\main\java\cn\dm\exception加入字符串常量ItemErrorCode.java)
public class ItemDetailServiceImpl implements ItemDetailService {
//引入
@Autowired
private RestDmSchedulerClient restDmSchedulerClient;
@Override
public Dto<List<ItemSchedulerVo>> queryItemScheduler(Long id) throws Exception {
//1.根据商品id查询商品对象信息
DmItem dmItem = dmItemClient.getDmItemById(id);
//2.商品对象信息判空校验
if (EmptyUtils.isEmpty(dmItem)) {
throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
}
//3.根据商品id查询排期对象信息集合
Map<String, Object> param = new HashMap<String, Object>();
param.put("itemId", dmItem.getId());
List<DmScheduler> dmSchedulerList = restDmSchedulerClient.getDmSchedulerListByMap(param);
//4.排期对象信息判空校验
if (EmptyUtils.isEmpty(dmSchedulerList)) {
throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
}
//5.组装VOList(itemSchedulerVoList)
List<ItemSchedulerVo> itemSchedulerVoList = new ArrayList<ItemSchedulerVo>();
for (int i = 0; i < dmSchedulerList.size(); i++) {
ItemSchedulerVo itemSchedulerVo = new ItemSchedulerVo();
BeanUtils.copyProperties(dmItem, itemSchedulerVo);
BeanUtils.copyProperties(dmSchedulerList.get(i), itemSchedulerVo);
//6.如果出现vo中的字段和javabean中的字段格式不一样,需要转化
itemSchedulerVo.setStartTime(DateUtil.format(dmSchedulerList.get(i).getStartTime()));
itemSchedulerVo.setEndTime(DateUtil.format(dmSchedulerList.get(i).getEndTime()));
itemSchedulerVoList.add(itemSchedulerVo);
}
return DtoUtil.returnDataSuccess(itemSchedulerVoList);
}
}
*********dm-item-consumer/ItemDetailController*********
@ResponseBody
@RequestMapping(value = "/p/queryItemScheduler",method = RequestMethod.POST)
public Dto<List<ItemSchedulerVo>> queryItemScheduler(@RequestBody Map<String, Object> param) throws Exception {
Integer id = Integer.parseInt(param.get("itemId").toString());
return itemDetailService.queryItemScheduler((long)id);
}
2.测试:
1.启动eureka微服务成功
2.启动dm-base-provider微服务
2.启动dm-item-provider微服务
3.启动dm-item-consumer微服务
4.启动dm-scheduler-provider微服务
打开postman:http://localhost:7201/api/p/queryItemScheduler
设置:
header
Content-Type application/json
body
{
"itemId":5
}
思路1:定位商品微服务工程dm-item-consumer
思路2:根据排期ID查询价格
思路3:条件是保证座位可选
思路4:确定排期价格相关信息在dm_scheduler_seat_price表中
思路5:确定座位状态字段status在dm_scheduler_seat表中
思路6:组装itemPriceVoList
**dm-item-consumer/ItemDetailService**
public Dto<List<ItemPriceVo>> queryItemPrice(Long id) throws Exception;
*********dm-item-consumer/ItemDetailServiceImpl*********
//引入:
@Autowired
private RestDmSchedulerSeatPriceClient restDmSchedulerSeatPriceClient;
@Autowired
private RestDmSchedulerSeatClient restDmSchedulerSeatClient;
public Dto<List<ItemPriceVo>> queryItemPrice(Long id) throws Exception {
Map<String, Object> param = new HashMap<String, Object>();
param.put("scheduleId", id);
//1.根据排期ID查询排期价格信息
List<DmSchedulerSeatPrice> dmSchedulerSeatPriceList = restDmSchedulerSeatPriceClient.getDmSchedulerSeatPriceListByMap(param);
//2.判空校验
if (EmptyUtils.isEmpty(dmSchedulerSeatPriceList)) {
throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
}
//3.组装itemPriceVoListVo
List<ItemPriceVo> itemPriceVoList = new ArrayList<ItemPriceVo>();
for (DmSchedulerSeatPrice dmSchedulerSeatPrice : dmSchedulerSeatPriceList) {
ItemPriceVo itemPriceVo = new ItemPriceVo();
BeanUtils.copyProperties(dmSchedulerSeatPrice, itemPriceVo);
//查询所有是有效的并且没有被锁定的座位
Map<String, Object> lockMap = new HashMap<String, Object>();
lockMap.put("status", 1);
lockMap.put("scheduleId", itemPriceVo.getScheduleId());
int num = restDmSchedulerSeatClient.getDmSchedulerSeatCountByMap(lockMap);
int isHaveSeat = num > 0 ? 1 : 0;
itemPriceVo.setIsHaveSeat(isHaveSeat);
itemPriceVoList.add(itemPriceVo);
}
return DtoUtil.returnDataSuccess(itemPriceVoList);
}
*********dm-item-consumer/ItemDetailController*********
@ResponseBody
@RequestMapping(value = "/p/queryItemPrice",method = RequestMethod.POST)
public Dto<List<ItemPriceVo>> queryItemPrice(@RequestBody Map<String, Object> param) throws Exception {
Integer id = Integer.parseInt(param.get("scheduleId").toString());
return itemDetailService.queryItemPrice((long)id);
}
测试:http://localhost:7201/api/p/queryItemPrice
设置:
raw
{
"scheduleId":12700
}
m.get(“scheduleId”).toString());
return itemDetailService.queryItemPrice((long)id);
}
##### 步骤四:
测试:http://localhost:7201/api/p/queryItemPrice
设置:
```java
raw
{
"scheduleId":12700
}