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) ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
} 
 |