正在显示
38 个修改的文件
包含
4280 行增加
和
211 行删除
| @@ -9,7 +9,6 @@ | @@ -9,7 +9,6 @@ | ||
| 9 | <version>1.0-SNAPSHOT</version> | 9 | <version>1.0-SNAPSHOT</version> |
| 10 | </parent> | 10 | </parent> |
| 11 | 11 | ||
| 12 | - <groupId>com.luhui</groupId> | ||
| 13 | <artifactId>lh-service-dao</artifactId> | 12 | <artifactId>lh-service-dao</artifactId> |
| 14 | 13 | ||
| 15 | <properties> | 14 | <properties> |
| @@ -18,4 +17,32 @@ | @@ -18,4 +17,32 @@ | ||
| 18 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | 17 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 19 | </properties> | 18 | </properties> |
| 20 | 19 | ||
| 20 | + <dependencies> | ||
| 21 | + <!-- 数据库 --> | ||
| 22 | + <dependency> | ||
| 23 | + <groupId>commons-dbcp</groupId> | ||
| 24 | + <artifactId>commons-dbcp</artifactId> | ||
| 25 | + <version>1.4</version> | ||
| 26 | + </dependency> | ||
| 27 | + <dependency> | ||
| 28 | + <groupId>commons-dbutils</groupId> | ||
| 29 | + <artifactId>commons-dbutils</artifactId> | ||
| 30 | + <version>1.6</version> | ||
| 31 | + </dependency> | ||
| 32 | + <dependency> | ||
| 33 | + <groupId>commons-pool</groupId> | ||
| 34 | + <artifactId>commons-pool</artifactId> | ||
| 35 | + <version>1.6</version> | ||
| 36 | + </dependency> | ||
| 37 | + <dependency> | ||
| 38 | + <groupId>mysql</groupId> | ||
| 39 | + <artifactId>mysql-connector-java</artifactId> | ||
| 40 | + </dependency> | ||
| 41 | + | ||
| 42 | + <!--常用工具类 --> | ||
| 43 | + <dependency> | ||
| 44 | + <groupId>org.apache.commons</groupId> | ||
| 45 | + <artifactId>commons-lang3</artifactId> | ||
| 46 | + </dependency> | ||
| 47 | + </dependencies> | ||
| 21 | </project> | 48 | </project> |
| 1 | +package com.zhonglai.luhui.service.dao; | ||
| 2 | + | ||
| 3 | +import com.zhonglai.luhui.service.dao.annotation.PublicSQLConfig; | ||
| 4 | +import com.zhonglai.luhui.service.dao.util.StringUtils; | ||
| 5 | +import org.apache.commons.dbutils.*; | ||
| 6 | +import org.apache.commons.dbutils.handlers.BeanHandler; | ||
| 7 | +import org.apache.commons.dbutils.handlers.BeanListHandler; | ||
| 8 | +import org.apache.commons.dbutils.handlers.MapListHandler; | ||
| 9 | +import org.apache.commons.dbutils.handlers.ScalarHandler; | ||
| 10 | + | ||
| 11 | +import java.lang.reflect.Field; | ||
| 12 | +import java.lang.reflect.InvocationTargetException; | ||
| 13 | +import java.lang.reflect.Method; | ||
| 14 | +import java.sql.SQLException; | ||
| 15 | +import java.util.ArrayList; | ||
| 16 | +import java.util.List; | ||
| 17 | +import java.util.Map; | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * 数据库操作 | ||
| 21 | + * Created by zhonglai on 2016/12/15. | ||
| 22 | + */ | ||
| 23 | +public class BaseDao { | ||
| 24 | + | ||
| 25 | + private DBFactory dBFactory = new DBFactoryImp(); | ||
| 26 | + | ||
| 27 | + public BaseDao(DBFactory dBFactory) | ||
| 28 | + { | ||
| 29 | + this.dBFactory = dBFactory; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public BaseDao() | ||
| 33 | + { | ||
| 34 | + | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * 指定表名插入对象 | ||
| 39 | + * @param object 传值对象 | ||
| 40 | + */ | ||
| 41 | + public void insert(Object object ) | ||
| 42 | + { | ||
| 43 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 44 | + String sql = "insert into "; | ||
| 45 | + sql += changTableNameFromObject(object) + "("; | ||
| 46 | + Field[] fields = object.getClass().getDeclaredFields( ); | ||
| 47 | + String values = "("; | ||
| 48 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 49 | + for(Field field:fields) | ||
| 50 | + {// | ||
| 51 | + PublicSQLConfig publicSQLConfig = field.getAnnotation(PublicSQLConfig.class); | ||
| 52 | + if(null !=publicSQLConfig && !publicSQLConfig.isSelect()) | ||
| 53 | + { | ||
| 54 | + continue; | ||
| 55 | + } | ||
| 56 | + Method method; | ||
| 57 | + try { | ||
| 58 | + method = object.getClass().getMethod("get"+ StringUtils.getName(field.getName())); | ||
| 59 | + | ||
| 60 | + Object value = method.invoke(object); | ||
| 61 | + if(null != value) | ||
| 62 | + { | ||
| 63 | + if(!"(".equals(values) ) | ||
| 64 | + { | ||
| 65 | + sql += ","; | ||
| 66 | + values += ","; | ||
| 67 | + } | ||
| 68 | + sql += "`"+ StringUtils.toUnderScoreCase(field.getName())+"`"; | ||
| 69 | + values += "?"; | ||
| 70 | + valueList.add(value); | ||
| 71 | + } | ||
| 72 | + } catch (NoSuchMethodException e) { | ||
| 73 | + // TODO Auto-generated catch block | ||
| 74 | + e.printStackTrace(); | ||
| 75 | + } catch (SecurityException e) { | ||
| 76 | + // TODO Auto-generated catch block | ||
| 77 | + e.printStackTrace(); | ||
| 78 | + } catch (IllegalAccessException e) { | ||
| 79 | + // TODO Auto-generated catch block | ||
| 80 | + e.printStackTrace(); | ||
| 81 | + } catch (IllegalArgumentException e) { | ||
| 82 | + // TODO Auto-generated catch block | ||
| 83 | + e.printStackTrace(); | ||
| 84 | + } catch (InvocationTargetException e) { | ||
| 85 | + // TODO Auto-generated catch block | ||
| 86 | + e.printStackTrace(); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + } | ||
| 90 | + sql += ")"; | ||
| 91 | + values += ")"; | ||
| 92 | + sql = sql+" values "+values; | ||
| 93 | + | ||
| 94 | + try { | ||
| 95 | + // 创建一个BeanProcessor对象 | ||
| 96 | + // GenerousBeanProcessor 仅仅重写了父类BeanProcessor的mapColumnsToProperties方法 | ||
| 97 | + BeanProcessor bean = new GenerousBeanProcessor(); | ||
| 98 | + // 将GenerousBeanProcessor对象传递给BasicRowProcessor | ||
| 99 | + RowProcessor processor = new BasicRowProcessor(bean); | ||
| 100 | + object = runner.insert(sql,new BeanHandler<Object>(Object.class,processor),valueList.toArray()); | ||
| 101 | + } catch (SQLException e) { | ||
| 102 | + e.printStackTrace(); | ||
| 103 | + } | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * 插入对象集合 | ||
| 108 | + * @param objectList | ||
| 109 | + */ | ||
| 110 | + public int insertList(List<Object> objectList) | ||
| 111 | + { | ||
| 112 | + return insertList(objectList,null); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + /** | ||
| 116 | + * 指定表名插入对象集合 | ||
| 117 | + * @param objectList | ||
| 118 | + */ | ||
| 119 | + public int insertList(List<?> objectList ,String tableName) | ||
| 120 | + { | ||
| 121 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 122 | + | ||
| 123 | + Object object = objectList.get(0); | ||
| 124 | + | ||
| 125 | + String sql = "insert into "; | ||
| 126 | + if(StringUtils.isBlank(tableName)) | ||
| 127 | + { | ||
| 128 | + tableName = StringUtils.toUnderScoreCase(object.getClass().getSimpleName()); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 132 | + sql += tableName + msaicAttribute(object) + " values " +msaicValues(objectList,valueList); | ||
| 133 | + | ||
| 134 | + try { | ||
| 135 | + return runner.update(sql,valueList.toArray()); | ||
| 136 | + } catch (SQLException e) { | ||
| 137 | + e.printStackTrace(); | ||
| 138 | + } | ||
| 139 | + return 0; | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + /** | ||
| 143 | + * 拼接属性条件 | ||
| 144 | + * @param object | ||
| 145 | + * @return | ||
| 146 | + */ | ||
| 147 | + private String msaicAttribute(Object object) | ||
| 148 | + { | ||
| 149 | + Field[] fields = object.getClass().getDeclaredFields( ); | ||
| 150 | + String attributeStr = "("; | ||
| 151 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 152 | + for(Field field:fields) | ||
| 153 | + {// | ||
| 154 | + if(!"(".equals(attributeStr) ) | ||
| 155 | + { | ||
| 156 | + attributeStr += ","; | ||
| 157 | + } | ||
| 158 | + attributeStr += "`"+ StringUtils.toUnderScoreCase(field.getName())+"`"; | ||
| 159 | + } | ||
| 160 | + attributeStr += ")"; | ||
| 161 | + return attributeStr; | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + private String msaicValues(List<?> objectList,List<Object> valueList) | ||
| 165 | + { | ||
| 166 | + StringBuffer returnValues = new StringBuffer(); | ||
| 167 | + for(Object object:objectList) | ||
| 168 | + { | ||
| 169 | + Field[] fields = object.getClass().getDeclaredFields( ); | ||
| 170 | + String values = "("; | ||
| 171 | + for(Field field:fields) | ||
| 172 | + {// | ||
| 173 | + PublicSQLConfig publicSQLConfig = field.getAnnotation(PublicSQLConfig.class); | ||
| 174 | + if(null !=publicSQLConfig && !publicSQLConfig.isSelect()) | ||
| 175 | + { | ||
| 176 | + continue; | ||
| 177 | + } | ||
| 178 | + Method method; | ||
| 179 | + try { | ||
| 180 | + method = object.getClass().getMethod("get"+ StringUtils.getName(field.getName())); | ||
| 181 | + Object value = method.invoke(object); | ||
| 182 | + if(!"(".equals(values) ) | ||
| 183 | + { | ||
| 184 | + values += ","; | ||
| 185 | + } | ||
| 186 | + values += "?"; | ||
| 187 | + valueList.add(value); | ||
| 188 | + } catch (NoSuchMethodException e) { | ||
| 189 | + // TODO Auto-generated catch block | ||
| 190 | + e.printStackTrace(); | ||
| 191 | + } catch (SecurityException e) { | ||
| 192 | + // TODO Auto-generated catch block | ||
| 193 | + e.printStackTrace(); | ||
| 194 | + } catch (IllegalAccessException e) { | ||
| 195 | + // TODO Auto-generated catch block | ||
| 196 | + e.printStackTrace(); | ||
| 197 | + } catch (IllegalArgumentException e) { | ||
| 198 | + // TODO Auto-generated catch block | ||
| 199 | + e.printStackTrace(); | ||
| 200 | + } catch (InvocationTargetException e) { | ||
| 201 | + // TODO Auto-generated catch block | ||
| 202 | + e.printStackTrace(); | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + } | ||
| 206 | + values += ")"; | ||
| 207 | + if(returnValues.length()!=0) | ||
| 208 | + { | ||
| 209 | + returnValues.append(","); | ||
| 210 | + } | ||
| 211 | + returnValues.append(values); | ||
| 212 | + } | ||
| 213 | + return returnValues.toString(); | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + | ||
| 217 | + /** | ||
| 218 | + * 以主键id更新对象 | ||
| 219 | + * @param object | ||
| 220 | + */ | ||
| 221 | + public int update(Object object) | ||
| 222 | + { | ||
| 223 | + return update(object,null); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + /** | ||
| 227 | + * 根据条件更新对象 | ||
| 228 | + * @param object 传值对象 | ||
| 229 | + * @param whereFieldNames 条件(多个用,分割) | ||
| 230 | + */ | ||
| 231 | + public int update(Object object,String whereFieldNames) | ||
| 232 | + { | ||
| 233 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 234 | + | ||
| 235 | + String sql = "update "; | ||
| 236 | + sql += changTableNameFromObject(object); | ||
| 237 | + Field[] fields = object.getClass().getDeclaredFields(); | ||
| 238 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 239 | + | ||
| 240 | + if(null != fields && fields.length !=0 ) | ||
| 241 | + { | ||
| 242 | + sql += " set "; | ||
| 243 | + int j = 0; | ||
| 244 | + for(int i=0;i<fields.length;i++) | ||
| 245 | + { | ||
| 246 | + | ||
| 247 | + Field field = fields[i]; | ||
| 248 | + try { | ||
| 249 | + PublicSQLConfig publicSQLConfig = field.getAnnotation(PublicSQLConfig.class); | ||
| 250 | + if(null !=publicSQLConfig && !publicSQLConfig.isSelect()) | ||
| 251 | + { | ||
| 252 | + continue; | ||
| 253 | + } | ||
| 254 | + Method method = null; | ||
| 255 | + try { | ||
| 256 | + method = object.getClass().getMethod("get"+ StringUtils.getName(field.getName())); | ||
| 257 | + } catch (NoSuchMethodException e) { | ||
| 258 | + continue; | ||
| 259 | + } | ||
| 260 | + Object value = method.invoke(object); | ||
| 261 | + if(null != value) | ||
| 262 | + { | ||
| 263 | + if(j!=0) | ||
| 264 | + { | ||
| 265 | + sql += ","; | ||
| 266 | + } | ||
| 267 | + sql += "`"+ StringUtils.toUnderScoreCase(field.getName())+"`"+"=?"; | ||
| 268 | + j++; | ||
| 269 | + valueList.add(value); | ||
| 270 | + } | ||
| 271 | + } catch (SecurityException e) { | ||
| 272 | + // TODO Auto-generated catch block | ||
| 273 | + e.printStackTrace(); | ||
| 274 | + } catch (IllegalAccessException e) { | ||
| 275 | + // TODO Auto-generated catch block | ||
| 276 | + e.printStackTrace(); | ||
| 277 | + } catch (IllegalArgumentException e) { | ||
| 278 | + // TODO Auto-generated catch block | ||
| 279 | + e.printStackTrace(); | ||
| 280 | + } catch (InvocationTargetException e) { | ||
| 281 | + // TODO Auto-generated catch block | ||
| 282 | + e.printStackTrace(); | ||
| 283 | + } | ||
| 284 | + | ||
| 285 | + } | ||
| 286 | + | ||
| 287 | + sql += " where 1=1 "; | ||
| 288 | + if(StringUtils.isNoneBlank(whereFieldNames)) | ||
| 289 | + { | ||
| 290 | + String[] wheres = whereFieldNames.split(","); | ||
| 291 | + if(StringUtils.isNotBlank(whereFieldNames)) | ||
| 292 | + { | ||
| 293 | + for(int i =0;i<wheres.length;i++) | ||
| 294 | + { | ||
| 295 | + try { | ||
| 296 | + Method method = object.getClass().getMethod("get"+ StringUtils.getName(wheres[i])); | ||
| 297 | + Object value = method.invoke(object); | ||
| 298 | + sql += " and "; | ||
| 299 | + sql += StringUtils.toUnderScoreCase(wheres[i]) + "=?"; | ||
| 300 | + valueList.add(value); | ||
| 301 | + } catch (IllegalAccessException e) { | ||
| 302 | + // TODO Auto-generated catch block | ||
| 303 | + e.printStackTrace(); | ||
| 304 | + } catch (IllegalArgumentException e) { | ||
| 305 | + // TODO Auto-generated catch block | ||
| 306 | + e.printStackTrace(); | ||
| 307 | + } catch (InvocationTargetException e) { | ||
| 308 | + // TODO Auto-generated catch block | ||
| 309 | + e.printStackTrace(); | ||
| 310 | + } catch (NoSuchMethodException e) { | ||
| 311 | + // TODO Auto-generated catch block | ||
| 312 | + e.printStackTrace(); | ||
| 313 | + } catch (SecurityException e) { | ||
| 314 | + // TODO Auto-generated catch block | ||
| 315 | + e.printStackTrace(); | ||
| 316 | + } | ||
| 317 | + | ||
| 318 | + } | ||
| 319 | + } | ||
| 320 | + }else{ | ||
| 321 | + Method method = null; | ||
| 322 | + try { | ||
| 323 | + String idName = "id"; | ||
| 324 | + for(Field field:fields) | ||
| 325 | + { | ||
| 326 | + PublicSQLConfig publicSQLConfig1 = field.getAnnotation(PublicSQLConfig.class); | ||
| 327 | + if(null !=publicSQLConfig1 && !publicSQLConfig1.isSelect()) | ||
| 328 | + { | ||
| 329 | + continue; | ||
| 330 | + } | ||
| 331 | + PublicSQLConfig publicSQLConfig = field.getAnnotation(PublicSQLConfig.class); | ||
| 332 | + if(null != publicSQLConfig && publicSQLConfig.isPrimarykey()) | ||
| 333 | + { | ||
| 334 | + idName = field.getName(); | ||
| 335 | + } | ||
| 336 | + } | ||
| 337 | + method = object.getClass().getMethod("get"+ StringUtils.getName(idName)); | ||
| 338 | + Object value = method.invoke(object); | ||
| 339 | + sql += " and "; | ||
| 340 | + | ||
| 341 | + sql += idName+"=?"; | ||
| 342 | + valueList.add(value); | ||
| 343 | + } catch (NoSuchMethodException e) { | ||
| 344 | + e.printStackTrace(); | ||
| 345 | + } catch (InvocationTargetException e) { | ||
| 346 | + e.printStackTrace(); | ||
| 347 | + } catch (IllegalAccessException e) { | ||
| 348 | + e.printStackTrace(); | ||
| 349 | + } | ||
| 350 | + | ||
| 351 | + } | ||
| 352 | + } | ||
| 353 | + | ||
| 354 | + try { | ||
| 355 | + return runner.update(sql,valueList.toArray()); | ||
| 356 | + } catch (SQLException e) { | ||
| 357 | + e.printStackTrace(); | ||
| 358 | + } | ||
| 359 | + return 0; | ||
| 360 | + } | ||
| 361 | + | ||
| 362 | + /** | ||
| 363 | + * 根据条件获取对象 | ||
| 364 | + * @param clas 对象 | ||
| 365 | + * @param where 条件 | ||
| 366 | + * @param <T> | ||
| 367 | + * @return | ||
| 368 | + */ | ||
| 369 | + public <T> Object get(Class<T> clas,Map<String,Object> where) | ||
| 370 | + { | ||
| 371 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 372 | + | ||
| 373 | + String tableName = StringUtils.toUnderScoreCase(clas.getSimpleName()); | ||
| 374 | + | ||
| 375 | + String sql = "select * from "+tableName+" where 1=1 "; | ||
| 376 | + try { | ||
| 377 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 378 | + sql += getCountFrommapWhere(where,valueList); | ||
| 379 | + return runner.query(sql, new BeanHandler<T>(clas, getRowProcessor()),valueList.toArray()); | ||
| 380 | + } catch (SQLException e) { | ||
| 381 | + e.printStackTrace(); | ||
| 382 | + } | ||
| 383 | + return null; | ||
| 384 | + } | ||
| 385 | + | ||
| 386 | + /** | ||
| 387 | + * 根据条件获取对象 | ||
| 388 | + * @param clas 对象 | ||
| 389 | + * @param where 条件 | ||
| 390 | + * @param <T> | ||
| 391 | + * @return | ||
| 392 | + */ | ||
| 393 | + public <T> Object get(Class<T> clas,String where,String tableName) | ||
| 394 | + { | ||
| 395 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 396 | + | ||
| 397 | + String sql = "select * from "+tableName+" where 1=1 "; | ||
| 398 | + try { | ||
| 399 | + sql += "and "+where; | ||
| 400 | + return runner.query(sql, new BeanHandler<T>(clas, getRowProcessor())); | ||
| 401 | + } catch (SQLException e) { | ||
| 402 | + e.printStackTrace(); | ||
| 403 | + } | ||
| 404 | + return null; | ||
| 405 | + } | ||
| 406 | + | ||
| 407 | + /** | ||
| 408 | + * 根据id获取对象 | ||
| 409 | + * @param clas 对象 | ||
| 410 | + * @param id 主键id | ||
| 411 | + * @param <T> | ||
| 412 | + * @return | ||
| 413 | + */ | ||
| 414 | + public <T> Object get(Class<T> clas,Object id) | ||
| 415 | + { | ||
| 416 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 417 | + | ||
| 418 | + String tableName = StringUtils.toUnderScoreCase(clas.getSimpleName()); | ||
| 419 | + | ||
| 420 | + String sql = "select * from "+tableName+" where 1=1 "; | ||
| 421 | + String idName = "id"; | ||
| 422 | + Field[] fields = clas.getDeclaredFields(); | ||
| 423 | + for(Field field:fields) | ||
| 424 | + { | ||
| 425 | + PublicSQLConfig publicSQLConfig1 = field.getAnnotation(PublicSQLConfig.class); | ||
| 426 | + if(null !=publicSQLConfig1 && !publicSQLConfig1.isSelect()) | ||
| 427 | + { | ||
| 428 | + continue; | ||
| 429 | + } | ||
| 430 | + PublicSQLConfig publicSQLConfig = field.getAnnotation(PublicSQLConfig.class); | ||
| 431 | + if(null != publicSQLConfig && publicSQLConfig.isPrimarykey()) | ||
| 432 | + { | ||
| 433 | + idName = field.getName(); | ||
| 434 | + } | ||
| 435 | + } | ||
| 436 | + | ||
| 437 | + try { | ||
| 438 | + sql += " and "+idName+"=?"; | ||
| 439 | + Object[] params = {id}; | ||
| 440 | + return runner.query(sql, new BeanHandler<T>(clas, getRowProcessor()),params); | ||
| 441 | + } catch (SQLException e) { | ||
| 442 | + e.printStackTrace(); | ||
| 443 | + } | ||
| 444 | + return null; | ||
| 445 | + } | ||
| 446 | + | ||
| 447 | + /** | ||
| 448 | + * 根据条件删除对象 | ||
| 449 | + * @param clas 对象 | ||
| 450 | + * @param where 条件 | ||
| 451 | + * @return | ||
| 452 | + */ | ||
| 453 | + public int delete(Class<?> clas, Map<String,Object> where) | ||
| 454 | + { | ||
| 455 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 456 | + | ||
| 457 | + String tableName = StringUtils.toUnderScoreCase(clas.getSimpleName()); | ||
| 458 | + String sql = "DELETE FROM "+tableName+" WHERE 1=1 "; | ||
| 459 | + try { | ||
| 460 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 461 | + sql += getCountFrommapWhere(where,valueList); | ||
| 462 | + return runner.update(sql,valueList.toArray()); | ||
| 463 | + } catch (SQLException e) { | ||
| 464 | + e.printStackTrace(); | ||
| 465 | + } | ||
| 466 | + return 0; | ||
| 467 | + } | ||
| 468 | + | ||
| 469 | + /** | ||
| 470 | + * 根据id删除对象 | ||
| 471 | + * @param clas 对象 | ||
| 472 | + * @param id 主键id | ||
| 473 | + */ | ||
| 474 | + public int delete(Class<?> clas,Object id) | ||
| 475 | + { | ||
| 476 | + return delete(clas,id,null); | ||
| 477 | + } | ||
| 478 | + | ||
| 479 | + /** | ||
| 480 | + * 根据id删除对象 | ||
| 481 | + * @param clas 对象 | ||
| 482 | + * @param id 主键id | ||
| 483 | + */ | ||
| 484 | + public int delete(Class<?> clas,Object id,String tableName) | ||
| 485 | + { | ||
| 486 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 487 | + | ||
| 488 | + if(StringUtils.isBlank(tableName)) | ||
| 489 | + { | ||
| 490 | + tableName = StringUtils.toUnderScoreCase(clas.getSimpleName()); | ||
| 491 | + } | ||
| 492 | + String sql = "DELETE FROM "+tableName+" WHERE 1=1 "; | ||
| 493 | + try { | ||
| 494 | + sql += " and id=?"; | ||
| 495 | + Object[] params = {id}; | ||
| 496 | + return runner.update(sql,params); | ||
| 497 | + } catch (SQLException e) { | ||
| 498 | + e.printStackTrace(); | ||
| 499 | + } | ||
| 500 | + return 0; | ||
| 501 | + } | ||
| 502 | + | ||
| 503 | + /** | ||
| 504 | + * 对象条件获取对象列表 | ||
| 505 | + * @param object 对象条件 | ||
| 506 | + * @param <T> | ||
| 507 | + * @return | ||
| 508 | + */ | ||
| 509 | + public <T> T find(Object object) | ||
| 510 | + { | ||
| 511 | + return find(object,"*"); | ||
| 512 | + } | ||
| 513 | + | ||
| 514 | + /** | ||
| 515 | + * 对象条件获取对象指定属性值列表 | ||
| 516 | + * @param object 对象条件 | ||
| 517 | + * @param selectStr 指定属性值(用数据库表字段多个,分割) | ||
| 518 | + * @param <T> | ||
| 519 | + * @return | ||
| 520 | + */ | ||
| 521 | + public <T> T find(Object object,String selectStr) | ||
| 522 | + { | ||
| 523 | + return find(object,selectStr,null); | ||
| 524 | + } | ||
| 525 | + | ||
| 526 | + /** | ||
| 527 | + * 对象条件的条件获取对象列表 | ||
| 528 | + * @param object 对象条件 | ||
| 529 | + * @param selectStr 指定属性值(用数据库表字段多个,分割) | ||
| 530 | + * @param whereMap 对象条件的条件 | ||
| 531 | + * @param <T> | ||
| 532 | + * @return | ||
| 533 | + */ | ||
| 534 | + public <T> T find(Object object,String selectStr,Map<String,String> whereMap) | ||
| 535 | + { | ||
| 536 | + return find(object,selectStr,whereMap,null); | ||
| 537 | + } | ||
| 538 | + | ||
| 539 | + /** | ||
| 540 | + * 对象条件的条件按一定排序获取对象列表 | ||
| 541 | + * @param object 对象条件 | ||
| 542 | + * @param selectStr 指定属性值(用数据库表字段多个,分割) | ||
| 543 | + * @param whereMap 对象条件的条件 | ||
| 544 | + * @param order 排序条件 | ||
| 545 | + * @param <T> | ||
| 546 | + * @return | ||
| 547 | + */ | ||
| 548 | + public <T> T find(Object object,String selectStr,Map<String,String> whereMap,String order) | ||
| 549 | + { | ||
| 550 | + return find(object,selectStr,whereMap,order,0,0); | ||
| 551 | + } | ||
| 552 | + | ||
| 553 | + | ||
| 554 | + /** | ||
| 555 | + * 对象条件获取对象列表 | ||
| 556 | + * @param object 对象条件 | ||
| 557 | + * @param selectStr 指定属性值(用数据库表字段多个,分割) | ||
| 558 | + * @param whereMap 对象条件的条件 | ||
| 559 | + * @param order 排序条件 | ||
| 560 | + * @param pageSize 页面大小 | ||
| 561 | + * @param pageNo 页码 | ||
| 562 | + * @param <T> | ||
| 563 | + * @return | ||
| 564 | + */ | ||
| 565 | + public <T> T find(Object object,String selectStr,Map<String,String> whereMap,String order,Integer pageSize,Integer pageNo ) | ||
| 566 | + { | ||
| 567 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 568 | + | ||
| 569 | + if(StringUtils.isBlank(order)) | ||
| 570 | + { | ||
| 571 | + order = ""; | ||
| 572 | + } | ||
| 573 | + if(null == pageSize) | ||
| 574 | + { | ||
| 575 | + pageSize = 0; | ||
| 576 | + } | ||
| 577 | + if(null == pageNo) | ||
| 578 | + { | ||
| 579 | + pageNo = 0; | ||
| 580 | + } | ||
| 581 | + if(StringUtils.isBlank("selectStr")) | ||
| 582 | + { | ||
| 583 | + selectStr = "*"; | ||
| 584 | + } | ||
| 585 | + | ||
| 586 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 587 | + String sql = getFindSql(object,selectStr,whereMap,valueList); | ||
| 588 | + | ||
| 589 | + if(StringUtils.isNotBlank(order)) | ||
| 590 | + { | ||
| 591 | + sql += " order by "+order; | ||
| 592 | + } | ||
| 593 | + if(0 != pageSize && 0 != pageNo) | ||
| 594 | + { | ||
| 595 | + sql += " limit "+((pageNo-1)*pageSize)+","+pageSize; | ||
| 596 | + } | ||
| 597 | + try { | ||
| 598 | + return (T) runner. | ||
| 599 | + query(sql,new BeanListHandler(object.getClass(),getRowProcessor()),valueList.toArray()); | ||
| 600 | + } catch (SQLException e) { | ||
| 601 | + e.printStackTrace(); | ||
| 602 | + } | ||
| 603 | + return null; | ||
| 604 | + } | ||
| 605 | + | ||
| 606 | + /** | ||
| 607 | + * sql执行查询 | ||
| 608 | + * @param sql | ||
| 609 | + * @return | ||
| 610 | + */ | ||
| 611 | + public <T> T findBysql(String sql,Class type,Object... params) | ||
| 612 | + { | ||
| 613 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 614 | + | ||
| 615 | + try { | ||
| 616 | + return (T) runner. | ||
| 617 | + query(sql,new BeanListHandler(type,getRowProcessor()),params); | ||
| 618 | + } catch (SQLException e) { | ||
| 619 | + e.printStackTrace(); | ||
| 620 | + } | ||
| 621 | + return null; | ||
| 622 | + } | ||
| 623 | + | ||
| 624 | + /** | ||
| 625 | + * 对象条件获取总数 | ||
| 626 | + * @param object 对象条件 | ||
| 627 | + * @return | ||
| 628 | + */ | ||
| 629 | + public Long getTotle(Object object ) | ||
| 630 | + { | ||
| 631 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 632 | + | ||
| 633 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 634 | + String sql = getFindSql(object,"count(*)",null,valueList); | ||
| 635 | + try { | ||
| 636 | + return runner.query(sql,new ScalarHandler<Long>(),valueList.toArray()); | ||
| 637 | + } catch (SQLException e) { | ||
| 638 | + e.printStackTrace(); | ||
| 639 | + } | ||
| 640 | + return 0l; | ||
| 641 | + } | ||
| 642 | + | ||
| 643 | + /** | ||
| 644 | + * 对象条件的条件获取总数 | ||
| 645 | + * @param object 对象条件 | ||
| 646 | + * @param whereMap 对象条件的条件 | ||
| 647 | + * @return | ||
| 648 | + */ | ||
| 649 | + public Long getTotle(Object object,Map<String,String> whereMap) | ||
| 650 | + { | ||
| 651 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 652 | + List<Object> valueList = new ArrayList<Object>(); | ||
| 653 | + String sql = getFindSql(object,"count(*)",whereMap,valueList); | ||
| 654 | + try { | ||
| 655 | + return runner.query(sql,new ScalarHandler<Long>(),valueList.toArray()); | ||
| 656 | + } catch (SQLException e) { | ||
| 657 | + e.printStackTrace(); | ||
| 658 | + } | ||
| 659 | + return 0l; | ||
| 660 | + } | ||
| 661 | + | ||
| 662 | + /** | ||
| 663 | + * sql执行查询 | ||
| 664 | + * @param sql | ||
| 665 | + * @return | ||
| 666 | + */ | ||
| 667 | + public List findListBysql(String sql) | ||
| 668 | + { | ||
| 669 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 670 | + | ||
| 671 | + List list = null; | ||
| 672 | + try { | ||
| 673 | + list = runner.query(sql,new MapListHandler()); | ||
| 674 | + } catch (SQLException e) { | ||
| 675 | + e.printStackTrace(); | ||
| 676 | + } | ||
| 677 | + return list; | ||
| 678 | + } | ||
| 679 | + | ||
| 680 | + /** | ||
| 681 | + * sql执行查询 | ||
| 682 | + * @param sql | ||
| 683 | + * @return | ||
| 684 | + */ | ||
| 685 | + public List<Map<String,Object>> findBysql(String sql) | ||
| 686 | + { | ||
| 687 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 688 | + | ||
| 689 | + List list = null; | ||
| 690 | + try { | ||
| 691 | + list = runner.query(sql,new MapListHandler()); | ||
| 692 | + } catch (SQLException e) { | ||
| 693 | + e.printStackTrace(); | ||
| 694 | + } | ||
| 695 | + return list; | ||
| 696 | + } | ||
| 697 | + | ||
| 698 | + /** | ||
| 699 | + * sql执行更新 | ||
| 700 | + * @param sql | ||
| 701 | + * @param params | ||
| 702 | + * @return | ||
| 703 | + */ | ||
| 704 | + public int updateBySql(String sql,Object... params) | ||
| 705 | + { | ||
| 706 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 707 | + try { | ||
| 708 | + return runner.update(sql,params); | ||
| 709 | + } catch (SQLException e) { | ||
| 710 | + e.printStackTrace(); | ||
| 711 | + } | ||
| 712 | + return 0; | ||
| 713 | + } | ||
| 714 | + | ||
| 715 | + /** | ||
| 716 | + * 生成查询条件 | ||
| 717 | + * @param object | ||
| 718 | + * @param selectStr | ||
| 719 | + * @param whereMap | ||
| 720 | + * @param valueList | ||
| 721 | + * @return | ||
| 722 | + */ | ||
| 723 | + private String getFindSql(Object object,String selectStr,Map<String,String> whereMap,List<Object> valueList ) | ||
| 724 | + { | ||
| 725 | + | ||
| 726 | + String sql = null; | ||
| 727 | + sql = "select "+selectStr+" from "+ changTableNameFromObject(object); | ||
| 728 | + String where = " where 1=1 "; | ||
| 729 | + String like = ""; | ||
| 730 | + Field[] fields = object.getClass().getDeclaredFields(); | ||
| 731 | + | ||
| 732 | + if(null != fields && fields.length !=0 ) | ||
| 733 | + { | ||
| 734 | + for(int i=0;i<fields.length;i++) | ||
| 735 | + { | ||
| 736 | + Field field = fields[i]; | ||
| 737 | + PublicSQLConfig publicSQLConfig = field.getAnnotation(PublicSQLConfig.class); | ||
| 738 | + if(null !=publicSQLConfig && !publicSQLConfig.isSelect()) | ||
| 739 | + { | ||
| 740 | + continue; | ||
| 741 | + } | ||
| 742 | + try { | ||
| 743 | + Method method; | ||
| 744 | + method = object.getClass().getMethod("get"+ StringUtils.getName(field.getName())); | ||
| 745 | + Object value = method.invoke(object); | ||
| 746 | + if(!(null == value)) | ||
| 747 | + { | ||
| 748 | + String orther = ""; | ||
| 749 | + String s = "="; | ||
| 750 | + if(!(null == whereMap || null == whereMap.get(field.getName()))) | ||
| 751 | + { | ||
| 752 | + s = whereMap.get(field.getName()); | ||
| 753 | + if("like".equals(s)) | ||
| 754 | + { | ||
| 755 | + value = "%"+value+"%"; | ||
| 756 | + like += " or " + "`"+ StringUtils.toUnderScoreCase(field.getName())+"`"+s+" ?"+orther ; | ||
| 757 | + valueList.add(value); | ||
| 758 | + continue; | ||
| 759 | + } | ||
| 760 | + if("time".equals(s)) | ||
| 761 | + { | ||
| 762 | + s = ">"; | ||
| 763 | + orther = " and `"+ StringUtils.toUnderScoreCase(field.getName())+"`< '"+whereMap.get("end_"+field.getName())+"'"; | ||
| 764 | + } | ||
| 765 | + } | ||
| 766 | + where += " and `"+ StringUtils.toUnderScoreCase(field.getName())+"`"+s+" ?"+orther; | ||
| 767 | + valueList.add(value); | ||
| 768 | + } | ||
| 769 | + } catch (NoSuchMethodException e) { | ||
| 770 | + e.printStackTrace(); | ||
| 771 | + } catch (SecurityException e) { | ||
| 772 | + // TODO Auto-generated catch block | ||
| 773 | + e.printStackTrace(); | ||
| 774 | + } catch (IllegalAccessException e) { | ||
| 775 | + // TODO Auto-generated catch block | ||
| 776 | + e.printStackTrace(); | ||
| 777 | + } catch (IllegalArgumentException e) { | ||
| 778 | + // TODO Auto-generated catch block | ||
| 779 | + e.printStackTrace(); | ||
| 780 | + } catch (InvocationTargetException e) { | ||
| 781 | + // TODO Auto-generated catch block | ||
| 782 | + e.printStackTrace(); | ||
| 783 | + } | ||
| 784 | + | ||
| 785 | + } | ||
| 786 | + } | ||
| 787 | + sql += where; | ||
| 788 | + if(StringUtils.isNoneBlank(like)) | ||
| 789 | + { | ||
| 790 | + sql += "and (1=2 "+like+")"; | ||
| 791 | + } | ||
| 792 | + return sql; | ||
| 793 | + } | ||
| 794 | + | ||
| 795 | + /** | ||
| 796 | + * 添加或更新对象 | ||
| 797 | + * INSERT INTO test(`in1`,`str1`) VALUES ('1','1'); | ||
| 798 | + * @param object 对象 | ||
| 799 | + * @return | ||
| 800 | + */ | ||
| 801 | + public void saveOrUpdateObject(Object object) | ||
| 802 | + { | ||
| 803 | + String sql = "insert into "; | ||
| 804 | + | ||
| 805 | + sql += changTableNameFromObject(object) + "("; | ||
| 806 | + Field[] fields = object.getClass().getDeclaredFields( ); | ||
| 807 | + String values = "("; | ||
| 808 | + String update = ""; | ||
| 809 | + for(Field field:fields) | ||
| 810 | + {// | ||
| 811 | + Method method; | ||
| 812 | + try { | ||
| 813 | + PublicSQLConfig publicSQLConfig = field.getAnnotation(PublicSQLConfig.class); | ||
| 814 | + if(null !=publicSQLConfig && !publicSQLConfig.isSelect()) | ||
| 815 | + { | ||
| 816 | + continue; | ||
| 817 | + } | ||
| 818 | + method = object.getClass().getMethod("get"+ StringUtils.getName(field.getName())); | ||
| 819 | + Object value = method.invoke(object); | ||
| 820 | + if(null != value) | ||
| 821 | + { | ||
| 822 | + if(!"(".equals(values) ) | ||
| 823 | + { | ||
| 824 | + sql += ","; | ||
| 825 | + values += ","; | ||
| 826 | + update += ","; | ||
| 827 | + } | ||
| 828 | + sql += "`"+ StringUtils.toUnderScoreCase(field.getName())+"`"; | ||
| 829 | + values += "'"+ value+"'"; | ||
| 830 | + update += "`"+ StringUtils.toUnderScoreCase(field.getName())+"`"+"=VALUES("+"`"+ StringUtils.toUnderScoreCase(field.getName())+"`)"; | ||
| 831 | + } | ||
| 832 | + } catch (NoSuchMethodException e) { | ||
| 833 | + } catch (SecurityException e) { | ||
| 834 | + // TODO Auto-generated catch block | ||
| 835 | + e.printStackTrace(); | ||
| 836 | + } catch (IllegalAccessException e) { | ||
| 837 | + // TODO Auto-generated catch block | ||
| 838 | + e.printStackTrace(); | ||
| 839 | + } catch (IllegalArgumentException e) { | ||
| 840 | + // TODO Auto-generated catch block | ||
| 841 | + e.printStackTrace(); | ||
| 842 | + } catch (InvocationTargetException e) { | ||
| 843 | + // TODO Auto-generated catch block | ||
| 844 | + e.printStackTrace(); | ||
| 845 | + } | ||
| 846 | + | ||
| 847 | + | ||
| 848 | + } | ||
| 849 | + sql += ")"; | ||
| 850 | + values += ")"; | ||
| 851 | + try { | ||
| 852 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 853 | + runner.update(sql+" values "+values+" ON DUPLICATE KEY UPDATE "+update); | ||
| 854 | + } catch (SQLException e) { | ||
| 855 | + e.printStackTrace(); | ||
| 856 | + } | ||
| 857 | + } | ||
| 858 | + | ||
| 859 | + /** | ||
| 860 | + * 添加或更新对象列表 | ||
| 861 | + * INSERT INTO `test` (`in1`,`str1`)VALUES ('1','2'),('2','2') ON DUPLICATE KEY UPDATE `in1`=VALUES(`in1`),`str1`=VALUES(`str1`); | ||
| 862 | + * @param objectlist 对象列表 | ||
| 863 | + * @return | ||
| 864 | + */ | ||
| 865 | + public void saveOrUpdateObjectList(List<Object> objectlist) | ||
| 866 | + { | ||
| 867 | + StringBuffer sb =new StringBuffer(); | ||
| 868 | + String update = ""; | ||
| 869 | + for(int i = 0; i<objectlist.size();i++) | ||
| 870 | + { | ||
| 871 | + Object object = objectlist.get(i); | ||
| 872 | + | ||
| 873 | + Field[] fields = object.getClass().getDeclaredFields( ); | ||
| 874 | + if(i==0) | ||
| 875 | + { | ||
| 876 | + sb.append("INSERT INTO `"+changTableNameFromObject(object)+"` "); | ||
| 877 | + sb.append("("); | ||
| 878 | + for(Field field:fields) | ||
| 879 | + { | ||
| 880 | + PublicSQLConfig publicSQLConfig = field.getAnnotation(PublicSQLConfig.class); | ||
| 881 | + if(null !=publicSQLConfig && !publicSQLConfig.isSelect()) | ||
| 882 | + { | ||
| 883 | + continue; | ||
| 884 | + } | ||
| 885 | + if(!"".equals(update) ) | ||
| 886 | + { | ||
| 887 | + sb.append(","); | ||
| 888 | + update += ","; | ||
| 889 | + } | ||
| 890 | + sb.append("`"+ StringUtils.toUnderScoreCase(field.getName())+"`"); | ||
| 891 | + update += "`"+ StringUtils.toUnderScoreCase(field.getName())+"`"+"=VALUES("+"`"+ StringUtils.toUnderScoreCase(field.getName())+"`)"; | ||
| 892 | + } | ||
| 893 | + sb.append(")"); | ||
| 894 | + sb.append("VALUES "); | ||
| 895 | + }else{ | ||
| 896 | + sb.append(","); | ||
| 897 | + } | ||
| 898 | + for(int j=0;j<fields.length;j++) | ||
| 899 | + { | ||
| 900 | + Field field = fields[j]; | ||
| 901 | + Method method; | ||
| 902 | + try { | ||
| 903 | + method = object.getClass().getMethod("get"+ StringUtils.getName(field.getName())); | ||
| 904 | + Object value = method.invoke(object); | ||
| 905 | + if(null == value) | ||
| 906 | + { | ||
| 907 | + value = ""; | ||
| 908 | + } | ||
| 909 | + if(j!=0) | ||
| 910 | + { | ||
| 911 | + sb.append(","); | ||
| 912 | + }else{ | ||
| 913 | + sb.append("("); | ||
| 914 | + } | ||
| 915 | + sb.append("'"+ value+"'"); | ||
| 916 | + if(j==fields.length-1) | ||
| 917 | + { | ||
| 918 | + sb.append(")"); | ||
| 919 | + } | ||
| 920 | + } catch (NoSuchMethodException e) { | ||
| 921 | + // TODO Auto-generated catch block | ||
| 922 | + e.printStackTrace(); | ||
| 923 | + } catch (SecurityException e) { | ||
| 924 | + // TODO Auto-generated catch block | ||
| 925 | + e.printStackTrace(); | ||
| 926 | + } catch (IllegalAccessException e) { | ||
| 927 | + // TODO Auto-generated catch block | ||
| 928 | + e.printStackTrace(); | ||
| 929 | + } catch (IllegalArgumentException e) { | ||
| 930 | + // TODO Auto-generated catch block | ||
| 931 | + e.printStackTrace(); | ||
| 932 | + } catch (InvocationTargetException e) { | ||
| 933 | + // TODO Auto-generated catch block | ||
| 934 | + e.printStackTrace(); | ||
| 935 | + } | ||
| 936 | + | ||
| 937 | + } | ||
| 938 | + } | ||
| 939 | + sb.append(" ON DUPLICATE KEY UPDATE "); | ||
| 940 | + sb.append(update); | ||
| 941 | + try { | ||
| 942 | + QueryRunner runner = new QueryRunner(dBFactory.getDataSource()); | ||
| 943 | + runner.update(sb.toString()); | ||
| 944 | + } catch (SQLException e) { | ||
| 945 | + e.printStackTrace(); | ||
| 946 | + } | ||
| 947 | + } | ||
| 948 | + | ||
| 949 | + /** | ||
| 950 | + * 将map条件转换成sql条件 | ||
| 951 | + * @param mapwhere map条件 | ||
| 952 | + * @param valueList sql条件参数 | ||
| 953 | + * @return | ||
| 954 | + */ | ||
| 955 | + private String getCountFrommapWhere(Map<String,Object> mapwhere,List<Object> valueList) | ||
| 956 | + { | ||
| 957 | + String where = ""; | ||
| 958 | + for(String key:mapwhere.keySet()) | ||
| 959 | + { | ||
| 960 | + Object value = mapwhere.get(key); | ||
| 961 | + where += " and "; | ||
| 962 | + where += StringUtils.toUnderScoreCase(key) + "=?"; | ||
| 963 | + valueList.add(value); | ||
| 964 | + } | ||
| 965 | + return where; | ||
| 966 | + } | ||
| 967 | + | ||
| 968 | + /** | ||
| 969 | + * 获取一个驼峰过滤规则类 | ||
| 970 | + * @return | ||
| 971 | + */ | ||
| 972 | + private RowProcessor getRowProcessor() | ||
| 973 | + { | ||
| 974 | + // 创建一个BeanProcessor对象 | ||
| 975 | + // GenerousBeanProcessor 仅仅重写了父类BeanProcessor的mapColumnsToProperties方法 | ||
| 976 | + BeanProcessor bean = new GenerousBeanProcessor(); | ||
| 977 | + // 将GenerousBeanProcessor对象传递给BasicRowProcessor | ||
| 978 | + return new BasicRowProcessor(bean); | ||
| 979 | + } | ||
| 980 | + | ||
| 981 | + /** | ||
| 982 | + * 对象转变数据库名 | ||
| 983 | + * @param object 对象 | ||
| 984 | + * @return | ||
| 985 | + */ | ||
| 986 | + public static String changTableNameFromObject(Object object) { | ||
| 987 | + Class clas = object.getClass(); | ||
| 988 | + | ||
| 989 | + String tableNmae = clas.getSimpleName(); | ||
| 990 | + | ||
| 991 | + Method method = null; // 父类对象调用子类方法(反射原理) | ||
| 992 | + try { | ||
| 993 | + method = clas.getMethod("getTableName"); | ||
| 994 | + Object tObject = method.invoke(object); | ||
| 995 | + if(null != tObject) | ||
| 996 | + { | ||
| 997 | + tableNmae = (String) tObject; | ||
| 998 | + } | ||
| 999 | + } catch (NoSuchMethodException e) { | ||
| 1000 | + } catch (IllegalAccessException e) { | ||
| 1001 | + e.printStackTrace(); | ||
| 1002 | + } catch (InvocationTargetException e) { | ||
| 1003 | + e.printStackTrace(); | ||
| 1004 | + } | ||
| 1005 | + | ||
| 1006 | + return StringUtils.toUnderScoreCase(tableNmae); | ||
| 1007 | + } | ||
| 1008 | + | ||
| 1009 | + public QueryRunner query() | ||
| 1010 | + { | ||
| 1011 | + return new QueryRunner(dBFactory.getDataSource()); | ||
| 1012 | + } | ||
| 1013 | + | ||
| 1014 | +} |
| 1 | +/** | ||
| 2 | + * @version 0.1 | ||
| 3 | + * @describe 数据库链接工厂 | ||
| 4 | + * @author yushigui | ||
| 5 | + * @date 2014-1-19 | ||
| 6 | + */ | ||
| 7 | +package com.zhonglai.luhui.service.dao; | ||
| 8 | + | ||
| 9 | +import org.apache.commons.dbcp.BasicDataSourceFactory; | ||
| 10 | + | ||
| 11 | +import javax.sql.DataSource; | ||
| 12 | +import java.io.FileInputStream; | ||
| 13 | +import java.sql.Connection; | ||
| 14 | +import java.sql.SQLException; | ||
| 15 | +import java.util.Properties; | ||
| 16 | + | ||
| 17 | +public class DBFactoryImp implements DBFactory{ | ||
| 18 | + private static DataSource ds = null; | ||
| 19 | + static { | ||
| 20 | + try { | ||
| 21 | + if(null==ds ) | ||
| 22 | + { | ||
| 23 | + String dbPath = System.getProperty("dbPath"); | ||
| 24 | + String path = null != dbPath?dbPath:System.getProperty("user.dir")+"/configs/"; | ||
| 25 | + Properties p = new Properties(); | ||
| 26 | + System.out.println("》》》》》》》》》》》》》数据库配置文件地址:"+path+"dbcpconfig.properties"); | ||
| 27 | + p.load(new FileInputStream(path+"dbcpconfig.properties")); | ||
| 28 | +// p.load(DBFactory.class | ||
| 29 | +// .getClassLoader().getResourceAsStream("configs/dbcpconfig.properties")); | ||
| 30 | + ds = BasicDataSourceFactory.createDataSource(p); | ||
| 31 | + } | ||
| 32 | + } catch (Exception e) { | ||
| 33 | + e.printStackTrace(); | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + public static Connection getConnection() { | ||
| 38 | + try { | ||
| 39 | + return ds.getConnection(); | ||
| 40 | + } catch (SQLException e) { | ||
| 41 | + e.printStackTrace(); | ||
| 42 | + return null; | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + public DataSource getDataSource(){ | ||
| 47 | + return ds; | ||
| 48 | + } | ||
| 49 | +} |
| 1 | +package com.zhonglai.luhui.service.dao.annotation; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import java.lang.annotation.ElementType; | ||
| 5 | +import java.lang.annotation.Retention; | ||
| 6 | +import java.lang.annotation.RetentionPolicy; | ||
| 7 | +import java.lang.annotation.Target; | ||
| 8 | + | ||
| 9 | +@Target(ElementType.FIELD) | ||
| 10 | +@Retention(RetentionPolicy.RUNTIME) | ||
| 11 | +public @interface PublicSQLConfig { | ||
| 12 | + boolean isSelect() default true; | ||
| 13 | + boolean isPrimarykey() default false; | ||
| 14 | +} |
| 1 | +package com.zhonglai.luhui.service.dao.util; | ||
| 2 | + | ||
| 3 | +import org.apache.commons.lang3.ArrayUtils; | ||
| 4 | + | ||
| 5 | +import java.math.BigDecimal; | ||
| 6 | +import java.math.BigInteger; | ||
| 7 | +import java.nio.ByteBuffer; | ||
| 8 | +import java.nio.charset.Charset; | ||
| 9 | +import java.text.NumberFormat; | ||
| 10 | +import java.util.Set; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * 类型转换器 | ||
| 14 | + * | ||
| 15 | + * @author ruoyi | ||
| 16 | + */ | ||
| 17 | +public class Convert | ||
| 18 | +{ | ||
| 19 | + /** UTF-8 */ | ||
| 20 | + public static final String UTF_8 = "UTF-8"; | ||
| 21 | + public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8); | ||
| 22 | + /** | ||
| 23 | + * 转换为字符串<br> | ||
| 24 | + * 如果给定的值为null,或者转换失败,返回默认值<br> | ||
| 25 | + * 转换失败不会报错 | ||
| 26 | + * | ||
| 27 | + * @param value 被转换的值 | ||
| 28 | + * @param defaultValue 转换错误时的默认值 | ||
| 29 | + * @return 结果 | ||
| 30 | + */ | ||
| 31 | + public static String toStr(Object value, String defaultValue) | ||
| 32 | + { | ||
| 33 | + if (null == value) | ||
| 34 | + { | ||
| 35 | + return defaultValue; | ||
| 36 | + } | ||
| 37 | + if (value instanceof String) | ||
| 38 | + { | ||
| 39 | + return (String) value; | ||
| 40 | + } | ||
| 41 | + return value.toString(); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * 转换为字符串<br> | ||
| 46 | + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br> | ||
| 47 | + * 转换失败不会报错 | ||
| 48 | + * | ||
| 49 | + * @param value 被转换的值 | ||
| 50 | + * @return 结果 | ||
| 51 | + */ | ||
| 52 | + public static String toStr(Object value) | ||
| 53 | + { | ||
| 54 | + return toStr(value, null); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * 转换为字符<br> | ||
| 59 | + * 如果给定的值为null,或者转换失败,返回默认值<br> | ||
| 60 | + * 转换失败不会报错 | ||
| 61 | + * | ||
| 62 | + * @param value 被转换的值 | ||
| 63 | + * @param defaultValue 转换错误时的默认值 | ||
| 64 | + * @return 结果 | ||
| 65 | + */ | ||
| 66 | + public static Character toChar(Object value, Character defaultValue) | ||
| 67 | + { | ||
| 68 | + if (null == value) | ||
| 69 | + { | ||
| 70 | + return defaultValue; | ||
| 71 | + } | ||
| 72 | + if (value instanceof Character) | ||
| 73 | + { | ||
| 74 | + return (Character) value; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + final String valueStr = toStr(value, null); | ||
| 78 | + return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + /** | ||
| 82 | + * 转换为字符<br> | ||
| 83 | + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br> | ||
| 84 | + * 转换失败不会报错 | ||
| 85 | + * | ||
| 86 | + * @param value 被转换的值 | ||
| 87 | + * @return 结果 | ||
| 88 | + */ | ||
| 89 | + public static Character toChar(Object value) | ||
| 90 | + { | ||
| 91 | + return toChar(value, null); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * 转换为byte<br> | ||
| 96 | + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br> | ||
| 97 | + * 转换失败不会报错 | ||
| 98 | + * | ||
| 99 | + * @param value 被转换的值 | ||
| 100 | + * @param defaultValue 转换错误时的默认值 | ||
| 101 | + * @return 结果 | ||
| 102 | + */ | ||
| 103 | + public static Byte toByte(Object value, Byte defaultValue) | ||
| 104 | + { | ||
| 105 | + if (value == null) | ||
| 106 | + { | ||
| 107 | + return defaultValue; | ||
| 108 | + } | ||
| 109 | + if (value instanceof Byte) | ||
| 110 | + { | ||
| 111 | + return (Byte) value; | ||
| 112 | + } | ||
| 113 | + if (value instanceof Number) | ||
| 114 | + { | ||
| 115 | + return ((Number) value).byteValue(); | ||
| 116 | + } | ||
| 117 | + final String valueStr = toStr(value, null); | ||
| 118 | + if (StringUtils.isEmpty(valueStr)) | ||
| 119 | + { | ||
| 120 | + return defaultValue; | ||
| 121 | + } | ||
| 122 | + try | ||
| 123 | + { | ||
| 124 | + return Byte.parseByte(valueStr); | ||
| 125 | + } | ||
| 126 | + catch (Exception e) | ||
| 127 | + { | ||
| 128 | + return defaultValue; | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + /** | ||
| 133 | + * 转换为byte<br> | ||
| 134 | + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br> | ||
| 135 | + * 转换失败不会报错 | ||
| 136 | + * | ||
| 137 | + * @param value 被转换的值 | ||
| 138 | + * @return 结果 | ||
| 139 | + */ | ||
| 140 | + public static Byte toByte(Object value) | ||
| 141 | + { | ||
| 142 | + return toByte(value, null); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + /** | ||
| 146 | + * 转换为Short<br> | ||
| 147 | + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br> | ||
| 148 | + * 转换失败不会报错 | ||
| 149 | + * | ||
| 150 | + * @param value 被转换的值 | ||
| 151 | + * @param defaultValue 转换错误时的默认值 | ||
| 152 | + * @return 结果 | ||
| 153 | + */ | ||
| 154 | + public static Short toShort(Object value, Short defaultValue) | ||
| 155 | + { | ||
| 156 | + if (value == null) | ||
| 157 | + { | ||
| 158 | + return defaultValue; | ||
| 159 | + } | ||
| 160 | + if (value instanceof Short) | ||
| 161 | + { | ||
| 162 | + return (Short) value; | ||
| 163 | + } | ||
| 164 | + if (value instanceof Number) | ||
| 165 | + { | ||
| 166 | + return ((Number) value).shortValue(); | ||
| 167 | + } | ||
| 168 | + final String valueStr = toStr(value, null); | ||
| 169 | + if (StringUtils.isEmpty(valueStr)) | ||
| 170 | + { | ||
| 171 | + return defaultValue; | ||
| 172 | + } | ||
| 173 | + try | ||
| 174 | + { | ||
| 175 | + return Short.parseShort(valueStr.trim()); | ||
| 176 | + } | ||
| 177 | + catch (Exception e) | ||
| 178 | + { | ||
| 179 | + return defaultValue; | ||
| 180 | + } | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + /** | ||
| 184 | + * 转换为Short<br> | ||
| 185 | + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br> | ||
| 186 | + * 转换失败不会报错 | ||
| 187 | + * | ||
| 188 | + * @param value 被转换的值 | ||
| 189 | + * @return 结果 | ||
| 190 | + */ | ||
| 191 | + public static Short toShort(Object value) | ||
| 192 | + { | ||
| 193 | + return toShort(value, null); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + /** | ||
| 197 | + * 转换为Number<br> | ||
| 198 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 199 | + * 转换失败不会报错 | ||
| 200 | + * | ||
| 201 | + * @param value 被转换的值 | ||
| 202 | + * @param defaultValue 转换错误时的默认值 | ||
| 203 | + * @return 结果 | ||
| 204 | + */ | ||
| 205 | + public static Number toNumber(Object value, Number defaultValue) | ||
| 206 | + { | ||
| 207 | + if (value == null) | ||
| 208 | + { | ||
| 209 | + return defaultValue; | ||
| 210 | + } | ||
| 211 | + if (value instanceof Number) | ||
| 212 | + { | ||
| 213 | + return (Number) value; | ||
| 214 | + } | ||
| 215 | + final String valueStr = toStr(value, null); | ||
| 216 | + if (StringUtils.isEmpty(valueStr)) | ||
| 217 | + { | ||
| 218 | + return defaultValue; | ||
| 219 | + } | ||
| 220 | + try | ||
| 221 | + { | ||
| 222 | + return NumberFormat.getInstance().parse(valueStr); | ||
| 223 | + } | ||
| 224 | + catch (Exception e) | ||
| 225 | + { | ||
| 226 | + return defaultValue; | ||
| 227 | + } | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + /** | ||
| 231 | + * 转换为Number<br> | ||
| 232 | + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br> | ||
| 233 | + * 转换失败不会报错 | ||
| 234 | + * | ||
| 235 | + * @param value 被转换的值 | ||
| 236 | + * @return 结果 | ||
| 237 | + */ | ||
| 238 | + public static Number toNumber(Object value) | ||
| 239 | + { | ||
| 240 | + return toNumber(value, null); | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + /** | ||
| 244 | + * 转换为int<br> | ||
| 245 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 246 | + * 转换失败不会报错 | ||
| 247 | + * | ||
| 248 | + * @param value 被转换的值 | ||
| 249 | + * @param defaultValue 转换错误时的默认值 | ||
| 250 | + * @return 结果 | ||
| 251 | + */ | ||
| 252 | + public static Integer toInt(Object value, Integer defaultValue) | ||
| 253 | + { | ||
| 254 | + if (value == null) | ||
| 255 | + { | ||
| 256 | + return defaultValue; | ||
| 257 | + } | ||
| 258 | + if (value instanceof Integer) | ||
| 259 | + { | ||
| 260 | + return (Integer) value; | ||
| 261 | + } | ||
| 262 | + if (value instanceof Number) | ||
| 263 | + { | ||
| 264 | + return ((Number) value).intValue(); | ||
| 265 | + } | ||
| 266 | + final String valueStr = toStr(value, null); | ||
| 267 | + if (StringUtils.isEmpty(valueStr)) | ||
| 268 | + { | ||
| 269 | + return defaultValue; | ||
| 270 | + } | ||
| 271 | + try | ||
| 272 | + { | ||
| 273 | + return Integer.parseInt(valueStr.trim()); | ||
| 274 | + } | ||
| 275 | + catch (Exception e) | ||
| 276 | + { | ||
| 277 | + return defaultValue; | ||
| 278 | + } | ||
| 279 | + } | ||
| 280 | + | ||
| 281 | + /** | ||
| 282 | + * 转换为int<br> | ||
| 283 | + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br> | ||
| 284 | + * 转换失败不会报错 | ||
| 285 | + * | ||
| 286 | + * @param value 被转换的值 | ||
| 287 | + * @return 结果 | ||
| 288 | + */ | ||
| 289 | + public static Integer toInt(Object value) | ||
| 290 | + { | ||
| 291 | + return toInt(value, null); | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + /** | ||
| 295 | + * 转换为Integer数组<br> | ||
| 296 | + * | ||
| 297 | + * @param str 被转换的值 | ||
| 298 | + * @return 结果 | ||
| 299 | + */ | ||
| 300 | + public static Integer[] toIntArray(String str) | ||
| 301 | + { | ||
| 302 | + return toIntArray(",", str); | ||
| 303 | + } | ||
| 304 | + | ||
| 305 | + /** | ||
| 306 | + * 转换为Long数组<br> | ||
| 307 | + * | ||
| 308 | + * @param str 被转换的值 | ||
| 309 | + * @return 结果 | ||
| 310 | + */ | ||
| 311 | + public static Long[] toLongArray(String str) | ||
| 312 | + { | ||
| 313 | + return toLongArray(",", str); | ||
| 314 | + } | ||
| 315 | + | ||
| 316 | + /** | ||
| 317 | + * 转换为Integer数组<br> | ||
| 318 | + * | ||
| 319 | + * @param split 分隔符 | ||
| 320 | + * @param split 被转换的值 | ||
| 321 | + * @return 结果 | ||
| 322 | + */ | ||
| 323 | + public static Integer[] toIntArray(String split, String str) | ||
| 324 | + { | ||
| 325 | + if (StringUtils.isEmpty(str)) | ||
| 326 | + { | ||
| 327 | + return new Integer[] {}; | ||
| 328 | + } | ||
| 329 | + String[] arr = str.split(split); | ||
| 330 | + final Integer[] ints = new Integer[arr.length]; | ||
| 331 | + for (int i = 0; i < arr.length; i++) | ||
| 332 | + { | ||
| 333 | + final Integer v = toInt(arr[i], 0); | ||
| 334 | + ints[i] = v; | ||
| 335 | + } | ||
| 336 | + return ints; | ||
| 337 | + } | ||
| 338 | + | ||
| 339 | + /** | ||
| 340 | + * 转换为Long数组<br> | ||
| 341 | + * | ||
| 342 | + * @param split 分隔符 | ||
| 343 | + * @param str 被转换的值 | ||
| 344 | + * @return 结果 | ||
| 345 | + */ | ||
| 346 | + public static Long[] toLongArray(String split, String str) | ||
| 347 | + { | ||
| 348 | + if (StringUtils.isEmpty(str)) | ||
| 349 | + { | ||
| 350 | + return new Long[] {}; | ||
| 351 | + } | ||
| 352 | + String[] arr = str.split(split); | ||
| 353 | + final Long[] longs = new Long[arr.length]; | ||
| 354 | + for (int i = 0; i < arr.length; i++) | ||
| 355 | + { | ||
| 356 | + final Long v = toLong(arr[i], null); | ||
| 357 | + longs[i] = v; | ||
| 358 | + } | ||
| 359 | + return longs; | ||
| 360 | + } | ||
| 361 | + | ||
| 362 | + /** | ||
| 363 | + * 转换为String数组<br> | ||
| 364 | + * | ||
| 365 | + * @param str 被转换的值 | ||
| 366 | + * @return 结果 | ||
| 367 | + */ | ||
| 368 | + public static String[] toStrArray(String str) | ||
| 369 | + { | ||
| 370 | + return toStrArray(",", str); | ||
| 371 | + } | ||
| 372 | + | ||
| 373 | + /** | ||
| 374 | + * 转换为String数组<br> | ||
| 375 | + * | ||
| 376 | + * @param split 分隔符 | ||
| 377 | + * @param split 被转换的值 | ||
| 378 | + * @return 结果 | ||
| 379 | + */ | ||
| 380 | + public static String[] toStrArray(String split, String str) | ||
| 381 | + { | ||
| 382 | + return str.split(split); | ||
| 383 | + } | ||
| 384 | + | ||
| 385 | + /** | ||
| 386 | + * 转换为long<br> | ||
| 387 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 388 | + * 转换失败不会报错 | ||
| 389 | + * | ||
| 390 | + * @param value 被转换的值 | ||
| 391 | + * @param defaultValue 转换错误时的默认值 | ||
| 392 | + * @return 结果 | ||
| 393 | + */ | ||
| 394 | + public static Long toLong(Object value, Long defaultValue) | ||
| 395 | + { | ||
| 396 | + if (value == null) | ||
| 397 | + { | ||
| 398 | + return defaultValue; | ||
| 399 | + } | ||
| 400 | + if (value instanceof Long) | ||
| 401 | + { | ||
| 402 | + return (Long) value; | ||
| 403 | + } | ||
| 404 | + if (value instanceof Number) | ||
| 405 | + { | ||
| 406 | + return ((Number) value).longValue(); | ||
| 407 | + } | ||
| 408 | + final String valueStr = toStr(value, null); | ||
| 409 | + if (StringUtils.isEmpty(valueStr)) | ||
| 410 | + { | ||
| 411 | + return defaultValue; | ||
| 412 | + } | ||
| 413 | + try | ||
| 414 | + { | ||
| 415 | + // 支持科学计数法 | ||
| 416 | + return new BigDecimal(valueStr.trim()).longValue(); | ||
| 417 | + } | ||
| 418 | + catch (Exception e) | ||
| 419 | + { | ||
| 420 | + return defaultValue; | ||
| 421 | + } | ||
| 422 | + } | ||
| 423 | + | ||
| 424 | + /** | ||
| 425 | + * 转换为long<br> | ||
| 426 | + * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br> | ||
| 427 | + * 转换失败不会报错 | ||
| 428 | + * | ||
| 429 | + * @param value 被转换的值 | ||
| 430 | + * @return 结果 | ||
| 431 | + */ | ||
| 432 | + public static Long toLong(Object value) | ||
| 433 | + { | ||
| 434 | + return toLong(value, null); | ||
| 435 | + } | ||
| 436 | + | ||
| 437 | + /** | ||
| 438 | + * 转换为double<br> | ||
| 439 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 440 | + * 转换失败不会报错 | ||
| 441 | + * | ||
| 442 | + * @param value 被转换的值 | ||
| 443 | + * @param defaultValue 转换错误时的默认值 | ||
| 444 | + * @return 结果 | ||
| 445 | + */ | ||
| 446 | + public static Double toDouble(Object value, Double defaultValue) | ||
| 447 | + { | ||
| 448 | + if (value == null) | ||
| 449 | + { | ||
| 450 | + return defaultValue; | ||
| 451 | + } | ||
| 452 | + if (value instanceof Double) | ||
| 453 | + { | ||
| 454 | + return (Double) value; | ||
| 455 | + } | ||
| 456 | + if (value instanceof Number) | ||
| 457 | + { | ||
| 458 | + return ((Number) value).doubleValue(); | ||
| 459 | + } | ||
| 460 | + final String valueStr = toStr(value, null); | ||
| 461 | + if (StringUtils.isEmpty(valueStr)) | ||
| 462 | + { | ||
| 463 | + return defaultValue; | ||
| 464 | + } | ||
| 465 | + try | ||
| 466 | + { | ||
| 467 | + // 支持科学计数法 | ||
| 468 | + return new BigDecimal(valueStr.trim()).doubleValue(); | ||
| 469 | + } | ||
| 470 | + catch (Exception e) | ||
| 471 | + { | ||
| 472 | + return defaultValue; | ||
| 473 | + } | ||
| 474 | + } | ||
| 475 | + | ||
| 476 | + /** | ||
| 477 | + * 转换为double<br> | ||
| 478 | + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br> | ||
| 479 | + * 转换失败不会报错 | ||
| 480 | + * | ||
| 481 | + * @param value 被转换的值 | ||
| 482 | + * @return 结果 | ||
| 483 | + */ | ||
| 484 | + public static Double toDouble(Object value) | ||
| 485 | + { | ||
| 486 | + return toDouble(value, null); | ||
| 487 | + } | ||
| 488 | + | ||
| 489 | + /** | ||
| 490 | + * 转换为Float<br> | ||
| 491 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 492 | + * 转换失败不会报错 | ||
| 493 | + * | ||
| 494 | + * @param value 被转换的值 | ||
| 495 | + * @param defaultValue 转换错误时的默认值 | ||
| 496 | + * @return 结果 | ||
| 497 | + */ | ||
| 498 | + public static Float toFloat(Object value, Float defaultValue) | ||
| 499 | + { | ||
| 500 | + if (value == null) | ||
| 501 | + { | ||
| 502 | + return defaultValue; | ||
| 503 | + } | ||
| 504 | + if (value instanceof Float) | ||
| 505 | + { | ||
| 506 | + return (Float) value; | ||
| 507 | + } | ||
| 508 | + if (value instanceof Number) | ||
| 509 | + { | ||
| 510 | + return ((Number) value).floatValue(); | ||
| 511 | + } | ||
| 512 | + final String valueStr = toStr(value, null); | ||
| 513 | + if (StringUtils.isEmpty(valueStr)) | ||
| 514 | + { | ||
| 515 | + return defaultValue; | ||
| 516 | + } | ||
| 517 | + try | ||
| 518 | + { | ||
| 519 | + return Float.parseFloat(valueStr.trim()); | ||
| 520 | + } | ||
| 521 | + catch (Exception e) | ||
| 522 | + { | ||
| 523 | + return defaultValue; | ||
| 524 | + } | ||
| 525 | + } | ||
| 526 | + | ||
| 527 | + /** | ||
| 528 | + * 转换为Float<br> | ||
| 529 | + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br> | ||
| 530 | + * 转换失败不会报错 | ||
| 531 | + * | ||
| 532 | + * @param value 被转换的值 | ||
| 533 | + * @return 结果 | ||
| 534 | + */ | ||
| 535 | + public static Float toFloat(Object value) | ||
| 536 | + { | ||
| 537 | + return toFloat(value, null); | ||
| 538 | + } | ||
| 539 | + | ||
| 540 | + /** | ||
| 541 | + * 转换为boolean<br> | ||
| 542 | + * String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 543 | + * 转换失败不会报错 | ||
| 544 | + * | ||
| 545 | + * @param value 被转换的值 | ||
| 546 | + * @param defaultValue 转换错误时的默认值 | ||
| 547 | + * @return 结果 | ||
| 548 | + */ | ||
| 549 | + public static Boolean toBool(Object value, Boolean defaultValue) | ||
| 550 | + { | ||
| 551 | + if (value == null) | ||
| 552 | + { | ||
| 553 | + return defaultValue; | ||
| 554 | + } | ||
| 555 | + if (value instanceof Boolean) | ||
| 556 | + { | ||
| 557 | + return (Boolean) value; | ||
| 558 | + } | ||
| 559 | + String valueStr = toStr(value, null); | ||
| 560 | + if (StringUtils.isEmpty(valueStr)) | ||
| 561 | + { | ||
| 562 | + return defaultValue; | ||
| 563 | + } | ||
| 564 | + valueStr = valueStr.trim().toLowerCase(); | ||
| 565 | + switch (valueStr) | ||
| 566 | + { | ||
| 567 | + case "true": | ||
| 568 | + return true; | ||
| 569 | + case "false": | ||
| 570 | + return false; | ||
| 571 | + case "yes": | ||
| 572 | + return true; | ||
| 573 | + case "ok": | ||
| 574 | + return true; | ||
| 575 | + case "no": | ||
| 576 | + return false; | ||
| 577 | + case "1": | ||
| 578 | + return true; | ||
| 579 | + case "0": | ||
| 580 | + return false; | ||
| 581 | + default: | ||
| 582 | + return defaultValue; | ||
| 583 | + } | ||
| 584 | + } | ||
| 585 | + | ||
| 586 | + /** | ||
| 587 | + * 转换为boolean<br> | ||
| 588 | + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br> | ||
| 589 | + * 转换失败不会报错 | ||
| 590 | + * | ||
| 591 | + * @param value 被转换的值 | ||
| 592 | + * @return 结果 | ||
| 593 | + */ | ||
| 594 | + public static Boolean toBool(Object value) | ||
| 595 | + { | ||
| 596 | + return toBool(value, null); | ||
| 597 | + } | ||
| 598 | + | ||
| 599 | + /** | ||
| 600 | + * 转换为Enum对象<br> | ||
| 601 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 602 | + * | ||
| 603 | + * @param clazz Enum的Class | ||
| 604 | + * @param value 值 | ||
| 605 | + * @param defaultValue 默认值 | ||
| 606 | + * @return Enum | ||
| 607 | + */ | ||
| 608 | + public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue) | ||
| 609 | + { | ||
| 610 | + if (value == null) | ||
| 611 | + { | ||
| 612 | + return defaultValue; | ||
| 613 | + } | ||
| 614 | + if (clazz.isAssignableFrom(value.getClass())) | ||
| 615 | + { | ||
| 616 | + @SuppressWarnings("unchecked") | ||
| 617 | + E myE = (E) value; | ||
| 618 | + return myE; | ||
| 619 | + } | ||
| 620 | + final String valueStr = toStr(value, null); | ||
| 621 | + if (StringUtils.isEmpty(valueStr)) | ||
| 622 | + { | ||
| 623 | + return defaultValue; | ||
| 624 | + } | ||
| 625 | + try | ||
| 626 | + { | ||
| 627 | + return Enum.valueOf(clazz, valueStr); | ||
| 628 | + } | ||
| 629 | + catch (Exception e) | ||
| 630 | + { | ||
| 631 | + return defaultValue; | ||
| 632 | + } | ||
| 633 | + } | ||
| 634 | + | ||
| 635 | + /** | ||
| 636 | + * 转换为Enum对象<br> | ||
| 637 | + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br> | ||
| 638 | + * | ||
| 639 | + * @param clazz Enum的Class | ||
| 640 | + * @param value 值 | ||
| 641 | + * @return Enum | ||
| 642 | + */ | ||
| 643 | + public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value) | ||
| 644 | + { | ||
| 645 | + return toEnum(clazz, value, null); | ||
| 646 | + } | ||
| 647 | + | ||
| 648 | + /** | ||
| 649 | + * 转换为BigInteger<br> | ||
| 650 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 651 | + * 转换失败不会报错 | ||
| 652 | + * | ||
| 653 | + * @param value 被转换的值 | ||
| 654 | + * @param defaultValue 转换错误时的默认值 | ||
| 655 | + * @return 结果 | ||
| 656 | + */ | ||
| 657 | + public static BigInteger toBigInteger(Object value, BigInteger defaultValue) | ||
| 658 | + { | ||
| 659 | + if (value == null) | ||
| 660 | + { | ||
| 661 | + return defaultValue; | ||
| 662 | + } | ||
| 663 | + if (value instanceof BigInteger) | ||
| 664 | + { | ||
| 665 | + return (BigInteger) value; | ||
| 666 | + } | ||
| 667 | + if (value instanceof Long) | ||
| 668 | + { | ||
| 669 | + return BigInteger.valueOf((Long) value); | ||
| 670 | + } | ||
| 671 | + final String valueStr = toStr(value, null); | ||
| 672 | + if (StringUtils.isEmpty(valueStr)) | ||
| 673 | + { | ||
| 674 | + return defaultValue; | ||
| 675 | + } | ||
| 676 | + try | ||
| 677 | + { | ||
| 678 | + return new BigInteger(valueStr); | ||
| 679 | + } | ||
| 680 | + catch (Exception e) | ||
| 681 | + { | ||
| 682 | + return defaultValue; | ||
| 683 | + } | ||
| 684 | + } | ||
| 685 | + | ||
| 686 | + /** | ||
| 687 | + * 转换为BigInteger<br> | ||
| 688 | + * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br> | ||
| 689 | + * 转换失败不会报错 | ||
| 690 | + * | ||
| 691 | + * @param value 被转换的值 | ||
| 692 | + * @return 结果 | ||
| 693 | + */ | ||
| 694 | + public static BigInteger toBigInteger(Object value) | ||
| 695 | + { | ||
| 696 | + return toBigInteger(value, null); | ||
| 697 | + } | ||
| 698 | + | ||
| 699 | + /** | ||
| 700 | + * 转换为BigDecimal<br> | ||
| 701 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 702 | + * 转换失败不会报错 | ||
| 703 | + * | ||
| 704 | + * @param value 被转换的值 | ||
| 705 | + * @param defaultValue 转换错误时的默认值 | ||
| 706 | + * @return 结果 | ||
| 707 | + */ | ||
| 708 | + public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) | ||
| 709 | + { | ||
| 710 | + if (value == null) | ||
| 711 | + { | ||
| 712 | + return defaultValue; | ||
| 713 | + } | ||
| 714 | + if (value instanceof BigDecimal) | ||
| 715 | + { | ||
| 716 | + return (BigDecimal) value; | ||
| 717 | + } | ||
| 718 | + if (value instanceof Long) | ||
| 719 | + { | ||
| 720 | + return new BigDecimal((Long) value); | ||
| 721 | + } | ||
| 722 | + if (value instanceof Double) | ||
| 723 | + { | ||
| 724 | + return new BigDecimal((Double) value); | ||
| 725 | + } | ||
| 726 | + if (value instanceof Integer) | ||
| 727 | + { | ||
| 728 | + return new BigDecimal((Integer) value); | ||
| 729 | + } | ||
| 730 | + final String valueStr = toStr(value, null); | ||
| 731 | + if (StringUtils.isEmpty(valueStr)) | ||
| 732 | + { | ||
| 733 | + return defaultValue; | ||
| 734 | + } | ||
| 735 | + try | ||
| 736 | + { | ||
| 737 | + return new BigDecimal(valueStr); | ||
| 738 | + } | ||
| 739 | + catch (Exception e) | ||
| 740 | + { | ||
| 741 | + return defaultValue; | ||
| 742 | + } | ||
| 743 | + } | ||
| 744 | + | ||
| 745 | + /** | ||
| 746 | + * 转换为BigDecimal<br> | ||
| 747 | + * 如果给定的值为空,或者转换失败,返回默认值<br> | ||
| 748 | + * 转换失败不会报错 | ||
| 749 | + * | ||
| 750 | + * @param value 被转换的值 | ||
| 751 | + * @return 结果 | ||
| 752 | + */ | ||
| 753 | + public static BigDecimal toBigDecimal(Object value) | ||
| 754 | + { | ||
| 755 | + return toBigDecimal(value, null); | ||
| 756 | + } | ||
| 757 | + | ||
| 758 | + /** | ||
| 759 | + * 将对象转为字符串<br> | ||
| 760 | + * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法 | ||
| 761 | + * | ||
| 762 | + * @param obj 对象 | ||
| 763 | + * @return 字符串 | ||
| 764 | + */ | ||
| 765 | + public static String utf8Str(Object obj) | ||
| 766 | + { | ||
| 767 | + return str(obj, CHARSET_UTF_8); | ||
| 768 | + } | ||
| 769 | + | ||
| 770 | + /** | ||
| 771 | + * 将对象转为字符串<br> | ||
| 772 | + * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法 | ||
| 773 | + * | ||
| 774 | + * @param obj 对象 | ||
| 775 | + * @param charsetName 字符集 | ||
| 776 | + * @return 字符串 | ||
| 777 | + */ | ||
| 778 | + public static String str(Object obj, String charsetName) | ||
| 779 | + { | ||
| 780 | + return str(obj, Charset.forName(charsetName)); | ||
| 781 | + } | ||
| 782 | + | ||
| 783 | + /** | ||
| 784 | + * 将对象转为字符串<br> | ||
| 785 | + * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法 | ||
| 786 | + * | ||
| 787 | + * @param obj 对象 | ||
| 788 | + * @param charset 字符集 | ||
| 789 | + * @return 字符串 | ||
| 790 | + */ | ||
| 791 | + public static String str(Object obj, Charset charset) | ||
| 792 | + { | ||
| 793 | + if (null == obj) | ||
| 794 | + { | ||
| 795 | + return null; | ||
| 796 | + } | ||
| 797 | + | ||
| 798 | + if (obj instanceof String) | ||
| 799 | + { | ||
| 800 | + return (String) obj; | ||
| 801 | + } | ||
| 802 | + else if (obj instanceof byte[]) | ||
| 803 | + { | ||
| 804 | + return str((byte[]) obj, charset); | ||
| 805 | + } | ||
| 806 | + else if (obj instanceof Byte[]) | ||
| 807 | + { | ||
| 808 | + byte[] bytes = ArrayUtils.toPrimitive((Byte[]) obj); | ||
| 809 | + return str(bytes, charset); | ||
| 810 | + } | ||
| 811 | + else if (obj instanceof ByteBuffer) | ||
| 812 | + { | ||
| 813 | + return str((ByteBuffer) obj, charset); | ||
| 814 | + } | ||
| 815 | + return obj.toString(); | ||
| 816 | + } | ||
| 817 | + | ||
| 818 | + /** | ||
| 819 | + * 将byte数组转为字符串 | ||
| 820 | + * | ||
| 821 | + * @param bytes byte数组 | ||
| 822 | + * @param charset 字符集 | ||
| 823 | + * @return 字符串 | ||
| 824 | + */ | ||
| 825 | + public static String str(byte[] bytes, String charset) | ||
| 826 | + { | ||
| 827 | + return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset)); | ||
| 828 | + } | ||
| 829 | + | ||
| 830 | + /** | ||
| 831 | + * 解码字节码 | ||
| 832 | + * | ||
| 833 | + * @param data 字符串 | ||
| 834 | + * @param charset 字符集,如果此字段为空,则解码的结果取决于平台 | ||
| 835 | + * @return 解码后的字符串 | ||
| 836 | + */ | ||
| 837 | + public static String str(byte[] data, Charset charset) | ||
| 838 | + { | ||
| 839 | + if (data == null) | ||
| 840 | + { | ||
| 841 | + return null; | ||
| 842 | + } | ||
| 843 | + | ||
| 844 | + if (null == charset) | ||
| 845 | + { | ||
| 846 | + return new String(data); | ||
| 847 | + } | ||
| 848 | + return new String(data, charset); | ||
| 849 | + } | ||
| 850 | + | ||
| 851 | + /** | ||
| 852 | + * 将编码的byteBuffer数据转换为字符串 | ||
| 853 | + * | ||
| 854 | + * @param data 数据 | ||
| 855 | + * @param charset 字符集,如果为空使用当前系统字符集 | ||
| 856 | + * @return 字符串 | ||
| 857 | + */ | ||
| 858 | + public static String str(ByteBuffer data, String charset) | ||
| 859 | + { | ||
| 860 | + if (data == null) | ||
| 861 | + { | ||
| 862 | + return null; | ||
| 863 | + } | ||
| 864 | + | ||
| 865 | + return str(data, Charset.forName(charset)); | ||
| 866 | + } | ||
| 867 | + | ||
| 868 | + /** | ||
| 869 | + * 将编码的byteBuffer数据转换为字符串 | ||
| 870 | + * | ||
| 871 | + * @param data 数据 | ||
| 872 | + * @param charset 字符集,如果为空使用当前系统字符集 | ||
| 873 | + * @return 字符串 | ||
| 874 | + */ | ||
| 875 | + public static String str(ByteBuffer data, Charset charset) | ||
| 876 | + { | ||
| 877 | + if (null == charset) | ||
| 878 | + { | ||
| 879 | + charset = Charset.defaultCharset(); | ||
| 880 | + } | ||
| 881 | + return charset.decode(data).toString(); | ||
| 882 | + } | ||
| 883 | + | ||
| 884 | + // ----------------------------------------------------------------------- 全角半角转换 | ||
| 885 | + /** | ||
| 886 | + * 半角转全角 | ||
| 887 | + * | ||
| 888 | + * @param input String. | ||
| 889 | + * @return 全角字符串. | ||
| 890 | + */ | ||
| 891 | + public static String toSBC(String input) | ||
| 892 | + { | ||
| 893 | + return toSBC(input, null); | ||
| 894 | + } | ||
| 895 | + | ||
| 896 | + /** | ||
| 897 | + * 半角转全角 | ||
| 898 | + * | ||
| 899 | + * @param input String | ||
| 900 | + * @param notConvertSet 不替换的字符集合 | ||
| 901 | + * @return 全角字符串. | ||
| 902 | + */ | ||
| 903 | + public static String toSBC(String input, Set<Character> notConvertSet) | ||
| 904 | + { | ||
| 905 | + char c[] = input.toCharArray(); | ||
| 906 | + for (int i = 0; i < c.length; i++) | ||
| 907 | + { | ||
| 908 | + if (null != notConvertSet && notConvertSet.contains(c[i])) | ||
| 909 | + { | ||
| 910 | + // 跳过不替换的字符 | ||
| 911 | + continue; | ||
| 912 | + } | ||
| 913 | + | ||
| 914 | + if (c[i] == ' ') | ||
| 915 | + { | ||
| 916 | + c[i] = '\u3000'; | ||
| 917 | + } | ||
| 918 | + else if (c[i] < '\177') | ||
| 919 | + { | ||
| 920 | + c[i] = (char) (c[i] + 65248); | ||
| 921 | + | ||
| 922 | + } | ||
| 923 | + } | ||
| 924 | + return new String(c); | ||
| 925 | + } | ||
| 926 | + | ||
| 927 | + /** | ||
| 928 | + * 全角转半角 | ||
| 929 | + * | ||
| 930 | + * @param input String. | ||
| 931 | + * @return 半角字符串 | ||
| 932 | + */ | ||
| 933 | + public static String toDBC(String input) | ||
| 934 | + { | ||
| 935 | + return toDBC(input, null); | ||
| 936 | + } | ||
| 937 | + | ||
| 938 | + /** | ||
| 939 | + * 替换全角为半角 | ||
| 940 | + * | ||
| 941 | + * @param text 文本 | ||
| 942 | + * @param notConvertSet 不替换的字符集合 | ||
| 943 | + * @return 替换后的字符 | ||
| 944 | + */ | ||
| 945 | + public static String toDBC(String text, Set<Character> notConvertSet) | ||
| 946 | + { | ||
| 947 | + char c[] = text.toCharArray(); | ||
| 948 | + for (int i = 0; i < c.length; i++) | ||
| 949 | + { | ||
| 950 | + if (null != notConvertSet && notConvertSet.contains(c[i])) | ||
| 951 | + { | ||
| 952 | + // 跳过不替换的字符 | ||
| 953 | + continue; | ||
| 954 | + } | ||
| 955 | + | ||
| 956 | + if (c[i] == '\u3000') | ||
| 957 | + { | ||
| 958 | + c[i] = ' '; | ||
| 959 | + } | ||
| 960 | + else if (c[i] > '\uFF00' && c[i] < '\uFF5F') | ||
| 961 | + { | ||
| 962 | + c[i] = (char) (c[i] - 65248); | ||
| 963 | + } | ||
| 964 | + } | ||
| 965 | + String returnString = new String(c); | ||
| 966 | + | ||
| 967 | + return returnString; | ||
| 968 | + } | ||
| 969 | + | ||
| 970 | + /** | ||
| 971 | + * 数字金额大写转换 先写个完整的然后将如零拾替换成零 | ||
| 972 | + * | ||
| 973 | + * @param n 数字 | ||
| 974 | + * @return 中文大写数字 | ||
| 975 | + */ | ||
| 976 | + public static String digitUppercase(double n) | ||
| 977 | + { | ||
| 978 | + String[] fraction = { "角", "分" }; | ||
| 979 | + String[] digit = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; | ||
| 980 | + String[][] unit = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } }; | ||
| 981 | + | ||
| 982 | + String head = n < 0 ? "负" : ""; | ||
| 983 | + n = Math.abs(n); | ||
| 984 | + | ||
| 985 | + String s = ""; | ||
| 986 | + for (int i = 0; i < fraction.length; i++) | ||
| 987 | + { | ||
| 988 | + s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", ""); | ||
| 989 | + } | ||
| 990 | + if (s.length() < 1) | ||
| 991 | + { | ||
| 992 | + s = "整"; | ||
| 993 | + } | ||
| 994 | + int integerPart = (int) Math.floor(n); | ||
| 995 | + | ||
| 996 | + for (int i = 0; i < unit[0].length && integerPart > 0; i++) | ||
| 997 | + { | ||
| 998 | + String p = ""; | ||
| 999 | + for (int j = 0; j < unit[1].length && n > 0; j++) | ||
| 1000 | + { | ||
| 1001 | + p = digit[integerPart % 10] + unit[1][j] + p; | ||
| 1002 | + integerPart = integerPart / 10; | ||
| 1003 | + } | ||
| 1004 | + s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s; | ||
| 1005 | + } | ||
| 1006 | + return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整"); | ||
| 1007 | + } | ||
| 1008 | +} |
lh-common/lh-service-dao/src/main/java/com/zhonglai/luhui/service/dao/util/StrFormatter.java
0 → 100644
| 1 | +package com.zhonglai.luhui.service.dao.util; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * 字符串格式化 | ||
| 5 | + * | ||
| 6 | + * @author ruoyi | ||
| 7 | + */ | ||
| 8 | +public class StrFormatter | ||
| 9 | +{ | ||
| 10 | + public static final String EMPTY_JSON = "{}"; | ||
| 11 | + public static final char C_BACKSLASH = '\\'; | ||
| 12 | + public static final char C_DELIM_START = '{'; | ||
| 13 | + public static final char C_DELIM_END = '}'; | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 格式化字符串<br> | ||
| 17 | + * 此方法只是简单将占位符 {} 按照顺序替换为参数<br> | ||
| 18 | + * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br> | ||
| 19 | + * 例:<br> | ||
| 20 | + * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br> | ||
| 21 | + * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> | ||
| 22 | + * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> | ||
| 23 | + * | ||
| 24 | + * @param strPattern 字符串模板 | ||
| 25 | + * @param argArray 参数列表 | ||
| 26 | + * @return 结果 | ||
| 27 | + */ | ||
| 28 | + public static String format(final String strPattern, final Object... argArray) | ||
| 29 | + { | ||
| 30 | + if (StringUtils.isEmpty(strPattern) || StringUtils.isEmpty(argArray)) | ||
| 31 | + { | ||
| 32 | + return strPattern; | ||
| 33 | + } | ||
| 34 | + final int strPatternLength = strPattern.length(); | ||
| 35 | + | ||
| 36 | + // 初始化定义好的长度以获得更好的性能 | ||
| 37 | + StringBuilder sbuf = new StringBuilder(strPatternLength + 50); | ||
| 38 | + | ||
| 39 | + int handledPosition = 0; | ||
| 40 | + int delimIndex;// 占位符所在位置 | ||
| 41 | + for (int argIndex = 0; argIndex < argArray.length; argIndex++) | ||
| 42 | + { | ||
| 43 | + delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition); | ||
| 44 | + if (delimIndex == -1) | ||
| 45 | + { | ||
| 46 | + if (handledPosition == 0) | ||
| 47 | + { | ||
| 48 | + return strPattern; | ||
| 49 | + } | ||
| 50 | + else | ||
| 51 | + { // 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果 | ||
| 52 | + sbuf.append(strPattern, handledPosition, strPatternLength); | ||
| 53 | + return sbuf.toString(); | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + else | ||
| 57 | + { | ||
| 58 | + if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) | ||
| 59 | + { | ||
| 60 | + if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) | ||
| 61 | + { | ||
| 62 | + // 转义符之前还有一个转义符,占位符依旧有效 | ||
| 63 | + sbuf.append(strPattern, handledPosition, delimIndex - 1); | ||
| 64 | + sbuf.append(Convert.utf8Str(argArray[argIndex])); | ||
| 65 | + handledPosition = delimIndex + 2; | ||
| 66 | + } | ||
| 67 | + else | ||
| 68 | + { | ||
| 69 | + // 占位符被转义 | ||
| 70 | + argIndex--; | ||
| 71 | + sbuf.append(strPattern, handledPosition, delimIndex - 1); | ||
| 72 | + sbuf.append(C_DELIM_START); | ||
| 73 | + handledPosition = delimIndex + 1; | ||
| 74 | + } | ||
| 75 | + } | ||
| 76 | + else | ||
| 77 | + { | ||
| 78 | + // 正常占位符 | ||
| 79 | + sbuf.append(strPattern, handledPosition, delimIndex); | ||
| 80 | + sbuf.append(Convert.utf8Str(argArray[argIndex])); | ||
| 81 | + handledPosition = delimIndex + 2; | ||
| 82 | + } | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + // 加入最后一个占位符后所有的字符 | ||
| 86 | + sbuf.append(strPattern, handledPosition, strPattern.length()); | ||
| 87 | + | ||
| 88 | + return sbuf.toString(); | ||
| 89 | + } | ||
| 90 | +} |
lh-common/lh-service-dao/src/main/java/com/zhonglai/luhui/service/dao/util/StringUtils.java
0 → 100644
| 1 | +package com.zhonglai.luhui.service.dao.util; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import java.util.*; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 字符串工具类 | ||
| 8 | + * | ||
| 9 | + * @author ruoyi | ||
| 10 | + */ | ||
| 11 | +public class StringUtils extends org.apache.commons.lang3.StringUtils | ||
| 12 | +{ | ||
| 13 | + /** | ||
| 14 | + * http请求 | ||
| 15 | + */ | ||
| 16 | + public static final String HTTP = "http://"; | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * https请求 | ||
| 20 | + */ | ||
| 21 | + public static final String HTTPS = "https://"; | ||
| 22 | + /** 空字符串 */ | ||
| 23 | + private static final String NULLSTR = ""; | ||
| 24 | + | ||
| 25 | + /** 下划线 */ | ||
| 26 | + private static final char SEPARATOR = '_'; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * 获取参数不为空值 | ||
| 30 | + * | ||
| 31 | + * @param value defaultValue 要判断的value | ||
| 32 | + * @return value 返回值 | ||
| 33 | + */ | ||
| 34 | + public static <T> T nvl(T value, T defaultValue) | ||
| 35 | + { | ||
| 36 | + return value != null ? value : defaultValue; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * * 判断一个Collection是否为空, 包含List,Set,Queue | ||
| 41 | + * | ||
| 42 | + * @param coll 要判断的Collection | ||
| 43 | + * @return true:为空 false:非空 | ||
| 44 | + */ | ||
| 45 | + public static boolean isEmpty(Collection<?> coll) | ||
| 46 | + { | ||
| 47 | + return isNull(coll) || coll.isEmpty(); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + /** | ||
| 51 | + * * 判断一个Collection是否非空,包含List,Set,Queue | ||
| 52 | + * | ||
| 53 | + * @param coll 要判断的Collection | ||
| 54 | + * @return true:非空 false:空 | ||
| 55 | + */ | ||
| 56 | + public static boolean isNotEmpty(Collection<?> coll) | ||
| 57 | + { | ||
| 58 | + return !isEmpty(coll); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + /** | ||
| 62 | + * * 判断一个对象数组是否为空 | ||
| 63 | + * | ||
| 64 | + * @param objects 要判断的对象数组 | ||
| 65 | + ** @return true:为空 false:非空 | ||
| 66 | + */ | ||
| 67 | + public static boolean isEmpty(Object[] objects) | ||
| 68 | + { | ||
| 69 | + return isNull(objects) || (objects.length == 0); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * * 判断一个对象数组是否非空 | ||
| 74 | + * | ||
| 75 | + * @param objects 要判断的对象数组 | ||
| 76 | + * @return true:非空 false:空 | ||
| 77 | + */ | ||
| 78 | + public static boolean isNotEmpty(Object[] objects) | ||
| 79 | + { | ||
| 80 | + return !isEmpty(objects); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * * 判断一个Map是否为空 | ||
| 85 | + * | ||
| 86 | + * @param map 要判断的Map | ||
| 87 | + * @return true:为空 false:非空 | ||
| 88 | + */ | ||
| 89 | + public static boolean isEmpty(Map<?, ?> map) | ||
| 90 | + { | ||
| 91 | + return isNull(map) || map.isEmpty(); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * * 判断一个Map是否为空 | ||
| 96 | + * | ||
| 97 | + * @param map 要判断的Map | ||
| 98 | + * @return true:非空 false:空 | ||
| 99 | + */ | ||
| 100 | + public static boolean isNotEmpty(Map<?, ?> map) | ||
| 101 | + { | ||
| 102 | + return !isEmpty(map); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + /** | ||
| 106 | + * * 判断一个字符串是否为空串 | ||
| 107 | + * | ||
| 108 | + * @param str String | ||
| 109 | + * @return true:为空 false:非空 | ||
| 110 | + */ | ||
| 111 | + public static boolean isEmpty(String str) | ||
| 112 | + { | ||
| 113 | + return isNull(str) || NULLSTR.equals(str.trim()); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + /** | ||
| 117 | + * * 判断一个字符串是否为非空串 | ||
| 118 | + * | ||
| 119 | + * @param str String | ||
| 120 | + * @return true:非空串 false:空串 | ||
| 121 | + */ | ||
| 122 | + public static boolean isNotEmpty(String str) | ||
| 123 | + { | ||
| 124 | + return !isEmpty(str); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + /** | ||
| 128 | + * * 判断一个对象是否为空 | ||
| 129 | + * | ||
| 130 | + * @param object Object | ||
| 131 | + * @return true:为空 false:非空 | ||
| 132 | + */ | ||
| 133 | + public static boolean isNull(Object object) | ||
| 134 | + { | ||
| 135 | + return object == null; | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + /** | ||
| 139 | + * * 判断一个对象是否非空 | ||
| 140 | + * | ||
| 141 | + * @param object Object | ||
| 142 | + * @return true:非空 false:空 | ||
| 143 | + */ | ||
| 144 | + public static boolean isNotNull(Object object) | ||
| 145 | + { | ||
| 146 | + return !isNull(object); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + /** | ||
| 150 | + * * 判断一个对象是否是数组类型(Java基本型别的数组) | ||
| 151 | + * | ||
| 152 | + * @param object 对象 | ||
| 153 | + * @return true:是数组 false:不是数组 | ||
| 154 | + */ | ||
| 155 | + public static boolean isArray(Object object) | ||
| 156 | + { | ||
| 157 | + return isNotNull(object) && object.getClass().isArray(); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + /** | ||
| 161 | + * 去空格 | ||
| 162 | + */ | ||
| 163 | + public static String trim(String str) | ||
| 164 | + { | ||
| 165 | + return (str == null ? "" : str.trim()); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + /** | ||
| 169 | + * 截取字符串 | ||
| 170 | + * | ||
| 171 | + * @param str 字符串 | ||
| 172 | + * @param start 开始 | ||
| 173 | + * @return 结果 | ||
| 174 | + */ | ||
| 175 | + public static String substring(final String str, int start) | ||
| 176 | + { | ||
| 177 | + if (str == null) | ||
| 178 | + { | ||
| 179 | + return NULLSTR; | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + if (start < 0) | ||
| 183 | + { | ||
| 184 | + start = str.length() + start; | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + if (start < 0) | ||
| 188 | + { | ||
| 189 | + start = 0; | ||
| 190 | + } | ||
| 191 | + if (start > str.length()) | ||
| 192 | + { | ||
| 193 | + return NULLSTR; | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + return str.substring(start); | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + /** | ||
| 200 | + * 截取字符串 | ||
| 201 | + * | ||
| 202 | + * @param str 字符串 | ||
| 203 | + * @param start 开始 | ||
| 204 | + * @param end 结束 | ||
| 205 | + * @return 结果 | ||
| 206 | + */ | ||
| 207 | + public static String substring(final String str, int start, int end) | ||
| 208 | + { | ||
| 209 | + if (str == null) | ||
| 210 | + { | ||
| 211 | + return NULLSTR; | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + if (end < 0) | ||
| 215 | + { | ||
| 216 | + end = str.length() + end; | ||
| 217 | + } | ||
| 218 | + if (start < 0) | ||
| 219 | + { | ||
| 220 | + start = str.length() + start; | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + if (end > str.length()) | ||
| 224 | + { | ||
| 225 | + end = str.length(); | ||
| 226 | + } | ||
| 227 | + | ||
| 228 | + if (start > end) | ||
| 229 | + { | ||
| 230 | + return NULLSTR; | ||
| 231 | + } | ||
| 232 | + | ||
| 233 | + if (start < 0) | ||
| 234 | + { | ||
| 235 | + start = 0; | ||
| 236 | + } | ||
| 237 | + if (end < 0) | ||
| 238 | + { | ||
| 239 | + end = 0; | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + return str.substring(start, end); | ||
| 243 | + } | ||
| 244 | + | ||
| 245 | + /** | ||
| 246 | + * 格式化文本, {} 表示占位符<br> | ||
| 247 | + * 此方法只是简单将占位符 {} 按照顺序替换为参数<br> | ||
| 248 | + * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br> | ||
| 249 | + * 例:<br> | ||
| 250 | + * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br> | ||
| 251 | + * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> | ||
| 252 | + * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> | ||
| 253 | + * | ||
| 254 | + * @param template 文本模板,被替换的部分用 {} 表示 | ||
| 255 | + * @param params 参数值 | ||
| 256 | + * @return 格式化后的文本 | ||
| 257 | + */ | ||
| 258 | + public static String format(String template, Object... params) | ||
| 259 | + { | ||
| 260 | + if (isEmpty(params) || isEmpty(template)) | ||
| 261 | + { | ||
| 262 | + return template; | ||
| 263 | + } | ||
| 264 | + return StrFormatter.format(template, params); | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + /** | ||
| 268 | + * 是否为http(s)://开头 | ||
| 269 | + * | ||
| 270 | + * @param link 链接 | ||
| 271 | + * @return 结果 | ||
| 272 | + */ | ||
| 273 | + public static boolean ishttp(String link) | ||
| 274 | + { | ||
| 275 | + return StringUtils.startsWithAny(link, HTTP, HTTPS); | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + /** | ||
| 279 | + * 字符串转set | ||
| 280 | + * | ||
| 281 | + * @param str 字符串 | ||
| 282 | + * @param sep 分隔符 | ||
| 283 | + * @return set集合 | ||
| 284 | + */ | ||
| 285 | + public static final Set<String> str2Set(String str, String sep) | ||
| 286 | + { | ||
| 287 | + return new HashSet<String>(str2List(str, sep, true, false)); | ||
| 288 | + } | ||
| 289 | + | ||
| 290 | + /** | ||
| 291 | + * 字符串转list | ||
| 292 | + * | ||
| 293 | + * @param str 字符串 | ||
| 294 | + * @param sep 分隔符 | ||
| 295 | + * @param filterBlank 过滤纯空白 | ||
| 296 | + * @param trim 去掉首尾空白 | ||
| 297 | + * @return list集合 | ||
| 298 | + */ | ||
| 299 | + public static final List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) | ||
| 300 | + { | ||
| 301 | + List<String> list = new ArrayList<String>(); | ||
| 302 | + if (StringUtils.isEmpty(str)) | ||
| 303 | + { | ||
| 304 | + return list; | ||
| 305 | + } | ||
| 306 | + | ||
| 307 | + // 过滤空白字符串 | ||
| 308 | + if (filterBlank && StringUtils.isBlank(str)) | ||
| 309 | + { | ||
| 310 | + return list; | ||
| 311 | + } | ||
| 312 | + String[] split = str.split(sep); | ||
| 313 | + for (String string : split) | ||
| 314 | + { | ||
| 315 | + if (filterBlank && StringUtils.isBlank(string)) | ||
| 316 | + { | ||
| 317 | + continue; | ||
| 318 | + } | ||
| 319 | + if (trim) | ||
| 320 | + { | ||
| 321 | + string = string.trim(); | ||
| 322 | + } | ||
| 323 | + list.add(string); | ||
| 324 | + } | ||
| 325 | + | ||
| 326 | + return list; | ||
| 327 | + } | ||
| 328 | + | ||
| 329 | + /** | ||
| 330 | + * 查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写 | ||
| 331 | + * | ||
| 332 | + * @param cs 指定字符串 | ||
| 333 | + * @param searchCharSequences 需要检查的字符串数组 | ||
| 334 | + * @return 是否包含任意一个字符串 | ||
| 335 | + */ | ||
| 336 | + public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences) | ||
| 337 | + { | ||
| 338 | + if (isEmpty(cs) || isEmpty(searchCharSequences)) | ||
| 339 | + { | ||
| 340 | + return false; | ||
| 341 | + } | ||
| 342 | + for (CharSequence testStr : searchCharSequences) | ||
| 343 | + { | ||
| 344 | + if (containsIgnoreCase(cs, testStr)) | ||
| 345 | + { | ||
| 346 | + return true; | ||
| 347 | + } | ||
| 348 | + } | ||
| 349 | + return false; | ||
| 350 | + } | ||
| 351 | + | ||
| 352 | + /** | ||
| 353 | + * 驼峰转下划线命名 | ||
| 354 | + */ | ||
| 355 | + public static String toUnderScoreCase(String str) | ||
| 356 | + { | ||
| 357 | + if (str == null) | ||
| 358 | + { | ||
| 359 | + return null; | ||
| 360 | + } | ||
| 361 | + StringBuilder sb = new StringBuilder(); | ||
| 362 | + // 前置字符是否大写 | ||
| 363 | + boolean preCharIsUpperCase = true; | ||
| 364 | + // 当前字符是否大写 | ||
| 365 | + boolean curreCharIsUpperCase = true; | ||
| 366 | + // 下一字符是否大写 | ||
| 367 | + boolean nexteCharIsUpperCase = true; | ||
| 368 | + for (int i = 0; i < str.length(); i++) | ||
| 369 | + { | ||
| 370 | + char c = str.charAt(i); | ||
| 371 | + if (i > 0) | ||
| 372 | + { | ||
| 373 | + preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1)); | ||
| 374 | + } | ||
| 375 | + else | ||
| 376 | + { | ||
| 377 | + preCharIsUpperCase = false; | ||
| 378 | + } | ||
| 379 | + | ||
| 380 | + curreCharIsUpperCase = Character.isUpperCase(c); | ||
| 381 | + | ||
| 382 | + if (i < (str.length() - 1)) | ||
| 383 | + { | ||
| 384 | + nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1)); | ||
| 385 | + } | ||
| 386 | + | ||
| 387 | + if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) | ||
| 388 | + { | ||
| 389 | + sb.append(SEPARATOR); | ||
| 390 | + } | ||
| 391 | + else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) | ||
| 392 | + { | ||
| 393 | + sb.append(SEPARATOR); | ||
| 394 | + } | ||
| 395 | + sb.append(Character.toLowerCase(c)); | ||
| 396 | + } | ||
| 397 | + | ||
| 398 | + return sb.toString(); | ||
| 399 | + } | ||
| 400 | + | ||
| 401 | + /** | ||
| 402 | + * 是否包含字符串 | ||
| 403 | + * | ||
| 404 | + * @param str 验证字符串 | ||
| 405 | + * @param strs 字符串组 | ||
| 406 | + * @return 包含返回true | ||
| 407 | + */ | ||
| 408 | + public static boolean inStringIgnoreCase(String str, String... strs) | ||
| 409 | + { | ||
| 410 | + if (str != null && strs != null) | ||
| 411 | + { | ||
| 412 | + for (String s : strs) | ||
| 413 | + { | ||
| 414 | + if (str.equalsIgnoreCase(trim(s))) | ||
| 415 | + { | ||
| 416 | + return true; | ||
| 417 | + } | ||
| 418 | + } | ||
| 419 | + } | ||
| 420 | + return false; | ||
| 421 | + } | ||
| 422 | + | ||
| 423 | + /** | ||
| 424 | + * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld | ||
| 425 | + * | ||
| 426 | + * @param name 转换前的下划线大写方式命名的字符串 | ||
| 427 | + * @return 转换后的驼峰式命名的字符串 | ||
| 428 | + */ | ||
| 429 | + public static String convertToCamelCase(String name) | ||
| 430 | + { | ||
| 431 | + StringBuilder result = new StringBuilder(); | ||
| 432 | + // 快速检查 | ||
| 433 | + if (name == null || name.isEmpty()) | ||
| 434 | + { | ||
| 435 | + // 没必要转换 | ||
| 436 | + return ""; | ||
| 437 | + } | ||
| 438 | + else if (!name.contains("_")) | ||
| 439 | + { | ||
| 440 | + // 不含下划线,仅将首字母大写 | ||
| 441 | + return name.substring(0, 1).toUpperCase() + name.substring(1); | ||
| 442 | + } | ||
| 443 | + // 用下划线将原始字符串分割 | ||
| 444 | + String[] camels = name.split("_"); | ||
| 445 | + for (String camel : camels) | ||
| 446 | + { | ||
| 447 | + // 跳过原始字符串中开头、结尾的下换线或双重下划线 | ||
| 448 | + if (camel.isEmpty()) | ||
| 449 | + { | ||
| 450 | + continue; | ||
| 451 | + } | ||
| 452 | + // 首字母大写 | ||
| 453 | + result.append(camel.substring(0, 1).toUpperCase()); | ||
| 454 | + result.append(camel.substring(1).toLowerCase()); | ||
| 455 | + } | ||
| 456 | + return result.toString(); | ||
| 457 | + } | ||
| 458 | + | ||
| 459 | + /** | ||
| 460 | + * 驼峰式命名法 例如:user_name->userName | ||
| 461 | + */ | ||
| 462 | + public static String toCamelCase(String s) | ||
| 463 | + { | ||
| 464 | + if (s == null) | ||
| 465 | + { | ||
| 466 | + return null; | ||
| 467 | + } | ||
| 468 | + s = s.toLowerCase(); | ||
| 469 | + StringBuilder sb = new StringBuilder(s.length()); | ||
| 470 | + boolean upperCase = false; | ||
| 471 | + for (int i = 0; i < s.length(); i++) | ||
| 472 | + { | ||
| 473 | + char c = s.charAt(i); | ||
| 474 | + | ||
| 475 | + if (c == SEPARATOR) | ||
| 476 | + { | ||
| 477 | + upperCase = true; | ||
| 478 | + } | ||
| 479 | + else if (upperCase) | ||
| 480 | + { | ||
| 481 | + sb.append(Character.toUpperCase(c)); | ||
| 482 | + upperCase = false; | ||
| 483 | + } | ||
| 484 | + else | ||
| 485 | + { | ||
| 486 | + sb.append(c); | ||
| 487 | + } | ||
| 488 | + } | ||
| 489 | + return sb.toString(); | ||
| 490 | + } | ||
| 491 | + | ||
| 492 | + | ||
| 493 | + @SuppressWarnings("unchecked") | ||
| 494 | + public static <T> T cast(Object obj) | ||
| 495 | + { | ||
| 496 | + return (T) obj; | ||
| 497 | + } | ||
| 498 | + | ||
| 499 | + /** | ||
| 500 | + * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。 | ||
| 501 | + * | ||
| 502 | + * @param num 数字对象 | ||
| 503 | + * @param size 字符串指定长度 | ||
| 504 | + * @return 返回数字的字符串格式,该字符串为指定长度。 | ||
| 505 | + */ | ||
| 506 | + public static final String padl(final Number num, final int size) | ||
| 507 | + { | ||
| 508 | + return padl(num.toString(), size, '0'); | ||
| 509 | + } | ||
| 510 | + | ||
| 511 | + /** | ||
| 512 | + * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。 | ||
| 513 | + * | ||
| 514 | + * @param s 原始字符串 | ||
| 515 | + * @param size 字符串指定长度 | ||
| 516 | + * @param c 用于补齐的字符 | ||
| 517 | + * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。 | ||
| 518 | + */ | ||
| 519 | + public static final String padl(final String s, final int size, final char c) | ||
| 520 | + { | ||
| 521 | + final StringBuilder sb = new StringBuilder(size); | ||
| 522 | + if (s != null) | ||
| 523 | + { | ||
| 524 | + final int len = s.length(); | ||
| 525 | + if (s.length() <= size) | ||
| 526 | + { | ||
| 527 | + for (int i = size - len; i > 0; i--) | ||
| 528 | + { | ||
| 529 | + sb.append(c); | ||
| 530 | + } | ||
| 531 | + sb.append(s); | ||
| 532 | + } | ||
| 533 | + else | ||
| 534 | + { | ||
| 535 | + return s.substring(len - size, len); | ||
| 536 | + } | ||
| 537 | + } | ||
| 538 | + else | ||
| 539 | + { | ||
| 540 | + for (int i = size; i > 0; i--) | ||
| 541 | + { | ||
| 542 | + sb.append(c); | ||
| 543 | + } | ||
| 544 | + } | ||
| 545 | + return sb.toString(); | ||
| 546 | + } | ||
| 547 | + | ||
| 548 | + /** | ||
| 549 | + * [简要描述]:首字母大写 | ||
| 550 | + * | ||
| 551 | + * @author com.zhonglai | ||
| 552 | + * @param str | ||
| 553 | + * @return | ||
| 554 | + */ | ||
| 555 | + public static String getName(String str) { | ||
| 556 | + char ch = str.toCharArray()[0]; | ||
| 557 | + ch = (char) ((ch - 97) + 'A'); | ||
| 558 | + str = ch + str.substring(1); | ||
| 559 | + return str; | ||
| 560 | + } | ||
| 561 | + | ||
| 562 | + public static void main(String[] args) { | ||
| 563 | + System.out.println(StringUtils.toUnderScoreCase("deviceInfoId")); | ||
| 564 | + } | ||
| 565 | +} |
| 1 | package com.zhonglai.luhui.security.filter; | 1 | package com.zhonglai.luhui.security.filter; |
| 2 | 2 | ||
| 3 | +import com.ruoyi.common.exception.ServiceException; | ||
| 3 | import com.ruoyi.common.utils.StringUtils; | 4 | import com.ruoyi.common.utils.StringUtils; |
| 4 | import com.zhonglai.luhui.security.dto.BaseLoginUser; | 5 | import com.zhonglai.luhui.security.dto.BaseLoginUser; |
| 5 | import com.zhonglai.luhui.security.service.TokenService; | 6 | import com.zhonglai.luhui.security.service.TokenService; |
| 6 | import com.zhonglai.luhui.security.utils.SecurityUtils; | 7 | import com.zhonglai.luhui.security.utils.SecurityUtils; |
| 7 | import org.springframework.beans.factory.annotation.Autowired; | 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 8 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | 9 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 10 | +import org.springframework.security.core.AuthenticationException; | ||
| 9 | import org.springframework.security.core.context.SecurityContextHolder; | 11 | import org.springframework.security.core.context.SecurityContextHolder; |
| 10 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | 12 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
| 11 | import org.springframework.stereotype.Component; | 13 | import org.springframework.stereotype.Component; |
| @@ -29,12 +31,16 @@ public abstract class JwtAuthenticationTokenFilter extends OncePerRequestFilter | @@ -29,12 +31,16 @@ public abstract class JwtAuthenticationTokenFilter extends OncePerRequestFilter | ||
| 29 | throws ServletException, IOException | 31 | throws ServletException, IOException |
| 30 | { | 32 | { |
| 31 | BaseLoginUser loginUser = getBaseLoginUser(request); | 33 | BaseLoginUser loginUser = getBaseLoginUser(request); |
| 32 | - if(verifyToken(loginUser)) | 34 | + |
| 35 | + if( StringUtils.isNotNull(loginUser) && verifyToken(loginUser)) | ||
| 33 | { | 36 | { |
| 34 | UsernamePasswordAuthenticationToken authenticationToken = getUsernamePasswordAuthenticationToken(loginUser); | 37 | UsernamePasswordAuthenticationToken authenticationToken = getUsernamePasswordAuthenticationToken(loginUser); |
| 35 | authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | 38 | authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); |
| 36 | SecurityContextHolder.getContext().setAuthentication(authenticationToken); | 39 | SecurityContextHolder.getContext().setAuthentication(authenticationToken); |
| 40 | + }else { | ||
| 41 | + throw new ServiceException("token验证失败"); | ||
| 37 | } | 42 | } |
| 43 | + | ||
| 38 | chain.doFilter(request, response); | 44 | chain.doFilter(request, response); |
| 39 | } | 45 | } |
| 40 | public abstract BaseLoginUser getBaseLoginUser(HttpServletRequest request); | 46 | public abstract BaseLoginUser getBaseLoginUser(HttpServletRequest request); |
| @@ -49,8 +49,9 @@ public class SecurityConfigService { | @@ -49,8 +49,9 @@ public class SecurityConfigService { | ||
| 49 | private String antMatchers; | 49 | private String antMatchers; |
| 50 | 50 | ||
| 51 | public void configHttpSecurity(HttpSecurity httpSecurity) throws Exception{ | 51 | public void configHttpSecurity(HttpSecurity httpSecurity) throws Exception{ |
| 52 | - httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); | ||
| 53 | defaultConfigHttpSecurity(httpSecurity); | 52 | defaultConfigHttpSecurity(httpSecurity); |
| 53 | + | ||
| 54 | + httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); | ||
| 54 | // 添加JWT filter | 55 | // 添加JWT filter |
| 55 | httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); | 56 | httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); |
| 56 | // 添加CORS filter | 57 | // 添加CORS filter |
| @@ -25,6 +25,9 @@ public class GlobalExceptionHandler | @@ -25,6 +25,9 @@ public class GlobalExceptionHandler | ||
| 25 | { | 25 | { |
| 26 | private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); | 26 | private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); |
| 27 | 27 | ||
| 28 | + { | ||
| 29 | + log.info("全局异常处理器"); | ||
| 30 | + } | ||
| 28 | /** | 31 | /** |
| 29 | * 请求方式不支持 | 32 | * 请求方式不支持 |
| 30 | */ | 33 | */ |
| @@ -2,6 +2,8 @@ package com.zhonglai.luhui.device.domain; | @@ -2,6 +2,8 @@ package com.zhonglai.luhui.device.domain; | ||
| 2 | 2 | ||
| 3 | import com.ruoyi.common.annotation.PublicSQLConfig; | 3 | import com.ruoyi.common.annotation.PublicSQLConfig; |
| 4 | import com.ruoyi.common.tool.BaseEntity; | 4 | import com.ruoyi.common.tool.BaseEntity; |
| 5 | +import io.swagger.annotations.ApiModel; | ||
| 6 | +import io.swagger.annotations.ApiModelProperty; | ||
| 5 | import org.apache.commons.lang3.builder.ToStringBuilder; | 7 | import org.apache.commons.lang3.builder.ToStringBuilder; |
| 6 | import org.apache.commons.lang3.builder.ToStringStyle; | 8 | import org.apache.commons.lang3.builder.ToStringStyle; |
| 7 | 9 | ||
| @@ -11,33 +13,46 @@ import org.apache.commons.lang3.builder.ToStringStyle; | @@ -11,33 +13,46 @@ import org.apache.commons.lang3.builder.ToStringStyle; | ||
| 11 | * @author kerwincui | 13 | * @author kerwincui |
| 12 | * @date 2022-01-13 | 14 | * @date 2022-01-13 |
| 13 | */ | 15 | */ |
| 16 | +@ApiModel("设备告警对象") | ||
| 14 | public class IotAlert extends BaseEntity | 17 | public class IotAlert extends BaseEntity |
| 15 | { | 18 | { |
| 16 | @PublicSQLConfig(isSelect=false) | 19 | @PublicSQLConfig(isSelect=false) |
| 17 | private static final long serialVersionUID = 1L; | 20 | private static final long serialVersionUID = 1L; |
| 18 | 21 | ||
| 19 | /** 告警ID */ | 22 | /** 告警ID */ |
| 23 | + @ApiModelProperty("告警ID") | ||
| 20 | private Long alertId; | 24 | private Long alertId; |
| 21 | 25 | ||
| 22 | /** 告警名称 */ | 26 | /** 告警名称 */ |
| 27 | + @ApiModelProperty("告警名称") | ||
| 23 | private String alertName; | 28 | private String alertName; |
| 24 | 29 | ||
| 25 | /** 告警级别(1=提醒通知,2=轻微问题,3=严重警告) */ | 30 | /** 告警级别(1=提醒通知,2=轻微问题,3=严重警告) */ |
| 31 | + @ApiModelProperty("告警级别(1=提醒通知,2=轻微问题,3=严重警告)") | ||
| 26 | private Long alertLevel; | 32 | private Long alertLevel; |
| 27 | 33 | ||
| 34 | + /** 告警类型(1属性告警,2触发告警,3定时告警) */ | ||
| 35 | + @ApiModelProperty("告警类型(1属性告警,2触发告警,3定时告警)") | ||
| 36 | + private Integer alertType; | ||
| 37 | + | ||
| 28 | /** 产品ID */ | 38 | /** 产品ID */ |
| 39 | + @ApiModelProperty("产品ID") | ||
| 29 | private Long productId; | 40 | private Long productId; |
| 30 | 41 | ||
| 31 | /** 产品名称 */ | 42 | /** 产品名称 */ |
| 43 | + @ApiModelProperty("产品名称") | ||
| 32 | private String productName; | 44 | private String productName; |
| 33 | 45 | ||
| 34 | /** 触发器 */ | 46 | /** 触发器 */ |
| 47 | + @ApiModelProperty("触发器") | ||
| 35 | private String triggers; | 48 | private String triggers; |
| 36 | 49 | ||
| 37 | /** 执行动作 */ | 50 | /** 执行动作 */ |
| 51 | + @ApiModelProperty("执行动作") | ||
| 38 | private String actions; | 52 | private String actions; |
| 39 | 53 | ||
| 40 | /** 告警状态 (1-启动,2-停止)**/ | 54 | /** 告警状态 (1-启动,2-停止)**/ |
| 55 | + @ApiModelProperty("告警状态 (1-启动,2-停止)") | ||
| 41 | private Integer status; | 56 | private Integer status; |
| 42 | 57 | ||
| 43 | public Integer getStatus() { | 58 | public Integer getStatus() { |
| @@ -112,12 +127,21 @@ public class IotAlert extends BaseEntity | @@ -112,12 +127,21 @@ public class IotAlert extends BaseEntity | ||
| 112 | return actions; | 127 | return actions; |
| 113 | } | 128 | } |
| 114 | 129 | ||
| 130 | + public Integer getAlertType() { | ||
| 131 | + return alertType; | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + public void setAlertType(Integer alertType) { | ||
| 135 | + this.alertType = alertType; | ||
| 136 | + } | ||
| 137 | + | ||
| 115 | @Override | 138 | @Override |
| 116 | public String toString() { | 139 | public String toString() { |
| 117 | return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) | 140 | return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) |
| 118 | .append("alertId", getAlertId()) | 141 | .append("alertId", getAlertId()) |
| 119 | .append("alertName", getAlertName()) | 142 | .append("alertName", getAlertName()) |
| 120 | .append("alertLevel", getAlertLevel()) | 143 | .append("alertLevel", getAlertLevel()) |
| 144 | + .append("alertType", getAlertType()) | ||
| 121 | .append("productId", getProductId()) | 145 | .append("productId", getProductId()) |
| 122 | .append("productName", getProductName()) | 146 | .append("productName", getProductName()) |
| 123 | .append("triggers", getTriggers()) | 147 | .append("triggers", getTriggers()) |
| @@ -2,6 +2,8 @@ package com.zhonglai.luhui.device.domain; | @@ -2,6 +2,8 @@ package com.zhonglai.luhui.device.domain; | ||
| 2 | 2 | ||
| 3 | import com.ruoyi.common.annotation.PublicSQLConfig; | 3 | import com.ruoyi.common.annotation.PublicSQLConfig; |
| 4 | import com.ruoyi.common.tool.BaseEntity; | 4 | import com.ruoyi.common.tool.BaseEntity; |
| 5 | +import io.swagger.annotations.ApiModel; | ||
| 6 | +import io.swagger.annotations.ApiModelProperty; | ||
| 5 | import org.apache.commons.lang3.builder.ToStringBuilder; | 7 | import org.apache.commons.lang3.builder.ToStringBuilder; |
| 6 | import org.apache.commons.lang3.builder.ToStringStyle; | 8 | import org.apache.commons.lang3.builder.ToStringStyle; |
| 7 | 9 | ||
| @@ -11,77 +13,62 @@ import org.apache.commons.lang3.builder.ToStringStyle; | @@ -11,77 +13,62 @@ import org.apache.commons.lang3.builder.ToStringStyle; | ||
| 11 | * @author kerwincui | 13 | * @author kerwincui |
| 12 | * @date 2022-01-13 | 14 | * @date 2022-01-13 |
| 13 | */ | 15 | */ |
| 16 | +@ApiModel("设备告警记录对象") | ||
| 14 | public class IotAlertLog extends BaseEntity | 17 | public class IotAlertLog extends BaseEntity |
| 15 | { | 18 | { |
| 16 | @PublicSQLConfig(isSelect=false) | 19 | @PublicSQLConfig(isSelect=false) |
| 17 | private static final long serialVersionUID = 1L; | 20 | private static final long serialVersionUID = 1L; |
| 18 | 21 | ||
| 19 | /** 告警ID */ | 22 | /** 告警ID */ |
| 23 | + @ApiModelProperty("主键") | ||
| 20 | private Long alertLogId; | 24 | private Long alertLogId; |
| 21 | 25 | ||
| 22 | /** 告警名称 */ | 26 | /** 告警名称 */ |
| 27 | + @ApiModelProperty("告警名称") | ||
| 23 | private String alertName; | 28 | private String alertName; |
| 24 | 29 | ||
| 30 | + @ApiModelProperty("告警ID") | ||
| 31 | + private Long alertId; // bigint DEFAULT NULL COMMENT '告警ID', | ||
| 32 | + | ||
| 25 | /** 告警级别(1=提醒通知,2=轻微问题,3=严重警告,4=场景联动) */ | 33 | /** 告警级别(1=提醒通知,2=轻微问题,3=严重警告,4=场景联动) */ |
| 34 | + @ApiModelProperty("告警级别(1=提醒通知,2=轻微问题,3=严重警告,4=场景联动)") | ||
| 26 | private Long alertLevel; | 35 | private Long alertLevel; |
| 27 | 36 | ||
| 28 | /** 处理状态(0=不需要处理,1=未处理,2=已处理) */ | 37 | /** 处理状态(0=不需要处理,1=未处理,2=已处理) */ |
| 38 | + @ApiModelProperty("处理状态(0=不需要处理,1=未处理,2=已处理)") | ||
| 29 | private Long status; | 39 | private Long status; |
| 30 | 40 | ||
| 31 | - /** 产品ID */ | ||
| 32 | - private Long productId; | ||
| 33 | - | ||
| 34 | - /** 产品名称 */ | ||
| 35 | - private String productName; | ||
| 36 | - | ||
| 37 | /** 设备ID */ | 41 | /** 设备ID */ |
| 38 | - private Long deviceId; | ||
| 39 | - | ||
| 40 | - /** 设备名称 */ | ||
| 41 | - private String deviceName; | ||
| 42 | - | ||
| 43 | - /** 用户ID */ | ||
| 44 | - private Long userId; | ||
| 45 | - | ||
| 46 | - /** 用户昵称 */ | ||
| 47 | - private String userName; | ||
| 48 | - | ||
| 49 | - /** 租户ID */ | ||
| 50 | - private Long tenantId; | 42 | + @ApiModelProperty("设备ID") |
| 43 | + private String deviceId; | ||
| 51 | 44 | ||
| 52 | - /** 租户名称 */ | ||
| 53 | - private String tenantName; | 45 | + @ApiModelProperty("创建时间") |
| 46 | + private Long create_time; // datetime DEFAULT NULL COMMENT '创建时间', | ||
| 47 | + @ApiModelProperty("类型(1=告警,2=场景联动)") | ||
| 48 | + private Integer type; // tinyint DEFAULT NULL COMMENT '类型(1=告警,2=场景联动)', | ||
| 54 | 49 | ||
| 55 | - public Long getUserId() { | ||
| 56 | - return userId; | 50 | + public Long getAlertId() { |
| 51 | + return alertId; | ||
| 57 | } | 52 | } |
| 58 | 53 | ||
| 59 | - public void setUserId(Long userId) { | ||
| 60 | - this.userId = userId; | 54 | + public void setAlertId(Long alertId) { |
| 55 | + this.alertId = alertId; | ||
| 61 | } | 56 | } |
| 62 | 57 | ||
| 63 | - public String getUserName() { | ||
| 64 | - return userName; | 58 | + public Long getCreate_time() { |
| 59 | + return create_time; | ||
| 65 | } | 60 | } |
| 66 | 61 | ||
| 67 | - public void setUserName(String userName) { | ||
| 68 | - this.userName = userName; | 62 | + public void setCreate_time(Long create_time) { |
| 63 | + this.create_time = create_time; | ||
| 69 | } | 64 | } |
| 70 | 65 | ||
| 71 | - public Long getTenantId() { | ||
| 72 | - return tenantId; | 66 | + public Integer getType() { |
| 67 | + return type; | ||
| 73 | } | 68 | } |
| 74 | 69 | ||
| 75 | - public void setTenantId(Long tenantId) { | ||
| 76 | - this.tenantId = tenantId; | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - public String getTenantName() { | ||
| 80 | - return tenantName; | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | - public void setTenantName(String tenantName) { | ||
| 84 | - this.tenantName = tenantName; | 70 | + public void setType(Integer type) { |
| 71 | + this.type = type; | ||
| 85 | } | 72 | } |
| 86 | 73 | ||
| 87 | public void setAlertLogId(Long alertLogId) | 74 | public void setAlertLogId(Long alertLogId) |
| @@ -120,42 +107,15 @@ public class IotAlertLog extends BaseEntity | @@ -120,42 +107,15 @@ public class IotAlertLog extends BaseEntity | ||
| 120 | { | 107 | { |
| 121 | return status; | 108 | return status; |
| 122 | } | 109 | } |
| 123 | - public void setProductId(Long productId) | ||
| 124 | - { | ||
| 125 | - this.productId = productId; | ||
| 126 | - } | ||
| 127 | - | ||
| 128 | - public Long getProductId() | ||
| 129 | - { | ||
| 130 | - return productId; | ||
| 131 | - } | ||
| 132 | - public void setProductName(String productName) | ||
| 133 | - { | ||
| 134 | - this.productName = productName; | ||
| 135 | - } | ||
| 136 | - | ||
| 137 | - public String getProductName() | ||
| 138 | - { | ||
| 139 | - return productName; | ||
| 140 | - } | ||
| 141 | - public void setDeviceId(Long deviceId) | 110 | + public void setDeviceId(String deviceId) |
| 142 | { | 111 | { |
| 143 | this.deviceId = deviceId; | 112 | this.deviceId = deviceId; |
| 144 | } | 113 | } |
| 145 | 114 | ||
| 146 | - public Long getDeviceId() | 115 | + public String getDeviceId() |
| 147 | { | 116 | { |
| 148 | return deviceId; | 117 | return deviceId; |
| 149 | } | 118 | } |
| 150 | - public void setDeviceName(String deviceName) | ||
| 151 | - { | ||
| 152 | - this.deviceName = deviceName; | ||
| 153 | - } | ||
| 154 | - | ||
| 155 | - public String getDeviceName() | ||
| 156 | - { | ||
| 157 | - return deviceName; | ||
| 158 | - } | ||
| 159 | 119 | ||
| 160 | @Override | 120 | @Override |
| 161 | public String toString() { | 121 | public String toString() { |
| @@ -164,10 +124,7 @@ public class IotAlertLog extends BaseEntity | @@ -164,10 +124,7 @@ public class IotAlertLog extends BaseEntity | ||
| 164 | .append("alertName", getAlertName()) | 124 | .append("alertName", getAlertName()) |
| 165 | .append("alertLevel", getAlertLevel()) | 125 | .append("alertLevel", getAlertLevel()) |
| 166 | .append("status", getStatus()) | 126 | .append("status", getStatus()) |
| 167 | - .append("productId", getProductId()) | ||
| 168 | - .append("productName", getProductName()) | ||
| 169 | .append("deviceId", getDeviceId()) | 127 | .append("deviceId", getDeviceId()) |
| 170 | - .append("deviceName", getDeviceName()) | ||
| 171 | .append("createBy", getCreateBy()) | 128 | .append("createBy", getCreateBy()) |
| 172 | .append("createTime", getCreateTime()) | 129 | .append("createTime", getCreateTime()) |
| 173 | .append("updateBy", getUpdateBy()) | 130 | .append("updateBy", getUpdateBy()) |
| @@ -72,13 +72,13 @@ public class IotProduct implements Serializable | @@ -72,13 +72,13 @@ public class IotProduct implements Serializable | ||
| 72 | private Integer purification_clas; // varchar(100) DEFAULT 'com.zhonglai.luhui.device.protocol.factory.purification.DefaultProtocolPurificationFactoryImpl' COMMENT '清洗服务', | 72 | private Integer purification_clas; // varchar(100) DEFAULT 'com.zhonglai.luhui.device.protocol.factory.purification.DefaultProtocolPurificationFactoryImpl' COMMENT '清洗服务', |
| 73 | 73 | ||
| 74 | @ApiModelProperty("订阅服务器id") | 74 | @ApiModelProperty("订阅服务器id") |
| 75 | - private Integer subscribe_service_ip; | 75 | + private String subscribe_service_ip; |
| 76 | 76 | ||
| 77 | - public Integer getSubscribe_service_ip() { | 77 | + public String getSubscribe_service_ip() { |
| 78 | return subscribe_service_ip; | 78 | return subscribe_service_ip; |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | - public void setSubscribe_service_ip(Integer subscribe_service_ip) { | 81 | + public void setSubscribe_service_ip(String subscribe_service_ip) { |
| 82 | this.subscribe_service_ip = subscribe_service_ip; | 82 | this.subscribe_service_ip = subscribe_service_ip; |
| 83 | } | 83 | } |
| 84 | 84 |
| @@ -7,5 +7,9 @@ public enum CommandType { | @@ -7,5 +7,9 @@ public enum CommandType { | ||
| 7 | cleanDeviceHost, | 7 | cleanDeviceHost, |
| 8 | cleanDeviceInfo, | 8 | cleanDeviceInfo, |
| 9 | upIotThingsModel, | 9 | upIotThingsModel, |
| 10 | - upIotThingsModelTranslate | 10 | + upIotThingsModelTranslate, |
| 11 | + /** | ||
| 12 | + * 添加订阅 | ||
| 13 | + */ | ||
| 14 | + addSubscribe | ||
| 11 | } | 15 | } |
| @@ -7,39 +7,28 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -7,39 +7,28 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 7 | <resultMap type="com.zhonglai.luhui.device.domain.IotAlertLog" id="AlertLogResult"> | 7 | <resultMap type="com.zhonglai.luhui.device.domain.IotAlertLog" id="AlertLogResult"> |
| 8 | <result property="alertLogId" column="alert_log__id" /> | 8 | <result property="alertLogId" column="alert_log__id" /> |
| 9 | <result property="alertName" column="alert_name" /> | 9 | <result property="alertName" column="alert_name" /> |
| 10 | + <result property="alertId" column="alert_id" /> | ||
| 10 | <result property="alertLevel" column="alert_level" /> | 11 | <result property="alertLevel" column="alert_level" /> |
| 11 | <result property="status" column="status" /> | 12 | <result property="status" column="status" /> |
| 12 | - <result property="productId" column="product_id" /> | ||
| 13 | - <result property="productName" column="product_name" /> | ||
| 14 | <result property="deviceId" column="device_id" /> | 13 | <result property="deviceId" column="device_id" /> |
| 15 | - <result property="deviceName" column="device_name" /> | ||
| 16 | - <result property="createBy" column="create_by" /> | ||
| 17 | - <result property="createTime" column="create_time" /> | ||
| 18 | - <result property="updateBy" column="update_by" /> | ||
| 19 | - <result property="updateTime" column="update_time" /> | ||
| 20 | - <result property="remark" column="remark" /> | ||
| 21 | - <result property="userId" column="user_id" /> | ||
| 22 | - <result property="userName" column="user_name" /> | ||
| 23 | - <result property="tenantId" column="tenant_id" /> | ||
| 24 | - <result property="tenantName" column="tenant_name" /> | 14 | + <result property="create_time" column="create_time" /> |
| 15 | + <result property="type" column="type" /> | ||
| 25 | </resultMap> | 16 | </resultMap> |
| 26 | 17 | ||
| 27 | <sql id="selectAlertLogVo"> | 18 | <sql id="selectAlertLogVo"> |
| 28 | - select alert_log__id, alert_name, alert_level, status, product_id, product_name, device_id, device_name,user_id, user_name, tenant_id, tenant_name, create_by, create_time, update_by, update_time, remark from iot_alert_log | 19 | + select alert_log__id, alert_name,alert_id, alert_level, status, device_id, create_time, type from iot_alert_log |
| 29 | </sql> | 20 | </sql> |
| 30 | 21 | ||
| 31 | <select id="selectAlertLogList" parameterType="com.zhonglai.luhui.device.domain.IotAlertLog" resultMap="AlertLogResult"> | 22 | <select id="selectAlertLogList" parameterType="com.zhonglai.luhui.device.domain.IotAlertLog" resultMap="AlertLogResult"> |
| 32 | <include refid="selectAlertLogVo"/> | 23 | <include refid="selectAlertLogVo"/> |
| 33 | <where> | 24 | <where> |
| 34 | - <if test="userId != null and userId != 0"> and user_id = #{userId}</if> | ||
| 35 | - <if test="tenantId != null and tenantId != 0"> and tenant_id = #{tenantId}</if> | ||
| 36 | <if test="alertName != null and alertName != ''"> and alert_name like concat('%', #{alertName}, '%')</if> | 25 | <if test="alertName != null and alertName != ''"> and alert_name like concat('%', #{alertName}, '%')</if> |
| 37 | <if test="alertLevel != null "> and alert_level = #{alertLevel}</if> | 26 | <if test="alertLevel != null "> and alert_level = #{alertLevel}</if> |
| 38 | <if test="status != null "> and status = #{status}</if> | 27 | <if test="status != null "> and status = #{status}</if> |
| 39 | - <if test="productId != null "> and product_id = #{productId}</if> | ||
| 40 | - <if test="productName != null and productName != ''"> and product_name like concat('%', #{productName}, '%')</if> | ||
| 41 | <if test="deviceId != null "> and device_id = #{deviceId}</if> | 28 | <if test="deviceId != null "> and device_id = #{deviceId}</if> |
| 42 | - <if test="deviceName != null and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if> | 29 | + <if test="type != null "> and type = #{type}</if> |
| 30 | + <if test="params.beginTime != null "> and create_time >= #{params.beginTime}</if> | ||
| 31 | + <if test="params.endTime != null "> and create_time <= #{params.endTime}</if> | ||
| 43 | </where> | 32 | </where> |
| 44 | </select> | 33 | </select> |
| 45 | 34 | ||
| @@ -52,39 +41,21 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -52,39 +41,21 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 52 | insert into iot_alert_log | 41 | insert into iot_alert_log |
| 53 | <trim prefix="(" suffix=")" suffixOverrides=","> | 42 | <trim prefix="(" suffix=")" suffixOverrides=","> |
| 54 | <if test="alertName != null and alertName != ''">alert_name,</if> | 43 | <if test="alertName != null and alertName != ''">alert_name,</if> |
| 44 | + <if test="alertId != null">alert_id,</if> | ||
| 55 | <if test="alertLevel != null">alert_level,</if> | 45 | <if test="alertLevel != null">alert_level,</if> |
| 56 | <if test="status != null">status,</if> | 46 | <if test="status != null">status,</if> |
| 57 | - <if test="productId != null">product_id,</if> | ||
| 58 | - <if test="productName != null and productName != ''">product_name,</if> | ||
| 59 | <if test="deviceId != null">device_id,</if> | 47 | <if test="deviceId != null">device_id,</if> |
| 60 | - <if test="deviceName != null and deviceName != ''">device_name,</if> | ||
| 61 | - <if test="createBy != null">create_by,</if> | ||
| 62 | - <if test="createTime != null">create_time,</if> | ||
| 63 | - <if test="updateBy != null">update_by,</if> | ||
| 64 | - <if test="updateTime != null">update_time,</if> | ||
| 65 | - <if test="remark != null">remark,</if> | ||
| 66 | - <if test="userId != null">user_id,</if> | ||
| 67 | - <if test="userName != null and userName != ''">user_name,</if> | ||
| 68 | - <if test="tenantId != null">tenant_id,</if> | ||
| 69 | - <if test="tenantName != null and tenantName != ''">tenant_name,</if> | 48 | + <if test="create_time != null">create_time,</if> |
| 49 | + <if test="type != null">`type`,</if> | ||
| 70 | </trim> | 50 | </trim> |
| 71 | <trim prefix="values (" suffix=")" suffixOverrides=","> | 51 | <trim prefix="values (" suffix=")" suffixOverrides=","> |
| 72 | <if test="alertName != null and alertName != ''">#{alertName},</if> | 52 | <if test="alertName != null and alertName != ''">#{alertName},</if> |
| 53 | + <if test="alertId != null">#{alertId},</if> | ||
| 73 | <if test="alertLevel != null">#{alertLevel},</if> | 54 | <if test="alertLevel != null">#{alertLevel},</if> |
| 74 | <if test="status != null">#{status},</if> | 55 | <if test="status != null">#{status},</if> |
| 75 | - <if test="productId != null">#{productId},</if> | ||
| 76 | - <if test="productName != null and productName != ''">#{productName},</if> | ||
| 77 | <if test="deviceId != null">#{deviceId},</if> | 56 | <if test="deviceId != null">#{deviceId},</if> |
| 78 | - <if test="deviceName != null and deviceName != ''">#{deviceName},</if> | ||
| 79 | - <if test="createBy != null">#{createBy},</if> | ||
| 80 | - <if test="createTime != null">#{createTime},</if> | ||
| 81 | - <if test="updateBy != null">#{updateBy},</if> | ||
| 82 | - <if test="updateTime != null">#{updateTime},</if> | ||
| 83 | - <if test="remark != null">#{remark},</if> | ||
| 84 | - <if test="userId != null">#{userId},</if> | ||
| 85 | - <if test="userName != null and userName != ''">#{userName},</if> | ||
| 86 | - <if test="tenantId != null">#{tenantId},</if> | ||
| 87 | - <if test="tenantName != null and tenantName != ''">#{tenantName},</if> | 57 | + <if test="create_time != null">#{createTime},</if> |
| 58 | + <if test="type != null">#{type},</if> | ||
| 88 | </trim> | 59 | </trim> |
| 89 | </insert> | 60 | </insert> |
| 90 | 61 | ||
| @@ -92,21 +63,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -92,21 +63,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 92 | update iot_alert_log | 63 | update iot_alert_log |
| 93 | <trim prefix="SET" suffixOverrides=","> | 64 | <trim prefix="SET" suffixOverrides=","> |
| 94 | <if test="alertName != null and alertName != ''">alert_name = #{alertName},</if> | 65 | <if test="alertName != null and alertName != ''">alert_name = #{alertName},</if> |
| 66 | + <if test="alertId != null">alert_id = #{alertId},</if> | ||
| 95 | <if test="alertLevel != null">alert_level = #{alertLevel},</if> | 67 | <if test="alertLevel != null">alert_level = #{alertLevel},</if> |
| 96 | <if test="status != null">status = #{status},</if> | 68 | <if test="status != null">status = #{status},</if> |
| 97 | - <if test="productId != null">product_id = #{productId},</if> | ||
| 98 | - <if test="productName != null and productName != ''">product_name = #{productName},</if> | ||
| 99 | <if test="deviceId != null">device_id = #{deviceId},</if> | 69 | <if test="deviceId != null">device_id = #{deviceId},</if> |
| 100 | - <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if> | ||
| 101 | - <if test="createBy != null">create_by = #{createBy},</if> | ||
| 102 | - <if test="createTime != null">create_time = #{createTime},</if> | ||
| 103 | - <if test="updateBy != null">update_by = #{updateBy},</if> | ||
| 104 | - <if test="updateTime != null">update_time = #{updateTime},</if> | ||
| 105 | - <if test="remark != null">remark = #{remark},</if> | ||
| 106 | - <if test="userId != null">user_id = #{userId},</if> | ||
| 107 | - <if test="userName != null and userName != ''">user_name = #{userName},</if> | ||
| 108 | - <if test="tenantId != null">tenant_id = #{tenantId},</if> | ||
| 109 | - <if test="tenantName != null and tenantName != ''">tenant_name = #{tenantName},</if> | 70 | + <if test="create_time != null">create_time = #{create_time},</if> |
| 71 | + <if test="type != null">`type` = #{type},</if> | ||
| 110 | </trim> | 72 | </trim> |
| 111 | where alert_log__id = #{alertLogId} | 73 | where alert_log__id = #{alertLogId} |
| 112 | </update> | 74 | </update> |
| @@ -8,6 +8,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -8,6 +8,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 8 | <result property="alertId" column="alert_id" /> | 8 | <result property="alertId" column="alert_id" /> |
| 9 | <result property="alertName" column="alert_name" /> | 9 | <result property="alertName" column="alert_name" /> |
| 10 | <result property="alertLevel" column="alert_level" /> | 10 | <result property="alertLevel" column="alert_level" /> |
| 11 | + <result property="alertType" column="alert_type" /> | ||
| 11 | <result property="status" column="status" /> | 12 | <result property="status" column="status" /> |
| 12 | <result property="productId" column="product_id" /> | 13 | <result property="productId" column="product_id" /> |
| 13 | <result property="productName" column="product_name" /> | 14 | <result property="productName" column="product_name" /> |
| @@ -21,7 +22,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -21,7 +22,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 21 | </resultMap> | 22 | </resultMap> |
| 22 | 23 | ||
| 23 | <sql id="selectAlertVo"> | 24 | <sql id="selectAlertVo"> |
| 24 | - select alert_id, alert_name, alert_level,status, product_id, product_name, triggers, actions, create_by, create_time, update_by, update_time, remark from iot_alert | 25 | + select alert_id, alert_name, alert_type,alert_level,status, product_id, product_name, triggers, actions, create_by, create_time, update_by, update_time, remark from iot_alert |
| 25 | </sql> | 26 | </sql> |
| 26 | 27 | ||
| 27 | <select id="selectAlertList" parameterType="com.zhonglai.luhui.device.domain.IotAlert" resultMap="AlertResult"> | 28 | <select id="selectAlertList" parameterType="com.zhonglai.luhui.device.domain.IotAlert" resultMap="AlertResult"> |
| @@ -29,6 +30,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -29,6 +30,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 29 | <where> | 30 | <where> |
| 30 | <if test="alertName != null and alertName != ''"> and alert_name like concat('%', #{alertName}, '%')</if> | 31 | <if test="alertName != null and alertName != ''"> and alert_name like concat('%', #{alertName}, '%')</if> |
| 31 | <if test="alertLevel != null "> and alert_level = #{alertLevel}</if> | 32 | <if test="alertLevel != null "> and alert_level = #{alertLevel}</if> |
| 33 | + <if test="alertType != null "> and alert_type = #{alertType}</if> | ||
| 32 | <if test="status != null "> and status = #{status}</if> | 34 | <if test="status != null "> and status = #{status}</if> |
| 33 | <if test="productId != null "> and product_id = #{productId}</if> | 35 | <if test="productId != null "> and product_id = #{productId}</if> |
| 34 | <if test="productName != null and productName != ''"> and product_name like concat('%', #{productName}, '%')</if> | 36 | <if test="productName != null and productName != ''"> and product_name like concat('%', #{productName}, '%')</if> |
| @@ -45,6 +47,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -45,6 +47,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 45 | <trim prefix="(" suffix=")" suffixOverrides=","> | 47 | <trim prefix="(" suffix=")" suffixOverrides=","> |
| 46 | <if test="alertName != null and alertName != ''">alert_name,</if> | 48 | <if test="alertName != null and alertName != ''">alert_name,</if> |
| 47 | <if test="alertLevel != null">alert_level,</if> | 49 | <if test="alertLevel != null">alert_level,</if> |
| 50 | + <if test="alertType != null">alert_type,</if> | ||
| 48 | <if test="status != null">status,</if> | 51 | <if test="status != null">status,</if> |
| 49 | <if test="productId != null">product_id,</if> | 52 | <if test="productId != null">product_id,</if> |
| 50 | <if test="productName != null and productName != ''">product_name,</if> | 53 | <if test="productName != null and productName != ''">product_name,</if> |
| @@ -59,6 +62,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -59,6 +62,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 59 | <trim prefix="values (" suffix=")" suffixOverrides=","> | 62 | <trim prefix="values (" suffix=")" suffixOverrides=","> |
| 60 | <if test="alertName != null and alertName != ''">#{alertName},</if> | 63 | <if test="alertName != null and alertName != ''">#{alertName},</if> |
| 61 | <if test="alertLevel != null">#{alertLevel},</if> | 64 | <if test="alertLevel != null">#{alertLevel},</if> |
| 65 | + <if test="alertType != null">#{alertType},</if> | ||
| 62 | <if test="status != null">#{status},</if> | 66 | <if test="status != null">#{status},</if> |
| 63 | <if test="productId != null">#{productId},</if> | 67 | <if test="productId != null">#{productId},</if> |
| 64 | <if test="productName != null and productName != ''">#{productName},</if> | 68 | <if test="productName != null and productName != ''">#{productName},</if> |
| @@ -77,6 +81,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | @@ -77,6 +81,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
| 77 | <trim prefix="SET" suffixOverrides=","> | 81 | <trim prefix="SET" suffixOverrides=","> |
| 78 | <if test="alertName != null and alertName != ''">alert_name = #{alertName},</if> | 82 | <if test="alertName != null and alertName != ''">alert_name = #{alertName},</if> |
| 79 | <if test="alertLevel != null">alert_level = #{alertLevel},</if> | 83 | <if test="alertLevel != null">alert_level = #{alertLevel},</if> |
| 84 | + <if test="alertType != null">alert_type = #{alertType},</if> | ||
| 80 | <if test="status != null">status = #{status},</if> | 85 | <if test="status != null">status = #{status},</if> |
| 81 | <if test="productId != null">product_id = #{productId},</if> | 86 | <if test="productId != null">product_id = #{productId},</if> |
| 82 | <if test="productName != null and productName != ''">product_name = #{productName},</if> | 87 | <if test="productName != null and productName != ''">product_name = #{productName},</if> |
| @@ -8,15 +8,21 @@ import com.ruoyi.common.core.domain.MessageCode; | @@ -8,15 +8,21 @@ import com.ruoyi.common.core.domain.MessageCode; | ||
| 8 | import org.apache.rocketmq.client.exception.MQBrokerException; | 8 | import org.apache.rocketmq.client.exception.MQBrokerException; |
| 9 | import org.apache.rocketmq.client.exception.MQClientException; | 9 | import org.apache.rocketmq.client.exception.MQClientException; |
| 10 | import org.apache.rocketmq.client.exception.RequestTimeoutException; | 10 | import org.apache.rocketmq.client.exception.RequestTimeoutException; |
| 11 | +import org.apache.rocketmq.client.producer.SendCallback; | ||
| 12 | +import org.apache.rocketmq.client.producer.SendResult; | ||
| 11 | import org.apache.rocketmq.remoting.exception.RemotingException; | 13 | import org.apache.rocketmq.remoting.exception.RemotingException; |
| 12 | import org.apache.rocketmq.spring.core.RocketMQTemplate; | 14 | import org.apache.rocketmq.spring.core.RocketMQTemplate; |
| 15 | +import org.slf4j.Logger; | ||
| 16 | +import org.slf4j.LoggerFactory; | ||
| 13 | import org.springframework.beans.factory.annotation.Autowired; | 17 | import org.springframework.beans.factory.annotation.Autowired; |
| 14 | import org.springframework.beans.factory.annotation.Value; | 18 | import org.springframework.beans.factory.annotation.Value; |
| 19 | +import org.springframework.messaging.support.MessageBuilder; | ||
| 15 | import org.springframework.stereotype.Service; | 20 | import org.springframework.stereotype.Service; |
| 16 | 21 | ||
| 17 | 22 | ||
| 18 | @Service | 23 | @Service |
| 19 | public class RocketMqService { | 24 | public class RocketMqService { |
| 25 | + private final Logger log = LoggerFactory.getLogger(this.getClass()); | ||
| 20 | @Value("${rocketmq.producer.send-topic}") | 26 | @Value("${rocketmq.producer.send-topic}") |
| 21 | private String sendTopic; //客户端操作时间 | 27 | private String sendTopic; //客户端操作时间 |
| 22 | @Value("${rocketmq.producer.send-tags}") | 28 | @Value("${rocketmq.producer.send-tags}") |
| @@ -56,4 +62,8 @@ public class RocketMqService { | @@ -56,4 +62,8 @@ public class RocketMqService { | ||
| 56 | return new Message(MessageCode.DEFAULT_FAIL_CODE); | 62 | return new Message(MessageCode.DEFAULT_FAIL_CODE); |
| 57 | } | 63 | } |
| 58 | 64 | ||
| 65 | + public void syncSend(String topic, Object message) | ||
| 66 | + { | ||
| 67 | + rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload(message).build(), 3000, 1); | ||
| 68 | + } | ||
| 59 | } | 69 | } |
| @@ -14,11 +14,11 @@ import org.springframework.context.annotation.ComponentScan; | @@ -14,11 +14,11 @@ import org.springframework.context.annotation.ComponentScan; | ||
| 14 | "com.zhonglai.luhui.config", | 14 | "com.zhonglai.luhui.config", |
| 15 | "com.zhonglai.luhui.datasource", | 15 | "com.zhonglai.luhui.datasource", |
| 16 | "com.zhonglai.luhui.dao", | 16 | "com.zhonglai.luhui.dao", |
| 17 | + "com.zhonglai.luhui.security", | ||
| 17 | "com.zhonglai.luhui.sys", | 18 | "com.zhonglai.luhui.sys", |
| 18 | "com.zhonglai.luhui.device", | 19 | "com.zhonglai.luhui.device", |
| 19 | "com.zhonglai.luhui.redis.configure", | 20 | "com.zhonglai.luhui.redis.configure", |
| 20 | "com.zhonglai.luhui.redis.service", | 21 | "com.zhonglai.luhui.redis.service", |
| 21 | - "com.zhonglai.luhui.security", | ||
| 22 | "com.zhonglai.luhui.rocketmq", | 22 | "com.zhonglai.luhui.rocketmq", |
| 23 | "com.zhonglai.luhui.firewall", | 23 | "com.zhonglai.luhui.firewall", |
| 24 | "com.zhonglai.luhui.admin", | 24 | "com.zhonglai.luhui.admin", |
| @@ -127,6 +127,47 @@ public class ControlDeviceConreoller extends BaseController { | @@ -127,6 +127,47 @@ public class ControlDeviceConreoller extends BaseController { | ||
| 127 | return control(deviceCommand); | 127 | return control(deviceCommand); |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | + @ApiOperation("添加订阅") | ||
| 131 | + @ApiImplicitParams({ | ||
| 132 | + @ApiImplicitParam(value = "监听服务器的ip",name = "ip"), | ||
| 133 | + @ApiImplicitParam(value = "产品集合",name = "product_ids"), | ||
| 134 | + }) | ||
| 135 | + @PreAuthorize("@ss.hasPermi('iot:controlDevice:addSubscribe')") | ||
| 136 | + @Log(title = "添加订阅", businessType = BusinessType.CAHE) | ||
| 137 | + @PostMapping("/addSubscribe") | ||
| 138 | + public AjaxResult addSubscribe(String product_ids,String ip) { | ||
| 139 | + DeviceCommand deviceCommand = new DeviceCommand(); | ||
| 140 | + deviceCommand.setCommandType(CommandType.addSubscribe); | ||
| 141 | + JsonObject jsonObject = new JsonObject(); | ||
| 142 | + jsonObject.addProperty("product_ids",product_ids); | ||
| 143 | + jsonObject.addProperty("ip",ip); | ||
| 144 | + deviceCommand.setData(jsonObject); | ||
| 145 | + return control(deviceCommand); | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + @ApiOperation(value = "通知",notes = "body参数描述:\r\n" + | ||
| 149 | + "{\n" + | ||
| 150 | + " \"1\":{\n" + | ||
| 151 | + " \"id1\":\"value1\",\n" + | ||
| 152 | + " \"id2\":\"value2\",\n" + | ||
| 153 | + " \"id3\":\"value3\"\n" + | ||
| 154 | + " }\n" + | ||
| 155 | + "}") | ||
| 156 | + @ApiImplicitParams({ | ||
| 157 | + @ApiImplicitParam(value = "网关id",name = "deviceId"), | ||
| 158 | + }) | ||
| 159 | + @PreAuthorize("@ss.hasPermi('iot:controlDevice:notice')") | ||
| 160 | + @Log(title = "通知", businessType = BusinessType.CAHE) | ||
| 161 | + @PostMapping("/notice/{deviceId}") | ||
| 162 | + public AjaxResult notice(@PathVariable String deviceId, HttpServletRequest request) throws IOException { | ||
| 163 | + byte[] bodyBytes = StreamUtils.copyToByteArray(request.getInputStream()); | ||
| 164 | + String body = new String(bodyBytes, request.getCharacterEncoding()); | ||
| 165 | + DeviceCommand deviceCommand = new DeviceCommand(); | ||
| 166 | + deviceCommand.setDeviceId(deviceId); | ||
| 167 | + deviceCommand.setCommandType(CommandType.notice); | ||
| 168 | + deviceCommand.setData(GsonConstructor.get().fromJson(body,JsonObject.class)); | ||
| 169 | + return control(deviceCommand); | ||
| 170 | + } | ||
| 130 | 171 | ||
| 131 | private AjaxResult control( DeviceCommand deviceCommand) | 172 | private AjaxResult control( DeviceCommand deviceCommand) |
| 132 | { | 173 | { |
| @@ -7,8 +7,7 @@ import com.ruoyi.common.core.domain.AjaxResult; | @@ -7,8 +7,7 @@ import com.ruoyi.common.core.domain.AjaxResult; | ||
| 7 | import com.ruoyi.common.core.page.TableDataInfo; | 7 | import com.ruoyi.common.core.page.TableDataInfo; |
| 8 | import com.ruoyi.common.enums.BusinessType; | 8 | import com.ruoyi.common.enums.BusinessType; |
| 9 | import com.ruoyi.common.utils.StringUtils; | 9 | import com.ruoyi.common.utils.StringUtils; |
| 10 | -import com.zhonglai.luhui.admin.dto.AlarmActionConfig; | ||
| 11 | -import com.zhonglai.luhui.admin.dto.AlarmTriggersConfig; | 10 | +import com.zhonglai.luhui.admin.dto.*; |
| 12 | import com.zhonglai.luhui.action.BaseController; | 11 | import com.zhonglai.luhui.action.BaseController; |
| 13 | import com.zhonglai.luhui.device.domain.IotAlert; | 12 | import com.zhonglai.luhui.device.domain.IotAlert; |
| 14 | import com.zhonglai.luhui.device.service.IIotAlertService; | 13 | import com.zhonglai.luhui.device.service.IIotAlertService; |
| @@ -20,7 +19,9 @@ import org.springframework.security.access.prepost.PreAuthorize; | @@ -20,7 +19,9 @@ import org.springframework.security.access.prepost.PreAuthorize; | ||
| 20 | import org.springframework.web.bind.annotation.*; | 19 | import org.springframework.web.bind.annotation.*; |
| 21 | 20 | ||
| 22 | import javax.servlet.http.HttpServletResponse; | 21 | import javax.servlet.http.HttpServletResponse; |
| 22 | +import java.util.HashMap; | ||
| 23 | import java.util.List; | 23 | import java.util.List; |
| 24 | +import java.util.Map; | ||
| 24 | import java.util.Optional; | 25 | import java.util.Optional; |
| 25 | 26 | ||
| 26 | /** | 27 | /** |
| @@ -88,10 +89,10 @@ public class IotAlertController extends BaseController | @@ -88,10 +89,10 @@ public class IotAlertController extends BaseController | ||
| 88 | { | 89 | { |
| 89 | return AjaxResult.error("触发器参数必填"); | 90 | return AjaxResult.error("触发器参数必填"); |
| 90 | } | 91 | } |
| 91 | - if (checkActions(alert)) | ||
| 92 | - { | ||
| 93 | - return AjaxResult.error("执行动作参数必填"); | ||
| 94 | - } | 92 | +// if (checkActions(alert)) |
| 93 | +// { | ||
| 94 | +// return AjaxResult.error("执行动作参数必填"); | ||
| 95 | +// } | ||
| 95 | return toAjax(alertService.insertAlert(alert)); | 96 | return toAjax(alertService.insertAlert(alert)); |
| 96 | } | 97 | } |
| 97 | 98 | ||
| @@ -108,10 +109,10 @@ public class IotAlertController extends BaseController | @@ -108,10 +109,10 @@ public class IotAlertController extends BaseController | ||
| 108 | { | 109 | { |
| 109 | return AjaxResult.error("触发器参数必填"); | 110 | return AjaxResult.error("触发器参数必填"); |
| 110 | } | 111 | } |
| 111 | - if (checkActions(alert)) | ||
| 112 | - { | ||
| 113 | - return AjaxResult.error("执行动作参数必填"); | ||
| 114 | - } | 112 | +// if (checkActions(alert)) |
| 113 | +// { | ||
| 114 | +// return AjaxResult.error("执行动作参数必填"); | ||
| 115 | +// } | ||
| 115 | return toAjax(alertService.updateAlert(alert)); | 116 | return toAjax(alertService.updateAlert(alert)); |
| 116 | } | 117 | } |
| 117 | 118 | ||
| @@ -136,12 +137,35 @@ public class IotAlertController extends BaseController | @@ -136,12 +137,35 @@ public class IotAlertController extends BaseController | ||
| 136 | String triggers = Optional.ofNullable(alert).orElse(new IotAlert()).getTriggers(); | 137 | String triggers = Optional.ofNullable(alert).orElse(new IotAlert()).getTriggers(); |
| 137 | if(StringUtils.isNotEmpty(triggers)) | 138 | if(StringUtils.isNotEmpty(triggers)) |
| 138 | { | 139 | { |
| 139 | - AlarmTriggersConfig alarmTriggersConfig = JSONObject.parseObject(triggers, AlarmTriggersConfig.class); | ||
| 140 | - return BeanUtil.hasNullField(alarmTriggersConfig,"cron"); | 140 | + switch (alert.getAlertType()) |
| 141 | + { | ||
| 142 | + case 1: | ||
| 143 | + AttributeTriggers attributeTriggers = JSONObject.parseObject(triggers, AttributeTriggers.class); | ||
| 144 | + return BeanUtil.hasNullField(attributeTriggers); | ||
| 145 | + case 2: | ||
| 146 | + TriggerTriggers triggerTriggers = JSONObject.parseObject(triggers, TriggerTriggers.class); | ||
| 147 | + return BeanUtil.hasNullField(triggerTriggers); | ||
| 148 | + case 3: | ||
| 149 | + TimerTriggers timerTriggers = JSONObject.parseObject(triggers, TimerTriggers.class); | ||
| 150 | + return BeanUtil.hasNullField(timerTriggers,"terminalids"); | ||
| 151 | + } | ||
| 152 | + | ||
| 141 | } | 153 | } |
| 142 | return false; | 154 | return false; |
| 143 | } | 155 | } |
| 144 | 156 | ||
| 157 | + public static void main(String[] args) { | ||
| 158 | + String triggers = "{\"model_name\":\"alarm\",\"valueMapName\":{{\"1\":\"传感器检测失败故障\"},{\"3\",\"超低氧告警\"}}}"; | ||
| 159 | + AttributeTriggers attributeTriggers = new AttributeTriggers(); | ||
| 160 | + attributeTriggers.setModel_name("alarm"); | ||
| 161 | + Map<Object,String> valueMapName = new HashMap<>(); | ||
| 162 | + valueMapName.put("1","传感器检测失败故障"); | ||
| 163 | + valueMapName.put("3","超低氧告警"); | ||
| 164 | + attributeTriggers.setValueMapName(valueMapName); | ||
| 165 | + System.out.println(triggers); | ||
| 166 | + System.out.println(JSONObject.toJSON(attributeTriggers)); | ||
| 167 | + } | ||
| 168 | + | ||
| 145 | /** | 169 | /** |
| 146 | * 检验执行动作 | 170 | * 检验执行动作 |
| 147 | * @return | 171 | * @return |
| 1 | +package com.zhonglai.luhui.admin.dto; | ||
| 2 | + | ||
| 3 | +import java.util.Map; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * 属性警触发配置 | ||
| 7 | + */ | ||
| 8 | +public class AttributeTriggers { | ||
| 9 | + private String model_name; | ||
| 10 | + private Map<Object,String> valueMapName; | ||
| 11 | + | ||
| 12 | + public String getModel_name() { | ||
| 13 | + return model_name; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + public void setModel_name(String model_name) { | ||
| 17 | + this.model_name = model_name; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + public Map<Object, String> getValueMapName() { | ||
| 21 | + return valueMapName; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public void setValueMapName(Map<Object, String> valueMapName) { | ||
| 25 | + this.valueMapName = valueMapName; | ||
| 26 | + } | ||
| 27 | +} |
| 1 | +package com.zhonglai.luhui.admin.dto; | ||
| 2 | + | ||
| 3 | +import java.util.List; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * 定时告警触发配置 | ||
| 7 | + */ | ||
| 8 | +public class TimerTriggers { | ||
| 9 | + /** | ||
| 10 | + * cron表达式 | ||
| 11 | + */ | ||
| 12 | + private String cron; | ||
| 13 | + | ||
| 14 | + /** | ||
| 15 | + * 终端id集合,用英文逗号分割(可以为空) | ||
| 16 | + */ | ||
| 17 | + private String terminalids; | ||
| 18 | + | ||
| 19 | + /** | ||
| 20 | + * 触发条件 | ||
| 21 | + */ | ||
| 22 | + private List<TriggerTriggers> triggersList; | ||
| 23 | + | ||
| 24 | + public String getCron() { | ||
| 25 | + return cron; | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + public void setCron(String cron) { | ||
| 29 | + this.cron = cron; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public List<TriggerTriggers> getTriggersList() { | ||
| 33 | + return triggersList; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public void setTriggersList(List<TriggerTriggers> triggersList) { | ||
| 37 | + this.triggersList = triggersList; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + public String getTerminalids() { | ||
| 42 | + return terminalids; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + public void setTerminalids(String terminalids) { | ||
| 46 | + this.terminalids = terminalids; | ||
| 47 | + } | ||
| 48 | +} |
| 1 | +package com.zhonglai.luhui.admin.dto; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * 触发告警触发配置 | ||
| 5 | + */ | ||
| 6 | +public class TriggerTriggers { | ||
| 7 | + private String model_name; //要过滤的模型 | ||
| 8 | + private String equation; //等式 | ||
| 9 | + | ||
| 10 | + private Integer type; // 比较类型(1直接比较值,2和另一个读数模型比较,3和另一个参数模型比较) | ||
| 11 | + | ||
| 12 | + private Object compare_object; //被比较的对象 | ||
| 13 | + | ||
| 14 | + private boolean isor; //与或条件 | ||
| 15 | + | ||
| 16 | + public boolean isIsor() { | ||
| 17 | + return isor; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + public void setIsor(boolean isor) { | ||
| 21 | + this.isor = isor; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public String getModel_name() { | ||
| 25 | + return model_name; | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + public void setModel_name(String model_name) { | ||
| 29 | + this.model_name = model_name; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public String getEquation() { | ||
| 33 | + return equation; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public void setEquation(String equation) { | ||
| 37 | + this.equation = equation; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public Integer getType() { | ||
| 41 | + return type; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public void setType(Integer type) { | ||
| 45 | + this.type = type; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public Object getCompare_object() { | ||
| 49 | + return compare_object; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public void setCompare_object(Object compare_object) { | ||
| 53 | + this.compare_object = compare_object; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | +} |
lh-modules/lh-alarm-timer/pom.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | + <modelVersion>4.0.0</modelVersion> | ||
| 6 | + <parent> | ||
| 7 | + <groupId>com.zhonglai.luhui</groupId> | ||
| 8 | + <artifactId>lh-modules</artifactId> | ||
| 9 | + <version>1.0-SNAPSHOT</version> | ||
| 10 | + </parent> | ||
| 11 | + | ||
| 12 | + <artifactId>lh-alarm-timer</artifactId> | ||
| 13 | + | ||
| 14 | + <properties> | ||
| 15 | + <maven.compiler.source>8</maven.compiler.source> | ||
| 16 | + <maven.compiler.target>8</maven.compiler.target> | ||
| 17 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 18 | + </properties> | ||
| 19 | + | ||
| 20 | +</project> |
| @@ -12,56 +12,30 @@ | @@ -12,56 +12,30 @@ | ||
| 12 | <artifactId>lh-alarm</artifactId> | 12 | <artifactId>lh-alarm</artifactId> |
| 13 | 13 | ||
| 14 | <dependencies> | 14 | <dependencies> |
| 15 | - <!-- spring-boot-devtools --> | ||
| 16 | <dependency> | 15 | <dependency> |
| 17 | - <groupId>org.springframework.boot</groupId> | ||
| 18 | - <artifactId>spring-boot-devtools</artifactId> | ||
| 19 | - <optional>true</optional> <!-- 表示依赖不会传递 --> | 16 | + <groupId>com.zendesk</groupId> |
| 17 | + <artifactId>mysql-binlog-connector-java</artifactId> | ||
| 20 | </dependency> | 18 | </dependency> |
| 21 | - | ||
| 22 | - <!-- Mysql驱动包 --> | ||
| 23 | <dependency> | 19 | <dependency> |
| 24 | - <groupId>mysql</groupId> | ||
| 25 | - <artifactId>mysql-connector-java</artifactId> | 20 | + <groupId>com.alibaba</groupId> |
| 21 | + <artifactId>fastjson</artifactId> | ||
| 26 | </dependency> | 22 | </dependency> |
| 27 | 23 | ||
| 28 | - <!-- 核心模块--> | ||
| 29 | <dependency> | 24 | <dependency> |
| 30 | - <groupId>com.zhonglai.luhui</groupId> | ||
| 31 | - <artifactId>ruoyi-framework</artifactId> | 25 | + <groupId>com.alibaba.otter</groupId> |
| 26 | + <artifactId>canal.client</artifactId> | ||
| 32 | </dependency> | 27 | </dependency> |
| 33 | - <!-- 文档 --> | ||
| 34 | - <dependency > | ||
| 35 | - <groupId>io.springfox</groupId> | ||
| 36 | - <artifactId>springfox-swagger2</artifactId> | ||
| 37 | - <version>${swagger.version}</version> | ||
| 38 | - <exclusions> | ||
| 39 | - <exclusion> | ||
| 40 | - <groupId>io.swagger</groupId> | ||
| 41 | - <artifactId>swagger-models</artifactId> | ||
| 42 | - </exclusion> | ||
| 43 | - <exclusion> | ||
| 44 | - <groupId>com.google.guava</groupId> | ||
| 45 | - <artifactId>guava</artifactId> | ||
| 46 | - </exclusion> | ||
| 47 | - </exclusions> | ||
| 48 | - </dependency> | ||
| 49 | - <!--https://mvnrepository.com/artifact/io.swagger/swagger-models--> | ||
| 50 | <dependency> | 28 | <dependency> |
| 51 | - <groupId>io.swagger</groupId> | ||
| 52 | - <artifactId>swagger-models</artifactId> | ||
| 53 | - <version>${swagger-models.version}</version> | 29 | + <groupId>com.alibaba.otter</groupId> |
| 30 | + <artifactId>canal.protocol</artifactId> | ||
| 54 | </dependency> | 31 | </dependency> |
| 55 | <dependency> | 32 | <dependency> |
| 56 | - <groupId>io.springfox</groupId> | ||
| 57 | - <artifactId>springfox-swagger-ui</artifactId> | ||
| 58 | - <version>${swagger.version}</version> | 33 | + <groupId>com.zhonglai.luhui</groupId> |
| 34 | + <artifactId>lh-service-dao</artifactId> | ||
| 59 | </dependency> | 35 | </dependency> |
| 60 | - <!--<!– https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui –>--> | ||
| 61 | <dependency> | 36 | <dependency> |
| 62 | - <groupId>com.github.xiaoymin</groupId> | ||
| 63 | - <artifactId>swagger-bootstrap-ui</artifactId> | ||
| 64 | - <version>${swagger-ui.version}</version> | 37 | + <groupId>org.quartz-scheduler</groupId> |
| 38 | + <artifactId>quartz</artifactId> | ||
| 65 | </dependency> | 39 | </dependency> |
| 66 | </dependencies> | 40 | </dependencies> |
| 67 | 41 |
| 1 | -package com.zhonglai.luhui.alarm; | ||
| 2 | - | ||
| 3 | -import org.springframework.boot.SpringApplication; | ||
| 4 | -import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| 5 | -import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
| 6 | -import org.springframework.context.annotation.ComponentScan; | ||
| 7 | -import org.springframework.context.annotation.FilterType; | ||
| 8 | - | ||
| 9 | -@ComponentScan(basePackages = { | ||
| 10 | - "com.ruoyi.common", | ||
| 11 | - "com.ruoyi.framework", | ||
| 12 | -} | ||
| 13 | -// excludeFilters = {@ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE,classes = {LogAspect.class, | ||
| 14 | -// RateLimiterAspect.class, LoginService.class, TokenService.class, FilterConfig.class, JwtAuthenticationTokenFilter.class, | ||
| 15 | -// SysConfigServiceImpl.class, SysDictTypeServiceImpl.class, SysUserServiceImpl.class,SecurityConfig.class, LogoutSuccessHandlerImpl.class | ||
| 16 | -// })} | ||
| 17 | - | ||
| 18 | -) | ||
| 19 | -@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) | ||
| 20 | -public class AlarmApplication { | ||
| 21 | - public static void main(String[] args) { | ||
| 22 | - SpringApplication.run(AlarmApplication.class,args); | ||
| 23 | - System.out.println("启动成功"); | ||
| 24 | - } | ||
| 25 | -} |
| 1 | +package com.zhonglai.luhui.alarm; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import com.zhonglai.luhui.alarm.config.CachAlarmConfig; | ||
| 5 | +import com.zhonglai.luhui.alarm.handle.CleanupTask; | ||
| 6 | +import com.zhonglai.luhui.alarm.service.TimerAlarmService; | ||
| 7 | +import com.zhonglai.luhui.alarm.service.TriggerAlarmService; | ||
| 8 | + | ||
| 9 | +public class LhAlarmMain { | ||
| 10 | + public static void main(String[] args) { | ||
| 11 | + // 注册Shutdown Hook | ||
| 12 | + Runtime.getRuntime().addShutdownHook(new CleanupTask()); | ||
| 13 | + | ||
| 14 | + CachAlarmConfig.loadConfig(); | ||
| 15 | + //启动触发告警服务 | ||
| 16 | + TriggerAlarmService.start(); | ||
| 17 | + | ||
| 18 | + //启动定时任务告警 | ||
| 19 | + TimerAlarmService.start(); | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + } | ||
| 23 | +} |
| 1 | +package com.zhonglai.luhui.alarm.clas; | ||
| 2 | + | ||
| 3 | +import com.zhonglai.luhui.alarm.config.CachAlarmConfig; | ||
| 4 | +import com.zhonglai.luhui.alarm.dto.IotAlert; | ||
| 5 | +import com.zhonglai.luhui.alarm.service.TimerAlarmService; | ||
| 6 | + | ||
| 7 | +import java.io.Serializable; | ||
| 8 | + | ||
| 9 | +public class IotAlertAlarm extends UpAlarmFactory<IotAlert> implements Serializable { | ||
| 10 | + public IotAlertAlarm(IotAlert beforeupAlarmDb, IotAlert afterupAlarmDb) { | ||
| 11 | + super(beforeupAlarmDb, afterupAlarmDb); | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + @Override | ||
| 15 | + void deleteGenerateAlarm() { | ||
| 16 | + CachAlarmConfig.delete(beforeupAlarmDb.getAlertId()); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + @Override | ||
| 20 | + void insertGenerateAlarm() { | ||
| 21 | + switch (afterupAlarmDb.getAlertType()) | ||
| 22 | + { | ||
| 23 | + case 1: | ||
| 24 | + CachAlarmConfig.addAttributeAlarmRoute(afterupAlarmDb); | ||
| 25 | + break; | ||
| 26 | + case 2: | ||
| 27 | + CachAlarmConfig.addTriggerAlarmRoute(afterupAlarmDb); | ||
| 28 | + break; | ||
| 29 | + case 3: | ||
| 30 | + TimerAlarmService.addTimerAlarm(afterupAlarmDb); | ||
| 31 | + break; | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + @Override | ||
| 36 | + void updateGenerateAlarm() { | ||
| 37 | + switch (afterupAlarmDb.getAlertType()) | ||
| 38 | + { | ||
| 39 | + case 1: | ||
| 40 | + CachAlarmConfig.addAttributeAlarmRoute(afterupAlarmDb); | ||
| 41 | + break; | ||
| 42 | + case 2: | ||
| 43 | + CachAlarmConfig.addTriggerAlarmRoute(afterupAlarmDb); | ||
| 44 | + break; | ||
| 45 | + case 3: | ||
| 46 | + if (null != afterupAlarmDb.getStatus() && 2==afterupAlarmDb.getStatus()) | ||
| 47 | + { | ||
| 48 | + deleteGenerateAlarm(); | ||
| 49 | + }else { | ||
| 50 | + TimerAlarmService.addTimerAlarm(afterupAlarmDb); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + break; | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + @Override | ||
| 58 | + Object getNowValue(Object model_name, Integer type) { | ||
| 59 | + return null; | ||
| 60 | + } | ||
| 61 | +} |
| 1 | +package com.zhonglai.luhui.alarm.clas; | ||
| 2 | + | ||
| 3 | +import com.alibaba.fastjson.JSONObject; | ||
| 4 | +import com.zhonglai.luhui.alarm.config.CachAlarmConfig; | ||
| 5 | +import com.zhonglai.luhui.alarm.dto.*; | ||
| 6 | +import com.zhonglai.luhui.service.dao.util.StringUtils; | ||
| 7 | +import org.slf4j.Logger; | ||
| 8 | +import org.slf4j.LoggerFactory; | ||
| 9 | + | ||
| 10 | +import java.io.Serializable; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * 主机/网关对象 iot_device | ||
| 14 | + * | ||
| 15 | + * @author 钟来 | ||
| 16 | + * @date 2022-08-26 | ||
| 17 | + */ | ||
| 18 | +public class IotDeviceAlarm extends UpAlarmFactory<IotDevice> implements Serializable | ||
| 19 | +{ | ||
| 20 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
| 21 | + | ||
| 22 | + public IotDeviceAlarm(IotDevice beforeupAlarmDb, IotDevice afterupAlarmDb) { | ||
| 23 | + super(beforeupAlarmDb,afterupAlarmDb); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + @Override | ||
| 27 | + public void deleteGenerateAlarm() { | ||
| 28 | + logger.info("主机{}删除不需要告警",beforeupAlarmDb.getClient_id()); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + @Override | ||
| 32 | + public void insertGenerateAlarm() { | ||
| 33 | + logger.info("主机{}添加不需要告警",afterupAlarmDb.getClient_id()); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + @Override | ||
| 37 | + public void updateGenerateAlarm() { | ||
| 38 | + //如果是产品变更先更新产品 | ||
| 39 | + if (null != afterupAlarmDb.getProduct_id() && (beforeupAlarmDb.getProduct_id()-afterupAlarmDb.getProduct_id()) !=0) | ||
| 40 | + { | ||
| 41 | + CachAlarmConfig.putDeviceProduct(afterupAlarmDb.getClient_id(), afterupAlarmDb.getProduct_id()); | ||
| 42 | + } | ||
| 43 | + //通过变更的数据获得对应的告警规则 | ||
| 44 | + valueUp(beforeupAlarmDb.getClient_id(),CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getClient_id()),beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value()); | ||
| 45 | + | ||
| 46 | + } | ||
| 47 | + Object getNowValue(Object model_name,Integer type) { | ||
| 48 | + if(null != afterupAlarmDb ) | ||
| 49 | + { | ||
| 50 | + if(StringUtils.isNotEmpty(afterupAlarmDb.getThings_model_value())) | ||
| 51 | + { | ||
| 52 | + return getValueFromJSON(model_name+"",JSONObject.parseObject(afterupAlarmDb.getThings_model_value())); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + } | ||
| 56 | + return null; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | +} |
| 1 | +package com.zhonglai.luhui.alarm.clas; | ||
| 2 | + | ||
| 3 | +import com.alibaba.fastjson.JSONObject; | ||
| 4 | +import com.zhonglai.luhui.alarm.config.CachAlarmConfig; | ||
| 5 | +import com.zhonglai.luhui.alarm.dto.IotDevice; | ||
| 6 | +import com.zhonglai.luhui.alarm.dto.IotTerminal; | ||
| 7 | +import com.zhonglai.luhui.service.dao.util.StringUtils; | ||
| 8 | +import org.slf4j.Logger; | ||
| 9 | +import org.slf4j.LoggerFactory; | ||
| 10 | + | ||
| 11 | +import java.io.Serializable; | ||
| 12 | + | ||
| 13 | + | ||
| 14 | +/** | ||
| 15 | + * 终端对象 iot_terminal | ||
| 16 | + * | ||
| 17 | + * @author 钟来 | ||
| 18 | + * @date 2022-08-26 | ||
| 19 | + */ | ||
| 20 | +public class IotTerminalAlarm extends UpAlarmFactory<IotTerminal> implements Serializable | ||
| 21 | +{ | ||
| 22 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
| 23 | + | ||
| 24 | + public IotTerminalAlarm(IotTerminal beforeupAlarmDb, IotTerminal afterupAlarmDb) { | ||
| 25 | + super(beforeupAlarmDb,afterupAlarmDb); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + @Override | ||
| 29 | + public void deleteGenerateAlarm() { | ||
| 30 | + logger.info("终端{}删除不需要告警",beforeupAlarmDb.getId()); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + @Override | ||
| 34 | + public void insertGenerateAlarm() { | ||
| 35 | + logger.info("终端{}添加不需要告警",afterupAlarmDb.getId()); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + @Override | ||
| 39 | + public void updateGenerateAlarm() { | ||
| 40 | + //通过变更的数据获得对应的告警规则 | ||
| 41 | + valueUp(beforeupAlarmDb.getId(), CachAlarmConfig.getDeviceProduct(beforeupAlarmDb.getId()),beforeupAlarmDb.getThings_model_value(),afterupAlarmDb.getThings_model_value()); | ||
| 42 | + | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + @Override | ||
| 46 | + Object getNowValue(Object model_name,Integer type) { | ||
| 47 | + Object value = null; | ||
| 48 | + if(null != afterupAlarmDb ) | ||
| 49 | + { | ||
| 50 | + switch (type) | ||
| 51 | + { | ||
| 52 | + case 2: | ||
| 53 | + if(StringUtils.isNotEmpty(afterupAlarmDb.getThings_model_value())) | ||
| 54 | + { | ||
| 55 | + value = getValueFromJSON(model_name+"", JSONObject.parseObject(afterupAlarmDb.getThings_model_value())); | ||
| 56 | + } | ||
| 57 | + break; | ||
| 58 | + case 3: | ||
| 59 | + if(StringUtils.isNotEmpty(afterupAlarmDb.getThings_model_config())) | ||
| 60 | + { | ||
| 61 | + value = getValueFromJSON(model_name+"", JSONObject.parseObject(afterupAlarmDb.getThings_model_config())); | ||
| 62 | + } | ||
| 63 | + break; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + } | ||
| 68 | + return value; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + | ||
| 72 | +} |
| 1 | +package com.zhonglai.luhui.alarm.clas; | ||
| 2 | + | ||
| 3 | +import com.alibaba.fastjson.JSON; | ||
| 4 | +import com.alibaba.fastjson.JSONObject; | ||
| 5 | +import com.alibaba.otter.canal.protocol.CanalEntry; | ||
| 6 | +import com.zhonglai.luhui.alarm.config.CachAlarmConfig; | ||
| 7 | +import com.zhonglai.luhui.alarm.dto.*; | ||
| 8 | +import com.zhonglai.luhui.alarm.util.GenericComparator; | ||
| 9 | +import com.zhonglai.luhui.service.dao.util.StringUtils; | ||
| 10 | + | ||
| 11 | +import java.util.ArrayList; | ||
| 12 | +import java.util.List; | ||
| 13 | + | ||
| 14 | +/** | ||
| 15 | + * 告警工厂 | ||
| 16 | + */ | ||
| 17 | +public abstract class UpAlarmFactory<T> { | ||
| 18 | + | ||
| 19 | + protected List<IotAlertLog> list = new ArrayList<>(); | ||
| 20 | + | ||
| 21 | + protected T beforeupAlarmDb; | ||
| 22 | + protected T afterupAlarmDb; | ||
| 23 | + | ||
| 24 | + abstract void deleteGenerateAlarm(); | ||
| 25 | + abstract void insertGenerateAlarm(); | ||
| 26 | + abstract void updateGenerateAlarm(); | ||
| 27 | + | ||
| 28 | + abstract Object getNowValue(Object model_name,Integer type); | ||
| 29 | + public UpAlarmFactory(T beforeupAlarmDb, T afterupAlarmDb) | ||
| 30 | + { | ||
| 31 | + this.beforeupAlarmDb = beforeupAlarmDb; | ||
| 32 | + this.afterupAlarmDb = afterupAlarmDb; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public static UpAlarmFactory instantiate(List<CanalEntry.Column> beforecolumns, List<CanalEntry.Column> aftercolumns, String dbName, String tableName) | ||
| 36 | + { | ||
| 37 | + switch (dbName+"."+tableName) | ||
| 38 | + { | ||
| 39 | + case "mqtt_broker.iot_device": | ||
| 40 | + return new IotDeviceAlarm(IotDevice.instantiate(beforecolumns),IotDevice.instantiate(aftercolumns)); | ||
| 41 | + case "mqtt_broker.iot_terminal": | ||
| 42 | + return new IotTerminalAlarm(IotTerminal.instantiate(beforecolumns),IotTerminal.instantiate(aftercolumns)); | ||
| 43 | + case "mqtt_broker.iot_alert": | ||
| 44 | + return new IotAlertAlarm(IotAlert.instantiate(beforecolumns),IotAlert.instantiate(aftercolumns)); | ||
| 45 | + } | ||
| 46 | + return null; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + public void alarm(CanalEntry.EventType eventType) | ||
| 50 | + { | ||
| 51 | + switch (eventType) | ||
| 52 | + { | ||
| 53 | + case DELETE: | ||
| 54 | + if(null != beforeupAlarmDb) | ||
| 55 | + { | ||
| 56 | + deleteGenerateAlarm(); | ||
| 57 | + } | ||
| 58 | + break; | ||
| 59 | + case INSERT: | ||
| 60 | + if(null != afterupAlarmDb) | ||
| 61 | + { | ||
| 62 | + insertGenerateAlarm(); | ||
| 63 | + } | ||
| 64 | + break; | ||
| 65 | + default: | ||
| 66 | + updateGenerateAlarm(); | ||
| 67 | + break; | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + protected void valueUp(String client_id,Integer product_id,String oldvalue,String newValue) | ||
| 73 | + { | ||
| 74 | + if(StringUtils.isNotEmpty(newValue)) | ||
| 75 | + { | ||
| 76 | + JSONObject oldjson = null==oldvalue?new JSONObject():JSONObject.parseObject(oldvalue); | ||
| 77 | + JSONObject newjson = JSONObject.parseObject(newValue); | ||
| 78 | + if(null == product_id) | ||
| 79 | + { | ||
| 80 | + product_id = CachAlarmConfig.getDeviceProduct(client_id); | ||
| 81 | + } | ||
| 82 | + for (String key:newjson.keySet()) | ||
| 83 | + { | ||
| 84 | + if(!newjson.get(key).equals(oldjson.get(key))) //新老不一致,触发告警 | ||
| 85 | + { | ||
| 86 | + //属性告警 | ||
| 87 | + List<IotAlert> listAttribute = CachAlarmConfig.getAttributeIotAlert(product_id,key); | ||
| 88 | + if (null != listAttribute && listAttribute.size() != 0) | ||
| 89 | + { | ||
| 90 | + alarmAttribute(client_id,listAttribute,oldjson.get(key)); | ||
| 91 | + } | ||
| 92 | + //触发告警 | ||
| 93 | + List<IotAlert> listTrigger = CachAlarmConfig.getTriggerIotAlert(product_id,key); | ||
| 94 | + if (null != listTrigger && listTrigger.size() != 0) | ||
| 95 | + { | ||
| 96 | + alarmTrigger(client_id,listTrigger,oldjson.get(key)); | ||
| 97 | + } | ||
| 98 | + } | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + protected void alarmAttribute(String client_id,List<IotAlert> listAttribute,Object newValue) | ||
| 104 | + { | ||
| 105 | + for (IotAlert iotAlert:listAttribute) | ||
| 106 | + { | ||
| 107 | + AttributeTriggers attributeTriggers = JSONObject.parseObject(iotAlert.getTriggers(),AttributeTriggers.class); | ||
| 108 | + String alrmname = attributeTriggers.getValueMapName().get(newValue); | ||
| 109 | + if(StringUtils.isNotEmpty(alrmname)) | ||
| 110 | + { | ||
| 111 | + IotAlertLog iotAlertLog = new IotAlertLog(iotAlert.getAlertId(),alrmname,iotAlert.getAlertLevel().intValue(),2,client_id,System.currentTimeMillis(),1,iotAlert.getCreateBy()); | ||
| 112 | + list.add(iotAlertLog); | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + protected void alarmTrigger(String client_id,List<IotAlert> listAttribute,Object newValue) | ||
| 118 | + { | ||
| 119 | + | ||
| 120 | + for (IotAlert iotAlert:listAttribute) | ||
| 121 | + { | ||
| 122 | + List<TriggerTriggers> attributeTriggersList = JSON.parseArray(iotAlert.getTriggers(),TriggerTriggers.class); | ||
| 123 | + alarmTriggerTriggers(attributeTriggersList,newValue,iotAlert.getAlertId(),iotAlert.getAlertName(),iotAlert.getAlertLevel().intValue(),client_id,iotAlert.getCreateBy()); | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + public void alarmTriggerTriggers( List<TriggerTriggers> attributeTriggersList,Object newValue,Long alert_id, String alert_name, Integer alert_level, String device_id,String userId) | ||
| 128 | + { | ||
| 129 | + if(null != attributeTriggersList && attributeTriggersList.size() != 0) | ||
| 130 | + { | ||
| 131 | + //所有条件都是与 | ||
| 132 | + boolean jieguo = true; | ||
| 133 | + | ||
| 134 | + for (TriggerTriggers triggerTriggers:attributeTriggersList) | ||
| 135 | + { | ||
| 136 | + | ||
| 137 | + Object value = triggerTriggers.getCompare_object(); | ||
| 138 | + | ||
| 139 | + switch (triggerTriggers.getType()) | ||
| 140 | + { | ||
| 141 | + case 1: | ||
| 142 | + value = triggerTriggers.getCompare_object(); | ||
| 143 | + break; | ||
| 144 | + case 2: | ||
| 145 | + | ||
| 146 | + value = getNowValue(triggerTriggers.getCompare_object(),2); | ||
| 147 | + | ||
| 148 | + break; | ||
| 149 | + case 3: | ||
| 150 | + value = getNowValue(triggerTriggers.getCompare_object(),3); | ||
| 151 | + | ||
| 152 | + break; | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + if (triggerTriggers.isIsor()) | ||
| 156 | + { | ||
| 157 | + jieguo = jieguo || GenericComparator.compare(newValue,value,triggerTriggers.getEquation()); | ||
| 158 | + }else { | ||
| 159 | + jieguo = jieguo && GenericComparator.compare(newValue,value,triggerTriggers.getEquation()); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + if(jieguo) | ||
| 165 | + { | ||
| 166 | + IotAlertLog iotAlertLog = new IotAlertLog(alert_id,alert_name,alert_level,2,device_id,System.currentTimeMillis(),1,userId); | ||
| 167 | + list.add(iotAlertLog); | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + protected Object getValueFromJSON(String key,JSONObject jsonObject) | ||
| 173 | + { | ||
| 174 | + if(null != jsonObject && jsonObject.containsKey(jsonObject)) | ||
| 175 | + { | ||
| 176 | + return jsonObject.getJSONObject(key).get("value"); | ||
| 177 | + } | ||
| 178 | + return null; | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + public List<IotAlertLog> getList() { | ||
| 182 | + return list; | ||
| 183 | + } | ||
| 184 | +} |
| 1 | +package com.zhonglai.luhui.alarm.config; | ||
| 2 | + | ||
| 3 | +import com.alibaba.fastjson.JSON; | ||
| 4 | +import com.alibaba.fastjson.JSONObject; | ||
| 5 | +import com.zhonglai.luhui.alarm.dao.DbOperateUtil; | ||
| 6 | +import com.zhonglai.luhui.alarm.dto.AttributeTriggers; | ||
| 7 | +import com.zhonglai.luhui.alarm.dto.IotAlert; | ||
| 8 | +import com.zhonglai.luhui.alarm.dto.TimerTriggers; | ||
| 9 | +import com.zhonglai.luhui.alarm.dto.TriggerTriggers; | ||
| 10 | +import com.zhonglai.luhui.alarm.handle.TimerAlarmJob; | ||
| 11 | +import com.zhonglai.luhui.alarm.service.TimerAlarmService; | ||
| 12 | +import com.zhonglai.luhui.service.dao.util.StringUtils; | ||
| 13 | +import org.quartz.*; | ||
| 14 | +import org.quartz.impl.StdSchedulerFactory; | ||
| 15 | +import org.slf4j.Logger; | ||
| 16 | +import org.slf4j.LoggerFactory; | ||
| 17 | + | ||
| 18 | +import java.sql.SQLException; | ||
| 19 | +import java.util.ArrayList; | ||
| 20 | +import java.util.HashMap; | ||
| 21 | +import java.util.List; | ||
| 22 | +import java.util.Map; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * 缓存的告警配置 | ||
| 26 | + */ | ||
| 27 | +public class CachAlarmConfig { | ||
| 28 | + private static final Logger logger = LoggerFactory.getLogger(CachAlarmConfig.class); | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + /** 属性告警路由 */ | ||
| 32 | + private static Map<Integer, Map<String, String>> product_attribute_route = new HashMap<>(); | ||
| 33 | + | ||
| 34 | + /** 触发告警路由 */ | ||
| 35 | + private static Map<Integer, Map<String, String>> product_trigger_route = new HashMap<>(); | ||
| 36 | + | ||
| 37 | + /** 告警配置 */ | ||
| 38 | + private static Map<Long, IotAlert> alarmConfig = new HashMap<>(); | ||
| 39 | + | ||
| 40 | + /** 告警配置 */ | ||
| 41 | + private static Map<String, Integer> device_product = new HashMap<>(); | ||
| 42 | + | ||
| 43 | + | ||
| 44 | + public static void loadConfig() | ||
| 45 | + { | ||
| 46 | + //加载属性告警配置 | ||
| 47 | + loadAttributeAlarmConfig(); | ||
| 48 | + | ||
| 49 | + //加载触发告警配置 | ||
| 50 | + loadTriggerAlarmConfig(); | ||
| 51 | + | ||
| 52 | + //加载定时任务 | ||
| 53 | + loadTimerAlarmConfig(); | ||
| 54 | + | ||
| 55 | + //加载用户告警配置 | ||
| 56 | + loadUserAlarmConfig(); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * 加载属性告警配置 | ||
| 61 | + */ | ||
| 62 | + private static void loadAttributeAlarmConfig() | ||
| 63 | + { | ||
| 64 | + List<IotAlert> list = DbOperateUtil.getIotAlertList(1); | ||
| 65 | + if(null != list && list.size() != 0) | ||
| 66 | + { | ||
| 67 | + for (IotAlert iotAlert:list) | ||
| 68 | + { | ||
| 69 | + addAttributeAlarmRoute(iotAlert); | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * 加载触发告警配置 | ||
| 78 | + */ | ||
| 79 | + private static void loadTriggerAlarmConfig() | ||
| 80 | + { | ||
| 81 | + List<IotAlert> list = DbOperateUtil.getIotAlertList(2); | ||
| 82 | + if(null != list && list.size() != 0) | ||
| 83 | + { | ||
| 84 | + for (IotAlert iotAlert:list) | ||
| 85 | + { | ||
| 86 | + addTriggerAlarmRoute(iotAlert); | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * 加载定时任务 | ||
| 93 | + */ | ||
| 94 | + private static void loadTimerAlarmConfig() | ||
| 95 | + { | ||
| 96 | + List<IotAlert> list = DbOperateUtil.getIotAlertList(3); | ||
| 97 | + if(null != list && list.size() != 0) | ||
| 98 | + { | ||
| 99 | + for (IotAlert iotAlert:list) | ||
| 100 | + { | ||
| 101 | + TimerAlarmService.addTimerAlarm(iotAlert); | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * 加载用户告警配置 | ||
| 108 | + */ | ||
| 109 | + private static void loadUserAlarmConfig() | ||
| 110 | + { | ||
| 111 | + List<IotAlert> list = DbOperateUtil.getUserIotAlertList(1); | ||
| 112 | + if(null != list && list.size() != 0) | ||
| 113 | + { | ||
| 114 | + for (IotAlert iotAlert:list) | ||
| 115 | + { | ||
| 116 | + addAttributeAlarmRoute(iotAlert); | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + list = DbOperateUtil.getUserIotAlertList(2); | ||
| 120 | + if(null != list && list.size() != 0) | ||
| 121 | + { | ||
| 122 | + for (IotAlert iotAlert:list) | ||
| 123 | + { | ||
| 124 | + addTriggerAlarmRoute(iotAlert); | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + list = DbOperateUtil.getUserIotAlertList(3); | ||
| 128 | + if(null != list && list.size() != 0) | ||
| 129 | + { | ||
| 130 | + for (IotAlert iotAlert:list) | ||
| 131 | + { | ||
| 132 | + TimerAlarmService.addTimerAlarm(iotAlert); | ||
| 133 | + } | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + | ||
| 138 | + /** | ||
| 139 | + * 添加属性告警路由 | ||
| 140 | + * @param iotAlert | ||
| 141 | + */ | ||
| 142 | + public static void addAttributeAlarmRoute(IotAlert iotAlert) | ||
| 143 | + { | ||
| 144 | + Map<String, String> iotAlertMap = product_attribute_route.get(iotAlert.getProductId()); | ||
| 145 | + if(null == iotAlertMap) | ||
| 146 | + { | ||
| 147 | + iotAlertMap = new HashMap<>(); | ||
| 148 | + product_attribute_route.put(iotAlert.getProductId().intValue(),iotAlertMap); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + String triggers = iotAlert.getTriggers(); | ||
| 152 | + | ||
| 153 | + if(StringUtils.isNotEmpty(triggers)) | ||
| 154 | + { | ||
| 155 | + AttributeTriggers attributeTriggers = JSONObject.parseObject(triggers,AttributeTriggers.class); | ||
| 156 | + | ||
| 157 | + //添加路由 | ||
| 158 | + putroute(iotAlertMap,attributeTriggers.getModel_name(),iotAlert.getAlertId()); | ||
| 159 | + //添加告警配置 | ||
| 160 | + putIotAlert(iotAlert); | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + /** | ||
| 165 | + * 添加触发告警路由 | ||
| 166 | + * @param iotAlert | ||
| 167 | + */ | ||
| 168 | + public static void addTriggerAlarmRoute(IotAlert iotAlert) | ||
| 169 | + { | ||
| 170 | + Map<String, String> iotAlertMap = product_trigger_route.get(iotAlert.getProductId()); | ||
| 171 | + if(null == iotAlertMap) | ||
| 172 | + { | ||
| 173 | + iotAlertMap = new HashMap<>(); | ||
| 174 | + product_trigger_route.put(iotAlert.getProductId().intValue(),iotAlertMap); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + String triggers = iotAlert.getTriggers(); | ||
| 178 | + | ||
| 179 | + if(StringUtils.isNotEmpty(triggers)) | ||
| 180 | + { | ||
| 181 | + List<TriggerTriggers> attributeTriggersList = JSON.parseArray(triggers,TriggerTriggers.class); | ||
| 182 | + if(null != attributeTriggersList && attributeTriggersList.size() !=0 ) | ||
| 183 | + { | ||
| 184 | + for (TriggerTriggers triggerTriggers:attributeTriggersList) | ||
| 185 | + { | ||
| 186 | + //添加路由 | ||
| 187 | + putroute(iotAlertMap,triggerTriggers.getModel_name(),iotAlert.getAlertId()); | ||
| 188 | + if(triggerTriggers.getType()==2) | ||
| 189 | + { | ||
| 190 | + putroute(iotAlertMap,triggerTriggers.getCompare_object()+"",iotAlert.getAlertId()); | ||
| 191 | + } | ||
| 192 | + //添加告警配置 | ||
| 193 | + putIotAlert(iotAlert); | ||
| 194 | + } | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + } | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + | ||
| 201 | + | ||
| 202 | + private static void putroute(Map<String, String> iotAlertMap,String model_name,Long alertid) | ||
| 203 | + { | ||
| 204 | + String route = iotAlertMap.get(model_name); | ||
| 205 | + if(null == route) | ||
| 206 | + { | ||
| 207 | + route = "|"; | ||
| 208 | + } | ||
| 209 | + if (route.indexOf("|"+alertid+"|")<0) //如果关系不存在,就加上 | ||
| 210 | + { | ||
| 211 | + route+=alertid+"|"; | ||
| 212 | + iotAlertMap.put(model_name,route); | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + | ||
| 217 | + | ||
| 218 | + /** | ||
| 219 | + * 获取属性告警配置 | ||
| 220 | + * @param product_id | ||
| 221 | + * @param modelKey | ||
| 222 | + * @return | ||
| 223 | + */ | ||
| 224 | + public static List<IotAlert> getAttributeIotAlert(Integer product_id,String modelKey) | ||
| 225 | + { | ||
| 226 | + if(product_attribute_route.containsKey(product_id) && product_attribute_route.get(product_id).containsKey(modelKey)) | ||
| 227 | + { | ||
| 228 | + String idsStr = product_attribute_route.get(product_id).get(modelKey); | ||
| 229 | + if(StringUtils.isNotEmpty(idsStr)) | ||
| 230 | + { | ||
| 231 | + idsStr = idsStr.substring(1,idsStr.length()-1); | ||
| 232 | + String[] ids = idsStr.split("\\|"); | ||
| 233 | + | ||
| 234 | + List<IotAlert> list = new ArrayList<>(); | ||
| 235 | + for (String id:ids) | ||
| 236 | + { | ||
| 237 | + IotAlert iotAlert = alarmConfig.get(Long.parseLong(id)); | ||
| 238 | + list.add(iotAlert); | ||
| 239 | + } | ||
| 240 | + return list; | ||
| 241 | + } | ||
| 242 | + } | ||
| 243 | + return null; | ||
| 244 | + } | ||
| 245 | + | ||
| 246 | + /** | ||
| 247 | + * 获取触发告警配置 | ||
| 248 | + * @param product_id | ||
| 249 | + * @param modelKey | ||
| 250 | + * @return | ||
| 251 | + */ | ||
| 252 | + public static List<IotAlert> getTriggerIotAlert(Integer product_id,String modelKey) | ||
| 253 | + { | ||
| 254 | + if(product_trigger_route.containsKey(product_id) && product_trigger_route.get(product_id).containsKey(modelKey)) | ||
| 255 | + { | ||
| 256 | + String idsStr = product_trigger_route.get(product_id).get(modelKey); | ||
| 257 | + if(StringUtils.isNotEmpty(idsStr)) | ||
| 258 | + { | ||
| 259 | + idsStr = idsStr.substring(1,idsStr.length()-1); | ||
| 260 | + String[] ids = idsStr.split("\\|"); | ||
| 261 | + | ||
| 262 | + List<IotAlert> list = new ArrayList<>(); | ||
| 263 | + for (String id:ids) | ||
| 264 | + { | ||
| 265 | + IotAlert iotAlert = alarmConfig.get(Long.parseLong(id)); | ||
| 266 | + list.add(iotAlert); | ||
| 267 | + } | ||
| 268 | + return list; | ||
| 269 | + } | ||
| 270 | + } | ||
| 271 | + return null; | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + public static Integer getDeviceProduct(String id) | ||
| 275 | + { | ||
| 276 | + String deviceId = id.split("_")[0]; | ||
| 277 | + Integer productId = device_product.get(deviceId); | ||
| 278 | + if(null == productId) | ||
| 279 | + { | ||
| 280 | + try { | ||
| 281 | + productId = DbOperateUtil.getProductId(deviceId); | ||
| 282 | + if (null != productId) | ||
| 283 | + { | ||
| 284 | + device_product.put(deviceId,productId); | ||
| 285 | + } | ||
| 286 | + } catch (SQLException e) { | ||
| 287 | + throw new RuntimeException(e); | ||
| 288 | + } | ||
| 289 | + } | ||
| 290 | + return productId; | ||
| 291 | + } | ||
| 292 | + | ||
| 293 | + public static void putDeviceProduct(String deviceId,Integer product_id) | ||
| 294 | + { | ||
| 295 | + device_product.put(deviceId,product_id); | ||
| 296 | + } | ||
| 297 | + | ||
| 298 | + public static void delete(Long id) | ||
| 299 | + { | ||
| 300 | + IotAlert iotAlert = alarmConfig.get(id); | ||
| 301 | + if(null != iotAlert) | ||
| 302 | + { | ||
| 303 | + alarmConfig.remove(id); | ||
| 304 | + switch (iotAlert.getAlertType()) | ||
| 305 | + { | ||
| 306 | + case 1: | ||
| 307 | + product_attribute_route.remove(iotAlert.getProductId()); | ||
| 308 | + break; | ||
| 309 | + case 2: | ||
| 310 | + product_trigger_route.remove(iotAlert.getProductId()); | ||
| 311 | + break; | ||
| 312 | + case 3: | ||
| 313 | + TimerAlarmService.removeTimerAlarm(iotAlert.getAlertId(),iotAlert.getProductId()); | ||
| 314 | + break; | ||
| 315 | + } | ||
| 316 | + } | ||
| 317 | + } | ||
| 318 | + | ||
| 319 | + public static void putIotAlert(IotAlert iotAlert) | ||
| 320 | + { | ||
| 321 | + alarmConfig.put(iotAlert.getAlertId(),iotAlert); | ||
| 322 | + } | ||
| 323 | +} |
| 1 | +package com.zhonglai.luhui.alarm.dao; | ||
| 2 | + | ||
| 3 | +import com.zhonglai.luhui.alarm.dto.IotAlert; | ||
| 4 | +import com.zhonglai.luhui.alarm.dto.IotAlertLog; | ||
| 5 | +import com.zhonglai.luhui.alarm.dto.IotTerminal; | ||
| 6 | +import com.zhonglai.luhui.service.dao.BaseDao; | ||
| 7 | +import com.zhonglai.luhui.service.dao.util.StringUtils; | ||
| 8 | +import org.apache.commons.dbutils.handlers.ScalarHandler; | ||
| 9 | + | ||
| 10 | +import java.sql.SQLException; | ||
| 11 | +import java.util.List; | ||
| 12 | + | ||
| 13 | +public class DbOperateUtil { | ||
| 14 | + private static BaseDao baseDao = new BaseDao(); | ||
| 15 | + | ||
| 16 | + public static List<IotAlert> getIotAlertList(Integer alertType) | ||
| 17 | + { | ||
| 18 | + IotAlert iotAlert = new IotAlert(); | ||
| 19 | + iotAlert.setStatus(1); | ||
| 20 | + iotAlert.setAlertType(alertType); | ||
| 21 | + List<IotAlert> list = baseDao.find(iotAlert); | ||
| 22 | + return list; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + public static List<IotAlert> getUserIotAlertList(Integer alertType) | ||
| 26 | + { | ||
| 27 | + return baseDao.findBysql("SELECT * FROM `iot_alert_user` WHERE `status`=1 AND alert_type=?",IotAlert.class,alertType); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public static Integer getProductId(String client_id) throws SQLException { | ||
| 31 | + return baseDao.query().query("SELECT product_id FROM `iot_device` WHERE client_id=?",new ScalarHandler<Integer>(),client_id); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public static int insertIotAlertLogList(List<IotAlertLog> list) | ||
| 35 | + { | ||
| 36 | + return baseDao.insertList(list,null); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + public static List<IotTerminal> getTimerAlarmIotTerminal(Long productId,String terminalids) | ||
| 40 | + { | ||
| 41 | + if(StringUtils.isNotEmpty(terminalids)) | ||
| 42 | + { | ||
| 43 | + return baseDao.findBysql("SELECT * FROM `iot_terminal` WHERE product_id=? AND id IN(?)",IotTerminal.class,productId,terminalids.split(",")); | ||
| 44 | + } | ||
| 45 | + return baseDao.findBysql("SELECT * FROM `iot_terminal` WHERE product_id=?",IotTerminal.class,productId); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public static List<IotAlertLog> getIotAlertLogList(Integer status,Integer limit) | ||
| 49 | + { | ||
| 50 | + List<IotAlertLog> list = baseDao.findBysql("select * from iot_alert_log where `status`=? limit ?",IotAlertLog.class,status,limit); | ||
| 51 | + return list; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public static int updateIotAlertLogStatus(List<Long> ids,Integer status) | ||
| 55 | + { | ||
| 56 | + return baseDao.updateBySql("update iot_alert_log set `status`=? where id in(?)",status,ids); | ||
| 57 | + } | ||
| 58 | +} |
| 1 | +package com.zhonglai.luhui.alarm.dto; | ||
| 2 | + | ||
| 3 | +import java.util.Map; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * 属性警触发配置 | ||
| 7 | + */ | ||
| 8 | +public class AttributeTriggers { | ||
| 9 | + private String model_name; | ||
| 10 | + private Map<Object,String> valueMapName; | ||
| 11 | + | ||
| 12 | + public String getModel_name() { | ||
| 13 | + return model_name; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + public void setModel_name(String model_name) { | ||
| 17 | + this.model_name = model_name; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + public Map<Object, String> getValueMapName() { | ||
| 21 | + return valueMapName; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public void setValueMapName(Map<Object, String> valueMapName) { | ||
| 25 | + this.valueMapName = valueMapName; | ||
| 26 | + } | ||
| 27 | +} |
| 1 | +package com.zhonglai.luhui.alarm.dto; | ||
| 2 | + | ||
| 3 | +import com.alibaba.otter.canal.protocol.CanalEntry; | ||
| 4 | + | ||
| 5 | +import java.lang.reflect.InvocationTargetException; | ||
| 6 | +import java.lang.reflect.Method; | ||
| 7 | +import java.util.List; | ||
| 8 | + | ||
| 9 | +public class IotAlert { | ||
| 10 | + /** 告警ID */ | ||
| 11 | + private Long alertId; | ||
| 12 | + | ||
| 13 | + /** 告警名称 */ | ||
| 14 | + private String alertName; | ||
| 15 | + | ||
| 16 | + /** 告警级别(1=提醒通知,2=轻微问题,3=严重警告) */ | ||
| 17 | + private Long alertLevel; | ||
| 18 | + | ||
| 19 | + /** 告警类型(1属性告警,2触发告警,3定时告警) */ | ||
| 20 | + private Integer alertType; | ||
| 21 | + | ||
| 22 | + /** 产品ID */ | ||
| 23 | + private Long productId; | ||
| 24 | + | ||
| 25 | + /** 产品名称 */ | ||
| 26 | + private String productName; | ||
| 27 | + | ||
| 28 | + /** 触发器 */ | ||
| 29 | + private String triggers; | ||
| 30 | + | ||
| 31 | + /** 执行动作 */ | ||
| 32 | + private String actions; | ||
| 33 | + | ||
| 34 | + /** 告警状态 (1-启动,2-停止)**/ | ||
| 35 | + private Integer status; | ||
| 36 | + | ||
| 37 | + private String createBy; | ||
| 38 | + | ||
| 39 | + public String getCreateBy() { | ||
| 40 | + return createBy; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public void setCreateBy(String createBy) { | ||
| 44 | + this.createBy = createBy; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public Long getAlertId() { | ||
| 48 | + return alertId; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + public void setAlertId(Long alertId) { | ||
| 52 | + this.alertId = alertId; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + public String getAlertName() { | ||
| 56 | + return alertName; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + public void setAlertName(String alertName) { | ||
| 60 | + this.alertName = alertName; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + public Long getAlertLevel() { | ||
| 64 | + return alertLevel; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public void setAlertLevel(Long alertLevel) { | ||
| 68 | + this.alertLevel = alertLevel; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + public Integer getAlertType() { | ||
| 72 | + return alertType; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + public void setAlertType(Integer alertType) { | ||
| 76 | + this.alertType = alertType; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + public Long getProductId() { | ||
| 80 | + return productId; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + public void setProductId(Long productId) { | ||
| 84 | + this.productId = productId; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + public String getProductName() { | ||
| 88 | + return productName; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + public void setProductName(String productName) { | ||
| 92 | + this.productName = productName; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + public String getTriggers() { | ||
| 96 | + return triggers; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + public void setTriggers(String triggers) { | ||
| 100 | + this.triggers = triggers; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + public String getActions() { | ||
| 104 | + return actions; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + public void setActions(String actions) { | ||
| 108 | + this.actions = actions; | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + public Integer getStatus() { | ||
| 112 | + return status; | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + public void setStatus(Integer status) { | ||
| 116 | + this.status = status; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + public static IotAlert instantiate(List<CanalEntry.Column> columns) { | ||
| 120 | + if (null == columns || columns.size() ==0) | ||
| 121 | + { | ||
| 122 | + return null; | ||
| 123 | + } | ||
| 124 | + IotAlert iotAlert = new IotAlert(); | ||
| 125 | + for (CanalEntry.Column column : columns) | ||
| 126 | + { | ||
| 127 | + try { | ||
| 128 | + Method method = iotAlert.getClass().getMethod("set"+getName(column.getName())); | ||
| 129 | + method.invoke(iotAlert, column.getValue()); | ||
| 130 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + if(null == iotAlert.getAlertId()) | ||
| 134 | + { | ||
| 135 | + return null; | ||
| 136 | + } | ||
| 137 | + return iotAlert; | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + /** | ||
| 141 | + * [简要描述]:首字母大写 | ||
| 142 | + * | ||
| 143 | + * @author com.zhonglai | ||
| 144 | + * @param str | ||
| 145 | + * @return | ||
| 146 | + */ | ||
| 147 | + public static String getName(String str) { | ||
| 148 | + char ch = str.toCharArray()[0]; | ||
| 149 | + ch = (char) ((ch - 97) + 'A'); | ||
| 150 | + str = ch + str.substring(1); | ||
| 151 | + return str; | ||
| 152 | + } | ||
| 153 | +} |
| 1 | +package com.zhonglai.luhui.alarm.dto; | ||
| 2 | + | ||
| 3 | +import com.zhonglai.luhui.service.dao.util.StringUtils; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * 设备告警对象 iot_alert_log | ||
| 7 | + * | ||
| 8 | + * @author kerwincui | ||
| 9 | + * @date 2022-01-13 | ||
| 10 | + */ | ||
| 11 | +public class IotAlertLog | ||
| 12 | +{ | ||
| 13 | + private Long alert_log_id; // bigint NOT NULL AUTO_INCREMENT COMMENT '主键', | ||
| 14 | + private Long alert_id; // bigint DEFAULT NULL COMMENT '告警ID', | ||
| 15 | + private String alert_name; // varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '告警名称', | ||
| 16 | + private Integer alert_level; // int NOT NULL COMMENT '告警级别(1=轻微问题,2=提醒通知,3=严重警告)', | ||
| 17 | + private Integer status; // int NOT NULL COMMENT '处理状态(1=不需要处理,2=未处理,3=已处理)', | ||
| 18 | + private String device_id; // varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '设备ID', | ||
| 19 | + private Long create_time; // datetime DEFAULT NULL COMMENT '创建时间', | ||
| 20 | + private Integer type; // tinyint DEFAULT NULL COMMENT '类型(1=告警,2=场景联动)', | ||
| 21 | + private Integer user_id; //关联的用户id(0为系统生成) | ||
| 22 | + public IotAlertLog() { | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + public IotAlertLog(Long alert_id, String alert_name, Integer alert_level, Integer status, String device_id, Long create_time, Integer type,String user_id) { | ||
| 26 | + this.alert_id = alert_id; | ||
| 27 | + this.alert_name = alert_name; | ||
| 28 | + this.alert_level = alert_level; | ||
| 29 | + this.status = status; | ||
| 30 | + this.device_id = device_id; | ||
| 31 | + this.create_time = create_time; | ||
| 32 | + this.type = type; | ||
| 33 | + if(StringUtils.isNotEmpty(user_id)) | ||
| 34 | + { | ||
| 35 | + this.user_id = Integer.parseInt(user_id); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public Integer getUser_id() { | ||
| 41 | + return user_id; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public void setUser_id(Integer user_id) { | ||
| 45 | + this.user_id = user_id; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public Long getAlert_log_id() { | ||
| 49 | + return alert_log_id; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public void setAlert_log_id(Long alert_log_id) { | ||
| 53 | + this.alert_log_id = alert_log_id; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + public Long getAlert_id() { | ||
| 57 | + return alert_id; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + public void setAlert_id(Long alert_id) { | ||
| 61 | + this.alert_id = alert_id; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + public String getAlert_name() { | ||
| 65 | + return alert_name; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + public void setAlert_name(String alert_name) { | ||
| 69 | + this.alert_name = alert_name; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + public Integer getAlert_level() { | ||
| 73 | + return alert_level; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + public void setAlert_level(Integer alert_level) { | ||
| 77 | + this.alert_level = alert_level; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + public Integer getStatus() { | ||
| 81 | + return status; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + public void setStatus(Integer status) { | ||
| 85 | + this.status = status; | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + public String getDevice_id() { | ||
| 89 | + return device_id; | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + public void setDevice_id(String device_id) { | ||
| 93 | + this.device_id = device_id; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + public Long getCreate_time() { | ||
| 97 | + return create_time; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + public void setCreate_time(Long create_time) { | ||
| 101 | + this.create_time = create_time; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + public Integer getType() { | ||
| 105 | + return type; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + public void setType(Integer type) { | ||
| 109 | + this.type = type; | ||
| 110 | + } | ||
| 111 | +} |
| 1 | +package com.zhonglai.luhui.alarm.dto; | ||
| 2 | + | ||
| 3 | +import com.alibaba.otter.canal.protocol.CanalEntry; | ||
| 4 | +import com.zhonglai.luhui.alarm.clas.UpAlarmFactory; | ||
| 5 | + | ||
| 6 | +import java.util.List; | ||
| 7 | + | ||
| 8 | +public class IotDevice { | ||
| 9 | + /** 主键 */ | ||
| 10 | + private String client_id; | ||
| 11 | + | ||
| 12 | + /** 设备摘要,格式[{"name":"device"},{"chip":"esp8266"}] */ | ||
| 13 | + private String summary; | ||
| 14 | + | ||
| 15 | + /** 物模型值 */ | ||
| 16 | + private String things_model_value; | ||
| 17 | + private Integer product_id; | ||
| 18 | + | ||
| 19 | + public Integer getProduct_id() { | ||
| 20 | + return product_id; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public void setProduct_id(Integer product_id) { | ||
| 24 | + this.product_id = product_id; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + public String getClient_id() { | ||
| 28 | + return client_id; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + public void setClient_id(String client_id) { | ||
| 32 | + this.client_id = client_id; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public String getSummary() { | ||
| 36 | + return summary; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + public void setSummary(String summary) { | ||
| 40 | + this.summary = summary; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public String getThings_model_value() { | ||
| 44 | + return things_model_value; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public void setThings_model_value(String things_model_value) { | ||
| 48 | + this.things_model_value = things_model_value; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + public static IotDevice instantiate(List<CanalEntry.Column> columns) { | ||
| 52 | + if (null == columns || columns.size() ==0) | ||
| 53 | + { | ||
| 54 | + return null; | ||
| 55 | + } | ||
| 56 | + IotDevice iotDevice = new IotDevice(); | ||
| 57 | + for (CanalEntry.Column column : columns) | ||
| 58 | + { | ||
| 59 | + switch (column.getName()) | ||
| 60 | + { | ||
| 61 | + case "client_id": | ||
| 62 | + iotDevice.setClient_id(column.getValue()); | ||
| 63 | + break; | ||
| 64 | + case "summary": | ||
| 65 | + iotDevice.setSummary( column.getValue()); | ||
| 66 | + break; | ||
| 67 | + case "things_model_value": | ||
| 68 | + iotDevice.setThings_model_value ( column.getValue()); | ||
| 69 | + break; | ||
| 70 | + case "product_id": | ||
| 71 | + iotDevice.setProduct_id ( null!=column.getValue()&&!"".equals(column.getValue())?Integer.parseInt(column.getValue()):null); | ||
| 72 | + break; | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + if(null == iotDevice.getSummary() || null == iotDevice.getThings_model_value() || null == iotDevice.getClient_id()) | ||
| 76 | + { | ||
| 77 | + return null; | ||
| 78 | + } | ||
| 79 | + return iotDevice; | ||
| 80 | + } | ||
| 81 | +} |
-
请 注册 或 登录 后发表评论