package com.vincent.rsf.server.system.controller;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.vincent.rsf.framework.common.Cools;
|
import com.vincent.rsf.framework.common.R;
|
import com.vincent.rsf.framework.exception.CoolException;
|
import com.vincent.rsf.server.common.utils.ExcelUtil;
|
import com.vincent.rsf.server.common.annotation.OperationLog;
|
import com.vincent.rsf.server.common.domain.BaseParam;
|
import com.vincent.rsf.server.common.domain.KeyValVo;
|
import com.vincent.rsf.server.common.domain.PageParam;
|
import com.vincent.rsf.server.system.entity.DictData;
|
import com.vincent.rsf.server.system.service.DictDataService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.util.*;
|
|
@RestController
|
public class DictDataController extends BaseController {
|
|
@Autowired
|
private DictDataService dictDataService;
|
|
@PreAuthorize("hasAuthority('system:dictData:list')")
|
@PostMapping("/dictData/page")
|
public R page(@RequestBody Map<String, Object> map) {
|
BaseParam baseParam = buildParam(map, BaseParam.class);
|
PageParam<DictData, BaseParam> pageParam = new PageParam<>(baseParam, DictData.class);
|
return R.ok().add(dictDataService.page(pageParam, pageParam.buildWrapper(true)));
|
}
|
|
@PreAuthorize("hasAuthority('system:dictData:list')")
|
@PostMapping("/dictData/list")
|
public R list(@RequestBody Map<String, Object> map) {
|
return R.ok().add(dictDataService.list());
|
}
|
|
@PreAuthorize("hasAuthority('system:dictData:list')")
|
@PostMapping({"/dictData/many/{ids}", "/dictDatas/many/{ids}"})
|
public R many(@PathVariable String[] ids) {
|
if (ids == null || ids.length == 0) {
|
return R.ok().add(Collections.emptyList());
|
}
|
List<Long> idList = new ArrayList<>();
|
List<String> valueList = new ArrayList<>();
|
for (String raw : ids) {
|
if (raw == null) {
|
continue;
|
}
|
String item = raw.trim();
|
if (item.isEmpty()) {
|
continue;
|
}
|
valueList.add(item);
|
if (item.matches("^\\d+$")) {
|
try {
|
idList.add(Long.parseLong(item));
|
} catch (NumberFormatException ignored) {
|
}
|
}
|
}
|
if (valueList.isEmpty()) {
|
return R.ok().add(Collections.emptyList());
|
}
|
LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
wrapper.in(DictData::getValue, valueList);
|
if (!idList.isEmpty()) {
|
wrapper.or().in(DictData::getId, idList);
|
}
|
return R.ok().add(dictDataService.list(wrapper));
|
}
|
|
@PreAuthorize("hasAuthority('system:dictData:list')")
|
@GetMapping("/dictData/{id}")
|
public R get(@PathVariable("id") String id) {
|
if (id == null || id.trim().isEmpty()) {
|
return R.ok().add(null);
|
}
|
String key = id.trim();
|
DictData data = null;
|
if (key.matches("^\\d+$")) {
|
try {
|
data = dictDataService.getById(Long.parseLong(key));
|
} catch (NumberFormatException ignored) {
|
}
|
}
|
if (data == null) {
|
data = dictDataService.getOne(new LambdaQueryWrapper<DictData>().eq(DictData::getValue, key), false);
|
}
|
return R.ok().add(data);
|
}
|
|
@PreAuthorize("hasAuthority('system:dictData:save')")
|
@OperationLog("Create 字典数据集")
|
@PostMapping("/dictData/save")
|
public R save(@RequestBody DictData dictData) {
|
if (Objects.isNull(dictData.getLabel())) {
|
throw new CoolException("别名不能为空!!");
|
}
|
if (Objects.isNull(dictData.getValue())) {
|
throw new CoolException("值不能为空!!");
|
}
|
if (Objects.isNull(dictData.getDictTypeCode())) {
|
throw new CoolException("编码不能为空!!");
|
}
|
if (Objects.isNull(dictData.getDictTypeId())) {
|
throw new CoolException("主单ID不能为空!!");
|
}
|
dictData.setCreateBy(getLoginUserId());
|
dictData.setUpdateBy(getLoginUserId());
|
if (!dictDataService.save(dictData)) {
|
return R.error("Save Fail");
|
}
|
return R.ok("Save Success").add(dictData);
|
}
|
|
@PreAuthorize("hasAuthority('system:dictData:update')")
|
@OperationLog("Update 字典数据集")
|
@PostMapping("/dictData/update")
|
public R update(@RequestBody DictData dictData) {
|
dictData.setUpdateBy(getLoginUserId());
|
dictData.setUpdateTime(new Date());
|
if (!dictDataService.updateById(dictData)) {
|
return R.error("Update Fail");
|
}
|
return R.ok("Update Success").add(dictData);
|
}
|
|
@PreAuthorize("hasAuthority('system:dictData:remove')")
|
@OperationLog("Delete 字典数据集")
|
@PostMapping("/dictData/remove/{ids}")
|
public R remove(@PathVariable String[] ids) {
|
if (ids == null || ids.length == 0) {
|
return R.ok("Delete Success").add(Collections.emptyList());
|
}
|
List<Long> idList = new ArrayList<>();
|
List<String> valueList = new ArrayList<>();
|
for (String raw : ids) {
|
if (raw == null) {
|
continue;
|
}
|
String item = raw.trim();
|
if (item.isEmpty()) {
|
continue;
|
}
|
valueList.add(item);
|
if (item.matches("^\\d+$")) {
|
try {
|
idList.add(Long.parseLong(item));
|
} catch (NumberFormatException ignored) {
|
}
|
}
|
}
|
if (valueList.isEmpty()) {
|
return R.ok("Delete Success").add(Collections.emptyList());
|
}
|
LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
wrapper.in(DictData::getValue, valueList);
|
if (!idList.isEmpty()) {
|
wrapper.or().in(DictData::getId, idList);
|
}
|
List<DictData> rows = dictDataService.list(wrapper);
|
List<Long> removeIds = new ArrayList<>();
|
for (DictData row : rows) {
|
if (row != null && row.getId() != null) {
|
removeIds.add(row.getId());
|
}
|
}
|
if (!removeIds.isEmpty() && !dictDataService.removeByIds(removeIds)) {
|
return R.error("Delete Fail");
|
}
|
return R.ok("Delete Success").add(ids);
|
}
|
|
@PreAuthorize("hasAuthority('system:dictData:list')")
|
@PostMapping("/dictData/query")
|
public R query(@RequestParam(required = false) String condition) {
|
List<KeyValVo> vos = new ArrayList<>();
|
LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
|
if (!Cools.isEmpty(condition)) {
|
wrapper.like(DictData::getId, condition);
|
}
|
dictDataService.page(new Page<>(1, 30), wrapper).getRecords().forEach(
|
item -> vos.add(new KeyValVo(item.getId(), item.getId()))
|
);
|
return R.ok().add(vos);
|
}
|
|
@PreAuthorize("hasAuthority('system:dictData:list')")
|
@PostMapping("/dictData/export")
|
public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
|
ExcelUtil.build(ExcelUtil.create(dictDataService.list(), DictData.class), response);
|
}
|
|
}
|