123 lines
4.4 KiB
Java
123 lines
4.4 KiB
Java
|
package cn.com.tenlion.controller;
|
||
|
|
||
|
import cn.com.tenlion.mapper.IDataMapper;
|
||
|
import cn.com.tenlion.service.SqlService;
|
||
|
import net.sf.jsqlparser.statement.delete.Delete;
|
||
|
import net.sf.jsqlparser.statement.insert.Insert;
|
||
|
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||
|
import net.sf.jsqlparser.statement.update.Update;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
||
|
import java.util.HashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
|
||
|
@RestController
|
||
|
@RequestMapping("/api/data")
|
||
|
public class DataController {
|
||
|
|
||
|
@Autowired
|
||
|
private SqlService sqlService;
|
||
|
@Autowired
|
||
|
private IDataMapper dataMapper;
|
||
|
|
||
|
|
||
|
@PostMapping("/save/{projContext}/{modContext}")
|
||
|
public Map<String, Object> save(@PathVariable String projContext,
|
||
|
@PathVariable String modContext,
|
||
|
@RequestBody Map<String, Object> body) {
|
||
|
Insert insert = sqlService.insertSql(projContext, modContext, body);
|
||
|
dataMapper.save(insert.toString());
|
||
|
return new HashMap<String, Object>() {{
|
||
|
put("code", 0);
|
||
|
put("msg", "success");
|
||
|
}};
|
||
|
}
|
||
|
|
||
|
@DeleteMapping("/delete/{projContext}/{modContext}")
|
||
|
public Map<String, Object> delete(@PathVariable String projContext,
|
||
|
@PathVariable String modContext,
|
||
|
@RequestParam List<Long> ids) {
|
||
|
Delete delete = sqlService.deleteSql(projContext, modContext, ids);
|
||
|
dataMapper.delete(delete.toString());
|
||
|
return new HashMap<String, Object>() {{
|
||
|
put("code", 0);
|
||
|
put("msg", "success");
|
||
|
}};
|
||
|
}
|
||
|
|
||
|
|
||
|
@PutMapping("/update/{projContext}/{modContext}/id/{id}")
|
||
|
public Map<String, Object> update(@PathVariable String projContext,
|
||
|
@PathVariable String modContext,
|
||
|
@PathVariable Long id,
|
||
|
@RequestBody Map<String, Object> body) {
|
||
|
Update update = sqlService.updateSql(projContext, modContext, id, body);
|
||
|
dataMapper.update(update.toString());
|
||
|
return new HashMap<String, Object>() {{
|
||
|
put("code", 0);
|
||
|
put("msg", "success");
|
||
|
}};
|
||
|
}
|
||
|
|
||
|
|
||
|
@GetMapping("/get/{projContext}/{modContext}/id/{id}")
|
||
|
public Map<String, Object> update(@PathVariable String projContext,
|
||
|
@PathVariable String modContext,
|
||
|
@PathVariable Long id) {
|
||
|
PlainSelect oneSql = sqlService.findOneSql(projContext, modContext, id);
|
||
|
List<Map<String, Object>> list = dataMapper.findBatch(oneSql.toString());
|
||
|
if (list.isEmpty()) {
|
||
|
return null;
|
||
|
}
|
||
|
setKeyCamel(list);
|
||
|
return list.get(0);
|
||
|
}
|
||
|
|
||
|
|
||
|
@GetMapping("/list/{projContext}/{modContext}")
|
||
|
public List<Map<String, Object>> list(@PathVariable String projContext,
|
||
|
@PathVariable String modContext,
|
||
|
@RequestParam(required = false) String keywords) {
|
||
|
PlainSelect oneSql = sqlService.findBatch(projContext, modContext, keywords);
|
||
|
List<Map<String, Object>> list = dataMapper.findBatch(oneSql.toString());
|
||
|
setKeyCamel(list);
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
private void setKeyCamel(List<Map<String, Object>> mapList) {
|
||
|
for (int i = 0; i < mapList.size(); i++) {
|
||
|
Map<String, Object> map = mapList.get(i);
|
||
|
Map<String, Object> newMap = new HashMap<>();
|
||
|
for (String key : map.keySet()) {
|
||
|
newMap.put(str2Camel(key.toLowerCase()), map.get(key));
|
||
|
}
|
||
|
mapList.set(i, newMap);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 字符串转驼峰
|
||
|
*
|
||
|
* @param str
|
||
|
* @param separator
|
||
|
* @return
|
||
|
*/
|
||
|
private String str2Camel(String input) {
|
||
|
StringBuilder result = new StringBuilder();
|
||
|
for (int i = 0; i < input.length(); i++) {
|
||
|
char currentChar = input.charAt(i);
|
||
|
if (currentChar == '_' && i + 1 < input.length()) {
|
||
|
// 如果当前字符是下划线,且后面还有字符
|
||
|
i++; // 跳过下划线
|
||
|
result.append(Character.toUpperCase(input.charAt(i)));
|
||
|
} else {
|
||
|
result.append(Character.toLowerCase(currentChar));
|
||
|
}
|
||
|
}
|
||
|
return result.toString();
|
||
|
}
|
||
|
|
||
|
}
|