|
@@ -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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|