admin
2019-05-10 1980a44902bc179420448d96100e0c5477a93005
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
package com.yeshi.fanli.controller.admin;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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.clazz.GoodsSubClassLabel;
import com.yeshi.fanli.exception.GoodsClassException;
import com.yeshi.fanli.service.inter.clazz.GoodsSubClassLabelService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("admin/new/api/v1/goodsSubClassLabel")
public class GoodsSubClassLabelAdminController {
 
    @Resource
    private GoodsSubClassLabelService goodsSubClassLabelService;
 
    /**
     * 标签列表
     * @param callback
     * @param classId
     * @param pageIndex
     * @param pageSize
     * @param request
     * @param out
     */
    @RequestMapping(value = "labelList")
    public void labelList(String callback, Long classId, int pageIndex, int pageSize, HttpServletRequest request,
            PrintWriter out) {
 
        if (classId != null && classId == 0)
            classId = null;
        List<GoodsSubClassLabel> list = goodsSubClassLabelService.listLabelByClassId(classId);
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.serializeNulls(); // 重点
        Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
 
        JSONObject data = new JSONObject();
        data.put("resultList", gson.toJson(list));
        PageEntity pe = new PageEntity(pageIndex, pageSize, list.size(), 1);
        data.put("pe", pe);
        out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
    }
 
    /**
     * 保存信息
     * 
     * @param callback
     * @param special
     * @param out
     */
    @RequestMapping(value = "saveLabel")
    public void save(String callback, GoodsSubClassLabel label, HttpServletRequest request, PrintWriter out) {
        if (StringUtil.isNullOrEmpty(label.getName()) || label.getGoodsClass() == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("数据不完整"));
            return;
        }
        if (label.getId() == null)// 新增
        {
            if (label.getGoodsClass().getId() == null) {
                JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("分类不能为空"));
                return;
            }
 
            label.setCreateTime(new Date());
            try {
                goodsSubClassLabelService.addSubClassLabel(label);
            } catch (GoodsClassException e) {
                JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMessage()));
            }
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("添加成功"));
        } else {// 修改
            goodsSubClassLabelService.updateSubClassLabel(label);
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("修改成功"));
        }
    }
 
    /**
     * 批量删除
     * 
     * @param callback
     * @param ids
     * @param out
     */
    @RequestMapping(value = "deleteLabelBatch")
    public void deleteBatch(String callback, String ids, PrintWriter out) {
        Gson gson = new Gson();
        try {
            List<String> recordIds = gson.fromJson(ids, new TypeToken<ArrayList<String>>() {
            }.getType());
 
            if (recordIds == null || recordIds.size() == 0) {
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("请选择需删除数据")));
            } else {
 
                for (String id : recordIds)
                    goodsSubClassLabelService.deleteLabel(Long.parseLong(id));
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("删除成功")));
            }
        } catch (Exception e) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult("操作异常")));
            e.printStackTrace();
        }
    }
 
}