Sfoglia il codice sorgente

优化学校统计,添加配置和修改配置,获取所有配置

Li 3 anni fa
parent
commit
d4c464f0b7

+ 4 - 2
TEAMModelBI/Controllers/BISchool/SchoolController.cs

@@ -503,6 +503,10 @@ namespace TEAMModelBI.Controllers.BISchool
                 {
                     dic.Add("userId", $"{userId}");
                 }
+                if (string.IsNullOrEmpty($"{userId}") && string.IsNullOrEmpty($"{tmdId}")) 
+                {
+                    return Ok(new { state = 400, msg = "钉钉账户或醍摩豆账户需要至少要传一个!" });
+                }
 
                 Dictionary<string, List<string>> noSchools = new();
                 List<DingDingUserInfo> ddUserInfoList = await table.FindListByDict<DingDingUserInfo>(dic);
@@ -569,7 +573,6 @@ namespace TEAMModelBI.Controllers.BISchool
             }
         }
 
-
         /// <summary>
         /// 依据学校ID查学校管理员信息
         /// </summary>
@@ -868,7 +871,6 @@ namespace TEAMModelBI.Controllers.BISchool
             return Ok(new { state = 200, allSize, useSize, teacherSpace, useSpaceInfo });
         }
 
-
         /// <summary>
         /// 未加入区域的学校
         /// </summary>

+ 23 - 13
TEAMModelBI/Controllers/Census/SchoolController.cs

@@ -42,26 +42,36 @@ namespace TEAMModelBI.Controllers.Census
         public async Task<IActionResult> GetAll() 
         {
             var cosmosClient = _azureCosmos.GetCosmosClient();
-            
-            List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, "select c.id from c", "Base");
-            int tecCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "Teacher", "Base");
-            int stuCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "Student", "Base");
-            long roomCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c where c.pk = 'Room' and c.serial != null", new List<string>() { "School" });
+
+            long allLessCount = 0; //所有课例          
+            long lastYearLessCount = 0; //去年课例
+            long yearLessCount = 0;//今年课例
+            long allActivityCount = 0; //所有活动
+            long lastActivityCount = 0; //去年活动
+            long activityCount = 0;   //今年活动
+            int tecCount = 0;  //教师数量
+            int stuCount = 0;  //学生数量
+            long roomCount = 0;  //教师数量
+
+
 
             var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year");
             var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year");
 
+            List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, "select c.id from c", "Base");
+            tecCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "Teacher", "Base");
+            stuCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "Student", "Base");
+            roomCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c where c.pk = 'Room' and c.serial != null", new List<string>() { "School" });
+
+
             //所有课例
-            long allLessCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord'", new List<string>() { "School", "Teacher" });
+            allLessCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord'", new List<string>() { "School", "Teacher" });
             //去年课例
-            long lastYearLessCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={lastYearStart} and c.startTime<={lastYearEnd}", new List<string>() { "School", "Teacher" });
+            lastYearLessCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={lastYearStart} and c.startTime<={lastYearEnd}", new List<string>() { "School", "Teacher" });
             //今年课例
-            long yearLessCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={yearStart} and c.startTime<={yearEnd}", new List<string>() { "School", "Teacher" });
-
-            long allActivityCount = 0;
-            long lastActivityCount = 0;
-            long activityCount = 0;
+            yearLessCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={yearStart} and c.startTime<={yearEnd}", new List<string>() { "School", "Teacher" });
 
+            //活动
             foreach (var type in StaticValue.activityTypes)
             {
                 allActivityCount += await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='{type}' ", new List<string>() { "Common" });
@@ -69,7 +79,7 @@ namespace TEAMModelBI.Controllers.Census
                 activityCount += await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='{type}' and c.createTime >={yearStart} and c.createTime<= {yearEnd}", new List<string>() { "Common" });
             }
 
-            return Ok(new { state = 200, scCount = schoolIds.Count, tecCount, stuCount, roomCount, lastYearLessCount, yearLessCount, allActivityCount, lastActivityCount, activityCount });
+            return Ok(new { state = 200, scCount = schoolIds.Count, tecCount, stuCount, roomCount, allLessCount, lastYearLessCount, yearLessCount, allActivityCount, lastActivityCount, activityCount });
         }
 
         /// <summary>

+ 149 - 2
TEAMModelBI/Controllers/DingDingStruc/SystemConfigController.cs

@@ -15,6 +15,9 @@ using Microsoft.Extensions.Options;
 using TEAMModelBI.Tool.Extension;
 using TEAMModelOS.SDK.DI;
 using TEAMModelBI.Filter;
+using TEAMModelOS.SDK.Extension;
+using System.Linq;
+using TEAMModelBI.Models;
 
 namespace TEAMModelBI.Controllers.DingDingStruc
 {
@@ -38,7 +41,7 @@ namespace TEAMModelBI.Controllers.DingDingStruc
         }
 
         /// <summary>
-        /// 
+        /// 获取应用配置文件
         /// </summary>
         /// <returns></returns>
         [HttpPost("get-config")]
@@ -61,7 +64,7 @@ namespace TEAMModelBI.Controllers.DingDingStruc
         }
 
         /// <summary>
-        /// 配置文件修改
+        /// 应用配置文件修改
         /// </summary>
         /// <param name="jsonElement"></param>
         /// <returns></returns>
@@ -247,9 +250,153 @@ namespace TEAMModelBI.Controllers.DingDingStruc
             }
         }
 
+        /// <summary>
+        /// 获取当前应用的
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [AuthToken(Roles = "admin")]
+        [HttpPost("get-allconfig")]
+        public async Task<IActionResult> GetAllConfig() 
+        {
+            var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
+            StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
+            StringBuilder stringBuilder = new();
+            string text;
+            while ((text = streamReader.ReadLine()) != null)
+            {
+                stringBuilder.Append(text.ToString());
+            }
+            string input = stringBuilder.ToString();
+            List<SysConfig> allConfigs = input.ToObject<List<SysConfig>>();
+
+            return Ok(new { state = 200, allConfigs });
+        }
 
+        /// <summary>
+        /// 修改或者添加配置文件,到多个配置文件中
+        /// </summary>
+        /// <param name="sysConfig"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [AuthToken(Roles = "admin")]
+        [HttpPost("set-singleconfig")]
+        public async Task<IActionResult> SetSingleConfig(SysConfig sysConfig) 
+        {
+            var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
+            StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
+            StringBuilder stringBuilder = new();
+            string text;
+            while ((text = streamReader.ReadLine()) != null)
+            {
+                stringBuilder.Append(text.ToString());
+            }
+            string input = stringBuilder.ToString();
 
+            List<SysConfig> allConfigs = new();
+            if (!string.IsNullOrEmpty(input)) 
+            {
+                try
+                {
+                    allConfigs = input.ToObject<List<SysConfig>>();
+                }
+                catch
+                {
+                    allConfigs.Add(sysConfig);
+                }
+                var tempSysConfig = allConfigs.Where(x => x.site.Equals(sysConfig.site)).FirstOrDefault();
+                if (tempSysConfig != null)
+                {
 
+                    tempSysConfig.site = sysConfig.site;
+                    tempSysConfig.proDeptId = sysConfig.proDeptId;
+                    tempSysConfig.clientKey = sysConfig.clientKey;
+                    tempSysConfig.dDAuth = sysConfig.dDAuth;
+                    tempSysConfig.azureClient = sysConfig.azureClient;
+                }
+                else
+                {
+                    allConfigs.Add(sysConfig);
+                }
+            }
+            else
+            {
+                allConfigs.Add(sysConfig);
+            }
+
+            string ListJson = JsonConvert.SerializeObject(allConfigs, Formatting.Indented);
+            System.IO.File.WriteAllText(builder, ListJson);
+
+            return Ok(new { state = 200, allConfigs });
+        }
+
+        /// <summary>
+        /// 切换站点
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [AuthToken(Roles = "admin")]
+        [HttpPost("cut-site")]
+        public async Task<IActionResult> CutSite(JsonElement jsonElement) 
+        {
+            if (!jsonElement.TryGetProperty("site", out JsonElement site)) return BadRequest();
+            var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
+            var formalDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.json";
+            var extendDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.Development.json";
+
+            StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
+            StringBuilder stringBuilder = new();
+            string text;
+            while ((text = streamReader.ReadLine()) != null)
+            {
+                stringBuilder.Append(text.ToString());
+            }
+            string input = stringBuilder.ToString();
+
+            List<SysConfig> allConfigs = input.ToObject<List<SysConfig>>();
+
+            var tempSysConfig = allConfigs.Where(x => x.site.Equals($"{site}")).FirstOrDefault();
+
+            // 有排版  注释全无
+            StreamReader reader = System.IO.File.OpenText(formalDeploy);
+            JsonTextReader jsonTextReader = new(reader);
+            JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
+
+            StreamReader readerDeve = System.IO.File.OpenText(extendDeploy);
+            JsonTextReader jsonTextReaderDeve = new(readerDeve);
+            JObject jsonObjectDeve = (JObject)JToken.ReadFrom(jsonTextReaderDeve);
+
+            if (tempSysConfig != null)
+            {
+                jsonObject["Option"]["Location"] = tempSysConfig.site;
+                jsonObject["CustomParam"]["proDeptId"] = tempSysConfig.proDeptId;
+
+                jsonObject["CoreService"]["clientID"] = tempSysConfig.clientKey.clientID;
+                jsonObject["CoreService"]["clientSecret"] = tempSysConfig.clientKey.clientSecret;
+
+                jsonObject["DingDingAuth"]["Agentld"] = tempSysConfig.dDAuth.agebtId;
+                jsonObject["DingDingAuth"]["appKey"] = tempSysConfig.dDAuth.appKey;
+                jsonObject["DingDingAuth"]["appSecret"] = tempSysConfig.dDAuth.appSecret;
+
+                jsonObject["Azure"]["Storage"]["ConnectionString"] = tempSysConfig.azureClient.storage;
+                jsonObject["Azure"]["Cosmos"]["ConnectionString"] = tempSysConfig.azureClient.cosmos;
+                jsonObject["Azure"]["Redis"]["ConnectionString"] = tempSysConfig.azureClient.redis;
+                jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = tempSysConfig.azureClient.servicBus;
+            }
+            else return Ok(new { state = 404,msg = "未找到你需要的参数" });
+
+            reader.Close();
+            string output = JsonConvert.SerializeObject(jsonObject, Formatting.Indented); //是否json文件有缩进
+            System.IO.File.WriteAllText(formalDeploy, output);
+
+            readerDeve.Close();
+            string outputDeve = JsonConvert.SerializeObject(jsonObjectDeve, Formatting.Indented); //是否json文件有缩进
+            System.IO.File.WriteAllText(extendDeploy, outputDeve);
+
+            return Ok(new { state = 200, SysConfig = tempSysConfig });
+        }
 
     }
 }

+ 40 - 0
TEAMModelBI/JsonFile/SystemConfig.json

@@ -0,0 +1,40 @@
+[
+  {
+    "site": "Chins",
+    "proDeptId": 13456,
+    "clientKey": {
+      "clientID": "123456",
+      "clientSecret": "25463678"
+    },
+    "dDAuth": {
+      "agebtId": "123456",
+      "appKey": "321",
+      "appSecret": "123456"
+    },
+    "azureClient": {
+      "storage": "123456",
+      "cosmos": "12345",
+      "redis": "2245",
+      "servicBus": "123455"
+    }
+  },
+  {
+    "site": "Chins-214",
+    "proDeptId": 13456,
+    "clientKey": {
+      "clientID": "021251",
+      "clientSecret": "200005463678"
+    },
+    "dDAuth": {
+      "agebtId": "12300456",
+      "appKey": "32001",
+      "appSecret": "12300456"
+    },
+    "azureClient": {
+      "storage": "0000",
+      "cosmos": "1234005",
+      "redis": "224005",
+      "servicBus": "12300455"
+    }
+  }
+]

+ 51 - 0
TEAMModelBI/Models/SysConfig.cs

@@ -0,0 +1,51 @@
+namespace TEAMModelBI.Models
+{
+    /// <summary>
+    /// 配置文件
+    /// </summary>
+    public class SysConfig
+    {
+        //public string id { get; set; }
+        /// <summary>
+        /// 站点
+        /// </summary>
+        public string site { get; set; }
+        /// <summary>
+        /// 部门Id
+        /// </summary>
+        public long proDeptId { get; set; }
+        /// <summary>
+        /// 连接秘钥
+        /// </summary>
+        public ClientKey clientKey { get; set; }
+        /// <summary>
+        /// 钉钉秘钥
+        /// </summary>
+        public DDAuth dDAuth { get; set; }
+        /// <summary>
+        /// 数据库连接字串
+        /// </summary>
+        public AzureClient azureClient { get; set; }
+    }
+
+    public class ClientKey
+    {
+        public string clientID { get; set; }
+        public string clientSecret { get; set; }
+    }
+
+    public class DDAuth
+    {
+        public string agebtId { get; set; }
+        public string appKey { get; set; }
+        public string appSecret { get; set; }
+    }
+
+    public class AzureClient
+    {
+        public string storage { get; set; }
+        public string cosmos { get; set; }
+        public string redis { get; set; }
+        public string servicBus { get; set; }
+    }
+}

+ 2 - 0
TEAMModelBI/Startup.cs

@@ -15,6 +15,7 @@ using System.Collections.Generic;
 using System.IdentityModel.Tokens.Jwt;
 using System.Linq;
 using System.Threading.Tasks;
+using TEAMModelBI.Models;
 using TEAMModelOS.Models;
 using TEAMModelOS.SDK;
 using TEAMModelOS.SDK.DI;
@@ -98,6 +99,7 @@ namespace TEAMModelBI
             services.AddHttpClient<CoreAPIHttpService>();
             services.AddMemoryCache();
             services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = false; });
+            services.Configure<SysConfig>(Configuration.GetSection("sysConfig"));  //注册连接
             //HttpContextAccessor,并用来访问HttpContext。(提供組件或非控制器服務存取HttpContext)
             services.AddHttpContextAccessor();
             services.Configure<Option>(options => Configuration.GetSection("Option").Bind(options));

+ 8 - 1
TEAMModelBI/Tool/TimeHelper.cs

@@ -226,7 +226,14 @@ namespace TEAMModelBI.Tool
                     DateTimeOffset weekEnd  = dateTime.AddDays(7-(int)(dateTime.DayOfWeek));
                     tempStrart = new DateTime(weekStrart.Year, weekStrart.Month, weekStrart.Day);
                     tempEnt = new DateTime(weekEnd.Year, weekEnd.Month, weekEnd.Day, 23, 59, 59); 
-
+                    break;
+                case "lastweek":
+                    var m = (dateTime.DayOfWeek == DayOfWeek.Sunday ? (DayOfWeek)7 : dateTime.DayOfWeek) - DayOfWeek.Monday;
+                    var s = (dateTime.DayOfWeek == DayOfWeek.Sunday ? (DayOfWeek)7 : dateTime.DayOfWeek) - (DayOfWeek)7;
+                    var Mon = dateTime.AddDays((-7 - m));//星期一
+                    var Sun = dateTime.AddDays((-7 - s));  //星期日
+                    tempStrart = new DateTime(Mon.Year, Mon.Month, Mon.Day);
+                    tempEnt = new DateTime(Sun.Year, Sun.Month, Sun.Day, 23, 59, 59);
 
                     break;
                 default: