喻健
2018-11-13 6feaf1324e207ebee22d0e263a8f32061b8374bf
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
package com.yeshi.fanli.controller.admin;
 
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.yeshi.fanli.entity.system.System;
import com.yeshi.fanli.entity.system.SystemConfig;
import com.yeshi.fanli.exception.NotExistObjectException;
import com.yeshi.fanli.service.inter.config.SystemConfigService;
import com.yeshi.fanli.service.inter.config.SystemService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.Utils;
import org.yeshi.utils.JsonUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("admin/new/api/v1/systemConfig")
public class SystemConfigAdminController {
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private SystemService systemService;
 
    @RequestMapping(value = "getSystemConfigList", method = RequestMethod.POST)
    public void getSystemConfigList(int pageIndex, String platform,
            String packages, String key, PrintWriter out) {
 
        platform = Utils.getMap().get(platform);
        System system = systemService.getSystem(platform, packages);
        List<SystemConfig> systemConfigList = systemConfigService
                .getSystemConfigList(pageIndex - 1, system, key);
        int count = systemConfigService.getCount(system, key);
        int totalPage = count % Constant.PAGE_SIZE == 0 ? count
                / Constant.PAGE_SIZE : count / Constant.PAGE_SIZE + 1;
        PageEntity pe = new PageEntity(pageIndex, Constant.PAGE_SIZE, count,
                totalPage);
        Map<String, String> map = new HashMap<String, String>();
        map.put("key", key);
        pe.setParams(map);
        JSONObject data = new JSONObject();
        data.put("pe", pe);
        List<System> systemList = systemService.getSystems();
        data.put("systemList", systemList);
        data.put("systemConfigList", systemConfigList);
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    @RequestMapping(value = "addSystemConfig", method = RequestMethod.POST)
    public void addSystemConfig(@RequestBody SystemConfig sc, PrintWriter out) {
        systemConfigService.addSystemConfig(sc);
        out.print(JsonUtil.loadTrueResult("添加成功"));
    }
    @RequestMapping(value = "setSystem", method = RequestMethod.POST)
    public void setSystem(long id, String type, String platform,
            String packages, PrintWriter out) {
        platform = Utils.getMap().get(platform);
        System system = systemService.getSystem(platform, packages);
        if (system == null) {
            out.print(JsonUtil.loadFalseResult("不存在该系统"));
            return;
        }
        try {
            if (Constant.DEL.equals(type)) {
                systemConfigService.deleteSystem(id, system);
            } else {
                systemConfigService.addSystem(id, system);
            }
            out.print(JsonUtil.loadTrueResult("操作成功"));
        } catch (NotExistObjectException e) {
            out.print(JsonUtil.loadFalseResult("操作失败"));
        }
    }
    
    @RequestMapping(value = "getSystemConfig", method = RequestMethod.POST)
    public void getSystemConfig(long id,PrintWriter out){
        SystemConfig sc =  systemConfigService.getSystemConfig(id);
        if(sc==null){
            out.print(JsonUtil.loadFalseResult("不存在该对象"));
            return;
        }
        out.print(JsonUtil.loadTrueResult(sc));
    }
    
    @RequestMapping(value = "updateSystemConfig", method = RequestMethod.POST)
    public void updateSystemConfig(@RequestBody SystemConfig sc,PrintWriter out){
        try {
            systemConfigService.updateSystemConfig(sc);
            out.print(JsonUtil.loadTrueResult("修改成功"));
        } catch (NotExistObjectException e) {
            out.print(JsonUtil.loadFalseResult(e.getMessage()));
        }
    }
    
    @RequestMapping(value = "deleteSystemConfig", method = RequestMethod.POST)
    public void deleteSystemConfig(long[] ids,PrintWriter out){
        for (long id : ids) {
            systemConfigService.deleteSystemConfig(id);
        }
        out.print(JsonUtil.loadTrueResult("删除成功"));
    }
    
    @RequestMapping(value="get")
    public void get(String key,PrintWriter out){
        String value="";
        try {
            value = systemConfigService.get(key);
        } catch (NotExistObjectException e) {
            out.print(JsonUtil.loadFalseResult(e.getMessage()));
            return;
        }
        out.print(JsonUtil.loadTrueResult(value));
    }
}