Skip to content

Instantly share code, notes, and snippets.

@yijunwu
Created September 30, 2015 04:26
Show Gist options
  • Save yijunwu/b544a78df6b9a205127f to your computer and use it in GitHub Desktop.
Save yijunwu/b544a78df6b9a205127f to your computer and use it in GitHub Desktop.
异步代码的重构实例2——VswmPmsCouponController。这是重构之前的代码
package com.vip.phoenix.vswm.pms.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vip.penguin.coupon.param.BindParams;
import com.vip.penguin.dto.BaseDto;
import com.vip.penguin.pms.param.CouponParam;
import com.vip.penguin.pms.pojo.Coupon;
import com.vip.penguin.pms.service.PMSCouponService;
import com.vip.phoenixfwk.play.support.HttpContext;
import com.vip.phoenixfwk.play.web.UserBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import play.data.Form;
import play.libs.F;
import play.mvc.Result;
import java.util.List;
/**
* Created by jacob.wu on 2015/9/25.
*/
@Component
public class VswmPmsCouponController extends UserBaseController {
@Autowired
PMSCouponService pmsCouponService;
/**
* 绑定优惠券
*/
public F.Promise<Result> bind() {
BindParams params = Form.form(BindParams.class).bindFromRequest().get();
final int uid = (int) HttpContext.getUserId();
params.setUserId(uid);
//绑定优惠券
F.Promise<BaseDto> couponPromise = pmsCouponService.bind(params);
//后续处理
F.Promise<BaseDto<Coupon>> couponInListPromise = couponPromise.flatMap(new F.Function<BaseDto, F.Promise<BaseDto<Coupon>>>() {
@Override
public F.Promise<BaseDto<Coupon>> apply(final BaseDto boundCouponBaseDto) throws Throwable {
Coupon coupon = (Coupon)boundCouponBaseDto.getData();
if (coupon != null) {
final String sn = coupon.getCoupon_sn();
if (sn != null) {
CouponParam param = new CouponParam();
param.setUserId((long) uid);
param.setSize(10000);
param.setOffset(0);
//得到用户所有的优惠券的列表
F.Promise<BaseDto> couponListPromise = pmsCouponService.getList(param);
//然后在列表中找出这次绑定的优惠券,获取详细信息。之所以要这样做,因为绑定优惠券的返回结果包含字段不全。
return couponListPromise.map(new F.Function<BaseDto, BaseDto<Coupon>>() {
@Override
public BaseDto<Coupon> apply(BaseDto listBaseDto) throws Throwable {
List<Coupon> couponList = (List<Coupon>)listBaseDto.getData();
if (couponList != null) {
//在列表中找到这次绑定的优惠券
Coupon target = findCouponBySn(couponList, sn);
if (target != null) {
//设置到返回结果
boundCouponBaseDto.setData(target);
}
}
return (BaseDto<Coupon>)boundCouponBaseDto;
}
});
}
}
//如果没有在列表中找到这次绑定的券,则直接返回绑定得到的结果
return F.Promise.pure((BaseDto<Coupon>)boundCouponBaseDto);
}
});
return couponInListPromise.map(new F.Function<BaseDto<?>, Result>() {
@Override
public Result apply(BaseDto<?> baseDto) throws Throwable {
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(baseDto);
return ok(jsonString);
}
});
}
private static Coupon findCouponBySn(List<Coupon> couponList, String sn) {
if (sn == null) {
return null;
}
Coupon target = null;
for (Coupon couponInList : couponList) {
if (sn.equals(couponInList.getCoupon_sn())) {
target = couponInList;
break;
}
}
return target;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment