yujian
2019-02-26 e0341e9f2bba31cbb9072a7ecfda2f9766b0aae6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.yeshi.fanli.controller.apph5;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.JsonUtil;
 
import com.alibaba.fastjson.JSONArray;
import com.yeshi.fanli.entity.bus.user.ShamUser;
import com.yeshi.fanli.service.inter.user.ShamUserService;
 
import net.sf.json.JSONObject;
 
/**
 * 用户
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("api/apph5/v1/user")
public class AppH5UserController {
 
 
    @Resource
    private ShamUserService shamUserService;
 
    
    /**
     * 获取抽奖广告列表
     * 
     * @param callback
     * @param out
     */
    @RequestMapping(value = "getRadioList")
    public void getRadioList(String callback, PrintWriter out) {
 
        try {
            // 随机20条数据
            List<ShamUser> listUser = shamUserService.listRandUser(20);
 
            JSONArray array = new JSONArray();
            for (ShamUser shamUser : listUser) {
                JSONObject dataInfo = new JSONObject();
 
                String name = shamUser.getName();
                if (name.length() == 1) {
                    name = "Jx****" + name;
                } else {
                    name = name.substring(0, 1) + "****" + name.substring(name.length() - 2, name.length() - 1);
                }
 
                String prize = generateAward();
                if (prize == null) {
                    prize = "抽中一张福利免单券";
                }
 
                dataInfo.put("pic", shamUser.getPicUrl());
                dataInfo.put("content", name + ",抽中" + prize);
                array.add(dataInfo);
            }
 
            JSONObject data = new JSONObject();
            data.put("result_list", array);
 
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
 
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作失败"));
            e.printStackTrace();
        }
    }
 
 
    /**
     * 生成奖项
     * 
     * @return
     */
    public String generateAward() {
 
        RandomGift randomGift1 = new RandomGift();
        randomGift1.prize = "抽中华为手机20一台";
        randomGift1.probability = 2;
 
        RandomGift randomGift2 = new RandomGift();
        randomGift2.prize = "现金红包¥188";
        randomGift2.probability = 5;
 
        RandomGift randomGift3 = new RandomGift();
        randomGift3.prize = "现金红包¥88";
        randomGift3.probability = 13;
 
        RandomGift randomGift4 = new RandomGift();
        randomGift4.prize = "一张福利免单券";
        randomGift4.probability = 40;
 
        RandomGift randomGift5 = new RandomGift();
        randomGift5.prize = "一张返利奖励券";
        randomGift5.probability = 40;
 
        List<RandomGift> giftList = new ArrayList<RandomGift>();
        giftList.add(randomGift1);
        giftList.add(randomGift2);
        giftList.add(randomGift3);
        giftList.add(randomGift4);
        giftList.add(randomGift5);
 
        long result = (1 + Math.round(Math.random() * (99)));
 
        int minRange = 0;
        int maxRange = 0;
 
        String prize = null;
 
        for (int i = 0; i < giftList.size(); i++) {
            RandomGift obj2 = giftList.get(i);
            int probability = obj2.probability;
 
            maxRange = maxRange + probability;
            minRange = 100 - maxRange;
 
            if (probability != 0) {
                if (result > minRange && result <= maxRange) {
                    prize = obj2.prize;
                    break;
                }
            }
        }
 
        return prize;
    }
 
    class RandomGift {
        public String prize;// 奖项
        public int probability; // 概率
    }
}