Files
emo-grab/src/main/java/quant/rich/emoney/controller/manage/ProxySettingControllerV1.java

155 lines
5.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package quant.rich.emoney.controller.manage;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import quant.rich.emoney.controller.common.BaseController;
import quant.rich.emoney.entity.sqlite.ProxySetting;
import quant.rich.emoney.exception.RException;
import quant.rich.emoney.pojo.dto.LayPageReq;
import quant.rich.emoney.pojo.dto.LayPageResp;
import quant.rich.emoney.pojo.dto.R;
import quant.rich.emoney.service.sqlite.ProxySettingService;
@Slf4j
@Controller
@RequestMapping("/admin/v1/manage/proxySetting")
public class ProxySettingControllerV1 extends BaseController {
@Autowired
ProxySettingService proxySettingService;
@GetMapping({"", "/", "/index"})
public String index() {
return "/admin/v1/manage/proxySetting/index";
}
@GetMapping("/list")
@ResponseBody
public LayPageResp<?> list(LayPageReq<ProxySetting> pageReq) {
Page<ProxySetting> planPage = proxySettingService.page(pageReq);
return new LayPageResp<>(planPage);
}
@GetMapping("/getOne")
@ResponseBody
public R<?> getOne(String id) {
// 如果 planId 是空,说明可能希望新建一个 ProxySetting需要返回默认实例化对象
if (id == null) {
return R.ok(new ProxySetting());
}
// 否则从数据库取
ProxySetting proxy = proxySettingService.getById(id);
return R.judge(proxy != null, proxy, "无法找到对应 ID 的 ProxySetting");
}
@PostMapping("/updateBool")
@ResponseBody
public R<?> updateBool(String id, String field, Boolean value) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(ProxySetting.class);
try {
Field declaredField = ProxySetting.class.getDeclaredField(field);
Optional<TableFieldInfo> fieldInfo = tableInfo.getFieldList().stream()
.filter(f -> f.getProperty().equals(field))
.findFirst();
if (declaredField.getType().equals(Boolean.class)) {
proxySettingService.update(
new UpdateWrapper<ProxySetting>()
.eq("id", id)
.set(fieldInfo.get().getColumn(), value));
return R.ok();
}
}
catch (Exception e) {}
throw RException.badRequest();
}
@PostMapping("/save")
@ResponseBody
public R<?> save(@RequestBody ProxySetting proxySetting) {
if (!Objects.isNull(proxySetting.getId())) {
proxySettingService.updateById(proxySetting);
}
else {
proxySettingService.save(proxySetting.setId(null));
}
return R.ok();
}
@PostMapping("/delete")
@ResponseBody
public R<?> delete(String id) {
return R.judge(proxySettingService.removeById(id), "删除失败,是否已删除?");
}
@PostMapping("/batchOp")
@ResponseBody
public R<?> batchOp(
@RequestParam(value="ids[]", required=true)
String[] ids, ProxySettingBatchOp op) {
if (Objects.isNull(ids) || ids.length == 0) {
throw RException.badRequest("提供的计划 ID 不能为空");
}
List<String> idArray = Arrays.asList(ids);
if (op == null) {
// op 为空是删除
throw RException.badRequest("操作类型不能为空");
}
else if (ProxySettingBatchOp.DELETE == op) {
return R.judge(
proxySettingService.removeBatchByIds(idArray));
}
LambdaUpdateWrapper<ProxySetting> uw = new LambdaUpdateWrapper<>();
uw.in(ProxySetting::getId, idArray);
switch (op) {
case CHECK:
// TODO: 检查连通性
break;
case DISABLE_HTTPS_VERIFY:
uw.set(ProxySetting::getIgnoreHttpsVerification, false);
break;
case ENABLE_HTTP_VERIFY:
uw.set(ProxySetting::getIgnoreHttpsVerification, true);
break;
default:
throw RException.badRequest("未知操作");
}
return R.judge(() -> proxySettingService.update(uw));
}
private static enum ProxySettingBatchOp {
DELETE,
CHECK,
DISABLE_HTTPS_VERIFY,
ENABLE_HTTP_VERIFY
}
}