admin
2020-11-25 06d1a63564b1fc4b85ae86f7e2f80bc56de5c8ca
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
package com.ks.goldcorn.controller;
 
import com.google.gson.*;
import com.ks.goldcorn.exception.GoldSourceException;
import com.ks.goldcorn.pojo.DO.GoldCornAppInfo;
import com.ks.goldcorn.pojo.DO.GoldCornConsumeSource;
import com.ks.goldcorn.pojo.DO.GoldCornGetSource;
import com.ks.goldcorn.service.GoldCornAppManager;
import com.ks.goldcorn.service.remote.GoldCornConsumeSourceService;
import com.ks.goldcorn.vo.CommonSearchQuery;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.StringUtil;
import org.yeshi.utils.TimeUtil;
 
import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
 
@Controller
@RequestMapping("admin/api/source/consume")
public class ConsumeSourceController {
    Logger logger = LoggerFactory.getLogger(ConsumeSourceController.class);
 
    @Resource
    private GoldCornConsumeSourceService goldCornConsumeSourceService;
 
    @Resource
    private GoldCornAppManager goldCornAppManager;
 
    /**
     * @param query
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public String list(CommonSearchQuery query, String appCode) {
        JSONObject data = new JSONObject();
        GoldCornAppInfo app = goldCornAppManager.selectByAppCode(appCode);
        Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                    @Override
                    public JsonElement serialize(Date arg0, Type arg1, JsonSerializationContext arg2) {
                        if (arg0 != null) {
                            return new JsonPrimitive(TimeUtil.getGernalTime(arg0.getTime(), "yyyy-MM-dd HH:mm"));
                        } else {
                            return new JsonPrimitive("");
                        }
                    }
                }
        ).create();
 
        List<GoldCornConsumeSource> list = goldCornConsumeSourceService.searchByName(appCode, query.getKey(), query.getPage(), query.getLimit());
        if (list != null) {
            for (GoldCornConsumeSource source : list) {
                source.setApp(app);
            }
        }
        long count = goldCornConsumeSourceService.countByName(appCode, query.getKey());
        data.put("count", count);
        data.put("data", gson.toJson(list));
 
        return JsonUtil.loadTrueResult(data);
    }
 
    @RequestMapping("update")
    @ResponseBody
    public String update(GoldCornConsumeSource source) {
        if (source == null || source.getId() == null) {
            return JsonUtil.loadFalseResult("id不能为空");
        }
 
        try {
            goldCornConsumeSourceService.updateSource(source);
        } catch (GoldSourceException e) {
            return JsonUtil.loadFalseResult(e.getCode(), e.getMessage());
        }
 
        return JsonUtil.loadTrueResult("更新成功");
    }
 
 
    @RequestMapping("add")
    @ResponseBody
    public String add(GoldCornConsumeSource source, String appCode) {
        if (source == null) {
            return JsonUtil.loadTrueResult("数据不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(appCode)) {
            return JsonUtil.loadTrueResult("请选择应用");
        }
 
        GoldCornAppInfo app = goldCornAppManager.selectByAppCode(appCode);
        if (app == null) {
            return JsonUtil.loadTrueResult("应用不存在");
        }
        source.setAppId(app.getId());
 
        try {
            goldCornConsumeSourceService.addSource(source);
            return JsonUtil.loadTrueResult("");
        } catch (GoldSourceException e) {
            return JsonUtil.loadFalseResult(e.getCode(), e.getMessage());
        }
    }
 
 
    @RequestMapping("delete")
    @ResponseBody
    public String delete(String ids) {
        JSONArray array = null;
        try {
            array = JSONArray.fromObject(ids);
        } catch (Exception e) {
        }
 
        if (array == null || array.size() == 0) {
            return JsonUtil.loadFalseResult("id为空");
        }
 
 
        for (int i = 0; i < array.size(); i++) {
            goldCornConsumeSourceService.deleteSource(array.optLong(i));
        }
        return JsonUtil.loadTrueResult("");
    }
 
 
}