2 ay önce
ebeveyn
işleme
ded39ef4bb

+ 0 - 13
edu-travel-common/edu-travel-common-core/src/main/java/edu/travel/App.java

@@ -1,13 +0,0 @@
-package edu.travel;
-
-/**
- * Hello world!
- *
- */
-public class App 
-{
-    public static void main( String[] args )
-    {
-        System.out.println( "Hello World!" );
-    }
-}

+ 0 - 38
edu-travel-common/edu-travel-common-core/src/test/java/edu/travel/AppTest.java

@@ -1,38 +0,0 @@
-package edu.travel;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-/**
- * Unit test for simple App.
- */
-public class AppTest 
-    extends TestCase
-{
-    /**
-     * Create the test case
-     *
-     * @param testName name of the test case
-     */
-    public AppTest( String testName )
-    {
-        super( testName );
-    }
-
-    /**
-     * @return the suite of tests being tested
-     */
-    public static Test suite()
-    {
-        return new TestSuite( AppTest.class );
-    }
-
-    /**
-     * Rigourous Test :-)
-     */
-    public void testApp()
-    {
-        assertTrue( true );
-    }
-}

+ 0 - 13
edu-travel-common/edu-travel-common-datasource/src/main/java/edu/travel/App.java

@@ -1,13 +0,0 @@
-package edu.travel;
-
-/**
- * Hello world!
- *
- */
-public class App 
-{
-    public static void main( String[] args )
-    {
-        System.out.println( "Hello World!" );
-    }
-}

+ 23 - 0
edu-travel-common/edu-travel-common-datasource/src/main/java/edu/travel/Test.java

@@ -0,0 +1,23 @@
+package edu.travel;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public final class Test{;
+    /**
+     * 自动审核通过。
+     */
+    public static final int AGREE = 1;
+    /**
+     * 自动审核未通过。
+     */
+    public static final int REJECT = 2;
+    private final static  Map<Integer, String> testMap = new HashMap<Integer, String>();
+    public Object getValue(Integer key) {
+        return testMap.get(key);
+    }
+    private Test(){
+        testMap.put(AGREE,"是");
+        testMap.put(REJECT,"否");
+    }
+}

+ 11 - 0
edu-travel-common/edu-travel-common-datasource/src/main/java/edu/travel/annotation/LinkConst.java

@@ -0,0 +1,11 @@
+package edu.travel.annotation;
+
+import java.lang.annotation.*;
+
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+@Inherited
+public @interface LinkConst {
+    Class clazz() default Object.class;
+}

+ 15 - 0
edu-travel-common/edu-travel-common-datasource/src/main/java/edu/travel/annotation/LinkMany.java

@@ -0,0 +1,15 @@
+package edu.travel.annotation;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+import java.lang.annotation.*;
+
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+@Inherited
+public @interface LinkMany {
+    String linkField() ;
+    Class linkMapper();
+    String linkPrimaryField();
+}

+ 15 - 0
edu-travel-common/edu-travel-common-datasource/src/main/java/edu/travel/annotation/LinkOne.java

@@ -0,0 +1,15 @@
+package edu.travel.annotation;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+import java.lang.annotation.*;
+
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+@Inherited
+public @interface LinkOne {
+    String linkField();
+    Class linkMapper();
+    String linkPrimaryField();
+}

+ 155 - 0
edu-travel-common/edu-travel-common-datasource/src/main/java/edu/travel/service/SysServiceImpl.java

@@ -0,0 +1,155 @@
+package edu.travel.service;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import edu.travel.annotation.LinkConst;
+import edu.travel.annotation.LinkMany;
+import edu.travel.annotation.LinkOne;
+import org.springframework.context.ApplicationContext;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SysServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,T> implements IService<T> {
+    private ApplicationContext applicationContext;
+
+    public void setApplicationContext(ApplicationContext applicationContext) {
+        this.applicationContext = applicationContext;
+    }
+
+    public T getOneLink(QueryWrapper<T> queryWrapper) {
+        T one = super.getOne(queryWrapper);
+        if (one == null) {
+            return null;
+        }
+        doOneLink(one);
+        return one;
+    }
+
+    public List<T> getListLink(QueryWrapper<T> queryWrapper) {
+        List<T> list = super.list(queryWrapper);
+        if (list == null || list.isEmpty()) {
+            return list;
+        }
+        doManyLink(list);
+        return list;
+    }
+
+    public IPage<T> getPageLink(QueryWrapper<T> queryWrapper,IPage<T> page) {
+        IPage<T> iPage = super.page(page, queryWrapper);
+        if (iPage == null ) {
+            return new Page();
+        }
+        List<T> records = iPage.getRecords();
+        doManyLink(records);
+        iPage.setRecords(records);
+        return iPage;
+    }
+
+    private void doManyLink(List<T> list) {
+        for (T many : list) {
+            doOneLink(many);
+        }
+    }
+
+    private void doOneLink(T one) {
+        Class<?> clazz = one.getClass();
+        Field[] declaredFields = clazz.getDeclaredFields();
+        for (Field field : declaredFields) {
+            field.setAccessible(true);
+            doLinkOneToOne(one, field);
+            doLinkOneToMany(one,field);
+            doConst(one,field);
+        }
+    }
+    private void doConst(T one,Field field) {
+        try {
+            LinkConst linkConst = field.getAnnotation(LinkConst.class);
+            if (linkConst != null) {
+                Class clazz = linkConst.clazz();
+                Object object = clazz.newInstance();
+                Method getValueMethod = clazz.getMethod("getValue");
+                if (getValueMethod == null) {
+                    throw new RuntimeException("没有获取到getValue方法");
+                }
+                Object getValue = getValueMethod.invoke(object, Integer.valueOf(field.get(one).toString()));
+                if (getValue == null) {
+                    throw new RuntimeException("常量字典没有这个类");
+                }
+                HashMap<String, String> objectObjectHashMap = new HashMap<>();
+                objectObjectHashMap.put(field.get(one).toString(), getValue.toString());
+                field.set(one, objectObjectHashMap);
+            }
+        }catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+    private void doLinkOneToMany(T one, Field field) {
+        try {
+            LinkMany linkMany = field.getAnnotation(LinkMany.class);
+            if (linkMany != null) {
+                String linkField = linkMany.linkField();
+                Field declaredField = one.getClass().getDeclaredField(linkField);
+                declaredField.setAccessible(true);
+                Object object = declaredField.get(one);
+                Class aClass = linkMany.linkMapper();
+                if (object != null) {
+                    BaseMapper applicationContextBean = (BaseMapper) this.applicationContext.getBean(aClass);
+                    if (applicationContextBean != null) {
+                        List<Map> oneToManyResult = applicationContextBean.selectMaps(new QueryWrapper<T>().eq(linkMany.linkPrimaryField(), object));
+                        if (oneToManyResult != null && !oneToManyResult.isEmpty()) {
+                            field.set(one,oneToManyResult);
+                        }
+                    }   else {
+                        return;
+                    }
+                }else {
+                    return;
+                }
+            }
+        }catch (Exception e) {
+            e.printStackTrace();
+            return;
+        }
+
+    }
+
+    private void doLinkOneToOne(T one,Field field) {
+        try {
+            LinkOne linkOne = field.getAnnotation(LinkOne.class);
+            if (linkOne != null) {
+                String linkField = linkOne.linkField();
+                Field declaredField = one.getClass().getDeclaredField(linkField);
+                declaredField.setAccessible(true);
+                Object object = declaredField.get(one);
+                Class aClass = linkOne.linkMapper();
+                if (object != null) {
+                    BaseMapper applicationContextBean = (BaseMapper) this.applicationContext.getBean(aClass);
+                    if (applicationContextBean != null) {
+                        List<Map> list = applicationContextBean.selectMaps(new QueryWrapper<T>().eq(linkOne.linkPrimaryField(), object));
+                        if (list != null && !list.isEmpty()) {
+                            Object oneToOneResult = list.get(0);
+                            field.set(one,oneToOneResult);
+                        }
+                    }   else {
+                        return;
+                    }
+                } else {
+                    return;
+                }
+            }
+        }catch (Exception e) {
+            e.printStackTrace();
+            return;
+        }
+
+    }
+}

+ 0 - 38
edu-travel-common/edu-travel-common-datasource/src/test/java/edu/travel/AppTest.java

@@ -1,38 +0,0 @@
-package edu.travel;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-/**
- * Unit test for simple App.
- */
-public class AppTest 
-    extends TestCase
-{
-    /**
-     * Create the test case
-     *
-     * @param testName name of the test case
-     */
-    public AppTest( String testName )
-    {
-        super( testName );
-    }
-
-    /**
-     * @return the suite of tests being tested
-     */
-    public static Test suite()
-    {
-        return new TestSuite( AppTest.class );
-    }
-
-    /**
-     * Rigourous Test :-)
-     */
-    public void testApp()
-    {
-        assertTrue( true );
-    }
-}