package com.dj.service.sys.impl;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dj.dao.base.BaseDaoI;
import com.dj.model.sys.Tauth;
import com.dj.model.sys.Tbug;
import com.dj.model.sys.Tdept;
import com.dj.model.sys.Tmenu;
import com.dj.model.sys.Tonline;
import com.dj.model.sys.Trole;
import com.dj.model.sys.Troletauth;
import com.dj.model.sys.Tuser;
import com.dj.model.sys.Tusertrole;
import com.dj.service.base.impl.BaseServiceImpl;
import com.dj.service.sys.RepairServiceI;
import com.dj.util.base.Encrypt;
@Service("repairService")
public class RepairServiceImpl extends BaseServiceImpl implements RepairServiceI {
private BaseDaoI<Tbug> bugDao;
private BaseDaoI<Tuser> userDao;
private BaseDaoI<Tdept> deptDao;
private BaseDaoI<Tmenu> menuDao;
private BaseDaoI<Tonline> onlineDao;
private BaseDaoI<Tauth> authDao;
private BaseDaoI<Trole> roleDao;
private BaseDaoI<Tusertrole> userroleDao;
private BaseDaoI<Troletauth> roleauthDao;
public BaseDaoI<Tbug> getBugDao() {
return bugDao;
}
@Autowired
public void setBugDao(BaseDaoI<Tbug> bugDao) {
this.bugDao = bugDao;
}
public BaseDaoI<Troletauth> getRoleauthDao() {
return roleauthDao;
}
@Autowired
public void setRoleauthDao(BaseDaoI<Troletauth> roleauthDao) {
this.roleauthDao = roleauthDao;
}
public BaseDaoI<Tusertrole> getUserroleDao() {
return userroleDao;
}
@Autowired
public void setUserroleDao(BaseDaoI<Tusertrole> userroleDao) {
this.userroleDao = userroleDao;
}
public BaseDaoI<Trole> getRoleDao() {
return roleDao;
}
@Autowired
public void setRoleDao(BaseDaoI<Trole> roleDao) {
this.roleDao = roleDao;
}
public BaseDaoI<Tauth> getAuthDao() {
return authDao;
}
@Autowired
public void setAuthDao(BaseDaoI<Tauth> authDao) {
this.authDao = authDao;
}
public BaseDaoI<Tonline> getOnlineDao() {
return onlineDao;
}
@Autowired
public void setOnlineDao(BaseDaoI<Tonline> onlineDao) {
this.onlineDao = onlineDao;
}
public BaseDaoI<Tmenu> getMenuDao() {
return menuDao;
}
@Autowired
public void setMenuDao(BaseDaoI<Tmenu> menuDao) {
this.menuDao = menuDao;
}
public BaseDaoI<Tuser> getUserDao() {
return userDao;
}
@Autowired
public void setUserDao(BaseDaoI<Tuser> userDao) {
this.userDao = userDao;
}
public BaseDaoI<Tdept> getDeptDao() {
return deptDao;
}
@Autowired
public void setDeptDao(BaseDaoI<Tdept> deptDao) {
this.deptDao = deptDao;
}
synchronized public void deleteAndRepair() {
bugDao.executeHql("delete Tbug");
onlineDao.executeHql("delete Tonline");
menuDao.executeHql("update Tmenu t set t.tmenu = null");
menuDao.executeHql("delete Tmenu");
roleauthDao.executeHql("delete Troletauth");
userroleDao.executeHql("delete Tusertrole");
authDao.executeHql("update Tauth t set t.tauth = null");
authDao.executeHql("delete Tauth");
roleDao.executeHql("delete Trole");
userDao.executeHql("delete Tuser");
repair();
}
synchronized public void repair() {
repairMenu();// 修复菜单
repairAuth();// 修复权限
repairRole();// 修复角色
repairDept();// 修复用户
repairUser();// 修复用户
repairRoleAuth();// 修复角色和权限的关系
repairUserRole();// 修复用户和角色的关系
}
private void repairUserRole() {
userroleDao.executeHql("delete Tusertrole t where t.tuser.cid in ( '0' )");
Tusertrole userrole = new Tusertrole();
userrole.setCid(UUID.randomUUID().toString());
userrole.setTrole(roleDao.get(Trole.class, "0"));
userrole.setTuser(userDao.get(Tuser.class, "0"));
userroleDao.save(userrole);
}
private void repairRoleAuth() {
roleauthDao.executeHql("delete Troletauth t where t.trole.cid = '0'");
Trole role = roleDao.get(Trole.class, "0");
List<Tauth> auths = authDao.find("from Tauth");
if (auths != null && auths.size() > 0) {
for (Tauth auth : auths) {
Troletauth roleauth = new Troletauth();
roleauth.setCid(UUID.randomUUID().toString());
roleauth.setTrole(role);
roleauth.setTauth(auth);
roleauthDao.save(roleauth);
}
}
}
private void repairRole() {
Trole admin = new Trole();
admin.setCid("0");
admin.setCname("超级管理员");
admin.setCdesc("拥有系统所有权限");
roleDao.saveOrUpdate(admin);
Trole guest = new Trole();
guest.setCid("1");
guest.setCname("Guest");
guest.setCdesc("");
roleDao.saveOrUpdate(guest);
}
private void repairAuth() {
authDao.executeHql("update Tauth a set a.tauth = null");
Tauth sshe = new Tauth();
sshe.setCid("0");
sshe.setTauth(null);
sshe.setCname("首页");
sshe.setCurl("");
sshe.setCseq(BigDecimal.valueOf(1));
sshe.setCdesc("基础开发平台");
authDao.saveOrUpdate(sshe);
Tauth sjkgl = new Tauth();
sjkgl.setCid("sjkgl");
sjkgl.setTauth(sshe);
sjkgl.setCname("数据库管理");
sjkgl.setCurl("");
sjkgl.setCseq(BigDecimal.valueOf(1));
sjkgl.setCdesc("可查看数据库链接信息,SQL语句执行情况");
authDao.saveOrUpdate(sjkgl);
Tauth ljcjk = new Tauth();
ljcjk.setCid("ljcjk");
ljcjk.setTauth(sjkgl);
ljcjk.setCname("连接池监控");
ljcjk.setCurl("/datasource!druid.do");
ljcjk.setCseq(BigDecimal.valueOf(1));
ljcjk.setCdesc("可查看数据库链接信息");
authDao.saveOrUpdate(ljcjk);
Tauth xtgl = new Tauth();
xtgl.setCid("xtgl");
xtgl.setTauth(sshe);
xtgl.setCname("系统管理");
xtgl.setCurl("");
xtgl.setCseq(BigDecimal.valueOf(2));
xtgl.setCdesc("");
authDao.saveOrUpdate(xtgl);
Tauth yhgl = new Tauth();
yhgl.setCid("yhgl");
yhgl.setTauth(xtgl);
yhgl.setCname("用户管理");
yhgl.setCurl("");
yhgl.setCseq(BigDecimal.valueOf(1));
yhgl.setCdesc("");
authDao.saveOrUpdate(yhgl);
Tauth yhglym = new Tauth();
yhglym.setCid("yhglym");
yhglym.setTauth(yhgl);
yhglym.setCname("用户管理页面");
yhglym.setCurl("/user!user.do");
yhglym.setCseq(BigDecimal.valueOf(1));
yhglym.setCdesc("");
authDao.saveOrUpdate(yhglym);
Tauth yhcx = new Tauth();
yhcx.setCid("yhcx");
yhcx.setTauth(yhgl);
yhcx.setCname("用户查询");
yhcx.setCurl("/user!datagrid.do");
yhcx.setCseq(BigDecimal.valueOf(2));
yhcx.setCdesc("");
authDao.saveOrUpdate(yhcx);
Tauth yhaddym = new Tauth();
yhaddym.setCid("yhaddym");
yhaddym.setTauth(yhgl);
yhaddym.setCname("添加用户页面");
yhaddym.setCurl("/user!userAdd.do");
yhaddym.setCseq(BigDecimal.valueOf(3));
yhaddym.setCdesc("");
authDao.saveOrUpdate(yhaddym);
Tauth yhadd = new Tauth();
yhadd.setCid("yhadd");
yhadd.setTauth(yhgl);
yhadd.setCname("用户添加");
yhadd.setCurl("/user!add.do");
yhadd.setCseq(BigDecimal.valueOf(4));
yhadd.setCdesc("");
authDao.saveOrUpdate(yhadd);
Tauth yheditym = new Tauth();
yheditym.setCid("yheditym");
yheditym.setTauth(yhgl);
yheditym.setCname("修改用户页面");
yheditym.setCurl("/user!userEdit.do");
yheditym.setCseq(BigDecimal.valueOf(5));
yheditym.setCdesc("");
authDao.saveOrUpdate(yheditym);
Tauth yhedit = new Tauth();
yhedit.setCid("yhedit");
yhedit.setTauth(yhgl);
yhedit.setCname("用户修改");
yhedit.setCurl("/user!edit.do");
yhedit.setCseq(BigDecimal.valueOf(6));
yhedit.setCdesc("");
authDao.saveOrUpdate(yhedit);
Tauth yhsc = new Tauth();
yhsc.setCid("yhsc");
yhsc.setTauth(yhgl);
yhsc.setCname("用户删除");
yhsc.setCurl("/user!delete.do");
yhsc.setCseq(BigDecimal.valueOf(7));
yhsc.setCdesc("");
authDao.saveOrUpdate(yhsc);
Tauth yhxgjsym = new Tauth();
yhxgjsym.setCid("yhxgjsym");
yhxgjsym.setTauth(yhgl);