zhoutg 4 роки тому
батько
коміт
93fed78b66

+ 19 - 0
cdssman-service/src/main/java/com/diagbot/entity/Ex.java

@@ -0,0 +1,19 @@
+package com.diagbot.entity;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @description:
+ * @author: zhoutg
+ * @time: 2021/5/21 13:49
+ */
+@Target(value = { ElementType.TYPE, ElementType.FIELD })
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Ex {
+    public String name();
+}

+ 113 - 1
cdssman-service/src/main/java/com/diagbot/facade/KlDrugRegisterFacade.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.diagbot.dto.KlConceptSimDTO;
 import com.diagbot.dto.KlDrugRegisterDTO;
 import com.diagbot.entity.CommonParam;
+import com.diagbot.entity.Ex;
 import com.diagbot.entity.KlConcept;
 import com.diagbot.entity.KlDrugMapping;
 import com.diagbot.entity.KlDrugRegister;
@@ -17,7 +18,9 @@ import com.diagbot.service.KlDrugRegisterService;
 import com.diagbot.service.impl.KlDrugRegisterServiceImpl;
 import com.diagbot.util.BeanUtil;
 import com.diagbot.util.DateUtil;
+import com.diagbot.util.ExcelUtils;
 import com.diagbot.util.ListUtil;
+import com.diagbot.util.ReflectUtil;
 import com.diagbot.util.StringUtil;
 import com.diagbot.util.UserUtils;
 import com.diagbot.vo.KlDrugMappingGetVO;
@@ -25,6 +28,7 @@ import com.diagbot.vo.KlDrugRegisterDelVO;
 import com.diagbot.vo.KlDrugRegisterGetVO;
 import com.diagbot.vo.KlDrugRegisterPageVO;
 import com.diagbot.vo.KlDrugRegisterSaveVO;
+import com.diagbot.vo.KlDrugRegisterTestVO;
 import com.diagbot.vo.KlDrugSearchVO;
 import com.google.common.collect.Lists;
 import org.apache.commons.collections4.map.LinkedMap;
@@ -35,6 +39,8 @@ import org.springframework.stereotype.Component;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -208,6 +214,11 @@ public class KlDrugRegisterFacade extends KlDrugRegisterServiceImpl {
             InputStream inputStream = file.getInputStream();
             List<String[]> list = XLSXCovertCSVReader.readerExcel(inputStream,
                     "注册药品", 100);
+            for (String[] arr : list) {
+                for (int i = 0; i < arr.length; i++) {
+                    arr[i] = getObject(arr[i]);
+                }
+            }
             List<KlDrugRegister> drugRegisterList = Lists.newLinkedList();
             int i = 0;
 
@@ -261,6 +272,7 @@ public class KlDrugRegisterFacade extends KlDrugRegisterServiceImpl {
             }
             klDrugMappingService.remove(new QueryWrapper<KlDrugMapping>());
             klDrugMappingService.saveBatch(klDrugMappingList);
+
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -269,7 +281,7 @@ public class KlDrugRegisterFacade extends KlDrugRegisterServiceImpl {
 
     public static String getObject(String s) {
         if (StringUtil.isBlank(s)) {
-            return "";
+            return s;
         }
         if (!s.startsWith("\"")) {
             return s;
@@ -283,4 +295,104 @@ public class KlDrugRegisterFacade extends KlDrugRegisterServiceImpl {
         }
         return s;
     }
+
+    public static void main(String[] args) {
+        String path = "D:\\newSVN\\2020新版CDSS\\05.其他资料\\数据\\其他数据\\【注册药品导入模板】 - 副本 - 副本.xlsx";
+
+        List<KlDrugRegisterTestVO> list1 = ExcelUtils.importExcel(path, 0, 1, KlDrugRegisterTestVO.class);
+        List<KlDrugRegisterTestVO> list = refExcel(KlDrugRegisterTestVO.class);
+        System.out.println(list);
+    }
+
+    public static <T> List<T> refExcel(Class<T> c) {
+        KlDrugRegisterTestVO klDrugRegisterTestVO = new KlDrugRegisterTestVO();
+        List<T> res = Lists.newArrayList();
+        try {
+            String path = "D:\\newSVN\\2020新版CDSS\\05.其他资料\\数据\\其他数据\\【注册药品导入模板】 - 副本 - 副本.xlsx";
+            List<String[]> list = XLSXCovertCSVReader.readerExcel(path,
+                    "注册药品", 20);
+
+            if (ListUtil.isEmpty(list)) {
+                throw new RuntimeException("无数据");
+            }
+
+            String[] titleRow = list.get(0);
+            Map<Integer, String> colMap = new LinkedMap();
+            for (int i = 0; i < titleRow.length; i++) {
+                if (StringUtil.isBlank(titleRow[i])) {
+                    break;
+                }
+                colMap.put(i, getObject(titleRow[i]));
+            }
+            System.out.println(colMap);
+
+            Field[] fields = c.getDeclaredFields();
+            // 属性和表格列对应
+            Map<String, Map<String, String>> classMap = new LinkedHashMap();
+            for (Field field : fields) {
+                if (field.isAnnotationPresent(Ex.class)) {
+                    Ex annotation = field.getAnnotation(Ex.class);
+                    Map<String, String> propertyMap = new LinkedHashMap<>();
+                    propertyMap.put("field", field.getName());
+                    propertyMap.put("convertType", field.getGenericType().toString());
+                    classMap.put(annotation.name(), propertyMap);
+                }
+                field.getClass();
+            }
+            System.out.println(classMap);
+
+            int rowNum = 1;
+            for (String[] row : list) {
+                if (rowNum++ == 1) {
+                    continue;
+                }
+                T obj = c.newInstance();
+                for (int i = 0; i < row.length; i++) {
+                    if (classMap.get(colMap.get(i)) == null) {
+                        continue;
+                    }
+                    String property = classMap.get(colMap.get(i)).get("field");
+                    String convertType = classMap.get(colMap.get(i)).get("convertType");
+                    System.out.println(property + ":" + row[i]);
+                    if (StringUtil.isNotBlank(property)) {
+                        Object value = convertValue(getObject(row[i]), convertType);
+                        if (value != null) {
+                            ReflectUtil.setFieldValue(obj, property, value);
+                        }
+                    }
+                }
+                res.add(obj);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return res;
+    }
+
+    public static Object convertValue(String object, String convertType) {
+        if (object == null) {
+            return null;
+        }
+        switch (convertType) {
+            case "class java.lang.Integer":
+            case "int":
+                return Integer.parseInt(object);
+            case "class java.lang.Long":
+            case "long":
+                return Long.parseLong(object);
+            case "class java.lang.Float":
+            case "float":
+                return Float.parseFloat(object);
+            case "class java.lang.Double":
+            case "double":
+                return Double.parseDouble(object);
+            case "class java.lang.Boolean":
+            case "boolean":
+                return Boolean.parseBoolean(object);
+            case "class java.lang.String":
+                return object;
+            default:
+                return object;
+        }
+    }
 }

+ 127 - 0
cdssman-service/src/main/java/com/diagbot/util/ReflectUtil.java

@@ -0,0 +1,127 @@
+package com.diagbot.util;
+
+import com.google.common.collect.Lists;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+/**
+ * @description: 反射工具类
+ * @author: zhoutg
+ * @date: 2021/4/27 13:29
+ */
+public class ReflectUtil {
+
+    /**
+     * 获取属性对应的值,以list形式返回
+     *
+     * @param list
+     * @param propertyName
+     * @param <T>
+     * @param <V>
+     * @return
+     */
+    public static <T, V> List<V> getPropertyList(List<T> list, String propertyName) {
+        List<V> res = Lists.newArrayList();
+        if (ListUtil.isEmpty(list)) {
+            return res;
+        }
+        for (T t : list) {
+            try {
+                V val = getProperty(t, propertyName);
+                if (val != null) {
+                    // 字符串类型不为空判断
+                    if (StringUtil.isNotBlank(String.valueOf(val))) {
+                        res.add(val);
+                    }
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return res;
+    }
+
+    /**
+     * 获取属性对应的值,以list形式返回——重载
+     *
+     * @param list
+     * @param <T>
+     * @return
+     */
+    public static <T> List<String> getPropertyList(List<T> list) {
+        return getPropertyList(list, "name");
+    }
+
+    /**
+     * 循环向上转型, 获取对象的 DeclaredField
+     *
+     * @param object    : 子类对象
+     * @param fieldName : 父类中的属性名
+     * @return 父类中的属性对象
+     */
+    public static Field getDeclaredField(Object object, String fieldName) {
+        Field field = null;
+        Class<?> clazz = object.getClass();
+        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
+            try {
+                field = clazz.getDeclaredField(fieldName);
+                return field;
+            } catch (Exception e) {
+                //这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。
+                //如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了
+            }
+        }
+        return null;
+    }
+
+    /**
+     * 获取对象的属性值,直接使用getDeclaredFields()方法只能获取当前类声明的字段,需要递归向上获取
+     *
+     * @param object    : 子类对象
+     * @param fieldName : 父类中的属性名
+     * @return : 父类中的属性值
+     */
+    public static <T> T getProperty(Object object, String fieldName) {
+        try {
+            //根据 对象和属性名通过反射获取Field对象
+            Field field = getDeclaredField(object, fieldName);
+            // 容错处理
+            if (field == null) {
+                return null;
+            }
+            //抑制Java对其的检查
+            field.setAccessible(true);
+            //获取 object 中 field 所代表的属性值
+            return (T) field.get(object);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    /**
+     * 对象赋值
+     *
+     * @param object
+     * @param property
+     * @param value
+     */
+    public static void setFieldValue(Object object, String property, Object value){
+        //根据 对象和属性名通过反射 调用上面的方法获取 Field对象
+        Field field = getDeclaredField(object, property) ;
+        //抑制Java对其的检查
+        field.setAccessible(true) ;
+        try {
+            field.set(object, value);
+        } catch (IllegalArgumentException e) {
+            e.printStackTrace();
+        } catch (IllegalAccessException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void main(String[] args) {
+
+    }
+}

+ 5 - 0
cdssman-service/src/main/java/com/diagbot/vo/KlDrugRegisterPageVO.java

@@ -91,4 +91,9 @@ public class KlDrugRegisterPageVO extends Page {
      */
     private String commonName;
 
+    /**
+     * 药品类别
+     */
+    private String drugType;
+
 }

+ 71 - 0
cdssman-service/src/main/java/com/diagbot/vo/KlDrugRegisterTestVO.java

@@ -0,0 +1,71 @@
+package com.diagbot.vo;
+
+import cn.afterturn.easypoi.excel.annotation.Excel;
+import com.diagbot.entity.Ex;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * <p>
+ * 药品注册表
+ * </p>
+ *
+ * @author zhoutg
+ * @since 2021-05-11
+ */
+@Data
+public class KlDrugRegisterTestVO implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Ex(name="药品代码")
+    @Excel(name="药品代码")
+    private String drugCode;
+
+    @Ex(name="注册名称")
+    private String name;
+
+    @Ex(name="英文名称")
+    private String enName;
+
+    @Ex(name="商品名称")
+    private String tradeName;
+
+    @Ex(name="注册剂型")
+    private String form;
+
+    @Ex(name="注册规格")
+    private String specification;
+
+    @Ex(name="最小包装数量")
+    @Excel(name="最小包装数量")
+    private Integer minPackQuantity;
+
+    @Ex(name="最小包装单位")
+    private String minPackUnit;
+
+    @Ex(name="药品企业")
+    private String company;
+
+    @Ex(name="批准文号")
+    private String approval;
+
+    @Ex(name="药品本位码")
+    private String standardCode;
+
+    @Ex(name="甲乙类")
+    @Excel(name="甲乙类")
+    private String insuranceType;
+
+    @Ex(name="医保备注")
+    @Excel(name="医保备注")
+    private String insuranceRemrk;
+
+    @Ex(name="药品类别")
+    private String drugType;
+
+    @Ex(name="药品通用名")
+    private String drugName;
+
+}