You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

223 lines
12 KiB

  1. using Learun.DataBase.Repository;
  2. using Learun.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Learun.Application.Base.SystemModule
  9. {
  10. /// <summary>
  11. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  12. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  13. /// 创 建:超级管理员
  14. /// 日 期:2017-12-19 12:03
  15. /// 描 述:系统数据初始化
  16. /// </summary>
  17. public class DatabaseInitService : RepositoryFactory
  18. {
  19. /// <summary>
  20. /// 系统数据初始化
  21. /// <summary>
  22. /// <returns></returns>
  23. public void InitDatabase()
  24. {
  25. try
  26. {
  27. //备份数据库;清除系统所有业务数据,只保留system账号,admin账号、角色数据等信息;
  28. //文件存放路径格式:/Resource/DataBaseBackup/{date}/{file}_{time}.{后缀名}
  29. var pathoffirst = Config.GetValue("dbbackuppath");
  30. string pathofdate = DateTime.Now.ToString("yyyyMMdd");
  31. string pathoftime = DateTime.Now.ToString("HHmmss") + CommonHelper.RndNum(6);
  32. string FileEextension = ".bak";
  33. var databaseName1 = this.BaseRepository("BaseDb").getDbConnection().Database;
  34. var databaseName2 = this.BaseRepository("CollegeMIS").getDbConnection().Database;
  35. var directoryPath = $"{pathoffirst}/{pathofdate}";
  36. var filePath1 = $"{pathoffirst}/{pathofdate}/{databaseName1 + "_" + pathoftime}{FileEextension}";
  37. var filePath2 = $"{pathoffirst}/{pathofdate}/{databaseName2 + "_" + pathoftime}{FileEextension}";
  38. //创建文件夹
  39. if (!Directory.Exists(directoryPath))
  40. {
  41. Directory.CreateDirectory(directoryPath);
  42. }
  43. //备份数据库
  44. string backupSql1 = "backup database " + databaseName1 + " to disk='" + filePath1 + "' with format;";
  45. this.BaseRepository("BaseDb").ExecuteAsyncBySql(backupSql1);
  46. string backupSql2 = "backup database " + databaseName2 + " to disk='" + filePath2 + "' with format;";
  47. this.BaseRepository("CollegeMIS").ExecuteAsyncBySql(backupSql2);
  48. //清除触发器
  49. if (true)
  50. {
  51. var strSql1 = new StringBuilder();
  52. strSql1.Append(@"select a.name,a.id,a.xtype,a.parent_obj,b.name as pname,b.id as pid,b.xtype as pxtype,b.type as ptype,
  53. 'drop trigger ' + a.name + ';' as sql
  54. from sysobjects a
  55. left join sysobjects b on a.parent_obj = b.id
  56. where a.xtype = 'TR'
  57. and b.name not like '%sys%' and b.name not like '%dt%'
  58. order by b.name
  59. ; ");
  60. //adms7ultimate2表
  61. var sqlList = this.BaseRepository().FindList<SysobjectModel>(strSql1.ToString()).Select(x=>x.sql);
  62. //清除
  63. if (sqlList.Any())
  64. {
  65. foreach (var item in sqlList)
  66. {
  67. this.BaseRepository().ExecuteBySql(item);
  68. }
  69. }
  70. //CollegeMIS表
  71. var sqlList2 = this.BaseRepository("CollegeMIS").FindList<SysobjectModel>(strSql1.ToString()).Select(x => x.sql);
  72. //清除
  73. if (sqlList2.Any())
  74. {
  75. foreach (var item in sqlList2)
  76. {
  77. this.BaseRepository("CollegeMIS").ExecuteBySql(item);
  78. }
  79. }
  80. }
  81. //清除约束
  82. if (true)
  83. {
  84. var strSql1 = new StringBuilder();
  85. strSql1.Append(@"select a.name,a.id,a.xtype,a.parent_obj,b.name as pname,b.id as pid,b.xtype as pxtype,b.type as ptype,
  86. 'alter table '+b.name+' drop constraint '+a.name+';' as sql
  87. from sysobjects a
  88. left join sysobjects b on a.parent_obj=b.id
  89. where a.xtype in ('F','C','D','UQ')
  90. and b.name not like '%sys%' and b.name not like '%dt%'
  91. order by b.name
  92. ;");
  93. //adms7ultimate2表
  94. var sqlList = this.BaseRepository().FindList<SysobjectModel>(strSql1.ToString()).Select(x => x.sql);
  95. //清除
  96. if (sqlList.Any())
  97. {
  98. foreach (var item in sqlList)
  99. {
  100. this.BaseRepository().ExecuteBySql(item);
  101. }
  102. }
  103. //CollegeMIS表
  104. var sqlList2 = this.BaseRepository("CollegeMIS").FindList<SysobjectModel>(strSql1.ToString()).Select(x => x.sql);
  105. //清除
  106. if (sqlList2.Any())
  107. {
  108. foreach (var item in sqlList2)
  109. {
  110. this.BaseRepository("CollegeMIS").ExecuteBySql(item);
  111. }
  112. }
  113. }
  114. //清除业务数据(当表有触发器和约束时,会执行异常!错误信息为:“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”)
  115. if (true)
  116. {
  117. var strSql1 = new StringBuilder();
  118. strSql1.Append("declare @trun_name varchar(50) ");
  119. strSql1.Append("declare name_cursor cursor for ");
  120. strSql1.Append("select 'truncate table ' + name+';' from sysobjects where xtype='U' and name <> 'LR_Base_User' and name <> 'LR_Base_Role' and name<>'LR_App_Function' and name<>'LR_Base_Authorize' and name<>'LR_Base_DatabaseLink' ");
  121. strSql1.Append(" and name<>'LR_Base_DataCondition' and name<>'LR_Base_DataItem' and name<>'LR_Base_DataItemDetail' and name<>'LR_Base_DataRelation' and name<>'LR_Base_DataSource' and name<>'LR_Base_Area' ");
  122. strSql1.Append(" and name<>'LR_Base_DbField' and name<>'LR_Base_Interface' and name<>'LR_Base_Logo' and name<>'LR_Base_Module' and name<>'LR_Base_ModuleButton' and name<>'LR_Base_ModuleColumn' ");
  123. strSql1.Append(" and name<>'LR_Base_ModuleForm' and name<>'LR_Base_UserRelation' and name<>'LR_DT_Chart' and name<>'LR_DT_List' and name<>'LR_DT_Target' and name<>'LR_Excel_Export' and name<>'LR_Excel_Import' ");
  124. strSql1.Append(" and name<>'LR_Excel_ImportFileds' and name<>'LR_Form_Relation' and name<>'LR_Form_Scheme' and name<>'LR_Form_SchemeInfo' and name<>'LR_Lg_Map' and name<>'LR_Lg_Type' and name<>'LR_NWF_Scheme' ");
  125. strSql1.Append(" and name<>'LR_NWF_SchemeAuth' and name<>'LR_NWF_SchemeInfo' and name <>'LR_Base_ThematicManagement' and name <>'LoginModel' and name <>'LR_MS_StrategyInfo' and name <>'Sys_QRCodeInLogin' and name <>'Sys_UpdateRecord' and name <>'TeachSwitch'");
  126. strSql1.Append("open name_cursor ");
  127. strSql1.Append("fetch next from name_cursor into @trun_name ");
  128. strSql1.Append("while @@FETCH_STATUS = 0 ");
  129. strSql1.Append("begin exec (@trun_name) fetch next from name_cursor into @trun_name end ");
  130. strSql1.Append("close name_cursor ");
  131. strSql1.Append("deallocate name_cursor ");
  132. this.BaseRepository().ExecuteBySql(strSql1.ToString());
  133. var strSql2 = new StringBuilder();
  134. strSql2.Append("delete from LR_Base_User where F_Account <> 'System' and F_Account <> 'admin';");
  135. strSql2.Append("delete from LR_Base_Authorize where F_ObjectId <> 'd61e1853-cdee-4d49-a5e1-e230f1098e52';");
  136. strSql2.Append("delete from LR_Base_UserRelation where F_UserId <> '9f1a9d12-f4c2-4c28-9c60-5491090084ed';");
  137. this.BaseRepository().ExecuteBySql(strSql2.ToString());
  138. }
  139. if (true)
  140. {
  141. var strSql1 = new StringBuilder();
  142. strSql1.Append("declare @trun_name varchar(50) ");
  143. strSql1.Append("declare name_cursor cursor for ");
  144. strSql1.Append("select 'delete from ' + name+';' from sysobjects where xtype='U' and name<>'DIC_AREA' and name<>'DIC_CITY' and name<>'DIC_PROVINCE' and name<>'BCdNationality' and name<>'BCdFamilyOrigin' and name<>'BCdPartyFace' and name<>'BCdSubjectSpecies' and name<>'BCdForeignLanguage' and name<>'BCdTitleOfTechPost' and name<>'BCdDegree' and name<>'BCdCultureDegree' and name<>'BCdOverseasChinese' and name<>'BCdHealthStatus' and name<>'BCdAbnormityMoveReason' and name<>'BCdAbnormitySort' and name<>'BCdEncourgeLevel' and name<>'BCdEncourgeSort' and name<>'BCdMatriculateSort' and name<>'BCdPlanForm' and name<>'BCdPunish' and name<>'BCdTestMode' and name<>'BCdTestStuSort' ");
  145. strSql1.Append("open name_cursor ");
  146. strSql1.Append("fetch next from name_cursor into @trun_name ");
  147. strSql1.Append("while @@FETCH_STATUS = 0 ");
  148. strSql1.Append("begin exec (@trun_name) fetch next from name_cursor into @trun_name end ");
  149. strSql1.Append("close name_cursor ");
  150. strSql1.Append("deallocate name_cursor ");
  151. this.BaseRepository("CollegeMIS").ExecuteBySql(strSql1.ToString());
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. if (ex is ExceptionEx)
  157. {
  158. throw;
  159. }
  160. else
  161. {
  162. throw ExceptionEx.ThrowServiceException(ex);
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 视图临时模型
  168. /// </summary>
  169. public class SysobjectModel {
  170. /// <summary>
  171. /// 触发器、约束名称
  172. /// </summary>
  173. public string name { get; set; }
  174. /// <summary>
  175. /// 触发器、约束id
  176. /// </summary>
  177. public string id { get; set; }
  178. /// <summary>
  179. /// 类型
  180. /// </summary>
  181. public string xtype { get; set; }
  182. /// <summary>
  183. /// 触发器、约束所属表id
  184. /// </summary>
  185. public string parent_obj { get; set; }
  186. /// <summary>
  187. /// 触发器、约束所属表名
  188. /// </summary>
  189. public string pname { get; set; }
  190. /// <summary>
  191. /// 表id
  192. /// </summary>
  193. public string pid { get; set; }
  194. /// <summary>
  195. /// 表类型
  196. /// </summary>
  197. public string pxtype { get; set; }
  198. /// <summary>
  199. /// 表类型
  200. /// </summary>
  201. public string ptype { get; set; }
  202. /// <summary>
  203. /// 删除语句
  204. /// </summary>
  205. public string sql { get; set; }
  206. }
  207. }
  208. }