ProcessDefinitionMapperProvider.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package cn.escheduler.dao.mapper;
  18. import cn.escheduler.common.enums.Flag;
  19. import cn.escheduler.common.enums.ReleaseState;
  20. import cn.escheduler.common.utils.EnumFieldUtil;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.ibatis.jdbc.SQL;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * process definition mapper provider
  27. */
  28. public class ProcessDefinitionMapperProvider {
  29. private static final String TABLE_NAME = "t_escheduler_process_definition";
  30. /**
  31. * 插入流程定义
  32. *
  33. * @param parameter
  34. * @return
  35. */
  36. public String insert(Map<String, Object> parameter) {
  37. return new SQL() {
  38. {
  39. INSERT_INTO(TABLE_NAME);
  40. VALUES("`name`", "#{processDefinition.name}");
  41. VALUES("`version`", "#{processDefinition.version}");
  42. VALUES("`release_state`", EnumFieldUtil.genFieldStr("processDefinition.releaseState", ReleaseState.class));
  43. VALUES("`project_id`", "#{processDefinition.projectId}");
  44. VALUES("`process_definition_json`", "#{processDefinition.processDefinitionJson}");
  45. VALUES("`desc`", "#{processDefinition.desc}");
  46. VALUES("`global_params`", "#{processDefinition.globalParams}");
  47. VALUES("`locations`", "#{processDefinition.locations}");
  48. VALUES("`connects`", "#{processDefinition.connects}");
  49. VALUES("`create_time`", "#{processDefinition.createTime}");
  50. VALUES("`update_time`", "#{processDefinition.updateTime}");
  51. VALUES("`timeout`", "#{processDefinition.timeout}");
  52. VALUES("`flag`", EnumFieldUtil.genFieldStr("processDefinition.flag", ReleaseState.class));
  53. VALUES("`user_id`", "#{processDefinition.userId}");
  54. }
  55. }.toString();
  56. }
  57. /**
  58. * 删除流程定义
  59. *
  60. * @param parameter
  61. * @return
  62. */
  63. public String delete(Map<String, Object> parameter) {
  64. return new SQL() {
  65. {
  66. DELETE_FROM(TABLE_NAME);
  67. WHERE("`id`=#{processDefinitionId}");
  68. }
  69. }.toString();
  70. }
  71. /**
  72. * 更新流程定义
  73. *
  74. * @param parameter
  75. * @return
  76. */
  77. public String update(Map<String, Object> parameter) {
  78. return new SQL() {
  79. {
  80. UPDATE(TABLE_NAME);
  81. SET("`name`=#{processDefinition.name}");
  82. SET("`version`=#{processDefinition.version}");
  83. SET("`release_state`="+EnumFieldUtil.genFieldStr("processDefinition.releaseState", ReleaseState.class));
  84. SET("`project_id`=#{processDefinition.projectId}");
  85. SET("`process_definition_json`=#{processDefinition.processDefinitionJson}");
  86. SET("`desc`=#{processDefinition.desc}");
  87. SET("`locations`=#{processDefinition.locations}");
  88. SET("`connects`=#{processDefinition.connects}");
  89. SET("`global_params`=#{processDefinition.globalParams}");
  90. SET("`create_time`=#{processDefinition.createTime}");
  91. SET("`update_time`=#{processDefinition.updateTime}");
  92. SET("`timeout`=#{processDefinition.timeout}");
  93. SET("`flag`="+EnumFieldUtil.genFieldStr("processDefinition.flag", Flag.class));
  94. SET("`user_id`=#{processDefinition.userId}");
  95. WHERE("`id`=#{processDefinition.id}");
  96. }
  97. }.toString();
  98. }
  99. public String updateProcessDefinitionReleaseState(Map<String, Object> parameter) {
  100. return new SQL() {
  101. {
  102. UPDATE(TABLE_NAME);
  103. SET("`release_state`="+EnumFieldUtil.genFieldStr("releaseState", ReleaseState.class));
  104. WHERE("`id`=#{processDefinitionId}");
  105. }
  106. }.toString();
  107. }
  108. /**
  109. * 根据流程定义 id 查询 sql
  110. *
  111. * @param parameter
  112. * @return
  113. */
  114. public String queryByDefineId(Map<String, Object> parameter) {
  115. return new SQL() {
  116. {
  117. SELECT("pd.*,u.user_name,p.name as project_name");
  118. FROM(TABLE_NAME + " pd");
  119. JOIN("t_escheduler_user u ON pd.user_id = u.id");
  120. JOIN("t_escheduler_project p ON pd.project_id = p.id");
  121. WHERE("pd.id = #{processDefinitionId}");
  122. }
  123. }.toString();
  124. }
  125. /**
  126. * query By Define Name
  127. *
  128. * @param parameter
  129. * @return
  130. */
  131. public String queryByDefineName(Map<String, Object> parameter) {
  132. return new SQL() {
  133. {
  134. SELECT("pd.*,u.user_name,p.name as project_name,t.tenant_code,t.tenant_name,q.queue,q.queue_name");
  135. FROM(TABLE_NAME + " pd");
  136. JOIN("t_escheduler_user u ON pd.user_id = u.id");
  137. JOIN("t_escheduler_project p ON pd.project_id = p.id");
  138. JOIN("t_escheduler_tenant t ON t.id = u.tenant_id");
  139. JOIN("t_escheduler_queue q ON t.queue_id = q.id");
  140. WHERE("p.id = #{projectId}");
  141. WHERE("pd.name = #{processDefinitionName}");
  142. }
  143. }.toString();
  144. }
  145. /**
  146. * query define list paging
  147. * @param parameter
  148. * @return
  149. */
  150. public String queryDefineListPaging(Map<String, Object> parameter) {
  151. return new SQL() {{
  152. SELECT("td.*,sc.schedule_release_state");
  153. FROM(TABLE_NAME + " td");
  154. LEFT_OUTER_JOIN(" (select process_definition_id,release_state as schedule_release_state from `t_escheduler_schedules` " +
  155. "group by `process_definition_id`,`release_state`) sc on sc.process_definition_id = td.id");
  156. WHERE("td.project_id = #{projectId} ");
  157. Object searchVal = parameter.get("searchVal");
  158. if(searchVal != null && StringUtils.isNotEmpty(searchVal.toString())){
  159. WHERE( " td.name like concat('%', #{searchVal}, '%') ");
  160. }
  161. Object userId = parameter.get("userId");
  162. if(userId != null && 0 != Integer.parseInt(userId.toString())){
  163. WHERE("td.user_id = #{userId}");
  164. }
  165. ORDER_BY(" sc.schedule_release_state desc,td.update_time desc limit #{offset},#{pageSize} ");
  166. }}.toString();
  167. }
  168. /**
  169. * query all define list by project id
  170. * @param parameter
  171. * @return
  172. */
  173. public String queryAllDefinitionList(Map<String, Object> parameter) {
  174. return new SQL() {{
  175. SELECT("id,name,version,release_state,project_id,user_id,`desc`,create_time,update_time,flag,global_params,receivers,receivers_cc");
  176. FROM(TABLE_NAME );
  177. WHERE("project_id = #{projectId}");
  178. ORDER_BY("create_time desc ");
  179. }}.toString();
  180. }
  181. /**
  182. * count definition number group by project id
  183. * @param parameter
  184. * @return
  185. */
  186. public String countDefineNumber(Map<String, Object> parameter) {
  187. return new SQL() {{
  188. SELECT("count(0)");
  189. FROM(TABLE_NAME);
  190. WHERE("project_id = #{projectId}");
  191. Object searchVal = parameter.get("searchVal");
  192. if(searchVal != null && StringUtils.isNotEmpty(searchVal.toString())){
  193. WHERE( " name like concat('%', #{searchVal}, '%') ");
  194. }
  195. Object userId = parameter.get("userId");
  196. if(userId != null && 0 != Integer.parseInt(userId.toString())){
  197. WHERE("user_id = #{userId}");
  198. }
  199. }}.toString();
  200. }
  201. /**
  202. * count definition number by user type
  203. * @param parameter
  204. * @return
  205. */
  206. public String countDefinitionGroupByUser(Map<String, Object> parameter) {
  207. return new SQL() {{
  208. SELECT("td.user_id as user_id, tu.user_name as user_name, count(0) as count");
  209. FROM(TABLE_NAME + " td");
  210. JOIN("t_escheduler_user tu on tu.id=td.user_id");
  211. if(parameter.get("projectId") != null && (int)parameter.get("projectId") != 0){
  212. WHERE( "td.project_id = #{projectId} ");
  213. }else{
  214. if(parameter.get("userType") != null && String.valueOf(parameter.get("userType")) == "GENERAL_USER") {
  215. AND();
  216. WHERE("td.project_id in (select id as project_id from t_escheduler_project tp where tp.user_id= #{userId} " +
  217. "union select project_id from t_escheduler_relation_project_user tr where tr.user_id= #{userId} )");
  218. }
  219. }
  220. GROUP_BY("td.user_id");
  221. }}.toString();
  222. }
  223. /**
  224. * query definition list by define id list
  225. * @param parameter
  226. * @return
  227. */
  228. public String queryDefinitionListByIdList(Map<String, Object> parameter){
  229. List<String> ids = (List<String>) parameter.get("ids");
  230. return new SQL() {{
  231. SELECT("*");
  232. FROM(TABLE_NAME);
  233. WHERE("`id` in (" + String.join(",", ids) + ")");
  234. }}.toString();
  235. }
  236. /**
  237. * update receivers and cc by definition id
  238. *
  239. * @param parameter
  240. * @return
  241. */
  242. public String updateReceiversAndCcById(Map<String, Object> parameter) {
  243. return new SQL() {
  244. {
  245. UPDATE(TABLE_NAME);
  246. SET("`receivers`=#{receivers}");
  247. SET("`receivers_cc`=#{receiversCc}");
  248. WHERE("`id`=#{processDefinitionId}");
  249. }
  250. }.toString();
  251. }
  252. }