PreActivityHandler.java 1.71 KB
package com.zhaoonline.coupen.handler;

import com.zhaoonline.coupen.bean.Activity;
import com.zhaoonline.coupen.bean.CommonCoupenEntity;
import com.zhaoonline.coupen.dispatcher.UserCoupenMappingHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * Created by ZhaoOnline<br/>
 * User: yangyoupeng<br/>
 * Date: 2016/12/17<br/>
 * Time: 10:22<br/>
 * Description:在处理红包之前。
 */
public class PreActivityHandler {

    private UserCoupenMappingHandler userCoupenMappingHandler= null;

    @Autowired
    public PreActivityHandler(UserCoupenMappingHandler userCoupenMappingHandler){
        this.userCoupenMappingHandler =  userCoupenMappingHandler;
    }

    /**
     * 这是是要对根据用户和Activity来判断
     * @param activity
     * @param context
     * @return
     */
    public boolean preProcessRequest(Activity activity,DrawContext context){
            List<CommonCoupenEntity> coupnesOfUserInOneActivity=this.userCoupenMappingHandler.getMappingOfUser(activity,context.getDrawOwner());
            if(ObjectUtils.isEmpty(coupnesOfUserInOneActivity) || checkBeyondDrawLimit(context, coupnesOfUserInOneActivity)){
                return true;
            }
            return false;
    }


    /**判断是否用户领取已经超出了限制次数。正常逻辑,每个用户在一个活动中只允许领取一个红包
     * @param context
     * @param coupnesOfUserInOneActivity
     * @return
     */
    private boolean checkBeyondDrawLimit(DrawContext context, List<CommonCoupenEntity> coupnesOfUserInOneActivity) {
        return coupnesOfUserInOneActivity.size() <context.getConfig().getRepeatDrawCount();
    }
}