From 2ede1d60900fbff892d2baafebda88e6d32b3e87 Mon Sep 17 00:00:00 2001 From: dao Date: Thu, 17 Oct 2024 14:01:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=92=8C=E7=94=B0=E6=95=B0=E6=8D=AE=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/HTSchoolController.cs | 344 ++++++++++++++++++ DigitalSchoolApi/Controllers/YKTController.cs | 27 ++ DigitalSchoolApi/DigitalSchoolApi.csproj | 2 + DigitalSchoolApi/Models/HtEntity.cs | 83 +++++ DigitalSchoolApi/Web.config | 2 +- 5 files changed, 457 insertions(+), 1 deletion(-) create mode 100644 DigitalSchoolApi/Controllers/HTSchoolController.cs create mode 100644 DigitalSchoolApi/Models/HtEntity.cs diff --git a/DigitalSchoolApi/Controllers/HTSchoolController.cs b/DigitalSchoolApi/Controllers/HTSchoolController.cs new file mode 100644 index 0000000..752d89b --- /dev/null +++ b/DigitalSchoolApi/Controllers/HTSchoolController.cs @@ -0,0 +1,344 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Data.OracleClient; +using System.Data.SqlClient; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; +using System.Web; +using Dapper; +using DigitalSchoolApi.App_Data; +using DigitalSchoolApi.Models; +using Learun.Application.Organization; +using Learun.Util; +using Microsoft.AspNet.SignalR.Client; +using Newtonsoft.Json; +using Convert = System.Convert; +using DESEncrypt = Learun.Util.DESEncrypt; +using Md5Helper = Learun.Util.Md5Helper; + +namespace DigitalSchoolApi.Controllers +{ + /// + /// 塔里木学院定时需求 + /// + public class HTSchoolController + { + private readonly static string _admsConnection = ConfigurationManager.ConnectionStrings["CoreDBString"].ConnectionString; + private readonly static string _misConnection = ConfigurationManager.ConnectionStrings["ConnectionPfcMisDBString"].ConnectionString; + private static string _htMiddleConnection = ConfigurationManager.ConnectionStrings["htMiddleDBString"].ConnectionString; + + //private readonly static string _tlmMiddleConnection =ConfigurationManager.ConnectionStrings["TLMMiddleDBString"].ConnectionString; + + + + #region 中间库同步到数校 + + /// + /// 从中间库同步系部数据 + /// + public static void SyncDepartment() + { + try + { + IEnumerable entityList = null; + using (IDbConnection conn = new SqlConnection(_htMiddleConnection)) + { + entityList = conn.Query("SELECT * FROM nc_orgnization"); + } + using (IDbConnection conn = new SqlConnection(_admsConnection)) + { + try + { + var maxSort = -1; + try + { + maxSort = conn.QueryFirstOrDefault("select MAX(F_Order) FROM LR_BASE_DEPARTMENT"); + } + catch (Exception e) + { + maxSort = -1; + } + + //插入sql + foreach (var item in entityList) + { + DepartmentEntity model = null; + using (IDbConnection xbconn = new SqlConnection(_admsConnection)) + { + model = xbconn.QueryFirstOrDefault($"select * from LR_BASE_DEPARTMENT where DeptNo='{item.CODE}'"); + } + if (model == null) + { + maxSort++; + var id = item.ID.Length == 30 ? Guid.ParseExact(item.ID, "N") : Guid.NewGuid(); + //没有就新增 + var sql = + "INSERT INTO LR_BASE_DEPARTMENT (F_DepartmentId, F_CompanyId, F_ParentId, F_EnCode, F_FullName, F_Order,F_DeleteMark,F_EnabledMark) " + + $"VALUES ('{id}', '207fa1a9-160c-4943-a89b-8fa4db0547ce', '{item.FID}', '{item.CODE}', '{item.NAME}', {maxSort},0,1);"; + conn.Execute(sql); + } + else + { + //存在就修改 + var sql = $"UPDATE LR_BASE_DEPARTMENT SET F_FullName='{item.NAME}',F_ParentId='{item.FID}' where F_EnCode='{model.F_EnCode}';"; + conn.Execute(sql); + } + + } + //插入数据同步结果 + using (IDbConnection conncore = new SqlConnection(_admsConnection)) + { + conncore.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步部门信息到数校完成','部门同步数量:" + + entityList.Count() + "条',getdate())"); + } + } + catch (Exception e) + { + using (IDbConnection conncore = new SqlConnection(_admsConnection)) + { + conncore.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步部门信息到数校异常','" + + e.Message + "',getdate())"); + } + } + } + } + catch (Exception e) + { + using (IDbConnection conn = new SqlConnection(_admsConnection)) + { + conn.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步部门信息到数校异常','错误信息:" + + e.Message + "',getdate())"); + } + } + } + + /// + /// 从中间库同步学生数据 + /// + public static void SyncStuInfoAcc() + { + try + { + + IEnumerable entityList = null; + using (IDbConnection conn = new SqlConnection(_htMiddleConnection)) + { + entityList = conn.Query($"SELECT * FROM nc_stuInfo"); + } + + try + { + //插入sql + foreach (var item in entityList) + { + var id = item.ID.Length == 30 ? Guid.ParseExact(item.ID, "N") : Guid.NewGuid(); + var MZ = ""; + var xb = item.GenderNo == "1" ? 1 : 0; + + UserEntity model = null; + StuInfoBasicEntity stu = null; + using (IDbConnection xbconn = new SqlConnection(_admsConnection)) + { + model = xbconn.QueryFirstOrDefault($"select * from LR_Base_User where F_Account='{item.USERNAME}'"); + + if (model == null) + { + + var key = Md5Helper.Encrypt(CreateNo(), 16).ToLower(); + var pwd = Md5Helper.Encrypt(DESEncrypt.Encrypt(Md5Helper.Encrypt("ht123456", 32).ToLower(), key).ToLower(), 32).ToLower(); + + + var userSql = + $"INSERT INTO LR_Base_User (F_UserId,F_EnCode,F_Account,F_Mobile,F_Password,F_Secretkey,F_RealName,F_Gender,F_CompanyId," + + $"F_DepartmentId,F_DeleteMark,F_EnabledMark,F_Description,F_CreateDate,F_CreateUserId,F_CreateUserName,F_IdentityCardNo) VALUES('{Guid.NewGuid():D}'," + + $"'{item.USERNAME}','{item.USERNAME}','{item.MOBILE}','{pwd}','{key}','{item.StuName}','{xb}','207fa1a9-160c-4943-a89b-8fa4db0547ce','{item.DeptNo}',0,1,'学生'," + + $"'{DateTime.Now:yyyy-MM-dd hh:mm:ss}','System','数据同步','{item.IdentityCardNo}');"; + + xbconn.Execute(userSql); + + + } + + } + + using (var conn=new SqlConnection(_misConnection)) + { + stu= conn.QueryFirstOrDefault($"select * from StuInfoBasic where StuNo='{item.USERNAME}'"); + + if (stu == null) + { + var sql= "INSERT INTO StuInfoBasic(StuId,StuNo,StuCode,NoticeNo,GraduateYear,ksh,DeptNo,MajorNo,Grade,ClassNo,StuName,SpellFull,GenderNo,Birthday,PartyFaceNo," + + "FamilyOriginNo,NationalityNo,ResidenceNo,HealthStatusNo,GraduateNo,OverseasChineseNo,GoodAt,IdentityCardNo,InSchoolAddress," + + "InSchoolTelephone,Remark,mobile,CheckMark,InSchoolStatus,F_SchoolId,EduSystem,StudyModality,XJZT,XSDQZT) " + + $"VALUES('{id}','{item.USERNAME}','{item.CODE}','','','', '{item.DeptNo}', '{item.MajorNo}'," + + $"'{item.Grade}','{item.ClassNo}','{item.StuName}', '','{xb}','','',''," + + $"'{item.NationalityNo}','','1','2','0','','{item.IdentityCardNo}','','',''," + + $"'{item.MOBILE}','1','','207fa1a9-160c-4943-a89b-8fa4db0547ce', '2', '1','','');"; + conn.Execute(sql); + } + else + { + var sql = + $"UPDATE StuInfoBasic SET StuName='{item.StuName}',GenderNo={xb},IdentityCardNo='{item.IdentityCardNo}'," + + $"Grade='{item.Grade}',DeptNo='{item.DeptNo}',StuCode='{item.CODE}',mobile='{item.MOBILE}'," + + $"MajorNo='{item.MajorNo}',ClassNo='{item.ClassNo}' where StuNo='{stu.StuNo}';"; + conn.Execute(sql); + } + } + + + } + //插入数据同步结果 + using (IDbConnection conncore = new SqlConnection(_admsConnection)) + { + conncore.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步学生信息到数校完成','学生同步数量:" + + entityList.Count() + "条',getdate())"); + } + } + catch (Exception e) + { + using (IDbConnection conncore = new SqlConnection(_admsConnection)) + { + conncore.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步学生信息到数校异常','" + + e.Message + "',getdate())"); + } + } + } + catch (Exception e) + { + using (IDbConnection conn = new SqlConnection(_admsConnection)) + { + conn.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步学生信息到数校异常','错误信息:" + + e.Message + "',getdate())"); + } + } + } + + /// + /// 从中间库同步教师数据 + /// + public static void SyncEmpInfoAcc() + { + try + { + + IEnumerable entityList = null; + using (IDbConnection conn = new SqlConnection(_htMiddleConnection)) + { + entityList = conn.Query($"SELECT * FROM nc_user"); + } + + try + { + //插入sql + foreach (var item in entityList) + { + var id = item.ID.Length == 30 ? Guid.ParseExact(item.ID, "N") : Guid.NewGuid(); + var MZ = ""; + var xb = item.GENDER == "1" ? 1 : 0; + + UserEntity model = null; + EmpInfoEntity stu = null; + using (IDbConnection xbconn = new SqlConnection(_admsConnection)) + { + model = xbconn.QueryFirstOrDefault($"select * from LR_Base_User where F_Account='{item.USERNAME}'"); + + if (model == null) + { + + var key = Md5Helper.Encrypt(CreateNo(), 16).ToLower(); + var pwd = Md5Helper.Encrypt(DESEncrypt.Encrypt(Md5Helper.Encrypt("ht123456", 32).ToLower(), key).ToLower(), 32).ToLower(); + + + var userSql = + $"INSERT INTO LR_Base_User (F_UserId,F_EnCode,F_Account,F_Mobile,F_Password,F_Secretkey,F_RealName,F_Gender,F_CompanyId," + + $"F_DepartmentId,F_DeleteMark,F_EnabledMark,F_Description,F_CreateDate,F_CreateUserId,F_CreateUserName,F_IdentityCardNo) VALUES('{Guid.NewGuid():D}'," + + $"'{item.USERNAME}','{item.USERNAME}','{item.MOBILE}','{pwd}','{key}','{item.NAME}','{xb}','207fa1a9-160c-4943-a89b-8fa4db0547ce','{item.MOCODE}',0,1,'教师'," + + $"'{DateTime.Now:yyyy-MM-dd hh:mm:ss}','System','数据同步','{item.CARDNO}');"; + + xbconn.Execute(userSql); + + + } + + } + + using (var conn = new SqlConnection(_misConnection)) + { + stu = conn.QueryFirstOrDefault($"select * from EmpInfo where EmpNo='{item.USERNAME}'"); + + if (stu == null) + { + var sql = "INSERT INTO EmpInfo (EmpId,EmpNo,EmpName,GenderNo,DeptNo,IdentityCardNo,mobile,EMail,IsInActiveStatus,CheckMark,F_CompanyId) " + + $"VALUES('{id}','{item.USERNAME}','{item.NAME}','{xb}','{item.MOCODE}','{item.CARDNO}', '{item.MOBILE}', '{item.EMAIL}'," + + $"'{item.STATUS}','1','207fa1a9-160c-4943-a89b-8fa4db0547ce');"; + conn.Execute(sql); + } + else + { + var sql = + $"UPDATE EmpInfo SET EmpName='{item.NAME}',GenderNo={xb},IdentityCardNo='{item.CARDNO}'," + + $"DeptNo='{item.MOCODE}',mobile='{item.MOBILE}',EMail='{item.EMAIL}',IsInActiveStatus='{item.STATUS}' where EmpNo='{stu.EmpNo}';"; + conn.Execute(sql); + } + } + + + } + //插入数据同步结果 + using (IDbConnection conncore = new SqlConnection(_admsConnection)) + { + conncore.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步学生信息到数校完成','学生同步数量:" + + entityList.Count() + "条',getdate())"); + } + } + catch (Exception e) + { + using (IDbConnection conncore = new SqlConnection(_admsConnection)) + { + conncore.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步学生信息到数校异常','" + + e.Message + "',getdate())"); + } + } + } + catch (Exception e) + { + using (IDbConnection conn = new SqlConnection(_admsConnection)) + { + conn.Execute( + "insert into LR_Base_Log(F_LogId,F_CategoryId,F_SourceObjectId,F_SourceContentJson,F_OperateTime) values(newid(),999,'从中间库同步学生信息到数校异常','错误信息:" + + e.Message + "',getdate())"); + } + } + } + #endregion + + + private static string CreateNo() + { + Random random = new Random(); + string strRandom = random.Next(1000, 10000).ToString(); //生成编号 + string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如 + return code; + } + + + + } +} diff --git a/DigitalSchoolApi/Controllers/YKTController.cs b/DigitalSchoolApi/Controllers/YKTController.cs index 5bc2f32..f41345e 100644 --- a/DigitalSchoolApi/Controllers/YKTController.cs +++ b/DigitalSchoolApi/Controllers/YKTController.cs @@ -680,5 +680,32 @@ namespace DigitalSchoolApi.Controllers } #endregion + + #region 和田数据同步 + + /// + /// 从中间库中同步到数校 + /// + /// + public IHttpActionResult SyncDataToDs() + { + //部门 + RecurringJob.AddOrUpdate("HTSyncDepartmentToDs", + () => HTSchoolController.SyncDepartment(), + Cron.Daily(1), TimeZoneInfo.Local); + + //教师 + RecurringJob.AddOrUpdate("HTSyncEmpInfoToDs", + () => HTSchoolController.SyncEmpInfoAcc(), + Cron.Daily(1), TimeZoneInfo.Local); + //学生 + RecurringJob.AddOrUpdate("HTSyncStuInfoToDs", + () => HTSchoolController.SyncStuInfoAcc(), + Cron.Daily(1), TimeZoneInfo.Local); + + + return Ok(); + } + #endregion } } diff --git a/DigitalSchoolApi/DigitalSchoolApi.csproj b/DigitalSchoolApi/DigitalSchoolApi.csproj index c1c4720..81849f6 100644 --- a/DigitalSchoolApi/DigitalSchoolApi.csproj +++ b/DigitalSchoolApi/DigitalSchoolApi.csproj @@ -405,6 +405,7 @@ + @@ -422,6 +423,7 @@ + diff --git a/DigitalSchoolApi/Models/HtEntity.cs b/DigitalSchoolApi/Models/HtEntity.cs new file mode 100644 index 0000000..624210e --- /dev/null +++ b/DigitalSchoolApi/Models/HtEntity.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DigitalSchoolApi.Models +{ + internal interface HtEntity + { + } + + public class HTMiddleOrganize + { + public string ID { get; set; } + + public string NAME { get; set; } + + public string FID { get; set; } + + public string CODE { get; set; } + } + + public class HTMiddleStuInfo + { + + public string ID { get; set; } + public string USERNAME { get; set; } + + public string StuName { get; set; } + + public string IdentityCardNo { get; set; } + + public string NationalityNo { get; set; } + + public string Grade { get; set; } + + public string EntranceDate { get; set; } + + public string RegisterDate { get; set; } + + public string MailAddress { get; set; } + + public string DeptNo { get; set; } + + public string MajorNo { get; set; } + + public string ClassNo { get; set; } + + public string GenderNo { get; set; } + + public string CODE { get; set; } + + public string MOBILE { get; set; } + + public string EMAIL { get; set; } + } + + public class HTMiddleEmpInfo + { + + public string ID { get; set; } + + public string CODE { get; set; } + + + public string NAME { get; set; } + public string USERNAME { get; set; } + + public string MOCODE { get; set; } + + public string GENDER { get; set; } + + public string CARDNO { get; set; } + + public string MOBILE { get; set; } + + public string EMAIL { get; set; } + + public string STATUS { get; set; } + + } +} diff --git a/DigitalSchoolApi/Web.config b/DigitalSchoolApi/Web.config index 1842fb7..d8797c4 100644 --- a/DigitalSchoolApi/Web.config +++ b/DigitalSchoolApi/Web.config @@ -68,7 +68,7 @@ - +