admin
2019-07-30 714da0c87a1b94adf4057858b9cc0a2569fb0a04
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.yeshi.fanli.controller.client.v2;
 
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.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.JsonUtil;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbar;
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbar.NavbarTypeEnum;
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbarUser;
import com.yeshi.fanli.exception.homemodule.HomeNavbarUserException;
import com.yeshi.fanli.service.inter.homemodule.DeviceSexService;
import com.yeshi.fanli.service.inter.homemodule.HomeNavbarService;
import com.yeshi.fanli.service.inter.homemodule.HomeNavbarUserService;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("api/v2/navbar")
public class HomeNavbarControllerV2 {
 
    @Resource
    private HomeNavbarService homeNavbarService;
 
    @Resource
    private HomeNavbarUserService homeNavbarUserService;
    
    @Resource
    private DeviceSexService deviceSexService;
    
 
 
    /**
     * 改变性别
     * @param acceptData
     * @param sex
     * @param out
     */
    @RequestMapping(value = "changeSex", method = RequestMethod.POST)
    public void changeSex(AcceptData acceptData, Integer sex, PrintWriter out) {
        try {
            // 获取设备定义性别
            int deviceSex = deviceSexService.changeDeviceSex(sex, acceptData.getDevice());
            JSONObject data = new JSONObject();
            data.put("sex", deviceSex);
            out.print(JsonUtil.loadTrueResult(data));
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult("切换失败"));
        }
    }
 
    
    /**
     * 获取导航栏内容
     * @param acceptData
     * @param uid
     * @param sex 0通用 1男 2女
     * @param out
     */
    @RequestMapping(value = "getHomeItems", method = RequestMethod.POST)
    public void getHomeItems(AcceptData acceptData, Long uid, PrintWriter out) {
        
        // 获取设备定义性别
        int deviceSex = deviceSexService.getDeviceSex(acceptData.getDevice());
        
        List<HomeNavbar> listNavbar = homeNavbarUserService.listEffectiveNavbar(uid, acceptData.getDevice(), deviceSex);
        if (listNavbar == null) {
            listNavbar = new ArrayList<HomeNavbar>();
        }
        
        JSONObject data = new JSONObject();
        data.put("sex", deviceSex);
        data.put("count", listNavbar.size());
        data.put("listNavbar", JsonUtil.getApiCommonGson().toJson(listNavbar));
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    /**
     * 获取导航栏编辑内容
     * 
     * @param acceptData
     * @param out
     */
    @RequestMapping(value = "getUserItems", method = RequestMethod.POST)
    public void getUserItems(AcceptData acceptData, Long uid, PrintWriter out) {
        try {
            List<HomeNavbar> listExist = new ArrayList<HomeNavbar>();
            List<HomeNavbar> listItems = new ArrayList<HomeNavbar>();
 
            // 系统默认导航栏
            List<HomeNavbar> listNavbar = homeNavbarService.listQueryEffectiveNavbar();
            if (listNavbar != null && listNavbar.size() > 0) {
                listItems.addAll(listNavbar);
            }
 
            int nonCount = 0;
            // 查询用户自定义导航
            List<HomeNavbarUser> listUser = homeNavbarUserService.listUserNavbar(uid, acceptData.getDevice());
 
            if (listUser == null || listUser.size() == 0) {
                for (int i = 0; i < listItems.size(); i++) {
                    HomeNavbar homeNavbar = listItems.get(i);
                    // 活动剔除
                    if (!NavbarTypeEnum.category.equals(homeNavbar.getType())) {
                        listItems.remove(homeNavbar);
                        i--;
                        continue;
                    }
 
                    if (homeNavbar.getIsDefault()) {
                        if (homeNavbar.getIsFixed()) {
                            nonCount++;
                        }
                        
                        listExist.add(homeNavbar);
                        listItems.remove(homeNavbar);
                        i--;
                        continue;
                    }
                }
 
            } else {
                // 获取固定不可编辑
                for (int i = 0; i < listItems.size(); i++) {
                    HomeNavbar homeNavbar = listItems.get(i);
 
                    // 活动剔除
                    if (!NavbarTypeEnum.category.equals(homeNavbar.getType())) {
                        listItems.remove(homeNavbar);
                        i--;
                        continue;
                    }
 
                    if (homeNavbar.getIsFixed()) {
                        listExist.add(homeNavbar);
                        nonCount++;
 
                        listItems.remove(homeNavbar);
                        i--;
                    }
                }
 
                // 用户编辑的数据
                for (HomeNavbarUser homeNavbarUser : listUser) {
                    HomeNavbar homeNavbar = homeNavbarUser.getHomeNavbar();
                    if (homeNavbar == null) {
                        continue;
                    }
 
                    // 活动剔除
                    if (!NavbarTypeEnum.category.equals(homeNavbar.getType())) {
                        continue;
                    }
 
                    long homeId = homeNavbar.getId();
                    for (int i = 0; i < listItems.size(); i++) {
                        HomeNavbar navbar = listItems.get(i);
                        long id = navbar.getId();
                        if (homeId == id) {
                            // 加入用户已选
                            listExist.add(navbar);
                            // 移除用户已选
                            listItems.remove(navbar);
                            i--;
                            break;
                        }
                    }
                }
            }
 
            JSONObject data = new JSONObject();
            data.put("min", 6);
            data.put("max", 16);
            data.put("nonCount", nonCount);
            data.put("listExist", JsonUtil.getApiCommonGson().toJson(listExist));
            data.put("listItems", JsonUtil.getApiCommonGson().toJson(listItems));
            out.print(JsonUtil.loadTrueResult(data));
        } catch (HomeNavbarUserException e) {
            out.print(JsonUtil.loadFalseResult(1, e.getMsg()));
        }
    }
 
    /**
     * 设置自定义
     * 
     * @param acceptData
     * @param uid
     * @param list
     * @param out
     */
    @RequestMapping(value = "saveUserItems", method = RequestMethod.POST)
    public void saveUserItems(AcceptData acceptData, Long uid, String barIds, PrintWriter out) {
        try {
            if (StringUtil.isNullOrEmpty(barIds)) {
                out.print(JsonUtil.loadFalseResult("Id参数不能为空"));
                return;
            }
 
            Gson gson = new Gson();
            List<Long> list = gson.fromJson(barIds, new TypeToken<ArrayList<Long>>() {
            }.getType());
            if (list == null || list.size() == 0) {
                out.print(JsonUtil.loadFalseResult("未检测到数据"));
                return;
            }
 
            homeNavbarUserService.addNavbarUser(uid, acceptData.getDevice(), list);
            out.print(JsonUtil.loadTrueResult("保存成功"));
            
            // 设置为通用
            deviceSexService.changeDeviceSex(0, acceptData.getDevice());
            
        } catch (HomeNavbarUserException e) {
            out.print(JsonUtil.loadFalseResult(1, e.getMsg()));
        }
    }
 
    /**
     * 还原默认
     * 
     * @param acceptData
     * @param uid
     * @param out
     */
    @RequestMapping(value = "restoreDefault", method = RequestMethod.POST)
    public void restoreDefault(AcceptData acceptData, Long uid, PrintWriter out) {
        try {
            int nonCount = 0;
            List<HomeNavbar> listExist = new ArrayList<HomeNavbar>();
            List<HomeNavbar> listItems = new ArrayList<HomeNavbar>();
            
            
            List<HomeNavbar> listNavbar = homeNavbarUserService.restoreSystemDefault(uid, acceptData.getDevice());
            if (listNavbar != null && listNavbar.size() > 0) {
                listItems.addAll(listNavbar);
            }
 
            for (int i = 0; i < listItems.size(); i++) {
                HomeNavbar homeNavbar = listItems.get(i);
                // 活动剔除
                if (!NavbarTypeEnum.category.equals(homeNavbar.getType())) {
                    listItems.remove(homeNavbar);
                    i--;
                    continue;
                }
                
                // 筛选出默认项
                if (!homeNavbar.getIsDefault()) {
                    break;
                } else {
                    // 固定个数
                    if (homeNavbar.getIsFixed()) {
                        nonCount++;
                    }
                    // 默认项
                    listExist.add(homeNavbar);
                    listItems.remove(homeNavbar);
                    i--;
                    continue;
                }
            }
 
            JSONObject data = new JSONObject();
            data.put("nonCount", nonCount);
            data.put("listExist", JsonUtil.getApiCommonGson().toJson(listExist));
            data.put("listItems", JsonUtil.getApiCommonGson().toJson(listItems));
            out.print(JsonUtil.loadTrueResult(data));
            
            // 设置为通用
            deviceSexService.changeDeviceSex(0, acceptData.getDevice());
                        
        } catch (HomeNavbarUserException e) {
            out.print(JsonUtil.loadFalseResult(1, e.getMsg()));
        }
    }
 
}