|
|
@@ -4,6 +4,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.roma.romaapi.dao.SqlApiDao;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
@@ -11,14 +13,98 @@ public class SqlService {
|
|
|
|
|
|
@Autowired
|
|
|
SqlApiDao sqlApiDao; // 引入dao层
|
|
|
- public Map<String, Object> getSql(String sqlCode){
|
|
|
- Map<String, Object> hasSql = sqlApiDao.getSql(sqlCode);
|
|
|
+ public Map<String, Object> getSql(String sqlCode, Map<String, String[]> maps){
|
|
|
+ Map hasSql = sqlApiDao.getSql(sqlCode);
|
|
|
boolean isEmpty = hasSql.containsKey("sysErrorCode");
|
|
|
- if (!isEmpty) {
|
|
|
+ if(isEmpty) {
|
|
|
return hasSql;
|
|
|
}
|
|
|
- // 如果有sql数据,则转换语句
|
|
|
+ // 获取全部的请求参数
|
|
|
+ Map bindData = new HashMap<>();
|
|
|
+ String page = "1"; // 页数
|
|
|
+ String perPage = "15"; // 显示多少条
|
|
|
+ // 组装要绑定的参数数据
|
|
|
+ for (Map.Entry<String, String[]> entry : maps.entrySet()) {
|
|
|
+ String paramsKey = entry.getKey();
|
|
|
+ String paramsValue = entry.getValue()[0];
|
|
|
+ String fourBeforestring = "";
|
|
|
+ if(paramsKey.length() >= 4) {
|
|
|
+ fourBeforestring = paramsKey.substring(0,4);
|
|
|
+ if (fourBeforestring.equals("api_")) {
|
|
|
+ bindData.put(paramsKey, paramsValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(paramsKey.equals("page")) {
|
|
|
+ page = paramsValue;
|
|
|
+ }
|
|
|
+ if(paramsKey.equals("perPage")) {
|
|
|
+ perPage = paramsValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Object sqlType = hasSql.get("sql_type");
|
|
|
+ Map ret = new HashMap<>();
|
|
|
+ switch (sqlType.toString()) {
|
|
|
+ case "select":
|
|
|
+ // 查询语句,可带分页,或者不带分页
|
|
|
+ ret = this.transferSelect(hasSql, bindData, page, perPage);
|
|
|
+ break;
|
|
|
+ case "update":
|
|
|
+ Integer updateRow = this.transferUpdate(hasSql, bindData);
|
|
|
+ if(updateRow <= 0) {
|
|
|
+ ret.put("sysErrorCode", "500");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 转换查询
|
|
|
+ public Map transferSelect(Map sqlData, Map bindData, String page, String perPage) {
|
|
|
+ Map res = new HashMap<>();
|
|
|
+ Object sql = sqlData.get("detail");// 获取sql语句
|
|
|
+ Object isProcedure = sqlData.get("is_procedure");// 是否存储过程
|
|
|
+ Object isList = sqlData.get("is_list");// 是否查单条数据
|
|
|
+ // 如果不是存储过程
|
|
|
+ if(isProcedure.equals(0)) {
|
|
|
+ if(isList.equals(2)) {
|
|
|
+ // 如果查询单条数据
|
|
|
+ String selectSql = sql.toString();
|
|
|
+ Map info = sqlApiDao.selectData(selectSql, bindData);
|
|
|
+ Integer count = info.isEmpty() ? 0 : 1;
|
|
|
+ res.put("count", count);
|
|
|
+ res.put("rows", info);
|
|
|
+ } else if(isList.equals(1)) {
|
|
|
+ // 如果是查询列表
|
|
|
+ Integer nowPage = Integer.parseInt(page); // 当前第几页
|
|
|
+ Integer nowPerPage = Integer.parseInt(perPage); // 每页多少条
|
|
|
+ String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + sql + " ) AS ROMA";
|
|
|
+ Integer count = sqlApiDao.getSqlCount(countSql, bindData);
|
|
|
+ Integer maxPage = count / nowPerPage + (count % nowPerPage != 0 ? 1 : 0);// 总数/每页多少条=最多多少页
|
|
|
+ if (nowPage <= 1) {
|
|
|
+ nowPage = 1;
|
|
|
+ } else if (maxPage > 0 && nowPage >= maxPage) {
|
|
|
+ nowPage = maxPage;
|
|
|
+ }
|
|
|
+ Integer startNum = (nowPage - 1) * nowPerPage;
|
|
|
+ String selectSql = sql + " LIMIT " + startNum + "," + nowPerPage;
|
|
|
+ List info = sqlApiDao.selectMultipleData(selectSql, bindData);
|
|
|
+ res.put("count", count);
|
|
|
+ res.put("rows", info);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行删除语句
|
|
|
+ public Integer transferUpdate(Map sqlData, Map bindData){
|
|
|
+ Object sql = sqlData.get("detail");// 获取sql语句
|
|
|
+ Integer ret = sqlApiDao.updateData(sql.toString(), bindData);
|
|
|
|
|
|
- return hasSql;
|
|
|
+ return ret;
|
|
|
}
|
|
|
}
|