DbApiService.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.roma.romaapi.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import com.roma.romaapi.dao.DbApiDao;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. @Service
  9. public class DbApiService {
  10. @Autowired
  11. DbApiDao dbApiDao;
  12. public Map<String, Object> transferSql(String sqlCode, Map<String, String[]> maps){
  13. System.out.println("参数--------长度-----"+maps.size());
  14. Map hasSql = dbApiDao.dbQueryForMap(sqlCode);
  15. boolean isEmpty = hasSql.containsKey("sysErrorCode");
  16. if(isEmpty) {
  17. return hasSql;
  18. }
  19. // 获取全部的请求参数
  20. Map bindData = new HashMap<>();
  21. String page = "1"; // 页数
  22. String perPage = "15"; // 显示多少条
  23. // 组装要绑定的参数数据
  24. for (Map.Entry<String, String[]> entry : maps.entrySet()) {
  25. String paramsKey = entry.getKey();
  26. String paramsValue = entry.getValue()[0];
  27. String fourBeforestring = "";
  28. if(paramsKey.length() >= 4) {
  29. fourBeforestring = paramsKey.substring(0,4);
  30. if (fourBeforestring.equals("dbp_")) {
  31. System.out.println("参数-------------------"+paramsValue);
  32. bindData.put(paramsKey, paramsValue);
  33. }
  34. }
  35. if(paramsKey.equals("page")) {
  36. page = paramsValue;
  37. }
  38. if(paramsKey.equals("perPage")) {
  39. perPage = paramsValue;
  40. }
  41. }
  42. Object sqlBehavior = hasSql.get("sql_type");
  43. Map ret = new HashMap<>();
  44. // query查询行为 update数据有变动行为
  45. switch (sqlBehavior.toString()) {
  46. case "query":
  47. // 查询语句,可带分页,或者不带分页
  48. ret = this.transferSelect(hasSql, bindData, page, perPage);
  49. break;
  50. case "update":
  51. Integer updateRow = this.transferUpdate(hasSql, bindData);
  52. if(updateRow <= 0) {
  53. ret.put("sysErrorCode", "500");
  54. }
  55. break;
  56. default:
  57. break;
  58. }
  59. return ret;
  60. }
  61. // 转换查询
  62. public Map transferSelect(Map sqlData, Map bindData, String page, String perPage) {
  63. Map res = new HashMap<>();
  64. Object sql = sqlData.get("sql_string");// 获取sql语句
  65. Object isList = sqlData.get("is_list");// 是否查单条数据
  66. if(isList.equals(2)) {
  67. // 如果查询单条数据
  68. String selectSql = sql.toString();
  69. res = dbApiDao.dbCustomQueryForMap(selectSql, bindData);
  70. } else if(isList.equals(1)) {
  71. // 如果是查询列表
  72. Integer nowPage = Integer.parseInt(page); // 当前第几页
  73. Integer nowPerPage = Integer.parseInt(perPage); // 每页多少条
  74. String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + sql + " ) AS ROMA";
  75. Integer count = dbApiDao.dbCountQueryForObject(countSql, bindData);
  76. Integer maxPage = count / nowPerPage + (count % nowPerPage != 0 ? 1 : 0);// 总数/每页多少条=最多多少页
  77. if (nowPage <= 1) {
  78. nowPage = 1;
  79. } else if (maxPage > 0 && nowPage >= maxPage) {
  80. nowPage = maxPage;
  81. }
  82. Integer startNum = (nowPage - 1) * nowPerPage;
  83. String selectSql = sql + " LIMIT " + startNum + "," + nowPerPage;
  84. List info = dbApiDao.dbCustomQueryForList(selectSql, bindData);
  85. res.put("count", count);
  86. res.put("rows", info);
  87. }
  88. return res;
  89. }
  90. // 执行增删改
  91. public Integer transferUpdate(Map sqlData, Map bindData){
  92. Object sql = sqlData.get("sql_string");// 获取sql语句
  93. Integer ret = dbApiDao.dbUpdate(sql.toString(), bindData);
  94. return ret;
  95. }
  96. }