admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
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
package com.ks.app.controller.admin.config;
 
import com.google.gson.*;
import com.ks.app.entity.config.SystemConfig;
import com.ks.app.entity.config.SystemConfigKey;
import com.ks.app.entity.config.SystemConfigType;
import com.ks.app.service.inter.config.SystemConfigService;
import com.ks.app.service.query.config.SystemConfigQuery;
import com.ks.app.vo.AcceptAdminData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.TimeUtil;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
 
@Controller
@RequestMapping("/admin/api/config")
public class SystemConfigAdminController {
 
    @Resource
    private SystemConfigService systemConfigService;
 
 
    private String loadPrint(String callback, String root) {
        return root;
    }
 
    @ResponseBody
    @RequestMapping("list")
    public String list(SystemConfigQuery query, int page, int limit, String callback) {
        List<SystemConfig> list = systemConfigService.listByType(query.getType() == null ? SystemConfigType.common : query.getType());
        long count = list.size();
        JSONObject data = new JSONObject();
        Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
 
            @Override
            public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) {
                return date == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(date.getTime(), "yyyy.MM.dd HH:mm"));
            }
        }).create();
 
        data.put("list", gson.toJson(list));
        data.put("count", count);
        return loadPrint(callback, JsonUtil.loadTrueResult(data));
    }
 
    @ResponseBody
    @RequestMapping("add")
    public String add(SystemConfig bean, AcceptAdminData acceptAdminData, String callback) {
        try {
            bean.setSystem(acceptAdminData.getSystem());
            systemConfigService.add(bean);
            return loadPrint(callback, JsonUtil.loadTrueResult(""));
        } catch (Exception e) {
            return loadPrint(callback, JsonUtil.loadFalseResult(e.getMessage()));
        }
    }
 
    @ResponseBody
    @RequestMapping("get")
    public String get(String id, HttpSession session, String callback) {
        SystemConfig entity = systemConfigService.get(id);
        if (entity != null) {
            return loadPrint(callback, JsonUtil.loadTrueResult(entity));
        } else {
            return loadPrint(callback, JsonUtil.loadFalseResult("ID不存在"));
        }
    }
 
 
    @ResponseBody
    @RequestMapping("update")
    public String update(SystemConfig bean, HttpSession session, String callback) {
        if (bean.getId() == null) {
            return loadPrint(callback, JsonUtil.loadFalseResult("ID不能为空"));
        }
        try {
            systemConfigService.update(bean);
        } catch (Exception e) {
            return loadPrint(callback, JsonUtil.loadFalseResult(e.getMessage()));
        }
        return loadPrint(callback, JsonUtil.loadTrueResult(""));
    }
 
 
    @ResponseBody
    @RequestMapping("getKeyList")
    public String getKeyList(SystemConfigType type) {
        JSONArray array = new JSONArray();
        for (SystemConfigKey key : SystemConfigKey.values()) {
            if (key.getType() == (type == null ? SystemConfigType.common : type)) {
                JSONObject data = new JSONObject();
                data.put("key", key.name());
                data.put("value", key.getDesc());
                array.add(data);
            }
        }
        return JsonUtil.loadTrueResult(array);
    }
 
 
}