admin
2020-04-28 061e912ff3cec51f8f8dcb4e6336bcd2fc03c164
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
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.goods.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 || label.getOrderBy() == 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 {
                String[] names = label.getName().replace(",", ",").split(",");
                int orderBy = label.getOrderBy();
                for (String name : names) {
                    label.setId(null);
                    label.setOrderBy(orderBy++);
                    label.setName(name);
                    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();
        }
    }
 
}