Created
September 30, 2015 04:29
-
-
Save yijunwu/06c3efc3fcb311cd77b5 to your computer and use it in GitHub Desktop.
异步代码的重构实例2——VswmPmsCouponController。这是重构之后的代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Function; | |
import play.libs.F.Promise; | |
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 Promise<Result> bind() { | |
BindParams params = Form.form(BindParams.class).bindFromRequest().get(); | |
final int uid = (int) HttpContext.getUserId(); | |
params.setUserId(uid); | |
//绑定优惠券 | |
Promise<BaseDto> couponPromise = pmsCouponService.bind(params); | |
//从用户的优惠券列表中获取绑定的优惠券,作为返回结果 | |
Promise<BaseDto<Coupon>> resultCouponPromise = augmentCouponBaseDtoPromise(couponPromise, uid); | |
//返回结果 | |
return resultCouponPromise.map(new Function<BaseDto<?>, Result>() { | |
@Override | |
public Result apply(BaseDto<?> baseDto) throws Throwable { | |
ObjectMapper mapper = new ObjectMapper(); | |
String jsonString = mapper.writeValueAsString(baseDto); | |
return ok(jsonString); | |
} | |
}); | |
} | |
/** | |
* 对于绑定优惠券返回的结果(一个包含coupon的BaseDto对象),先查出用户所有的优惠券列表,然后在列表中找到对应的coupon,用列表中的coupon替代优惠券返回的coupon。 | |
* 之所以要这样做,因为绑定优惠券返回的结果coupon包含的字段不全,不包含前端所需要的"满多少减多少"等信息。 | |
*/ | |
private Promise<BaseDto<Coupon>> augmentCouponBaseDtoPromise(Promise<BaseDto> couponPromise, final int uid) { | |
Promise<BaseDto<Coupon>> resultPromise = couponPromise.flatMap(new Function<BaseDto, Promise<BaseDto<Coupon>>>() { | |
@Override | |
public Promise<BaseDto<Coupon>> apply(final BaseDto boundCouponBaseDto) throws Throwable { | |
Coupon coupon = (Coupon)boundCouponBaseDto.getData(); | |
Promise<BaseDto<Coupon>> resultPromise = Promise.pure((BaseDto<Coupon>) boundCouponBaseDto); | |
if (coupon != null) { | |
final String sn = coupon.getCoupon_sn(); | |
if (sn != null) { | |
Promise<Coupon> couponInListPromise = getCouponFromUserCouponList(uid, sn); | |
//在列表中找出这次绑定的优惠券,获取详细信息。 | |
resultPromise = couponInListPromise.map(new Function<Coupon, BaseDto<Coupon>>() { | |
@Override | |
public BaseDto<Coupon> apply(Coupon listBaseDto) throws Throwable { | |
Coupon coupon = listBaseDto; | |
if (coupon != null) { //在列表中找到这次绑定的优惠券 | |
//设置到返回结果 | |
boundCouponBaseDto.setData(coupon); | |
} | |
return (BaseDto<Coupon>)boundCouponBaseDto; | |
} | |
}); | |
} | |
} | |
//如果没有在列表中找到这次绑定的券,则直接返回绑定得到的结果 | |
return resultPromise; | |
} | |
}); | |
return resultPromise; | |
} | |
/** | |
* 得到用户所有的优惠券列表,然后根据输入的sn在列表中找到对应的优惠券 | |
*/ | |
private Promise<Coupon> getCouponFromUserCouponList(final long uid, final String sn) { | |
CouponParam param = new CouponParam(); | |
param.setUserId(uid); | |
param.setSize(10000); | |
param.setOffset(0); | |
//得到用户所有的优惠券的列表 | |
Promise<BaseDto> couponListPromise = pmsCouponService.getList(param); | |
return couponListPromise.map(new Function<BaseDto, Coupon>() { | |
@Override | |
public Coupon apply(BaseDto listBaseDto) throws Throwable { | |
List<Coupon> couponList = (List<Coupon>)listBaseDto.getData(); | |
//在列表中找到sn对应的优惠券 | |
Coupon target = VswmPmsCouponController.findCouponBySn(couponList, sn); | |
return target; | |
} | |
}); | |
} | |
/** | |
* 遍历列表,找到具有指定sn的优惠券 | |
*/ | |
private static Coupon findCouponBySn(List<Coupon> couponList, String sn) { | |
if (couponList == null || 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