概率抽奖

    /**
     * 后台抽奖,1天1次
     */
    public void index(){
        Integer userId=null;
        String msg="";
        int awardId=0;
        int index=0;
        Integer unlottery=null;//未中奖
        boolean flag=false;//默认今天未抽奖
        //从session获取用户的
        HttpSession session=super.getSession();
        Record users=(Record) session.getAttribute("users");
        if(users==null){
            userId=getParaToInt();//网页获取
        }else{
            userId=users.getInt("id");//微信获取
        }
        //award表中,奖品列表
        String awardSql="SELECT id,name,level,probablity "
                + "FROM award WHERE activity=1 AND stock>0 ORDER BY level ASC";//0为积分商城,1为抽奖
        List awardList=Db.find(awardSql);

        //awardexchange表中,所有人的中奖纪录
        String arSql="SELECT t.truckNum,a.mobile,a.awardName "
                + " FROM awardexchange a LEFT JOIN truck t ON a.userId=t.usersId "
                + " WHERE activity=1 AND  a.userId IS NOT NULL AND a.mobile IS NOT NULL";
        List arList=Db.find(arSql);
        //判断当前用户是否抽过奖,将该lotteryTime存放在签到表中
        String lotterySql="SELECT id,userId,lotteryTime "
                + "FROM signin WHERE userId="+userId;
        Record lottery=Db.findFirst(lotterySql);
        if(lottery==null){
            //用户未签过到且未抽过
            msg="用户未签到且未抽奖";
        }else{
            Date lotteryTime=lottery.getDate("lotteryTime");
            if(lotteryTime!=null){
                //判断今天是否抽过奖
                Date today=getTodayStartTime();
                if(today.after(lotteryTime)){
                    msg="今天还未抽过奖";
                }else{
                    flag=true;
                    msg="今天已经抽过奖了";
                }
            }else{
                msg="用户未曾抽过奖";
            }
        }
        StringBuffer awardNames=new StringBuffer();
        int[] idArr=new int[10];//奖品ID
        int[] proArr=new int[10];//概率
        int i=0;
        String awardName="";
        for(Record record:awardList){
            if(i>=10)break;
            idArr[i]=record.get("id");
            proArr[i]=record.get("probablity");
            awardName=record.getStr("name");
            awardNames.append(awardName);
            awardNames.append(",");
            if("再接再励".equalsIgnoreCase(awardName)){
                unlottery=i;
            }
            i++;
        }
        awardNames.append("谢谢光临");
        if(!flag&&userId!=null){//今天未抽奖且存在用户
            int result=getResult(proArr);//若result为-1表示未中奖
            if(result!=-1){
                awardId=idArr[result];
                index=result+1;
            }else{//未中奖,将其放置到再接再励
                if(unlottery!=null){
                    index=unlottery+1;
                    awardId=idArr[unlottery];
                }else{//如果运营没有再接再励,就指向谢谢光临
                    index=i+1;
                    awardId=0;
                }
            }
        }else{//如果今天抽过奖
            index=i+1;
            awardId=0;
        }
        setAttr("awardId",awardId);
        setAttr("index", index);
        setAttr("userId", userId);
        setAttr("awardNames",awardNames.toString());
        setAttr("arList",arList);
        render("/front/lottery/lottery.jsp");
    }

/**
* 抽奖算法
* @probArr概率矩阵
* @return -1表示未中奖
*/

public int getResult(int[] probArr){
        int len=probArr.length;
        List addPro=new ArrayList();//计算累计概率
        for(int i=0;iif(i==0){
                addPro.add(i, probArr[i]);
            }else{
                addPro.add(i, addPro.get(i-1)+probArr[i]);
            }
        }
        //开始摇号
        int randNum=(int) Math.floor(99*Math.random());
        for(int j=0;jif(addPro.get(j)>randNum){
                return j;
            }
        }
        return -1;
    }

你可能感兴趣的:(抽奖)