liurunyu
2024-04-15 55d5046ae0a9da1109e402d7d719f765a4bccdae
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
package com.dy.pmsBase.util;
 
import com.dy.common.util.ConfigXml;
import com.dy.common.util.MD5;
import com.dy.common.webListener.ConfigListener;
import com.dy.pmsGlobal.daoBa.BaUserMapper;
import com.dy.pmsGlobal.pojoBa.BaUser;
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,即userDao。
 * 所以采用了Spring事件监听器来实现
 */
@Component
public class InitListener implements ApplicationListener<ApplicationReadyEvent> {
 
    private BaUserMapper userDao ;
 
    @Autowired
    public void setUserDao(BaUserMapper userDao){
        this.userDao = userDao ;
    }
 
 
    /**
     * 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 {
            if(!this.existUsers()){
                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){
            if(configXml.existElement(doc, "config.user")){
                String name = configXml.getSetAttrTxt(doc, "config.user","name", null, false, null) ;
                String phone = configXml.getSetAttrTxt(doc, "config.user","phone", null, false, null) ;
                String password = configXml.getSetAttrTxt(doc, "config.user","password", null, false, null) ;
                Integer supperAdmin = configXml.getSetAttrPlusInt(doc, "config.user","supperAdmin", null, 0, 1,null) ;
                this.saveUser(name, phone, password, supperAdmin);
            }
        }
    }
 
 
    /**
     * 数据库中是否存在行政区划数据
     * @return 存在否
     */
    private boolean existUsers(){
        Long total = this.userDao.selectCount() ;
        return (total != null && total > 0) ;
    }
 
 
    /**
     * 保存用户
     * @param name 名称
     * @param phone 手机号
     * @param password 密码
     * @param supperAdmin 是否为超级管理员(1是,0否)
     */
    private void saveUser(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.name = name ;
            po.phone = phone ;
            po.password = MD5.encrypt(password) ;//进行加密码 ;
            po.supperAdmin = supperAdmin.byteValue() ;
            po.disabled = false ;
            po.deleted = false ;
            this.userDao.insertSelective(po) ;
        }
    }
 
}