package com.zy.system.controller;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.core.annotations.ManagerAuth;
|
import com.core.common.Cools;
|
import com.core.common.DateUtils;
|
import com.core.common.R;
|
import com.core.controller.AbstractBaseController;
|
import com.zy.common.i18n.I18nMessageService;
|
import com.zy.system.entity.Resource;
|
import com.zy.system.service.ResourceService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.*;
|
|
@RestController
|
public class ResourceController extends AbstractBaseController {
|
|
@Autowired
|
private ResourceService resourceService;
|
@Autowired
|
private I18nMessageService i18nMessageService;
|
|
@RequestMapping(value = "/resource/{id}/auth")
|
@ManagerAuth
|
public R get(@PathVariable("id") Long id) {
|
return R.ok(resourceService.getById(String.valueOf(id)));
|
}
|
|
@RequestMapping(value = "/resource/list/auth")
|
@ManagerAuth
|
public R list(@RequestParam(defaultValue = "1")Integer curr,
|
@RequestParam(defaultValue = "10")Integer limit,
|
@RequestParam(required = false)String orderByField,
|
@RequestParam(required = false)String orderByType,
|
@RequestParam Map<String, Object> param){
|
excludeTrash(param);
|
QueryWrapper<Resource> wrapper = new QueryWrapper<>();
|
convert(param, wrapper);
|
wrapper.orderBy(true, false, "id");
|
return R.ok(resourceService.page(new Page<>(curr, limit), wrapper));
|
}
|
|
private void convert(Map<String, Object> map, QueryWrapper wrapper){
|
for (Map.Entry<String, Object> entry : map.entrySet()){
|
if (entry.getKey().endsWith(">")) {
|
wrapper.ge(Cools.deleteChar(entry.getKey()), DateUtils.convert(String.valueOf(entry.getValue())));
|
} else if (entry.getKey().endsWith("<")) {
|
wrapper.le(Cools.deleteChar(entry.getKey()), DateUtils.convert(String.valueOf(entry.getValue())));
|
} else {
|
wrapper.eq(entry.getKey(), String.valueOf(entry.getValue()));
|
}
|
}
|
}
|
|
@RequestMapping(value = "/resource/edit/auth")
|
@ManagerAuth(memo = "菜单编辑")
|
public R edit(Resource resource) {
|
if (Cools.isEmpty(resource)){
|
return R.error();
|
}
|
if (null == resource.getId()){
|
if (resource.getSort() == null){
|
resource.setSort(999);
|
}
|
resourceService.save(resource);
|
} else {
|
resourceService.updateById(resource);
|
}
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/resource/add/auth")
|
@ManagerAuth(memo = "菜单添加")
|
public R add(Resource resource) {
|
resourceService.save(resource);
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/resource/update/auth")
|
@ManagerAuth(memo = "菜单修改")
|
public R update(Resource resource){
|
if (Cools.isEmpty(resource) || null==resource.getId()){
|
return R.error();
|
}
|
resourceService.updateById(resource);
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/resource/delete/auth")
|
@ManagerAuth(memo = "菜单删除")
|
public R delete(@RequestParam(value="ids[]") Long[] ids){
|
for (Long id : ids){
|
resourceService.removeById(id);
|
}
|
return R.ok();
|
}
|
|
@RequestMapping(value = "/resource/export/auth")
|
@ManagerAuth(memo = "菜单导出")
|
public R export(@RequestBody JSONObject param){
|
List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class);
|
QueryWrapper<Resource> wrapper = new QueryWrapper<>();
|
Map<String, Object> map = excludeTrash(param.getJSONObject("resource"));
|
convert(map, wrapper);
|
List<Resource> list = resourceService.list(wrapper);
|
return R.ok(exportSupport(list, fields));
|
}
|
|
@RequestMapping(value = "/resourceQuery/auth")
|
@ManagerAuth
|
public R query(String condition) {
|
QueryWrapper<Resource> wrapper = new QueryWrapper<>();
|
wrapper.like("name", condition);
|
Page<Resource> page = resourceService.page(new Page<>(0, 10), wrapper);
|
List<Map<String, Object>> result = new ArrayList<>();
|
for (Resource resource : page.getRecords()){
|
Map<String, Object> map = new HashMap<>();
|
map.put("id", resource.getId());
|
String levelText = i18nMessageService.translateLegacy(Cools.isEmpty(resource.getLevel$()) ? "" : resource.getLevel$());
|
String localizedName = i18nMessageService.resolveResourceText(resource.getName(), resource.getCode(), resource.getId());
|
String shortLevelText = levelText.matches(".*[\\u4E00-\\u9FA5].*")
|
? levelText.substring(0, Math.min(2, levelText.length()))
|
: levelText;
|
map.put("value", localizedName.concat("(").concat(shortLevelText).concat(")"));
|
result.add(map);
|
}
|
return R.ok(result);
|
}
|
|
@RequestMapping(value = "/resource/tree/auth")
|
@ManagerAuth
|
public R tree(@RequestParam(defaultValue = "1")Integer curr,
|
@RequestParam(defaultValue = "10")Integer limit,
|
@RequestParam(required = false)String orderByField,
|
@RequestParam(required = false)String orderByType,
|
@RequestParam Map<String, Object> param){
|
QueryWrapper<Resource> wrapper = new QueryWrapper<>();
|
excludeTrash(param);
|
convert(param, wrapper);
|
if (!Cools.isEmpty(orderByField)){wrapper.orderBy(true, "asc".equals(orderByType), humpToLine(orderByField));}
|
else {
|
wrapper.orderBy(true, true, "sort");
|
}
|
List<Resource> resources = resourceService.list(wrapper);
|
for (Resource resource : resources) {
|
resource.setName(i18nMessageService.resolveResourceText(resource.getName(), resource.getCode(), resource.getId()));
|
}
|
return R.parse("0-操作成功").add(resources);
|
}
|
|
}
|