admin
2019-07-24 c8a99ed8218fe034e9c33236969fcbec9eac75bb
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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;
    }
 
    
 
    /**
     * 获取抽奖广告列表
     * 
     * @param callback
     * @param out
     */
    @RequestMapping(value = "getDailyRadioList")
    public void getDailyRadioList(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 = dailyGenerateAward();
                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 dailyGenerateAward() {
 
        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; // 概率
    }
}