公共接口

parent 480f12c6
package com.aps.controller.common;
import com.aps.common.util.R;
import com.aps.entity.common.Paged;
import com.aps.service.common.ChromosomeDataService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/queryChromosome")
@Tag(name = "染色体数据查询", description = "根据实体名称查询染色体中的数据")
public class ChromosomeDataController {
@Autowired
private ChromosomeDataService chromosomeDataService;
/**
* 通用接口,根据实体名称查询Chromosome中的数据,支持分页和条件过滤
* 示例: POST /queryChromosome/order/page
* Body: { "sceneId": "SCENE001", "pageIndex": 1, "pageSize": 10, "conditions": [...] }
*
* @param entityName 实体名称 (如: order, entry, machine等)
* @param paged 分页和条件对象
* @return 分页数据
*/
@PostMapping("/{entityName}/page")
public R<Map<String, Object>> queryChromosomeDataWithConditions(
@PathVariable String entityName,
@RequestBody Paged paged) {
String sceneId = paged.getCondition("sceneId") != null ?
paged.getCondition("sceneId").getFieldValue() : null;
if (sceneId == null || sceneId.isEmpty()) {
return R.failed("sceneId不能为空");
}
Map<String, Object> result = chromosomeDataService.queryChromosomeDataWithConditions(
sceneId, entityName, paged);
return R.ok(result);
}
/**
* 通用接口,根据实体名称查询Chromosome中的列表数据,支持条件过滤
* 示例: POST /queryChromosome/order/list
* Body: { "sceneId": "SCENE001", "conditions": [...] }
*
* @param entityName 实体名称 (如: order, entry, machine等)
* @param paged 条件对象
* @return 列表数据
*/
@PostMapping("/{entityName}/list")
public R<List<Object>> queryChromosomeDataList(
@PathVariable String entityName,
@RequestBody Paged paged) {
String sceneId = paged.getCondition("sceneId") != null ?
paged.getCondition("sceneId").getFieldValue() : null;
if (sceneId == null || sceneId.isEmpty()) {
return R.failed("sceneId不能为空");
}
Object result = chromosomeDataService.queryChromosomeData(sceneId, entityName);
// 如果结果是List类型,则应用条件过滤
if (result instanceof List) {
List<Object> dataList = (List<Object>) result;
// 应用条件过滤
if (!paged.getConditions().isEmpty()) {
// 这里可以调用服务层的过滤方法
Map<String, Object> filteredResult = chromosomeDataService.queryChromosomeDataWithConditions(
sceneId, entityName, paged);
dataList = (List<Object>) filteredResult.get("records");
}
return R.ok(dataList);
} else {
return R.ok(Collections.singletonList(result));
}
}
/**
* 通用接口,根据实体名称和ID查询Chromosome中的单个数据
* 示例: /queryChromosome/order/get?sceneId=xxx&id=123
*
* @param sceneId 场景ID
* @param entityName 实体名称 (如: order, entry, machine等)
* @param id 数据ID
* @return 单个数据对象
*/
@GetMapping("/{entityName}/get")
public R<Object> queryChromosomeDataById(
@RequestParam String sceneId,
@PathVariable String entityName,
@RequestParam String id) {
Object result = chromosomeDataService.queryChromosomeData(sceneId, entityName);
// 如果结果是List类型,则根据ID查找特定项
if (result instanceof List) {
List<Object> dataList = (List<Object>) result;
Object item = dataList.stream()
.filter(obj -> {
try {
// 尝试获取对象的id字段
java.lang.reflect.Field idField = obj.getClass().getDeclaredField("id");
idField.setAccessible(true);
Object itemId = idField.get(obj);
return itemId != null && itemId.toString().equals(id);
} catch (Exception e) {
// 如果没有id字段或访问失败,尝试使用Id字段
try {
java.lang.reflect.Field idField = obj.getClass().getDeclaredField("Id");
idField.setAccessible(true);
Object itemId = idField.get(obj);
return itemId != null && itemId.toString().equals(id);
} catch (Exception ex) {
return false;
}
}
})
.findFirst()
.orElse(null);
if (item != null) {
return R.ok(item);
} else {
return R.failed("未找到ID为 " + id + " 的数据");
}
} else {
// 如果不是列表,直接返回结果
return R.ok(result);
}
}
// /**
// * 通用接口,根据实体名称更新Chromosome中的数据
// * 示例: POST /queryChromosome/order/update
// * Body: { "sceneId": "SCENE001", "data": {...} }
// *
// * @param entityName 实体名称 (如: order, entry, machine等)
// * @param requestBody 包含sceneId和更新数据的请求体
// * @return 更新结果
// */
// @PostMapping("/{entityName}/update")
// public R<String> updateChromosomeData(
// @PathVariable String entityName,
// @RequestBody Map<String, Object> requestBody) {
//
// String sceneId = (String) requestBody.get("sceneId");
// @SuppressWarnings("unchecked")
// Map<String, Object> data = (Map<String, Object>) requestBody.get("data");
//
// if (sceneId == null || sceneId.isEmpty()) {
// return R.failed("sceneId不能为空");
// }
//
// if (data == null || data.isEmpty()) {
// return R.failed("更新数据不能为空");
// }
//
// boolean success = chromosomeDataService.updateChromosomeData(sceneId, entityName, data);
// if (success) {
// return R.ok("数据更新成功");
// } else {
// return R.failed("数据更新失败");
// }
// }
//
// /**
// * 通用接口,根据实体名称批量操作Chromosome中的数据
// * 示例: POST /queryChromosome/order/batch
// * Body: { "sceneId": "SCENE001", "data": [...] }
// *
// * @param entityName 实体名称 (如: order, entry, machine等)
// * @param requestBody 包含sceneId和批量数据的请求体
// * @return 批量操作结果
// */
// @PostMapping("/{entityName}/batch")
// public R<String> batchChromosomeData(
// @PathVariable String entityName,
// @RequestBody Map<String, Object> requestBody) {
//
// String sceneId = (String) requestBody.get("sceneId");
// @SuppressWarnings("unchecked")
// List<Map<String, Object>> data = (List<Map<String, Object>>) requestBody.get("data");
//
// if (sceneId == null || sceneId.isEmpty()) {
// return R.failed("sceneId不能为空");
// }
//
// if (data == null || data.isEmpty()) {
// return R.failed("批量数据不能为空");
// }
//
// int successCount = chromosomeDataService.batchChromosomeData(sceneId, entityName, data);
// return R.ok("批量操作成功,共处理 " + data.size() + " 条数据,成功更新 " + successCount + " 条");
// }
//
// /**
// * 通用接口,根据实体名称删除Chromosome中的数据
// * 示例: POST /queryChromosome/order/delete
// * Body: { "sceneId": "SCENE001", "ids": [...] }
// *
// * @param entityName 实体名称 (如: order, entry, machine等)
// * @param requestBody 包含sceneId和要删除的数据ID列表的请求体
// * @return 删除操作结果
// */
// @PostMapping("/{entityName}/delete")
// public R<String> deleteChromosomeData(
// @PathVariable String entityName,
// @RequestBody Map<String, Object> requestBody) {
//
// String sceneId = (String) requestBody.get("sceneId");
// @SuppressWarnings("unchecked")
// List<Object> ids = (List<Object>) requestBody.get("ids");
//
// if (sceneId == null || sceneId.isEmpty()) {
// return R.failed("sceneId不能为空");
// }
//
// if (ids == null || ids.isEmpty()) {
// return R.failed("删除ID列表不能为空");
// }
//
// int deleteCount = chromosomeDataService.deleteChromosomeData(sceneId, entityName, ids);
// return R.ok("删除成功,共删除 " + ids.size() + " 条数据,实际删除 " + deleteCount + " 条");
// }
}
\ No newline at end of file
package com.aps.entity.common;
public class ConditionEntity {
private String fieldName;
private String fieldValue;
private String conditionalType;
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldValue() {
return fieldValue;
}
public String getSafeValue() {
String safe=fieldValue.replaceAll("(?i)(delete|drop|\\s|%|;|or|‘)","");
return safe;
}
public void setFieldValue(String fieldValue) {
this.fieldValue = fieldValue;
}
public String getConditionalType() {
return conditionalType;
}
public void setConditionalType(String conditionalType) {
this.conditionalType = conditionalType;
}
@Override
public String toString() {
return "ConditionEntity{" +
"fieldName='" + fieldName + '\'' +
", fieldValue='" + fieldValue + '\'' +
", conditionalType='" + conditionalType + '\'' +
'}';
}
}
package com.aps.entity.common;
public enum ConditionEnum {
Equal("Equal",0), // 等于
NoEqual("NoEqual",10), // 等于
Like("Like",1), //模糊
GreaterThan("GreaterThan",2), //大于
GreaterThanOrEqual("GreaterThanOrEqual",3), //大于等于
LessThan("LessThan",4), //小于
LessThanOrEqual("LessThanOrEqual",5), //小于等于
In("In",6), //包含
NotIn("NotIn",7),
Between("Between",8), //范围查找
Keys("Keys",9), //字段 或 连接
InSql("InSql",10) // 工单执行,应用在根据派工人查询工单
,
IsEmpty("IsEmpty",11) ,// 是null 或者 ""
NotEmpty("NotEmpty",12),//不是 null 也不是""
Exists("Exists",13),
NotExists("NotExists",14),
InSet("InSet",15);
private String name;
private Integer value;
ConditionEnum(String name, Integer value) {
this.name=name;
this.value=value;
}
public static ConditionEnum getByName(String name) {
for (ConditionEnum status : ConditionEnum.values()) {
if (status.getName().equalsIgnoreCase(name)) {
return status;
}
}
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
package com.aps.entity.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
*
* @Title: PagedGridResult.java
* @Package com.lei.utils
* @Description: 用来返回分页Grid的数据格式
*/
public class Paged {
private Integer pageIndex=1; //当前页
private Integer pageSize=1000; //每页多少条
private Integer total=0;// 特殊设置,总记录数,如果前台带有此值,则分页查询时不查询总数。
private String sortBy=""; //排序字段
private Boolean desc=true; //是否倒序
private String fields="";// 返回的字段
private String table;// 返回的表
//分组字段名称
private String groupName;
private List<ConditionEntity> conditions=new ArrayList<>(); //条件集合
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public Integer getPageIndex() {
return pageIndex;
}
public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String getSortBy() {
return sortBy;
}
public void setSortBy(String sortBy) {
this.sortBy = sortBy;
}
public Boolean getDesc() {
return desc;
}
public void setDesc(Boolean desc) {
this.desc = desc;
}
public List<ConditionEntity> getConditions() {
return conditions;
}
public ConditionEntity getCondition(String field){
Optional<ConditionEntity> first = conditions.stream().filter(s -> s.getFieldName().equalsIgnoreCase(field)).findFirst();
if(first.isPresent()){
return first.get();
}
return null;
}
public List<ConditionEntity> addCondition(String field, ConditionEnum conditionType,String sql) {
if(conditions==null){
conditions=new ArrayList<>();
}
ConditionEntity condition=new ConditionEntity();
condition.setFieldName(field);
condition.setConditionalType(conditionType.toString());
condition.setFieldValue(sql);
conditions.add(condition);
return conditions;
}
public void setConditions(List<ConditionEntity> conditions) {
this.conditions = conditions;
}
@Override
public String toString() {
return "Paged{" +
"pageIndex=" + pageIndex +
", pageSize=" + pageSize +
", sortBy='" + sortBy + '\'' +
", desc=" + desc +
", conditions=" + conditions +
'}';
}
public String getFields() {
return fields;
}
public void setFields(String fields) {
this.fields = fields;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment