SqlAutoCreateTest.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.trs.web2frame;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.trs.infra.util.CMyException;
  5. import com.trs.infra.util.CMyFile;
  6. import com.trs.infra.util.CMyString;
  7. import com.trs.infra.util.Loader;
  8. import junit.framework.TestCase;
  9. import java.util.Map;
  10. /**
  11. * create by li.jihong
  12. * 2019/7/3
  13. */
  14. public class SqlAutoCreateTest extends TestCase {
  15. /**
  16. * 合并视图数据
  17. * @throws CMyException
  18. */
  19. public void testCreateSql() throws CMyException {
  20. //TODO 需要项目组根据自己需要合并的表和视图填写
  21. //目标视图ID
  22. int toViewId = 12;
  23. //目标视图名称 **注意如果是元数据表 ID需要必填,所以新建号统一视图表后,可以通过Navicat修改表字段ID为自增。统一视图后修改回来,重启项目
  24. String toTabelName = "WCMMetaTableGovOpenDataGovOpenData";
  25. //来源表名称
  26. String srcTabelName = "wcmdocument";
  27. //需要合并到空视图的栏目IDS
  28. String channelIds = "1,2";
  29. //来源表数据过滤条件
  30. String where = " DOCCHANNEL in (" + channelIds + ") ";
  31. //TODO
  32. //生成批量插入sql
  33. String insertSql = buildInsertSql(srcTabelName, toTabelName, where);
  34. System.out.println("-- insert SQL");
  35. System.out.println(insertSql);
  36. //生成chnldoc SQL
  37. String updateChnldocSql = buildUpdateChnldocSql(toViewId, channelIds);
  38. System.out.println("-- update chnldoc SQL");
  39. System.out.println(updateChnldocSql);
  40. //生成wcmdocument sql
  41. String updateDocumentSql = buildUpdateDocumentSql(toViewId, channelIds);
  42. System.out.println("-- update wcmdocument SQL");
  43. System.out.println(updateDocumentSql.toString());
  44. //生成xwcmmetaviewemployer sql
  45. String deleteViewEmployerSql = buildDeleteViewEmployerSql(channelIds);
  46. System.out.println("-- delete xwcmmetaviewemployer SQL");
  47. System.out.println(deleteViewEmployerSql.toString());
  48. System.out.println("-- insert xwcmmetaviewemployer SQL");
  49. buildUpdateViewEmployerSql(channelIds, toViewId);
  50. }
  51. private void buildUpdateViewEmployerSql(String channelIds, int toViewId) {
  52. String[] channelIdArr = CMyString.split(channelIds, ",");
  53. for (String channelIdStr : channelIdArr){
  54. StringBuilder updateViewEmployerSql = new StringBuilder();
  55. updateViewEmployerSql.append("insert into xwcmmetaviewemployer" +
  56. " (" +
  57. " VIEWID," +
  58. " CHANNELID," +
  59. " METAVIEWEMPLOYERID," +
  60. " EmployerType," +
  61. " EmployerId" +
  62. " )" +
  63. " SELECT ")
  64. .append(toViewId).append(",")
  65. .append(channelIdStr).append(",")
  66. .append(" ifnull(max(METAVIEWEMPLOYERID), 0) + 1").append(",")
  67. .append(101).append(",")
  68. .append(channelIdStr)
  69. .append(" FROM xwcmmetaviewemployer as METAVIEWEMPLOYERID; ");
  70. System.out.println(updateViewEmployerSql);
  71. }
  72. }
  73. private String buildDeleteViewEmployerSql(String channelIds) {
  74. StringBuilder deleteViewEmployerSql = new StringBuilder();
  75. deleteViewEmployerSql.append("DELETE FROM xwcmmetaviewemployer WHERE EmployerType = 101 and EmployerId in (")
  76. .append(channelIds)
  77. .append(") ;");
  78. return deleteViewEmployerSql.toString();
  79. }
  80. private String buildUpdateDocumentSql(int toViewId, String channelIds) {
  81. StringBuilder updateDocumentSql = new StringBuilder();
  82. updateDocumentSql.append("UPDATE wcmdocument SET DOCKIND = ")
  83. .append(toViewId)
  84. .append(" WHERE DOCCHANNEL in (")
  85. .append(channelIds)
  86. .append(") ;");
  87. return updateDocumentSql.toString();
  88. }
  89. private String buildUpdateChnldocSql(int toViewId, String channelIds) {
  90. StringBuilder updateChnldocSql = new StringBuilder();
  91. updateChnldocSql.append("UPDATE wcmchnldoc SET DOCKIND = ")
  92. .append(toViewId)
  93. .append(" WHERE CHNLID in (")
  94. .append(channelIds)
  95. .append(") ;");
  96. return updateChnldocSql.toString();
  97. }
  98. private String buildInsertSql(String srcTabelName, String toTabelName, String where) throws CMyException {
  99. String filePath = Loader.getResource("template").getPath() + "/relation.json";
  100. String jsonTxt = CMyFile.readFile(filePath, "utf-8");
  101. JSONObject jsonObject = JSON.parseObject(jsonTxt);
  102. StringBuilder keyBuilder = new StringBuilder();
  103. StringBuilder valueBuilder = new StringBuilder();
  104. for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
  105. keyBuilder.append(entry.getKey()).append(",");
  106. valueBuilder.append(entry.getValue()).append(",");
  107. }
  108. if (!jsonObject.isEmpty()){
  109. keyBuilder.deleteCharAt(keyBuilder.length() - 1);
  110. valueBuilder.deleteCharAt(valueBuilder.length() - 1);
  111. }
  112. StringBuilder insertSql = new StringBuilder(" INSERT INTO ");
  113. insertSql.append(toTabelName)
  114. .append(" (")
  115. .append(toTabelName+"Id,")
  116. .append(keyBuilder)
  117. .append(") SELECT ")
  118. .append(" (@max_num := @max_num + 1),")
  119. .append(valueBuilder)
  120. .append(" FROM ")
  121. .append(srcTabelName);
  122. if (!CMyString.isEmpty(where)){
  123. insertSql.append(" WHERE ").append(where);
  124. }
  125. insertSql.append(";");
  126. return insertSql.toString();
  127. }
  128. }