package com.ks.app.controller.admin;
|
|
import com.google.gson.*;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.makemoney.app.entity.admin.AdminRole;
|
import com.yeshi.makemoney.app.entity.admin.AdminRoleRule;
|
import com.yeshi.makemoney.app.service.inter.admin.AdminRoleRuleService;
|
import com.yeshi.makemoney.app.service.query.admin.AdminRoleRuleQuery;
|
import com.yeshi.makemoney.app.vo.AcceptAdminData;
|
import com.yeshi.makemoney.app.vo.admin.AdminRoleRuleAdminVO;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
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.ResponseBody;
|
import org.yeshi.utils.JsonUtil;
|
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/authority")
|
public class AdminRoleRuleController {
|
|
@Resource
|
private AdminRoleRuleService adminRoleRuleService;
|
|
|
@ResponseBody
|
@RequestMapping("list")
|
public String list(AdminRoleRuleQuery query, int page, int limit) {
|
List<AdminRoleRule> list = adminRoleRuleService.list(query, page, limit);
|
long count = adminRoleRuleService.count(query);
|
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"));
|
}
|
}).registerTypeAdapter(AdminRole.class, new JsonSerializer<AdminRole>() {
|
|
@Override
|
public JsonElement serialize(AdminRole role, Type type, JsonSerializationContext jsonSerializationContext) {
|
return role == null ? new JsonPrimitive("") : new JsonPrimitive(role.getName());
|
}
|
}).create();
|
|
data.put("list", gson.toJson(list));
|
data.put("count", count);
|
return JsonUtil.loadTrueResult(data);
|
|
}
|
|
@ResponseBody
|
@RequestMapping("delete")
|
public String delete(String ids) {
|
Type type = new TypeToken<List<String>>() {
|
}.getType();
|
List<String> idList = new Gson().fromJson(ids, type);
|
adminRoleRuleService.delete(idList);
|
return JsonUtil.loadTrueResult("");
|
|
}
|
|
@ResponseBody
|
@RequestMapping("add")
|
public String add(@RequestBody AdminRoleRuleAdminVO vo, AcceptAdminData acceptAdminData) {
|
try {
|
for (String path : vo.getPaths()) {
|
vo.setPath(path.trim());
|
adminRoleRuleService.add(vo.toEntity(acceptAdminData.getSystem()));
|
}
|
return JsonUtil.loadTrueResult("");
|
} catch (Exception e) {
|
return JsonUtil.loadFalseResult(e.getMessage());
|
|
}
|
}
|
|
@ResponseBody
|
@RequestMapping("get")
|
public String get(String id, AcceptAdminData acceptAdminData) {
|
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"));
|
}
|
}).registerTypeAdapter(AdminRole.class, new JsonSerializer<AdminRole>() {
|
|
@Override
|
public JsonElement serialize(AdminRole role, Type type, JsonSerializationContext jsonSerializationContext) {
|
return role == null ? new JsonPrimitive("") : new JsonPrimitive(role.getName());
|
}
|
}).create();
|
|
|
AdminRoleRule entity = adminRoleRuleService.get(id);
|
if (entity != null) {
|
return JsonUtil.loadTrueResult(gson.toJson(entity));
|
} else {
|
return JsonUtil.loadFalseResult("ID不存在");
|
}
|
}
|
|
|
@ResponseBody
|
@RequestMapping("update")
|
|
public String update(AdminRoleRuleAdminVO vo, AcceptAdminData acceptAdminData) {
|
if (vo.getId() == null) {
|
return JsonUtil.loadFalseResult("ID不能为空");
|
|
}
|
try {
|
adminRoleRuleService.update(vo.toEntity(null));
|
} catch (Exception e) {
|
return JsonUtil.loadFalseResult(e.getMessage());
|
|
}
|
return JsonUtil.loadTrueResult("");
|
|
}
|
|
|
@ResponseBody
|
@RequestMapping("listRoles")
|
public String listRoles(AdminRoleRuleAdminVO vo, AcceptAdminData acceptAdminData) {
|
JSONArray array = new JSONArray();
|
for (AdminRole role : AdminRole.values()) {
|
JSONObject item = new JSONObject();
|
item.put("key", role.name());
|
item.put("value", role.getName());
|
array.add(item);
|
}
|
return JsonUtil.loadTrueResult(array);
|
}
|
|
|
}
|