admin
2019-04-29 884196860ac5970eb1ac33386c65378284df4679
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package com.yeshi.fanli.controller.admin;
 
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import net.sf.json.JSONObject;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.yeshi.utils.JsonUtil;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.entity.bus.lable.Label;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.exception.LabelException;
import com.yeshi.fanli.service.AdminUserService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.lable.LabelGoodsService;
import com.yeshi.fanli.service.inter.lable.LabelService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
@Controller
@RequestMapping("admin/new/api/v1/lable")
public class LableAdminController {
 
    @Resource
    private LabelService labelService;
    
    @Resource
    private ConfigService configService;
    
    @Resource
    private AdminUserService adminUserService;
    
    @Resource
    private LabelGoodsService labelGoodsService;
    
    /**
     * 添加标签并上传图片
     * @param callback
     * @param label
     * @param request
     * @param out
     */
    @RequestMapping(value = "saveAdd")
    public void saveAdd(String callback, Long uid, Label label, HttpServletRequest request, PrintWriter out) {
        try {
            String title = label.getTitle();
            if (StringUtil.isNullOrEmpty(title)) {
                out.print(JsonUtil.loadFalseResult("标签名称为空"));
                return;
            }
 
            List<Label> labels = labelService.selectByTitle(title.trim());
            AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
            if (labels == null || labels.size() == 0) {
 
                label.setTitle(label.getTitle().trim());
 
                if (request instanceof MultipartHttpServletRequest) {
 
                    List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
 
                    if (files != null && files.size() > 0) {
                        labelService.insertSingle(label, admin, files.get(0));
                    } else {
                        labelService.insertSingle(label, admin, null);
                    }
 
                } else {
                    labelService.insertSingle(label, admin, null);
                }
 
                out.print(JsonUtil.loadTrueResult("添加成功"));
 
            } else {
                out.print(JsonUtil.loadTrueResult("已存在此标签"));
            }
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult("操作异常"));
            e.printStackTrace();
        }
    }
    
    
    
    /**
     * 批量添加标签
     * @param callback
     * @param label
     * @param request
     * @param out
     */
    @RequestMapping(value = "addBatch")
    public void addBatch(String callback, String param,    HttpServletRequest request, PrintWriter out) {
 
        try {
            Gson gson = new Gson();
            List<Label> labs = gson.fromJson(param,    new TypeToken<ArrayList<Label>>() {}.getType());
        
            List<Label> newList = new ArrayList<Label>();
            if (labs == null || labs.size() == 0) {
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未检索到数据")));
                return;
            } 
            
            for (Label label: labs) {
                String title = label.getTitle();
                if (!StringUtil.isNullOrEmpty(title)) {
                    
                    List<Label> labels = labelService.selectByTitle(title.trim());
                    if (labels == null || labels.size() == 0) {
                        newList.add(label);
                    }
                }
            }
            
            AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
            labelService.insertList(newList, admin);
 
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("添加成功")));
        } catch (LabelException e) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("添加异常")));
            e.printStackTrace();
        }
 
    }
    
    
 
    
    /**
     * 保存修改标签
     * @param callback
     * @param label
     * @param request
     * @param out
     */
    @RequestMapping(value = "saveModify")
    public void saveModify(String callback, Label label, PrintWriter out) {
        
        try {
            Long id = label.getId();
            Label resultObj = labelService.selectByPrimaryKey(id);
            
            if (resultObj == null) {
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该标签不存在或已被删除")));
            } else {
                Date nowTime = new Date();
                label.setUpdatetime(nowTime);
                // 选择性更新数据
                String title = label.getTitle();
                if (StringUtil.isNullOrEmpty(title)) {
                    out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("标签名称不能为空")));
                } else {
                    List<Label> labels = labelService.selectByTitle(label.getTitle().trim());
                    
                    if (labels != null && labels.size() > 0) {
                        out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("标签名称重复,修改失败")));
                    } else {
                        
                        int result = labelService.updateByPrimaryKeySelective(label);
                        
                        if (result > 0) {
                            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("修改成功")));
                        } else {
                            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("修改失败")));
                        }
                    }
                    
                }
                
            }
            
        } catch (LabelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
        }
            
    }
    
    
    /**
     * 批量删除2--- 跨域
     * @param callback
     * @param ids
     * @param out
     */
    @RequestMapping(value = "deleteLable")
    public void deleteLable(String callback, String ids, PrintWriter out) {
        try {
            
            Gson gson = new Gson();
            List<Long> labs = gson.fromJson(ids, new TypeToken<ArrayList<Long>>() {}.getType());
            
            if (labs == null || labs.size() == 0) {
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未检测到删除的数据")));
            } else {
                
                labelService.deleteBatchByPrimaryKey(labs);
                
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("删除成功")));
            }
            
        } catch (LabelException e) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("删除失败")));
            e.printStackTrace();
        }
        
    }
    
 
    /**
     * 上传excel文件
     * @param callback
     * @param file
     * @param request
     * @param out
     * @param response
     */
    @RequestMapping(value = "uploadFile")
    public void uploadFile(@RequestParam("file") CommonsMultipartFile file,    HttpServletRequest request,
            PrintWriter out) {
 
        if (file == null) {
            out.print(JsonUtil.loadFalseResult("文件不能为空!"));
            return;
        }
 
        try {
            AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
            labelService.analysisExcel(file.getInputStream(), admin);
 
            out.print((JsonUtil.loadTrueResult("上传成功")));
            // response.getWriter().print(JsonUtil.loadTrueResult("上传成功"));
 
        } catch (Exception e) {
            e.printStackTrace();
            out.print((JsonUtil.loadFalseResult("上传失败")));
        }
 
    }
    
    
    /**
     * 上传标签图片
     * @param callback
     * @param file
     * @param request
     * @param out
     * @param response
     */
    @RequestMapping(value = "uploadImg")
    public void uploadImg(Long id, @RequestParam("file") CommonsMultipartFile file, PrintWriter out) {
 
        try {
            Label label = labelService.selectByPrimaryKey(id);
 
            if (label == null) {
                out.print(JsonUtil.loadFalseResult("该标签不存在或已被删除"));
            } else {
 
                if (file == null) {
                    out.print(JsonUtil.loadFalseResult("文件不能为空!"));
                } else {
                    int result = labelService.uploadPicture(file, label);
 
                    if (result == 0) {
                        out.print(JsonUtil.loadFalseResult("图片上传成功,数据更新失败"));
                    } else if (result == 1) {
                        out.print(JsonUtil.loadTrueResult("图片上传成功"));
                    } else {
                        out.print(JsonUtil.loadFalseResult("图片上传失败"));
                    }
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
            out.print(JsonUtil.loadFalseResult("操作失败"));
        }
 
    }
    
    /**
     * 删除图片
     * @param callback
     * @param file
     * @param request
     * @param out
     * @param response
     */
    @RequestMapping(value = "deleteImg")
    public void deleteImg(String callback, Long id, PrintWriter out) {
 
        try {
            Label label = labelService.selectByPrimaryKey(id);
 
            if (label == null) {
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("该标签不存在或已被删除")));
            } else {
 
                
                int result = labelService.deleteImg(label);
                if (result == 0) {
                    out.print(JsonUtil.loadJSONP(callback,JsonUtil.loadFalseResult("图片删除成功,数据更新失败")));
                } else if (result == 1) {
                    out.print(JsonUtil.loadJSONP(callback,JsonUtil.loadTrueResult("图片删除成功")));
                } else {
                    out.print(JsonUtil.loadJSONP(callback,JsonUtil.loadFalseResult("图片删除失败")));
                }
                
            }
 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
        }
 
    }
    
    
    /**
     * 查找所有标签
     * @param callback
     * @param pageIndex
     * @param key
     * @param startTime
     * @param endTime
     * @param out
     */
    @RequestMapping(value = "query")
    public void query(String callback, Integer pageIndex, String key, String startTime,
            String endTime, String orderMode, PrintWriter out) {
 
        try {
 
            // 起始时间 为空设置默认值:
            if (StringUtil.isNullOrEmpty(startTime)) {
                startTime = "1970-01-01";
            }
 
            // 结束时间 为空设置默认值当前日期
            if (StringUtil.isNullOrEmpty(endTime)) {
                Date curDate = new Date();
                endTime = TimeUtil.getSimpleDate(curDate);
            }
 
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date enddate = sdf.parse(endTime);
            Calendar c = Calendar.getInstance();
            c.setTime(enddate);
            c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
 
            endTime = sdf.format(c.getTime());
 
            int pageSize = Constant.PAGE_SIZE;
            
            List<Label> labelList = labelService.query((pageIndex - 1) * pageSize, pageSize, key,
                    startTime, endTime,orderMode);
            
            if (labelList == null || labelList.size() == 0) {
                
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("未查找到相关数据")));
            
            } else {
                
                /* 统计每个标签关联商品数量*/
                for (Label label :labelList) {
                    Long relationNum = labelGoodsService.getRelationNum(label.getId());
                    if (relationNum == null)
                        relationNum = 0l;
                    
                    label.setCountRelation(relationNum); // 关联标签商品数量
                    
                    Long iosClick = label.getIosClick();
                    if (iosClick == null)
                        iosClick = 0l;
                    
                    Long androidClick = label.getAndroidClick();
                    if (androidClick == null)
                        androidClick = 0l;
                    
                    
                    label.setCountClick(iosClick + androidClick);// 总点击数量
                    
                    label.setIosClick(0l);
                    label.setAndroidClick(0l);
                }
                
                
                int count = labelService.getQueryCount(key, startTime, endTime);
                
                int totalPage = count % pageSize == 0 ? count / pageSize : count
                        / pageSize + 1;
                PageEntity pe = new PageEntity(pageIndex, pageSize, count,
                        totalPage);
                
                GsonBuilder gsonBuilder = new GsonBuilder();  
                gsonBuilder.serializeNulls(); //重点
                Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
                
                JSONObject data = new JSONObject();
                data.put("pe", pe);
                data.put("labelList", gson.toJson(labelList));
                
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
            }
 
        } catch (Exception e) {
            e.printStackTrace();
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("系统异常")));
        }
    }
    
    /**
     * 查找所有标签
     * @param callback
     * @param out
     */
    @RequestMapping(value = "getCountToday")
    public void getCountToday(String callback, PrintWriter out) {
 
        try {
            
            long totalToday = labelService.getCountToday();
            
            Map<String, Object> count = labelService.getCountByEntryMode();
                    
            JSONObject data = new JSONObject();
            data.put("count", count);
            data.put("totalToday", totalToday);
                
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
 
        } catch (Exception e) {
            e.printStackTrace();
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("系统异常")));
        }
    }
    
 
}