wuzeyu
2024-07-24 42beb426f466d79c55668c4d854e52613febb0da
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package com.dy.pipIrrBase.util;
 
import com.dy.common.multiDataSource.DataSourceContext;
import com.dy.common.mybatis.envm.Deleted;
import com.dy.common.mybatis.envm.Disabled;
import com.dy.common.util.ConfigXml;
import com.dy.common.util.MD5;
import com.dy.common.webListener.ConfigListener;
import com.dy.pipIrrGlobal.daoBa.BaDistrictMapper;
import com.dy.pipIrrGlobal.daoBa.BaSettingsMapper;
import com.dy.pipIrrGlobal.daoBa.BaUserMapper;
import com.dy.pipIrrGlobal.daoSe.SePaymentMethodMapper;
import com.dy.pipIrrGlobal.daoSe.SeWaterTypeMapper;
import com.dy.pipIrrGlobal.pojoBa.BaDistrict;
import com.dy.pipIrrGlobal.pojoBa.BaSettings;
import com.dy.pipIrrGlobal.pojoBa.BaUser;
import com.dy.pipIrrGlobal.pojoSe.SePaymentMethod;
import com.dy.pipIrrGlobal.pojoSe.SeWaterType;
import com.dy.pipIrrGlobal.util.DistrictLevel;
import org.jdom2.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
 
import java.net.URL;
 
/**
 * 监听器,实现功能:在系统启动时初始化,向数据库中插入数据
 * 本监听器不能采用ServletContextListener方式,因为Servlet上下文Context创建后
 * Spring容器并没有创建完,而本类中用了Spring容器中的Bean,即distriDao, userDao。
 * 所以采用了Spring事件监听器来实现
 */
@Component
public class InitListener implements ApplicationListener<ApplicationReadyEvent> {
 
    private BaDistrictMapper distriDao ;
    private BaUserMapper userDao ;
    private SePaymentMethodMapper paymentMethodDao ;
    private BaSettingsMapper settingsDao ;
    private SeWaterTypeMapper waterTypeDao ;
 
    @Autowired
    public void setDistriDao(BaDistrictMapper distriDao){
        this.distriDao = distriDao ;
    }
 
    @Autowired
    public void setUserDao(BaUserMapper userDao){
        this.userDao = userDao ;
    }
 
    @Autowired
    public void setPaymentDao(SePaymentMethodMapper paymentMethodDao){
        this.paymentMethodDao = paymentMethodDao ;
    }
 
    @Autowired
    public void setSettingsDao(BaSettingsMapper settingsDao){
        this.settingsDao = settingsDao ;
    }
 
    @Autowired
    public void setWaterTypeDao(SeWaterTypeMapper waterTypeDao){
        this.waterTypeDao = waterTypeDao ;
    }
 
 
    /**
     * SpringBoot容器已经准备好了
     * @param event 事件
     */
    @Override
    public void onApplicationEvent(@NonNull ApplicationReadyEvent event) {
        try {
            //等1秒,等待com.alibaba.druid.pool.DruidDataSource实始化完成
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            this.init(event);
        }
    }
 
    /**
     * 实始化
     */
    @SuppressWarnings("unused ")
    private void init(ApplicationReadyEvent event){
        try {
            URL configFileURL = ConfigListener.class.getResource("/init-config.xml" );
            ConfigXml configXml = new ConfigXml() ;
            Document doc = configXml.createDom(configFileURL) ;
            this.doInit(configXml, doc);
        } catch (Exception e) {
            System.out.println("系统启动时,初始化配置出错 !");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    private void doInit(ConfigXml configXml, Document doc) throws Exception{
        if(configXml != null && doc != null){
            for(int num = 1; num <= 10000; num++){
                if(configXml.existElement(doc, "config.orgs.org" + num)){
                    String orgTag = configXml.getSetAttrTxt(doc, "config.orgs.org" + num,"tag", null, false, null) ;
                    //设置数据源
                    DataSourceContext.set(orgTag);
                    if(!this.existDistricts()){
                        if(configXml.existElement(doc, "config.orgs.org" + num + ".districts.province")){
                            String distriName = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".districts.province","name", null, false, null) ;
                            String distriNum = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".districts.province","num", null, false, null) ;
                            Integer distriLevel = configXml.getSetAttrPlusInt(doc, "config.orgs.org" + num + ".districts.province","level", null, 1, 99,null) ;
                            Long supperId = this.saveDistrict(null, distriName, distriNum, distriLevel);
                            if(supperId != null && configXml.existElement(doc, "config.orgs.org" + num + ".districts.province.city")){
                                distriName = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".districts.province.city","name", null, false, null) ;
                                distriNum = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".districts.province.city","num", null, false, null) ;
                                distriLevel = configXml.getSetAttrPlusInt(doc, "config.orgs.org" + num + ".districts.province.city","level", null, 1, 99,null) ;
                                supperId = this.saveDistrict(supperId, distriName, distriNum, distriLevel);
                                if(supperId != null && configXml.existElement(doc, "config.orgs.org" + num + ".districts.province.city.country")) {
                                    distriName = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".districts.province.city.country", "name", null, false, null);
                                    distriNum = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".districts.province.city.country", "num", null, false, null);
                                    distriLevel = configXml.getSetAttrPlusInt(doc, "config.orgs.org" + num + ".districts.province.city.country", "level", null, 1, 99, null);
                                    this.saveDistrict(supperId, distriName, distriNum, distriLevel);
                                }
                            }
                        }
                    }
                    if(!this.existUsers()){
                        if(configXml.existElement(doc, "config.orgs.org" + num + ".user")){
                            String name = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".user","name", null, false, null) ;
                            String phone = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".user","phone", null, false, null) ;
                            String password = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".user","password", null, false, null) ;
                            Integer supperAdmin = configXml.getSetAttrPlusInt(doc, "config.orgs.org" + num + ".user","supperAdmin", null, 0, 1,null) ;
                            this.saveUser(orgTag, name, phone, password, supperAdmin);
                        }
                    }
                    if(!this.existPayments()){
                        if(configXml.existElement(doc, "config.orgs.org" + num + ".payments")){
                            for(int i = 1 ; i < 10000; i++){
                                if(configXml.existElement(doc, "config.orgs.org" + num + ".payments.item" + i)){
                                    String name = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".payments.item" + i,"name", null, false, null) ;
                                    this.savePayment(orgTag, name);
                                }else{
                                    break ;
                                }
                            }
                        }
                    }
                    if(!this.existSettings()){
                        if(configXml.existElement(doc, "config.orgs.org" + num + ".settings")){
                            for(int i = 1 ; i < 10000; i++){
                                if(configXml.existElement(doc, "config.orgs.org" + num + ".settings.item" + i)){
                                    String item_name = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".settings.item" + i,"item_name", null, false, null) ;
                                    String item_value = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".settings.item" + i,"item_value", null, false, null) ;
                                    String remarks = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".settings.item" + i,"remarks", null, false, null) ;
                                    this.saveSettings(orgTag, item_name, item_value, remarks);
                                }else{
                                    break ;
                                }
                            }
                        }
                    }
                    if(!this.existWaterTypes()){
                        if(configXml.existElement(doc, "config.orgs.org" + num + ".waterTypes")){
                            for(int i = 1 ; i < 10000; i++){
                                if(configXml.existElement(doc, "config.orgs.org" + num + ".waterTypes.item" + i)){
                                    String typeName = configXml.getSetAttrTxt(doc, "config.orgs.org" + num + ".waterTypes.item" + i,"typeName", null, false, null) ;
                                    this.saveWaterType(orgTag, typeName);
                                }else{
                                    break ;
                                }
                            }
                        }
                    }
                }else{
                    break ;
                }
            }
        }
    }
 
    /**
     * 数据库中是否存在行政区划数据
     * @return 存在否
     */
    private boolean existDistricts(){
        Long total = this.distriDao.selectCount(null) ;
        return (total != null && total > 0) ;
    }
 
    /**
     * 数据库中是否存在用户数据
     * @return 存在否
     */
    private boolean existUsers(){
        Long total = this.userDao.selectCount(null) ;
        return (total != null && total > 0) ;
    }
 
    /**
     * 数据库中是否存在支付方式数据
     * @return 存在否
     */
    private boolean existPayments(){
        Long total = this.paymentMethodDao.selectCount(null) ;
        return (total != null && total > 0) ;
    }
 
    /**
     * 数据库中是否存在系统配置数据
     * @return 存在否
     */
    private boolean existSettings(){
        Long total = this.settingsDao.selectCount(null) ;
        return (total != null && total > 0) ;
    }
 
    /**
     * 数据库中是否存在用水类型数据
     * @return 存在否
     */
    private boolean existWaterTypes(){
        Long total = this.waterTypeDao.selectCount(null) ;
        return (total != null && total > 0) ;
    }
 
    /**
     * 数据库中保存行政区划实体
     * @param supperId 上级ID
     * @param name 名称
     * @param num 编号
     * @param level 级别
     * @return id
     */
    private Long saveDistrict(Long supperId, String name, String num, Integer level){
        if((name != null && !name.trim().equals("")) &&
                (num != null && !num.trim().equals("")) &&
                level != null){
            BaDistrict po = new BaDistrict() ;
            po.supperId = supperId ;
            po.name = name ;
            po.num = num ;
            po.level = DistrictLevel.get(level.byteValue()) ;
            po.deleted = Deleted.NO ;
            this.distriDao.putin(po) ;
            return po.getId() ;
        }else{
            return null ;
        }
    }
 
    /**
     * 保存用户
     * @param orgTag 机构标签
     * @param name 名称
     * @param phone 手机号
     * @param password 密码
     * @param supperAdmin 是否为超级管理员(1是,0否)
     */
    private void saveUser(String orgTag, String name, String phone, String password, Integer supperAdmin) throws Exception{
        if((name != null && !name.trim().equals("")) &&
                (phone != null && !phone.trim().equals("")) &&
                (password != null && !password.trim().equals("")) &&
                supperAdmin != null){
            BaUser po = new BaUser() ;
            po.userName = name ;
            po.phone = phone ;
            po.password = MD5.encrypt(password) ;//进行加密码 ;
            po.orgTag = orgTag ;
            po.supperAdmin = supperAdmin.byteValue() ;
            po.disabled = Disabled.NO ;
            po.deleted = Deleted.NO ;
            this.userDao.putin(po) ;
        }
    }
    /**
     * 保存支付方式
     * @param orgTag 机构标签
     * @param name 名称
     */
    private void savePayment(String orgTag, String name) throws Exception{
        if(name != null && !name.trim().equals("")){
            SePaymentMethod po = new SePaymentMethod() ;
            po.setName(name);
            po.setDeleted(Deleted.NO.code);
            this.paymentMethodDao.insert(po) ;
        }
    }
 
    /**
     * 保存系统配置
     * @param item_name 配置项
     * @param item_value 配置项值
     * @param remarks 备注信息
     * @throws Exception
     */
    private void saveSettings(String orgTag,String item_name, String item_value,String remarks) throws Exception{
        if((item_name != null && !item_name.trim().equals("")) &&
                (item_value != null && !item_value.trim().equals("")) &&
                (remarks != null && !remarks.trim().equals(""))){
            BaSettings po = new BaSettings() ;
            po.setItemName(item_name);
            po.setItemValue(item_value);
            po.setRemarks(remarks);
            this.settingsDao.insert(po) ;
        }
    }
 
 
    /**
     * 保存用水类型
     * @param orgTag 机构标签
     * @param typeName 名称
     */
    private void saveWaterType(String orgTag, String typeName) throws Exception{
        if(typeName != null && !typeName.trim().equals("")){
            SeWaterType po = new SeWaterType() ;
            po.setTypename(typeName);
            this.waterTypeDao.insert(po) ;
        }
    }
 
}