fly há 2 anos atrás
pai
commit
50ceb4ba04
3 ficheiros alterados com 323 adições e 2 exclusões
  1. 267 0
      Common/ConvertHelper.cs
  2. 42 2
      Service/EducationService.Exam.cs
  3. 14 0
      Service/EducationService.cs

+ 267 - 0
Common/ConvertHelper.cs

@@ -0,0 +1,267 @@
+using System;
+using Newtonsoft.Json;
+using System.Globalization;
+using WingInterfaceLibrary.Enum;
+
+namespace WingEducationService.Common
+{
+    public static class ConvertHelper
+    {
+        /// <summary>
+        /// convert object to Json string
+        /// </summary>
+        /// <param name="obj">object</param>
+        /// <param name="formatting">format</param>
+        /// <param name="settings">settings</param>
+        /// <returns></returns>
+        public static string ToJson(this object obj, Formatting formatting = Formatting.None, JsonSerializerSettings settings = null)
+        {
+            if (obj == null) return string.Empty;
+            try
+            {
+                return JsonConvert.SerializeObject(obj, formatting, settings);
+            }
+            catch { return string.Empty; }
+        }
+
+        /// <summary>
+        /// convert Json string to object
+        /// </summary>
+        /// <typeparam name="T">object type</typeparam>
+        /// <param name="jsonStr">Json string</param>
+        /// <param name="settings">settings</param>
+        /// <returns></returns>
+        public static T JsonToObj<T>(this string jsonStr, JsonSerializerSettings settings = null)
+        {
+            if (string.IsNullOrWhiteSpace(jsonStr)) return default(T);
+            try
+            {
+                return JsonConvert.DeserializeObject<T>(jsonStr, settings);
+            }
+            catch(Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+                return default(T);
+            }
+        }
+
+        /// <summary>
+        /// convert Json string to object
+        /// </summary>
+        /// <param name="jsonStr">Json string</param>
+        /// <param name="type">object type</param>
+        /// <param name="settings">settings</param>
+        /// <returns></returns>
+        public static object JsonToObj(this string jsonStr, Type type, JsonSerializerSettings settings = null)
+        {
+            if (string.IsNullOrWhiteSpace(jsonStr)) return type.IsValueType ? Activator.CreateInstance(type) : null;
+            try
+            {
+                return JsonConvert.DeserializeObject(jsonStr, type, settings);
+            }
+            catch { return type.IsValueType ? Activator.CreateInstance(type) : null; }
+        }
+        /// <summary>
+        /// convert sting to int
+        /// </summary>
+        /// <param name="str">string</param>
+        /// <param name="defaultValue">default Value</param>
+        /// <returns></returns>
+        public static int ToInt(this string str, int defaultValue = 0)
+        {
+            if (string.IsNullOrWhiteSpace(str)) return defaultValue;
+            int.TryParse(str, out defaultValue);
+            return defaultValue;
+        }
+
+        public static double ToDouble(this string str, double defaultValue = 0)
+        {
+            if (string.IsNullOrWhiteSpace(str)) return defaultValue;
+            double.TryParse(str, out defaultValue);
+            return defaultValue;
+        }
+
+        /// <summary>
+        /// convert sting to float
+        /// </summary>
+        /// <param name="str"></param>
+        /// <param name="defaultValue"></param>
+        /// <returns></returns>
+        public static float ToFloat(this string str, float defaultValue = 0)
+        {
+            if (string.IsNullOrWhiteSpace(str)) return defaultValue;
+            float.TryParse(str, out defaultValue);
+            return defaultValue;
+        }
+
+        /// <summary>
+        /// convert sting to DateTime
+        /// </summary>
+        /// <param name="str"></param>
+        /// <param name="defaultValue"></param>
+        /// <returns></returns>
+        public static DateTime ToDateTime(this string str)
+        {
+            DateTime defaultValue;
+            if (string.IsNullOrWhiteSpace(str)) return DateTime.MinValue;
+            DateTime.TryParse(str, out defaultValue);
+            return defaultValue;
+        }
+
+        /// <summary>
+        /// convert sting to int
+        /// </summary>
+        /// <param name="str">string</param>
+        /// <param name="defaultValue">default Value</param>
+        /// <returns></returns>
+        public static long ToLong(this string str, long defaultValue = 0)
+        {
+            if (string.IsNullOrWhiteSpace(str)) return defaultValue;
+            long.TryParse(str, out defaultValue);
+            return defaultValue;
+        }
+
+        /// <summary>
+        /// Convert string to other type object
+        /// </summary>
+        /// <param name="str">string</param>
+        /// <param name="type">type</param>
+        /// <returns></returns>
+        public static object ToObject(this string str, Type type)
+        {
+            try
+            {
+                return Convert.ChangeType(str, type, CultureInfo.InvariantCulture);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+                return type.IsValueType ? Activator.CreateInstance(type) : null;
+            }
+        }
+
+        /// <summary>
+        /// convert sting to Boolean
+        /// </summary>
+        /// <param name="str">sting</param>
+        /// <returns></returns>
+        public static bool ToBool(this string str)
+        {
+            if (string.IsNullOrWhiteSpace(str)) return false;
+            bool.TryParse(str, out bool val);
+            return val;
+        }
+
+        /// <summary>
+        /// 验证是否为空
+        /// </summary>
+        /// <param name="str">sting</param>
+        /// <returns>true</returns>
+        public static bool IsEmpty(this string str)
+        {
+            if (string.IsNullOrWhiteSpace(str))
+            {
+                return true;
+            }
+            return false;
+        }
+
+        /// <summary>
+        /// 时间转长整形的时间戳
+        /// </summary>
+        /// <param name="time">当前时间</param>
+        /// <param name="isUtc">是否utc格式时间戳</param>
+        /// <returns>true</returns>
+        public static long ToTimestamp(this DateTime time, bool isUtc = false)
+        {
+            DateTime refTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Utc);
+            var diffSpan = (isUtc ? time : time.ToUniversalTime()) - refTime;
+            long unixTime = (long)Math.Round(diffSpan.TotalMilliseconds, MidpointRounding.AwayFromZero);
+            return unixTime;
+        }
+
+        
+        /// <summary>
+        /// url转token
+        /// </summary>
+        /// <param name="url">url地址</param>
+        /// <returns>true</returns>
+        public static string ToUrlToken(this string url)
+        {
+            string fileToken = "";
+            if (string.IsNullOrWhiteSpace(url))
+            {
+                return fileToken;
+            }
+            var fileName = "";
+            if (url.Contains("http://") || url.Contains("https://"))
+            {
+                fileToken = TransferUrlToToken(url);
+                return fileToken;
+            }
+            else
+            {
+                fileName = url;
+            }
+            //处理 传的是token 的情况
+            if (string.IsNullOrEmpty(fileName))
+            {
+                return fileToken;
+            }
+            if (fileName.Split('.').Length > 1)
+            {
+                int lastIndex = fileName.LastIndexOf('.');
+                var fileExtend = fileName.Substring(lastIndex + 1);
+                try
+                {
+                    var fileTypeEnum = (WingInterfaceLibrary.Enum.UploadFileTypeEnum)Enum.Parse(typeof(WingInterfaceLibrary.Enum.UploadFileTypeEnum), fileExtend.ToUpper());
+                    var fileType = Enum.GetName(typeof(WingInterfaceLibrary.Enum.UploadFileTypeEnum), (int)fileTypeEnum);
+                    if (!string.IsNullOrEmpty(fileType))
+                    {
+                        fileName = fileName.Substring(0, lastIndex);
+                    }
+                    else
+                    {
+                        fileName = "";
+                    }
+                }
+                catch (Exception ex)
+                {
+                    fileName = "";
+                }
+            }
+            if (fileName.Split('_').Length > 1)
+            {
+                fileToken = fileName;
+            }
+            return fileToken;
+        }
+
+        /// <summary>
+        /// url转token
+        /// </summary>
+        /// <param name="url">url地址</param>
+        /// <returns>true</returns>
+        private static string TransferUrlToToken(string url)
+        {
+            //需要做处理
+            var newUrl = url.Replace("http://", "").Replace("https://", "");
+            var urlArray = newUrl.Split('/');
+            var storageServer = WingServerCommon.Config.ConfigurationManager.GetParammeter<WingServerCommon.Config.Parameters.StringParameter>("Storage", "StorageServer").Value;
+            if (storageServer == "COS") {
+                if (urlArray.Length >= 2)
+                {
+                    var storageSettingsParam = WingServerCommon.Config.ConfigurationManager.StorageSettings;
+                    var fileName = storageSettingsParam.CDNServerUrl + "/" + urlArray[1];
+                    return fileName;
+                }
+                else {
+                    return url;
+                }
+            }
+            else {
+                return url;
+            }
+        }     
+    }
+}

+ 42 - 2
Service/EducationService.Exam.cs

@@ -16,6 +16,7 @@ using WingInterfaceLibrary.Enum.CourseEnum;
 using WingServerCommon.Mapper;
 using WingInterfaceLibrary.Request.DBRequest;
 using WingInterfaceLibrary.Request;
+using WingEducationService.Common;
 
 namespace WingEducationService.Service
 {
@@ -88,7 +89,43 @@ namespace WingEducationService.Service
         /// <returns></returns>
         public async Task<bool> UploadRemoteExaminationDataAsync(UploadRemoteExamRequest request)
         {
-            return false;
+            string deviceUniqueCode = request.DeviceUniqueCode;
+            //设备物理唯一码必填
+            if (string.IsNullOrWhiteSpace(deviceUniqueCode))
+            {
+                ThrowRpcException(CustomerRpcCode.UniqueCodeIsEmpty, "UniqueCodeIsEmpty");
+            }
+            var deviceInfo = await _deviceDBService.FindDeviceInfoBySerialNumberAsync(deviceUniqueCode);
+            if (deviceInfo == null || string.IsNullOrWhiteSpace(deviceInfo.DeviceCode))
+            {
+                ThrowRpcException(CustomerRpcCode.UniqueCodeIsError, "UniqueCodeIsError");
+            }
+            if (string.IsNullOrWhiteSpace(request.FileToken))
+            {
+                ThrowCustomerException(CustomerRpcCode.FileTokenIsEmpty, "FileToken is empty");
+            }
+            if (string.IsNullOrWhiteSpace(request.PreviewFileToken))
+            {
+                ThrowCustomerException(CustomerRpcCode.PreviewFileTokenIsEmpty, "PreviewFileToken is empty");
+            }
+            if (string.IsNullOrWhiteSpace(request.CoverImageToken))
+            {
+                ThrowCustomerException(CustomerRpcCode.CoverImageTokenIsEmpty, "CoverImageToken is empty");
+            }
+            var dataDTO = new RemoteExaminationDTO();
+            dataDTO.DeviceCode = deviceInfo.DeviceCode;
+            dataDTO.Application = request.Application;
+            var originImageUrl = request.FileToken;
+            dataDTO.OriginImageUrl = originImageUrl;//源站地址
+            dataDTO.ImageUrl = request.FileToken.ToUrlToken();//CDN 地址
+            dataDTO.PreviewUrl = request.PreviewFileToken.ToUrlToken();
+            dataDTO.CoverImageUrl = request.CoverImageToken.ToUrlToken();
+            dataDTO.ImageSize = request.FileSize;
+            dataDTO.FileDataType = request.FileDataType;
+            dataDTO.MeasuredResult = request.MeasuredResult;
+            dataDTO.CommentResult = request.CommentResult;
+            var result = await _educationDBService.CreateRemoteExaminationDTODBAsync(dataDTO)
+            return true;
         }
 
         /// <summary>
@@ -98,7 +135,10 @@ namespace WingEducationService.Service
         /// <returns></returns>
         public async Task<IList<RemoteExaminationPageDTO>> GetRemoteExaminationList(TokenRequest request)
         {
-            return null;
+            var userCode = await GetClientIdByTokenAsync(request.Token);
+            var dbRequest = new FindRemoteExaminationListDBRequest { UserCode = userCode };
+            var result = await _educationDBService.FindRemoteExaminationListDBAsync(dbRequest);
+            return result;
         }
 
     }

+ 14 - 0
Service/EducationService.cs

@@ -37,6 +37,7 @@ namespace WingEducationService.Service
         private IWingRtcService _wingRtcService;
         private ILockService _lockService;
         private LiveCourseRoomManager _liveCourseRoomManager;
+        private IDeviceInfoDBService _deviceDBService;
         /// <summary>
         /// 默认构造
         /// </summary>
@@ -56,6 +57,7 @@ namespace WingEducationService.Service
             _educationDBService = GetProxy<IEducationDBService>();
             _authenticationService = GetProxy<IAuthenticationService>();
             _wingRtcService = GetProxy<IWingRtcService>();
+            _deviceDBService = GetProxy<IDeviceInfoDBService>();
             _liveCourseRoomManager = new LiveCourseRoomManager(CreateRoom);
             LiveCourseRoom.OnOpenNotifyQueue = OpenNotifyQueue;
             LiveCourseRoom.OnBroadcastMessage = BroadcastMessage;
@@ -76,6 +78,18 @@ namespace WingEducationService.Service
 
         #region Basic Method
 
+        /// <summary>
+        /// 通用异常处理方法
+        /// </summary>
+        /// <param name="customerRpcCodeEnum">异常标识码</param>
+        /// <param name="msg">异常信息</param>
+        /// <param name="internalMessage">内部异常信息,非必填</param>
+        private void ThrowCustomerException(CustomerRpcCode customerRpcCodeEnum, string msg, string internalMessage = null)
+        {
+            ThrowRpcException((int)customerRpcCodeEnum, msg, internalMessage);
+        }
+
+
         private bool ApplyLock(string lockKey)
         {
             try