Commit a5ad429c247332f02bf4a8715aba2d8795628768
1 parent
ad5afc6649
Exists in
master
and in
6 other branches
bug
xiufu
Showing 4 changed files with 81 additions and 17 deletions
- platform-common/src/main/java/com/lyms/platform/common/utils/EnumUtil.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/ReportController.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/service/impl/ReportServiceImpl.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/CollectionUtils.java
platform-common/src/main/java/com/lyms/platform/common/utils/EnumUtil.java
View file @
a5ad429
| 1 | 1 | package com.lyms.platform.common.utils; |
| 2 | 2 | |
| 3 | 3 | import org.apache.commons.beanutils.PropertyUtils; |
| 4 | +import org.apache.commons.collections.CollectionUtils; | |
| 5 | +import org.apache.commons.collections.MapUtils; | |
| 6 | +import org.slf4j.Logger; | |
| 7 | +import org.slf4j.LoggerFactory; | |
| 4 | 8 | |
| 5 | 9 | import java.security.InvalidParameterException; |
| 6 | -import java.util.ArrayList; | |
| 7 | -import java.util.HashMap; | |
| 8 | -import java.util.List; | |
| 9 | -import java.util.Map; | |
| 10 | +import java.util.*; | |
| 10 | 11 | |
| 11 | 12 | /** |
| 12 | 13 | * @Author: litao |
| 13 | 14 | |
| 14 | 15 | |
| 15 | 16 | |
| 16 | 17 | |
| 17 | 18 | |
| 18 | 19 | |
| ... | ... | @@ -15,39 +16,89 @@ |
| 15 | 16 | */ |
| 16 | 17 | public class EnumUtil { |
| 17 | 18 | |
| 19 | + private static final Logger LOGGER = LoggerFactory.getLogger(EnumUtil.class); | |
| 20 | + | |
| 18 | 21 | private static final String DEFAULT_KEY_NAME = "id"; |
| 19 | 22 | private static final String DEFAULT_VALUE_NAME = "name"; |
| 20 | 23 | |
| 24 | + /** 返回list中map的k、v的名称 */ | |
| 25 | + private static final String RESULT_DEFAULT_KEY = "id"; | |
| 26 | + private static final String RESULT_DEFAULT_VALUE = "name"; | |
| 27 | + | |
| 28 | + | |
| 21 | 29 | public static <T> List<Map<String, Object>> toJson(Class<T> clazz) { |
| 22 | 30 | return toJson(clazz, DEFAULT_KEY_NAME, DEFAULT_VALUE_NAME); |
| 23 | 31 | } |
| 24 | 32 | |
| 25 | 33 | public static <T> List<Map<String, Object>> toJson(Class<T> clazz, String codeName, String valueName) { |
| 26 | - List<Map<String, Object>> list = new ArrayList<>(); | |
| 27 | - | |
| 28 | 34 | if (!clazz.isEnum()) { |
| 29 | 35 | throw new InvalidParameterException("请传入枚举类型参数"); |
| 30 | 36 | } |
| 31 | 37 | |
| 38 | + List<Map<String, Object>> list = new ArrayList<>(); | |
| 39 | + | |
| 32 | 40 | org.springframework.util.Assert.notNull(codeName, "key不能为null"); |
| 33 | 41 | org.springframework.util.Assert.notNull(valueName, "value不能为null"); |
| 34 | 42 | |
| 35 | 43 | try { |
| 36 | 44 | T[] enumConstants = clazz.getEnumConstants(); |
| 37 | 45 | for (T ec : enumConstants) { |
| 38 | - Object key = PropertyUtils.getProperty(ec,codeName); | |
| 39 | - Object value = PropertyUtils.getProperty(ec,valueName); | |
| 40 | - if(key != null && value != null) { | |
| 41 | - Map<String, Object> tempMap = new HashMap<>(); | |
| 42 | - tempMap.put("id", key.toString()); | |
| 43 | - tempMap.put("name", value); | |
| 46 | + Map<String, Object> tempMap = getTempMap(ec, codeName, valueName); | |
| 47 | + if(MapUtils.isNotEmpty(tempMap)) { | |
| 44 | 48 | list.add(tempMap); |
| 45 | 49 | } |
| 46 | 50 | } |
| 47 | 51 | } catch (Exception e) { |
| 52 | + LOGGER.error("枚举类转换json出错: ", e.fillInStackTrace()); | |
| 48 | 53 | e.printStackTrace(); |
| 49 | 54 | } |
| 50 | 55 | return list; |
| 56 | + } | |
| 57 | + | |
| 58 | + public static <T> List<Map<String, Object>> listToJson(List<T> couponEnums) { | |
| 59 | + return listToJson(couponEnums, DEFAULT_KEY_NAME, DEFAULT_VALUE_NAME); | |
| 60 | + } | |
| 61 | + | |
| 62 | + public static <T> List<Map<String, Object>> listToJson(List<T> list, String codeName, String valueName) { | |
| 63 | + List<Map<String, Object>> restList = new ArrayList<>(); | |
| 64 | + | |
| 65 | + if(CollectionUtils.isNotEmpty(list)) { | |
| 66 | + if(!list.get(0).getClass().isEnum()) { | |
| 67 | + throw new InvalidParameterException("请传入枚举类型参数"); | |
| 68 | + } | |
| 69 | + | |
| 70 | + Iterator<T> iterator = list.iterator(); | |
| 71 | + while(iterator.hasNext()) { | |
| 72 | + T t = iterator.next(); | |
| 73 | + Map<String, Object> tempMap = getTempMap(t, codeName, valueName); | |
| 74 | + if(MapUtils.isNotEmpty(tempMap)) { | |
| 75 | + restList.add(tempMap); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + } | |
| 79 | + | |
| 80 | + if(!list.get(0).getClass().isEnum()) { | |
| 81 | + throw new InvalidParameterException("请传入枚举类型参数"); | |
| 82 | + } | |
| 83 | + | |
| 84 | + return restList; | |
| 85 | + } | |
| 86 | + | |
| 87 | + private static Map<String, Object> getTempMap(Object bean, String codeName, String valueName) { | |
| 88 | + try { | |
| 89 | + Map<String, Object> tempMap = new HashMap<>(); | |
| 90 | + Object key = PropertyUtils.getProperty(bean, codeName); | |
| 91 | + Object value = PropertyUtils.getProperty(bean,valueName); | |
| 92 | + if(key != null && value != null) { | |
| 93 | + tempMap.put(RESULT_DEFAULT_KEY, key.toString()); | |
| 94 | + tempMap.put(RESULT_DEFAULT_VALUE, value); | |
| 95 | + } | |
| 96 | + return tempMap; | |
| 97 | + } catch (Exception e) { | |
| 98 | + LOGGER.error("enmu转map出错:", e.fillInStackTrace()); | |
| 99 | + e.printStackTrace(); | |
| 100 | + } | |
| 101 | + return null; | |
| 51 | 102 | } |
| 52 | 103 | |
| 53 | 104 | } |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/ReportController.java
View file @
a5ad429
| ... | ... | @@ -271,8 +271,8 @@ |
| 271 | 271 | @ResponseBody |
| 272 | 272 | @TokenRequired |
| 273 | 273 | @RequestMapping(value = "/init/coupon", method = RequestMethod.GET) |
| 274 | - public BaseObjectResponse initCoupon(HttpServletRequest request) { | |
| 275 | - return reportService.couponInit(CollectionUtils.createMap("userId", getUserId(request))); | |
| 274 | + public BaseObjectResponse initCoupon(HttpServletRequest request, String couponType) { | |
| 275 | + return reportService.couponInit(CollectionUtils.createMap("userId", getUserId(request), "couponType", couponType)); | |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | } |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/service/impl/ReportServiceImpl.java
View file @
a5ad429
| ... | ... | @@ -518,8 +518,17 @@ |
| 518 | 518 | param.put("hospitalIds", CollectionUtils.asList((String) param.get("hospitalId"))); |
| 519 | 519 | } |
| 520 | 520 | |
| 521 | - return RespBuilder.buildSuccess("coupons", EnumUtil.toJson(CouponEnums.class, "code", "name"), | |
| 522 | - "hTemp", couponMapper.findHospitals(param)); | |
| 521 | + | |
| 522 | + String couponType = (String) param.get("couponType"); | |
| 523 | + List<CouponEnums> enmus = new ArrayList<>(); | |
| 524 | + if(StringUtils.isNotBlank(couponType)) { | |
| 525 | + List<Integer> coupons = CollectionUtils.asList(couponType, Integer.class); | |
| 526 | + for (Integer coupon : coupons) { | |
| 527 | + enmus.add(CouponEnums.get(coupon)); | |
| 528 | + } | |
| 529 | + } | |
| 530 | + | |
| 531 | + return RespBuilder.buildSuccess("coupons", EnumUtil.listToJson(enmus, "code", "name"), "hTemp", couponMapper.findHospitals(param)); | |
| 523 | 532 | } |
| 524 | 533 | |
| 525 | 534 | @Override |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/CollectionUtils.java
View file @
a5ad429
| ... | ... | @@ -82,7 +82,11 @@ |
| 82 | 82 | List<T> list = new ArrayList<>(); |
| 83 | 83 | String[] id = ids.split(","); |
| 84 | 84 | for (String s : id) { |
| 85 | - list.add((T) s); | |
| 85 | + if(Integer.class == clazz) { | |
| 86 | + list.add((T) Integer.valueOf(s)); | |
| 87 | + } else { | |
| 88 | + list.add((T) s); | |
| 89 | + } | |
| 86 | 90 | } |
| 87 | 91 | return list; |
| 88 | 92 | } |