懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
提交 | 用户 | 时间
1ac2bc 1 package cn.stylefeng.guns.gen.modular.service;
2
3 import cn.stylefeng.guns.base.db.entity.DatabaseInfo;
4 import cn.stylefeng.guns.base.db.model.TableFieldInfo;
5 import cn.stylefeng.guns.base.db.service.DatabaseInfoService;
6 import cn.stylefeng.guns.base.db.util.DbUtil;
7 import cn.stylefeng.guns.gen.core.enums.GenSessionKeyFlags;
8 import cn.stylefeng.guns.gen.modular.model.FieldConfig;
9 import cn.stylefeng.guns.gen.modular.model.GenSessionFieldConfigs;
10 import cn.stylefeng.guns.gen.modular.model.params.SaveFieldConfigParam;
11 import cn.stylefeng.roses.core.util.HttpContext;
12 import cn.stylefeng.roses.core.util.SpringContextHolder;
13 import cn.stylefeng.roses.core.util.ToolUtil;
14 import cn.stylefeng.roses.kernel.model.exception.ServiceException;
15 import cn.stylefeng.roses.kernel.validator.stereotype.ParamValidator;
16 import org.springframework.stereotype.Service;
17
18 import javax.servlet.http.HttpSession;
19 import java.util.List;
20 import java.util.stream.Collectors;
21
22
23 /**
24  * 代码生成相关逻辑
25  *
26  * @author fengshuonan
27  * @date 2019-05-06-19:04
28  */
29 @Service
30 public class GenerateService {
31
32     /**
33      * 获取某个db容器下,某个表的字段配置
34      * <p>
35      * 如果之前设置过,则从缓存中拿出
36      *
37      * @author fengshuonan
38      * @Date 2020/1/23 10:54 下午
39      */
40     public List<FieldConfig> getTableFieldsConfig(Long dbId, String tableName) {
41
42         //查询session中有无已配置的字段数据,有配置过的直接返回
43         HttpSession session = HttpContext.getRequest().getSession();
44         GenSessionFieldConfigs sessionFieldConfigs = (GenSessionFieldConfigs) session.getAttribute(GenSessionKeyFlags.TABLE_FIELD_STYLES.name());
45         if (sessionFieldConfigs != null && sessionFieldConfigs.containConfigFlag(tableName)) {
46             return sessionFieldConfigs.getFieldConfigs(tableName);
47         }
48
49         //查找数据库元数据信息
50         DatabaseInfoService databaseInfoService = null;
51         try {
52             databaseInfoService = SpringContextHolder.getBean(DatabaseInfoService.class);
53         } catch (Exception e) {
54             throw new ServiceException(500, "请先开启数据源容器模块!");
55         }
56         DatabaseInfo databaseInfo = databaseInfoService.getById(dbId);
57
58         //获取对应表的所有字段
59         List<TableFieldInfo> tableFields = DbUtil.getTableFields(databaseInfo, tableName);
60
61         //将表的所有字段信息转化为配置信息
62         return tableFields.stream().map(i -> {
63             FieldConfig fieldConfig = new FieldConfig();
64             ToolUtil.copyProperties(i, fieldConfig);
65             return fieldConfig;
66         }).collect(Collectors.toList());
67     }
68
69     /**
70      * 设置表的字段配置
71      *
72      * @author fengshuonan
73      * @Date 2020/1/23 11:04 下午
74      */
75     @ParamValidator
76     public void setTableFieldsConfig(SaveFieldConfigParam saveFieldConfigParam) {
77
78         //从session获取配置
79         HttpSession session = HttpContext.getRequest().getSession();
80         GenSessionFieldConfigs sessionFieldConfigs = (GenSessionFieldConfigs) session.getAttribute(GenSessionKeyFlags.TABLE_FIELD_STYLES.name());
81         if (sessionFieldConfigs == null) {
82             sessionFieldConfigs = new GenSessionFieldConfigs();
83         }
84
85         //放入session配置树形
86         sessionFieldConfigs.getFieldConfigs().put(
87                 saveFieldConfigParam.getTableName(),
88                 saveFieldConfigParam.getFieldConfigList());
89
90         session.setAttribute(GenSessionKeyFlags.TABLE_FIELD_STYLES.name(), sessionFieldConfigs);
91     }
92
93 }