由于新增商品接口逻辑流程需要远程与优惠券服务通信,所以优惠券需提供以下功能的接口。
在webshop-prodect服务中先新建feign包,创建CouponFeignService类用于封装对优惠券服务的远程调用方法,如下:
在CouponFeignService类中通过@FeignClient注解指定需要访问的服务名,如下:
@FeignClient("webshop-coupon")
public interface CouponFeignService {
}
在webshop-prodect服务启动类中添加开启Feign功能的注解@EnableFeignClients,如下:
由于需要保存spu的积分信息
由于该方法在webshop-coupon服务中已有相应接口提供,是代码生成器自动生成的,如下:
所以在CouponFeignService类中添加该方法在优惠券服务的请求路径,就像平时写接口一样,如下:
@PostMapping("/coupon/spubounds/save")
R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);
需要保存sku的优惠、满减等信息
webshop-coupon服务缺少该接口,所以需要添加上,如下:
SkuFullReductionController类中添加如下接口:
@PostMapping("/saveinfo")
public R saveInfo(@RequestBody SkuReductionTo reductionTo){
skuFullReductionService.saveSkuReduction(reductionTo);
return R.ok();
}
SkuFullReductionService接口添加如下方法:
/**
* 保存商品满减信息
*
* @param skuReductionTo
*/
void saveSkuReduction(SkuReductionTo skuReductionTo);
SkuFullReductionServiceImpl类添加方法的实现,如下:
@Override
public void saveSkuReduction(SkuReductionTo skuReductionTo) {
SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
if (skuReductionTo.getFullCount() > 0) {
skuLadderService.save(skuLadderEntity);
}
SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
BeanUtils.copyProperties(skuReductionTo, reductionEntity);
if (reductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
this.save(reductionEntity);
}
List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
MemberPriceEntity priceEntity = new MemberPriceEntity();
priceEntity.setSkuId(skuReductionTo.getSkuId());
priceEntity.setMemberLevelId(item.getId());
priceEntity.setMemberLevelName(item.getName());
priceEntity.setMemberPrice(item.getPrice());
priceEntity.setAddOther(1);
return priceEntity;
}).filter(item -> {
return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
}).collect(Collectors.toList());
memberPriceService.saveBatch(collect);
}
最后再给返回对象R添加一个获取相应码的方法,方便判断远程方法调用是否成功以及给出相应日志或异常,如下:
public class R extends HashMap<String, Object> {
public Integer getCode() {
return (Integer) this.get("code");
}
}