haungxing
12 months ago
6 changed files with 97 additions and 12 deletions
@ -0,0 +1,14 @@ |
|||||||
|
package com.hnac.hzims.ticket.annotation; |
||||||
|
|
||||||
|
import java.lang.annotation.*; |
||||||
|
|
||||||
|
@Documented |
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
@Target({ElementType.FIELD}) |
||||||
|
public @interface DateTimeToMap { |
||||||
|
|
||||||
|
String prefix() default ""; |
||||||
|
|
||||||
|
String split() default "_"; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,57 @@ |
|||||||
|
package com.hnac.hzims.ticket.utils; |
||||||
|
|
||||||
|
import com.hnac.hzims.ticket.annotation.DateTimeToMap; |
||||||
|
import org.springblade.core.log.exception.ServiceException; |
||||||
|
import org.springblade.core.tool.utils.DateUtil; |
||||||
|
import org.springblade.core.tool.utils.ObjectUtil; |
||||||
|
|
||||||
|
import java.lang.reflect.Field; |
||||||
|
import java.time.LocalDateTime; |
||||||
|
import java.util.*; |
||||||
|
import java.util.stream.Collectors; |
||||||
|
|
||||||
|
public class ObjectUtils { |
||||||
|
|
||||||
|
public static Map obj2Map(Object obj) { |
||||||
|
if(ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |
||||||
|
Map result = new HashMap(); |
||||||
|
List<Field> fieldList = new ArrayList<>(); |
||||||
|
Class<?> clazz = obj.getClass(); |
||||||
|
while(clazz != null) { |
||||||
|
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); |
||||||
|
clazz = clazz.getSuperclass(); |
||||||
|
} |
||||||
|
fieldList.forEach(field -> { |
||||||
|
field.setAccessible(true); |
||||||
|
Object value; |
||||||
|
try { |
||||||
|
value = field.get(obj); |
||||||
|
} catch (IllegalAccessException e) { |
||||||
|
throw new ServiceException("获取属性性出错"); |
||||||
|
} |
||||||
|
if(value instanceof List) { |
||||||
|
List<Map> list = ((List<?>) value).stream().map(ObjectUtils::obj2Map).collect(Collectors.toList()); |
||||||
|
result.put(field.getName(),list); |
||||||
|
} |
||||||
|
else if(value instanceof LocalDateTime) { |
||||||
|
DateTimeToMap dateTimeToMap = field.getAnnotation(DateTimeToMap.class); |
||||||
|
LocalDateTime time = (LocalDateTime) value; |
||||||
|
if(ObjectUtil.isEmpty(dateTimeToMap)) { |
||||||
|
result.put(field.getName(),time.format(DateUtil.TIME_FORMATTER)); |
||||||
|
} |
||||||
|
else { |
||||||
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"year",time.getYear()); |
||||||
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"month",time.getMonth()); |
||||||
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"day",time.getDayOfMonth()); |
||||||
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"hour",time.getHour()); |
||||||
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"minute",time.getMinute()); |
||||||
|
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"second",time.getSecond()); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
return result; |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
<License> |
||||||
|
<Data> |
||||||
|
<Products> |
||||||
|
<Product>Aspose.Total for Java</Product> |
||||||
|
<Product>Aspose.Words for Java</Product> |
||||||
|
</Products> |
||||||
|
<EditionType>Enterprise</EditionType> |
||||||
|
<SubscriptionExpiry>20991231</SubscriptionExpiry> |
||||||
|
<LicenseExpiry>20991231</LicenseExpiry> |
||||||
|
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber> |
||||||
|
</Data> |
||||||
|
<Signature> |
||||||
|
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= |
||||||
|
</Signature> |
||||||
|
</License> |
Binary file not shown.
Loading…
Reference in new issue