354798ggg
2023-08-29 fd0c7c174d7b7313aa49061df05350405a3c1bf1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package com.product.data.sync.service;
 
import com.alibaba.fastjson.JSONObject;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldMetaEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.permission.PermissionService;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.service.support.QueryFilterService;
import com.product.core.transfer.Transactional;
import com.product.data.sync.config.SystemCode;
import com.product.data.sync.service.ide.IFunctionSynchrService;
import com.product.data.sync.util.DataManipulationUtils;
import com.product.util.BaseDaoServiceImpl;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
 
 
/**
 * Copyright LX
 *
 * @Title: FunctionSynchrService
 * @Project: product-server
 * @date: 2021-08-30 14:19
 * @author: ZhouJie
 * @Description: 开发库功能同步
 */
@Service
public class FunctionSynchrService extends AbstractBaseService implements IFunctionSynchrService {
 
    @Autowired
    public BaseDao baseDao;
    @Autowired
    PermissionService permissionService;
    @Autowired
    QueryFilterService queryFilterService;
 
    /**
     * 获取JDBC连接
     *
     * @param fs
     * @return
     * @throws BaseException
     */
    public Connection getConnection(FieldSetEntity fs) throws BaseException, SQLException, ClassNotFoundException {
        String databaseType = fs.getString("type");//数据库类型
        String ipAddress = fs.getString("IP");//ip地址
        String databaseName = fs.getString("database_name");//数据库名称
        String portNumber = fs.getString("port");//端口号
        String userName = fs.getString("username");//用户名
        String userPassword = fs.getString("password");//密码
        String instantiation = fs.getString("");//实例名
        String url;
        String diver;
        if ("mysql".equals(databaseType)) {
            diver = "com.mysql.cj.jdbc.Driver";
            url = "jdbc:mysql://" + ipAddress + ":" + portNumber + "/" + databaseName + "?useSSL=false&serverTimezone=UTC";
        } else if ("oracle".equals(databaseType)) {
            diver = "oracle.jdbc.driver.OracleDriver";
            url = "jdbc:oracle:thin:@" + ipAddress + ":" + portNumber + ":orcl";
        } else if ("sqlserver".equals(databaseType)) {
            diver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            url = "jdbc:sqlserver://" + ipAddress + ":" + portNumber + ";DataBaseName=" + databaseName;
        } else if ("informix".equals(databaseType)) {
            diver = "com.informix.jdbc.IfxDriver";
            url = "jdbc:informix-sqli://" + ipAddress + ":" + portNumber + "/" + databaseName + ":informixserver=" + instantiation;
        } else {
            throw new BaseException(SystemCode.SYSTEM_UNKNOWN_DATABASE_TYPE.getValue(), SystemCode.SYSTEM_UNKNOWN_DATABASE_TYPE.getText());
        }
        //获取jdbc连接
        return DataManipulationUtils.getConnection(diver, url, userName, userPassword);
    }
 
    /**
     * 开发库的模块功能树
     *
     * @return
     * @throws BaseException
     */
    public DataTableEntity getSyncTree(FieldSetEntity fse) throws BaseException, SQLException, ClassNotFoundException {
        StringBuilder str = new StringBuilder();
//        str.append(" SELECT uuid,product_name `name`,tricode,null tricode_parent,1 type,concat(tricode,'_',1) only_key FROM product_sys_products ");
//        str.append(" union all select a.uuid,`name`,b.tricode,b.tricode_parent,type,only_key from ( ");
//        str.append(" select  uuid,function_name `name`,2 type,concat(tricode,'_',2) only_key FROM product_sys_functions WHERE data_type=2 ");
//        str.append(" union all select uuid,function_name `name`,3 type,concat(tricode,'_',3) only_key FROM product_sys_functions where data_type=1) a join  ");
//        str.append(""product_sys_module_function_code b on (( a.type=2 and b.data_type=1 ) or (a.type=3 and b.data_type=2)) and a.uuid=b.data_uuid ");
//        str.append(" ORDER BY type ,tricode_parent asc,tricode,length(tricode) asc ");
        str.append(" SELECT uuid,product_name `name`,tricode,null tricode_parent,1 type,concat(tricode,'_',1) only_key FROM product_sys_products ");
        str.append(" union all select a.uuid,`name`,tricode,tricode_parent,type,only_key from ( ");
        str.append(" select  uuid,tricode,tricode_parent,function_name `name`,2 type,concat(tricode,'_',2) only_key FROM product_sys_functions WHERE data_type=2 ");
        str.append(" union all select uuid,tricode,tricode_parent,function_name `name`,3 type,concat(tricode,'_',3) only_key FROM product_sys_functions where data_type=1) a ");
        str.append(" ORDER BY type ,tricode_parent asc,tricode,length(tricode) asc ");
        Connection conn = getConnection(fse);
        DataTableEntity dt = null;
        try {
            dt = BaseDaoServiceImpl.getDataTable(conn, str.toString(), new Object[]{});
        } catch (Exception e) {
            DataManipulationUtils.close(null, null, conn);
            throw e;
        }
        return dt;
    }
 
    /**
     * 当前库的模块功能树
     *
     * @return
     * @throws BaseException
     */
    public DataTableEntity getNowTree(FieldSetEntity fse) throws BaseException, SQLException, ClassNotFoundException {
        StringBuilder str = new StringBuilder();
//        str.append(" SELECT uuid,product_name `name`,tricode,null tricode_parent,1 type,concat(tricode,'_',1) only_key FROM product_sys_products ");
//        str.append(" union all select a.uuid,`name`,b.tricode,b.tricode_parent,type,only_key from ( ");
//        str.append(" select  uuid,function_name `name`,2 type,concat(tricode,'_',2) only_key FROM product_sys_functions WHERE data_type=2 ");
//        str.append(" union all select uuid,function_name `name`,3 type,concat(tricode,'_',3) only_key FROM product_sys_functions where data_type=1 ) a join  ");
//        str.append(""product_sys_module_function_code b on (( a.type=2 and b.data_type=1 ) or (a.type=3 and b.data_type=2)) and a.uuid=b.data_uuid ");
//        str.append(" ORDER BY type ,tricode_parent asc,tricode,length(tricode) asc ");
        str.append(" SELECT uuid,product_name `name`,tricode,null tricode_parent,1 type,concat(tricode,'_',1) only_key FROM product_sys_products ");
        str.append(" union all select a.uuid,`name`,tricode,tricode_parent,type,only_key from ( ");
        str.append(" select  uuid,tricode,tricode_parent,function_name `name`,2 type,concat(tricode,'_',2) only_key FROM product_sys_functions WHERE data_type=2 ");
        str.append(" union all select uuid,tricode,tricode_parent,function_name `name`,3 type,concat(tricode,'_',3) only_key FROM product_sys_functions where data_type=1) a ");
        str.append(" ORDER BY type ,tricode_parent asc,tricode,length(tricode) asc ");
        DataTableEntity dt = baseDao.listTable(str.toString(), new Object[]{});
        return dt;
    }
 
 
    public boolean verificationCode() {
        String b = baseDao.getFieldSetEntityByFilter("product_sys_datamodel_field", "table_uuid='product_sys_functions' and field_name='tricode'", new String[]{}, false).getString("field_type");
        String c = baseDao.getFieldSetEntityByFilter("product_sys_datamodel_field", "table_uuid='product_sys_products' and field_name='tricode'", new String[]{}, false).getString("field_type");
        if ( b.equals("string") && c.equals("string")) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 功能同步
     *
     * @return
     * @throws BaseException
     */
    @Transactional
    public FieldSetEntity syncFuntion(FieldSetEntity fse) throws BaseException, SQLException, ClassNotFoundException {
        DataTableEntity dt = fse.getSubDataTable("function_info");
        if (dt != null) {
            JSONObject products_uuid = new JSONObject();
            JSONObject modules_uuid = new JSONObject();
            JSONObject functions_uuid = new JSONObject();
            for (int i = 0; i < dt.getRows(); i++) {
                FieldSetEntity fsub = dt.getFieldSetEntity(i);
                if ("1".equals(fsub.getString("type"))) {
                    products_uuid.put(fsub.getString("tricode"), fsub.getString("upADD"));
                } else if ("2".equals(fsub.getString("type"))) {
                    modules_uuid.put(fsub.getString("tricode"), fsub.getString("upADD"));
                } else if ("3".equals(fsub.getString("type"))) {
                    functions_uuid.put(fsub.getString("tricode"), fsub.getString("upADD"));
                }
            }
            ArrayList<String> products_uuid_list = new ArrayList<>();
            ArrayList<String> modules_uuid_list = new ArrayList<>();
            ArrayList<String> functions_uuid_list = new ArrayList<>();
            for (String str : products_uuid.keySet()) {
 
                products_uuid_list.add(str);
            }
            for (String str : modules_uuid.keySet()) {
                modules_uuid_list.add(str);
            }
            for (String str : functions_uuid.keySet()) {
                functions_uuid_list.add(str);
            }
            Connection conn = getConnection(fse);
            DataTableEntity productsDt = null;
            if (products_uuid_list.size() > 0) {
                productsDt = BaseDaoServiceImpl.getDataTable(conn, "product_sys_products", BaseUtil.buildQuestionMarkFilter("tricode", products_uuid_list.size(), true), products_uuid_list.toArray());
            }
            DataTableEntity modulesDt = null;
            if (modules_uuid_list.size() > 0) {
                modulesDt = BaseDaoServiceImpl.getDataTable(conn, "product_sys_functions","data_type=2 and" +BaseUtil.buildQuestionMarkFilter("tricode", modules_uuid_list.size(), true), modules_uuid_list.toArray());
//                FieldMetaEntity f = new FieldMetaEntity();
//                f.setTableName(new Object[] { "product_sys_modules" });
//                modulesDt.setMeta(f);
            }
            DataTableEntity functionsDt = null;
            if (functions_uuid_list.size() > 0) {
                functionsDt = BaseDaoServiceImpl.getDataTable(conn, "product_sys_functions","data_type=1 and" + BaseUtil.buildQuestionMarkFilter("tricode", functions_uuid_list.size(), true), functions_uuid_list.toArray());
            }
            saveDataDT(productsDt, products_uuid);
            saveDataDT(modulesDt, modules_uuid);
            saveDataDT(functionsDt, functions_uuid);
 
        }
        return fse;
    }
 
    public void saveDataDT(DataTableEntity dt, JSONObject js) {
        if (dt != null) {
            for (int i = 0; i < dt.getRows(); i++) {
                FieldSetEntity fs = dt.getFieldSetEntity(i);
                if ("1".equals(js.getString(fs.getString("tricode")))) {
                    if ("行政办公".equals(fs.getString("module_name"))) {
                        System.out.println(111);
                    }
                    fs.setValue("uuid", null);
                    if ("product_sys_products".equals(fs.getTableName())) {
                        fs.setValue("product_id", null);
                        baseDao.add(fs);
                        continue;
                    } else if ("product_sys_functions".equals(fs.getTableName())) {
                        fs.setValue("function_id", null);
//                        String version_uuid = productsMap.getString(fs.getString("version_uuid"));
//                        if (null != version_uuid) {
//                            fs.setValue("product_uuid", version_uuid);
//                        }
                        baseDao.add(fs);
//                        modulesMap.put(fs.getString("tricode"), fs.getString("uuid"));
                        continue;
                    }
//                    else if ("product_sys_functions".equals(fs.getTableName())&&"1".equals(fs.getString("data_type"))) {
//                        fs.setValue("function_id", null);
//                        String tricode = fs.getString("tricode");
//                        String modules_uuid = modulesMap.getString(tricode.substring(0, tricode.length() - 4));
//                        if (null != modules_uuid) {
//                            fs.setValue("module_uuid", modules_uuid);
//                        }
//                        baseDao.add(fs);
//                        continue;
                    }
                }
//                baseDao.update(fs);
//            }
        }
    }
 
    public static void main(String[] args) {
        String code = "001-017-000";
        String substring = code.substring(0, code.length() - 4);
        System.out.println(substring);
    }
}