商品详情接口

商品详情接口

思路1:根据查看"商品详情.md"接口文档,组装商品详情VO
思路2:确定dm-item-consumer微服务工程
思路3:接口OUTPUT中的信息包含了三张表(商品信息(dm_item),图片信息(dm_image),剧院信息(dm_cinema))
思路4:查询商品信息思路:通过入口参数商品id获取
思路5:查询图片信息思路:通过三个入口参数(商品id,商品类型,商品分类)获取
思路6:查询剧院信息思路:通过入口商品id获取

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YObg1dxn-1573032818163)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1573013135609.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vAZJ8FWY-1573032818164)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1573013172830.png)]

①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
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YXxGjkaX-1573032818165)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1573013430010.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值