package com.ks.push.admin;
|
|
import com.google.gson.*;
|
import com.ks.goldcorn.exception.GoldSourceException;
|
import com.ks.goldcorn.pojo.DO.GoldCornAppInfo;
|
import com.ks.goldcorn.pojo.DO.GoldCornGetSource;
|
import com.ks.goldcorn.service.GoldCornAppManager;
|
import com.ks.goldcorn.service.remote.GoldCornGetSourceService;
|
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/get")
|
public class GetSourceController {
|
Logger logger = LoggerFactory.getLogger(GetSourceController.class);
|
|
@Resource
|
private GoldCornGetSourceService goldCornGetSourceService;
|
|
@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<GoldCornGetSource> list = goldCornGetSourceService.searchByName(appCode, query.getKey(), query.getPage(), query.getLimit());
|
if (list != null) {
|
for (GoldCornGetSource source : list) {
|
source.setApp(app);
|
}
|
}
|
|
long count = goldCornGetSourceService.countByName(appCode, query.getKey());
|
data.put("count", count);
|
data.put("data", gson.toJson(list));
|
|
return JsonUtil.loadTrueResult(data);
|
}
|
|
@RequestMapping("update")
|
@ResponseBody
|
public String update(GoldCornGetSource source) {
|
if (source == null || source.getId() == null) {
|
return JsonUtil.loadFalseResult("id不能为空");
|
}
|
|
try {
|
goldCornGetSourceService.updateSource(source);
|
} catch (GoldSourceException e) {
|
return JsonUtil.loadFalseResult(e.getCode(), e.getMessage());
|
}
|
|
return JsonUtil.loadTrueResult("更新成功");
|
}
|
|
|
@RequestMapping("add")
|
@ResponseBody
|
public String add(GoldCornGetSource 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 {
|
goldCornGetSourceService.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++) {
|
goldCornGetSourceService.deleteSource(array.optLong(i));
|
}
|
return JsonUtil.loadTrueResult("");
|
}
|
|
|
}
|