CrazyIter 5 年 前
コミット
07158ae5f6

+ 192 - 4
HiTeachCE/Context/DBContext.cs

@@ -1,22 +1,24 @@
-using SqlSugar;
+using HiTeachCE.Models;
+using SqlSugar;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
+using System.Linq.Expressions;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using TEAMModelOS.SDK.Context.Configuration;
 using TEAMModelOS.SDK.Context.Configuration;
 
 
 namespace HiTeachCE.Context
 namespace HiTeachCE.Context
 {
 {
-    public class DBContext<T> where T : class, new()
+    public class DBContext<Entity> where Entity : class, new()
     {
     {
         public SqlSugarClient Db;
         public SqlSugarClient Db;
         /// <summary>
         /// <summary>
         /// 修改后的代码
         /// 修改后的代码
         /// </summary>
         /// </summary>
         /// <returns></returns>
         /// <returns></returns>
-        public static DBContext<T> OpDB()
+        public static DBContext<Entity> OpDB()
         {
         {
-            DBContext<T> dbcontext_t = new DBContext<T>();
+            DBContext<Entity> dbcontext_t = new DBContext<Entity>();
             dbcontext_t.Db = new SqlSugarClient(new ConnectionConfig()
             dbcontext_t.Db = new SqlSugarClient(new ConnectionConfig()
             {
             {
                 ConnectionString = BaseConfigModel.Configuration["DbConnection:MySqlConnectionString"],
                 ConnectionString = BaseConfigModel.Configuration["DbConnection:MySqlConnectionString"],
@@ -26,5 +28,191 @@ namespace HiTeachCE.Context
             });
             });
             return dbcontext_t;
             return dbcontext_t;
         }
         }
+        protected DBContext()
+        {
+            Db = new SqlSugarClient(new ConnectionConfig()
+            {
+                ConnectionString = BaseConfigModel.Configuration["DbConnection:MySqlConnectionString"],
+                DbType = SqlSugar.DbType.MySql,
+                IsAutoCloseConnection = true,
+                InitKeyType = InitKeyType.Attribute
+            });
+            //调式代码 用来打印SQL
+            Db.Aop.OnLogExecuting = (sql, pars) =>
+            {
+                Console.WriteLine(sql + "\r\n" +
+                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
+            };
+        }
+        public void Dispose()
+        {
+            if (Db != null)
+            {
+                Db.Dispose();
+            }
+        }
+        public SimpleClient<Entity> CurrentDb { get { return new SimpleClient<Entity>(Db); } }
+        /// <summary>
+        /// 获取所有
+        /// </summary>
+        /// <returns></returns>
+        public virtual List<Entity> GetList()
+        {
+            return CurrentDb.GetList();
+        }
+
+        /// <summary>
+        /// 根据表达式查询
+        /// </summary>
+        /// <returns></returns>
+        public virtual List<Entity> GetList(Expression<Func<Entity, bool>> whereExpression)
+        {
+            return CurrentDb.GetList(whereExpression);
+        }
+
+
+        /// <summary>
+        /// 根据表达式查询分页
+        /// </summary>
+        /// <returns></returns>
+        public virtual List<Entity> GetPageList(Expression<Func<Entity, bool>> whereExpression, PageModel pageModel)
+        {
+            return CurrentDb.GetPageList(whereExpression, pageModel);
+        }
+
+        /// <summary>
+        /// 根据表达式查询分页并排序
+        /// </summary>
+        /// <param name="whereExpression">it</param>
+        /// <param name="pageModel"></param>
+        /// <param name="orderByExpression">it=>it.id或者it=>new{it.id,it.name}</param>
+        /// <param name="orderByType">OrderByType.Desc</param>
+        /// <returns></returns>
+        public virtual List<Entity> GetPageList(Expression<Func<Entity, bool>> whereExpression, PageModel pageModel, Expression<Func<Entity, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
+        {
+            return CurrentDb.GetPageList(whereExpression, pageModel, orderByExpression, orderByType);
+        }
+
+
+        /// <summary>
+        /// 根据主键查询
+        /// </summary>
+        /// <returns></returns>
+        public virtual List<Entity> GetById(dynamic id)
+        {
+            return CurrentDb.GetById(id);
+        }
+        /// <summary>
+        /// 根据主键查询
+        /// </summary>
+        /// <returns></returns>
+        public virtual List<Entity> GetByIds(dynamic[] ids)
+        {
+            return Db.Queryable<Entity>().In("id", ids).ToList();
+        }
+
+        /// <summary>
+        /// 根据主键删除
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public virtual bool Delete(dynamic id)
+        {
+            if (string.IsNullOrEmpty(id.ObjToString))
+            {
+                Console.WriteLine(string.Format("要删除的主键id不能为空值!"));
+            }
+            return CurrentDb.Delete(id);
+        }
+
+
+        /// <summary>
+        /// 根据实体删除
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public virtual bool Delete(Entity data)
+        {
+            if (data == null)
+            {
+                Console.WriteLine(string.Format("要删除的实体对象不能为空值!"));
+            }
+            return CurrentDb.Delete(data);
+        }
+
+        /// <summary>
+        /// 根据主键删除
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public virtual bool Delete(dynamic[] ids)
+        {
+            if (ids.Count() <= 0)
+            {
+                Console.WriteLine(string.Format("要删除的主键ids不能为空值!"));
+            }
+            return CurrentDb.AsDeleteable().In(ids).ExecuteCommand() > 0;
+        }
+
+        /// <summary>
+        /// 根据表达式删除
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public virtual bool Delete(Expression<Func<Entity, bool>> whereExpression)
+        {
+            return CurrentDb.Delete(whereExpression);
+        }
+
+
+        /// <summary>
+        /// 根据实体更新,实体需要有主键
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public virtual bool Update(Entity obj)
+        {
+            if (obj == null)
+            {
+                Console.WriteLine(string.Format("要更新的实体不能为空,必须带上主键!"));
+            }
+            return CurrentDb.Update(obj);
+        }
+
+        /// <summary>
+        ///批量更新
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public virtual bool Update(List<Entity> objs)
+        {
+            if (objs.Count <= 0)
+            {
+                Console.WriteLine(string.Format("要批量更新的实体不能为空,必须带上主键!"));
+            }
+            return CurrentDb.UpdateRange(objs);
+        }
+
+        /// <summary>
+        /// 插入
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public virtual bool Insert(Entity obj)
+        {
+            return CurrentDb.Insert(obj);
+        }
+
+
+        /// <summary>
+        /// 批量
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public virtual bool Insert(List<Entity> objs)
+        {
+            return CurrentDb.InsertRange(objs);
+        }
+
     }
     }
 }
 }

+ 36 - 0
HiTeachCE/Controllers/LecturerController.cs

@@ -0,0 +1,36 @@
+using HiTeachCE.Context;
+using HiTeachCE.Models;
+using HiTeachCE.Services;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
+using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
+
+namespace HiTeachCE.Controllers
+{
+    [Route("api/[controller]")]
+    [ApiController]
+    public class LecturerController : Controller
+    {
+        private readonly LecturerService lecturerService;
+        public LecturerController(LecturerService lecturer) {
+            lecturerService = lecturer;
+        }
+        /// <summary>
+        /// 获取知识
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost("GetList")]
+        public  BaseJosnRPCResponse GetList(JosnRPCRequest<Dictionary<string, object>> request)
+        {
+            // request.@params.TryAdd("PartitionKey", request.lang);
+            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
+            List<Lecturer> data = lecturerService.GetList() ;
+            return builder.Data(Guid.NewGuid()).build();
+        }
+    }
+}

+ 70 - 0
HiTeachCE/Controllers/LoginController.cs

@@ -0,0 +1,70 @@
+using HiTeachCE.Models;
+using HiTeachCE.Services;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
+using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
+
+namespace HiTeachCE.Controllers
+{
+    [Route("api/[controller]")]
+    [ApiController]
+    public class LoginController : Controller
+    {
+        private readonly LecturerService lecturerService;
+        private readonly OrganizationService organizationService;
+        private readonly MemberService memberService;
+        public LoginController(LecturerService lecturer, OrganizationService organization, MemberService member)
+        {
+            lecturerService = lecturer;
+            organizationService = organization;
+            memberService = member;
+        }
+        /// <summary>
+        /// 登录
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost]
+        public BaseJosnRPCResponse Login(JosnRPCRequest<Dictionary<string, object>> request)
+        {
+            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
+            List<Lecturer> data = lecturerService.GetList();
+            Expression<Func<Member, bool>> mlinq = null;
+            mlinq = m => m.account == "huanghb";
+            List<Member> members= memberService.GetList(mlinq);
+            Expression<Func<Organization, bool>> olinq = null;
+            olinq = o => members.Select(x => x.orgCode).ToList().Contains(o.code);
+            List<Organization> organizations= organizationService.GetList(olinq);
+            return builder.Data(organizations).build();
+        }
+        /// <summary>
+        /// 初始化登录
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost("init")]
+        public BaseJosnRPCResponse Init(JosnRPCRequest<Dictionary<string, object>> request)
+        {
+            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
+           
+            return builder.Data(Guid.NewGuid().ToString("N")).build();
+        }
+        /// <summary>
+        /// 初始化登录
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost("smsLogin")]
+        public BaseJosnRPCResponse smsLogin(JosnRPCRequest<Dictionary<string, object>> request)
+        {
+            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
+
+            return builder.Data(Guid.NewGuid().ToString("N")).build();
+        }
+    }
+}

+ 2 - 1
HiTeachCE/HiTeachCE.csproj

@@ -7,7 +7,8 @@
     <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0" />
     <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0" />
     <PackageReference Include="MQTTnet.AspNetCore" Version="3.0.11" />
     <PackageReference Include="MQTTnet.AspNetCore" Version="3.0.11" />
     <PackageReference Include="MQTTnet.Extensions.WebSocket4Net" Version="3.0.11" />
     <PackageReference Include="MQTTnet.Extensions.WebSocket4Net" Version="3.0.11" />
-    <PackageReference Include="sqlSugarCore" Version="5.0.0.14" />
+    <PackageReference Include="MySql.Data" Version="8.0.20" />
+    <PackageReference Include="sqlSugarCore" Version="5.0.0.10" />
     <PackageReference Include="TEAMModelOS.SDK" Version="3.0.520" />
     <PackageReference Include="TEAMModelOS.SDK" Version="3.0.520" />
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 8 - 2
HiTeachCE/Models/ActivationCode.cs

@@ -7,12 +7,14 @@ using System.Threading.Tasks;
 namespace HiTeachCE.Models
 namespace HiTeachCE.Models
 {
 {
     [SugarTable("ActivationCode")]
     [SugarTable("ActivationCode")]
-    public class ActivationCode :ID
+    public class ActivationCode :Entity
     {
     {
+        [SugarColumn(IsNullable = false, IsPrimaryKey = true)]
         public string id { get; set; }
         public string id { get; set; }
         /// <summary>
         /// <summary>
         /// cdkey产品激活码
         /// cdkey产品激活码
         /// </summary>
         /// </summary>
+        [SugarColumn(IsNullable = false)]
         public string cdkey { get; set; }
         public string cdkey { get; set; }
         /// <summary>
         /// <summary>
         /// 最大人数
         /// 最大人数
@@ -21,10 +23,12 @@ namespace HiTeachCE.Models
         /// <summary>
         /// <summary>
         /// 绑定机构
         /// 绑定机构
         /// </summary>
         /// </summary>
+        [SugarColumn(IsNullable = false)]
         public string orgCode { get; set;}
         public string orgCode { get; set;}
         /// <summary>
         /// <summary>
         /// 到期日期秒数
         /// 到期日期秒数
-        /// </summary>
+        /// </summary>   
+ 
         public long expires { get; set; }
         public long expires { get; set; }
         /// <summary>
         /// <summary>
         /// 创建时间
         /// 创建时间
@@ -33,10 +37,12 @@ namespace HiTeachCE.Models
         /// <summary>
         /// <summary>
         /// 产品8码 可以重新生成
         /// 产品8码 可以重新生成
         /// </summary>
         /// </summary>
+        [SugarColumn(IsNullable = false)]
         public string clientId { get; set; }
         public string clientId { get; set; }
         /// <summary>
         /// <summary>
         /// 产品密钥
         /// 产品密钥
         /// </summary>
         /// </summary>
+        [SugarColumn(IsNullable = false)]
         public string secret { get; set; }
         public string secret { get; set; }
         public int status { get; set; }
         public int status { get; set; }
 
 

+ 2 - 1
HiTeachCE/Models/Classroom.cs

@@ -10,8 +10,9 @@ namespace HiTeachCE.Models
     /// 只用于保存记录/不能用于上课使用
     /// 只用于保存记录/不能用于上课使用
     /// </summary>
     /// </summary>
     [SugarTable("Classroom")]
     [SugarTable("Classroom")]
-    public class Classroom :ID
+    public class Classroom :Entity
     {
     {
+        [SugarColumn(IsNullable = false, IsPrimaryKey = true)]
         public string id { get; set; }
         public string id { get; set; }
         public string name { get; set; }
         public string name { get; set; }
         /// <summary>
         /// <summary>

+ 1 - 1
HiTeachCE/Models/ID.cs

@@ -5,7 +5,7 @@ using System.Threading.Tasks;
 
 
 namespace HiTeachCE.Models
 namespace HiTeachCE.Models
 {
 {
-     public interface ID
+     public interface Entity
     {
     {
         public string id { get; set; }
         public string id { get; set; }
     }
     }

+ 2 - 1
HiTeachCE/Models/Learner.cs

@@ -7,8 +7,9 @@ using System.Threading.Tasks;
 namespace HiTeachCE.Models
 namespace HiTeachCE.Models
 {
 {
     [SugarTable("Learner")]
     [SugarTable("Learner")]
-    public class Learner:ID
+    public class Learner:Entity
     {
     {
+        [SugarColumn(IsNullable = false, IsPrimaryKey = true)]
         public string id { get; set; }
         public string id { get; set; }
         public string username { get; set; }
         public string username { get; set; }
         public string password { get; set; }
         public string password { get; set; }

+ 2 - 1
HiTeachCE/Models/Lecturer.cs

@@ -7,8 +7,9 @@ using System.Threading.Tasks;
 namespace HiTeachCE.Models
 namespace HiTeachCE.Models
 {
 {
     [SugarTable("Lecturer")]
     [SugarTable("Lecturer")]
-    public class Lecturer:ID
+    public class Lecturer:Entity
     {
     {
+        [SugarColumn(IsNullable = false, IsPrimaryKey = true)]
         public string id { get; set; }
         public string id { get; set; }
         public string username { get; set; }
         public string username { get; set; }
         public string password { get; set; }
         public string password { get; set; }

+ 2 - 1
HiTeachCE/Models/Member.cs

@@ -7,8 +7,9 @@ using System.Threading.Tasks;
 namespace HiTeachCE.Models
 namespace HiTeachCE.Models
 {
 {
     [SugarTable("Member")]
     [SugarTable("Member")]
-    public class Member : ID
+    public class Member : Entity
     {
     {
+        [SugarColumn(IsNullable = false, IsPrimaryKey = true)]
         public string id { get ; set ; }
         public string id { get ; set ; }
         public string account { get; set; }
         public string account { get; set; }
         public string orgCode { get; set; }
         public string orgCode { get; set; }

+ 2 - 1
HiTeachCE/Models/Organization.cs

@@ -7,8 +7,9 @@ using System.Threading.Tasks;
 namespace HiTeachCE.Models
 namespace HiTeachCE.Models
 {
 {
     [SugarTable("Organization")]
     [SugarTable("Organization")]
-    public class Organization :ID
+    public class Organization :Entity
     {
     {
+        [SugarColumn(IsNullable = false, IsPrimaryKey = true)]
         public string id { get; set; }
         public string id { get; set; }
         public string name { get; set; }
         public string name { get; set; }
         public string code { get; set; }
         public string code { get; set; }

+ 2 - 4
HiTeachCE/Models/Subscriber.cs

@@ -10,11 +10,9 @@ namespace HiTeachCE.Models
     /// 订阅器
     /// 订阅器
     /// </summary>
     /// </summary>
     [SugarTable("Subscriber")]
     [SugarTable("Subscriber")]
-    public class Subscriber :ID
+    public class Subscriber :Entity
     {
     {
-        /// <summary>
-        /// 
-        /// </summary>
+        [SugarColumn(IsNullable = false, IsPrimaryKey = true)]
         public string id { get; set; }
         public string id { get; set; }
         public string topic { get; set; }
         public string topic { get; set; }
         public string orgCode { get; set; }
         public string orgCode { get; set; }

+ 14 - 0
HiTeachCE/Services/LecturerService.cs

@@ -0,0 +1,14 @@
+using HiTeachCE.Context;
+using HiTeachCE.Models;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Context.Configuration;
+
+namespace HiTeachCE.Services
+{
+    public class LecturerService :DBContext<Lecturer>, IBusinessService
+    {
+    }
+}

+ 14 - 0
HiTeachCE/Services/MemberService.cs

@@ -0,0 +1,14 @@
+using HiTeachCE.Context;
+using HiTeachCE.Models;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Context.Configuration;
+
+namespace HiTeachCE.Services
+{
+    public class MemberService : DBContext<Member>, IBusinessService
+    {
+    }
+}

+ 14 - 0
HiTeachCE/Services/OrganizationService.cs

@@ -0,0 +1,14 @@
+using HiTeachCE.Context;
+using HiTeachCE.Models;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Context.Configuration;
+
+namespace HiTeachCE.Services
+{
+    public class OrganizationService : DBContext<Organization>, IBusinessService
+    {
+    }
+}

+ 24 - 0
HiTeachCE/Startup.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Security.Authentication;
 using System.Security.Authentication;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
+using HiTeachCE.Models;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.HttpsPolicy;
 using Microsoft.AspNetCore.HttpsPolicy;
@@ -18,6 +19,7 @@ using MQTTnet.AspNetCore;
 using MQTTnet.Client.Receiving;
 using MQTTnet.Client.Receiving;
 using MQTTnet.Protocol;
 using MQTTnet.Protocol;
 using MQTTnet.Server;
 using MQTTnet.Server;
+using SqlSugar;
 using TEAMModelOS.SDK.Context.Configuration;
 using TEAMModelOS.SDK.Context.Configuration;
 using TEAMModelOS.SDK.Extension.JwtAuth;
 using TEAMModelOS.SDK.Extension.JwtAuth;
 
 
@@ -45,6 +47,12 @@ namespace HiTeachCE
             //注入CSRedis
             //注入CSRedis
             var csredis = new CSRedis.CSRedisClient(Configuration.GetSection("Redis:ConnectionString").Get<string>());
             var csredis = new CSRedis.CSRedisClient(Configuration.GetSection("Redis:ConnectionString").Get<string>());
             RedisHelper.Initialization(csredis);
             RedisHelper.Initialization(csredis);
+
+            //全局扫描基于IBusinessService接口的实现类
+            services.Scan(scan => scan.FromApplicationDependencies()
+               .AddClasses(classes => classes.AssignableTo<IBusinessService>())
+                   .AsSelfWithInterfaces()
+                   .WithScopedLifetime());
             services.AddCors(options =>
             services.AddCors(options =>
             {
             {
                 // CorsPolicy 是自訂的 Policy 名稱
                 // CorsPolicy 是自訂的 Policy 名稱
@@ -142,6 +150,22 @@ namespace HiTeachCE
             app.UseHttpsRedirection();
             app.UseHttpsRedirection();
             app.UseStaticFiles();
             app.UseStaticFiles();
 
 
+            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
+            {
+                ConnectionString = BaseConfigModel.Configuration["DbConnection:MySqlConnectionString"],
+                DbType = DbType.MySql,
+                IsAutoCloseConnection = true,
+                InitKeyType = InitKeyType.Attribute
+            });
+            db.CodeFirst.InitTables(
+                typeof(ActivationCode),
+                typeof(Classroom),
+                typeof(Learner),
+               // typeof(Lecturer),
+               // typeof(Member),
+                //typeof(Organization),
+                typeof(Subscriber)
+                );
             app.UseRouting();
             app.UseRouting();
             app.UseCors("CorsPolicy"); //使用跨域設定
             app.UseCors("CorsPolicy"); //使用跨域設定
             app.UseAuthorization();
             app.UseAuthorization();

+ 1 - 1
HiTeachCE/appsettings.json

@@ -8,7 +8,7 @@
   },
   },
   "AllowedHosts": "*",
   "AllowedHosts": "*",
   "DBConnection": {
   "DBConnection": {
-    "MySqlConnectionString": "server=106.12.23.251:3306;database=HiTeachCE;uid=root;pwd=teammodelabc123;charset='utf8mb4';SslMode=None"
+    "MySqlConnectionString": "server=106.12.23.251;database=HiTeachCE;port=3306;uid=root;pwd=teammodelabc123;charset='utf8mb4';SslMode=None"
   },
   },
   "Redis": {
   "Redis": {
     "ConnectionString": "106.12.23.251:6379,password=habook,ssl=false,abortConnect=False,defaultDatabase=13,writeBuffer=10240,poolsize=50,prefix=habook:"
     "ConnectionString": "106.12.23.251:6379,password=habook,ssl=false,abortConnect=False,defaultDatabase=13,writeBuffer=10240,poolsize=50,prefix=habook:"