商品详情接口
思路1:根据查看"商品详情.md"接口文档,组装商品详情VO
思路2:确定dm-item-consumer微服务工程
思路3:接口OUTPUT中的信息包含了三张表(商品信息(dm_item),图片信息(dm_image),剧院信息(dm_cinema))
思路4:查询商品信息思路:通过入口参数商品id获取
思路5:查询图片信息思路:通过三个入口参数(商品id,商品类型,商品分类)获取
思路6:查询剧院信息思路:通过入口商品id获取
①service/ItemDetailService
案例:
public interface ItemDetailService {
public Dto<ItemDetailVo> queryItemDetail(Long id)throws Exception;
}
②service/impl/ItemDetailServiceImpl
案例:
@Component
public class ItemDetailServiceImpl implements ItemDetailService {
@Autowired
private RestDmItemClient dmItemClient;
@Autowired
private RestDmImageClient dmImageClient;
@Autowired
private RestDmCinemaClient restDmCinemaClient;
@Override
public Dto<ItemDetailVo> queryItemDetail(Long id) throws Exception {
//1.查询商品信息,从库里获取商品库中的数据,进行判空操作
DmItem dmItem = dmItemClient.getDmItemById(id);
if (EmptyUtils.isEmpty(dmItem)) {
return null;
}
//查询图片信息
List<DmImage> dmImageList = getImageList(dmItem.getId(), Constants.Image.ImageType.carousel, Constants.Image.ImageCategory.item);
//查询剧院信息
DmCinema dmCinema = restDmCinemaClient.getDmCinemaById(dmItem.getCinemaId());
//组装商品详情VO
ItemDetailVo itemDetailVo = copyData(dmItem, dmCinema, dmImageList);
return DtoUtil.returnDataSuccess(itemDetailVo);
}
private ItemDetailVo copyData(DmItem dmItem, DmCinema dmCinema, List<DmImage> dmImageList) throws ParseException {
ItemDetailVo itemDetailVo = new ItemDetailVo();
BeanUtils.copyProperties(dmItem, itemDetailVo);
itemDetailVo.setStartTime(DateUtil.format(dmItem.getStartTime()));
itemDetailVo.setEndTime(DateUtil.format(dmItem.getEndTime()));
itemDetailVo.setState(dmItem.getState() + "");
itemDetailVo.setImgUrl(dmImageList.get(0).getImgUrl());
if (!EmptyUtils.isEmpty(dmCinema)) {
BeanUtils.copyProperties(dmCinema, itemDetailVo);
}
//注意!!!!一定要注意顺序!!!放前面会被覆盖
itemDetailVo.setId(dmItem.getId());
return itemDetailVo;
}
/**
* 查询图片
*
* @param id
* @param type
* @param category
* @return
*/
public List<DmImage> getImageList(Long id, Integer type, Integer category) throws Exception {
Map<String, Object> paramImage = new HashMap<String, Object>();
paramImage.put("targetId", id);
paramImage.put("type", type);
paramImage.put("category", category);
return dmImageClient.queryDmImageList(id, type, category);
}
}
③controller/ItemDetailController
案例:
@RestController
@RequestMapping("/api")
public class ItemDetailController {
@Resource
private ItemDetailService itemDetailService;
@RequestMapping(value = "/p/queryItemDetail", method = RequestMethod.POST)
public Dto<ItemDetailVo> queryItemDetail(@RequestBody Map<String, Object> param) throws Exception {
Integer id = Integer.parseInt(param.get("id").toString());
return itemDetailService.queryItemDetail((long) id);
}
}
④测试接口:(注意点:请求参数为json对象)
http://localhost:7201/api/p/queryItemDetail
Raw JSON(application/json)
{
"id":1
}