StringExtend.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* ==============================================================================
  2. * 功能描述:StringExtend
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/5/29 10:51:45
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Autodesk.Revit.DB;
  13. using Newtonsoft.Json.Linq;
  14. using SAGA.DotNetUtils;
  15. using SAGA.DotNetUtils.Extend;
  16. using SAGA.MBI.Model;
  17. using SAGA.RevitUtils.Extends;
  18. namespace SAGA.MBI.Tools
  19. {
  20. /// <summary>
  21. /// StringExtend
  22. /// </summary>
  23. public static class StringExtend
  24. {
  25. /// <summary>
  26. /// 云平台BIMId转化为Revit Id
  27. /// 去掉文件名前缀,找不到返回值为0
  28. /// </summary>
  29. /// <returns></returns>
  30. public static int GetBIMID(this string cbimId)
  31. {
  32. string bimId = cbimId;
  33. var strs = cbimId.Split(':');
  34. if (strs.Length > 1)
  35. bimId = strs[1];
  36. return bimId.ToInt();
  37. }
  38. /// <summary>
  39. /// 仅保留岗位,没有对应模型的bimid
  40. /// 格式为:
  41. /// a,只有楼层 id,没有模型bimid
  42. /// b,""
  43. /// </summary>
  44. /// <returns></returns>
  45. public static bool IsOnlyDutyNoModelBIMID(this string cbimId)
  46. {
  47. var strs = cbimId.Split(':');
  48. return strs.Length != 2;
  49. }
  50. /// <summary>
  51. /// 云平台BIMId转化为Floor Id
  52. /// 去掉文件名后缀
  53. /// </summary>
  54. /// <returns></returns>
  55. public static string GetFloorId(this string cbimId)
  56. {
  57. string bimId = cbimId;
  58. var strs = cbimId.Split(':');
  59. if (strs.Length > 1)
  60. bimId = strs[0];
  61. return bimId;
  62. }
  63. /// <summary>
  64. /// 获取Attachment类型的信息点值
  65. /// </summary>
  66. /// <param name="json"></param>
  67. /// <param name="key">信息点的code编码</param>
  68. public static ObservableCollection<MServiceAttachment> GetAttachmentsItems(this string json, string key)
  69. {
  70. var serviceDrawings = new ObservableCollection<MServiceAttachment>();
  71. JObject infoJObject = JObject.Parse(json);
  72. if (infoJObject.IsContainKeyEx(key))
  73. {
  74. foreach (JObject imageObj in infoJObject[key])
  75. {
  76. serviceDrawings.Add(new MServiceAttachment(imageObj.GetValueEx("key"),
  77. imageObj.GetValueEx("name"), imageObj.GetValueEx("type").ConvertToServiceImageType()));
  78. }
  79. }
  80. return serviceDrawings;
  81. }
  82. /// <summary>
  83. /// string 转化为图片类型枚举
  84. /// </summary>
  85. /// <param name="value"></param>
  86. /// <returns></returns>
  87. public static ServiceImageType ConvertToServiceImageType(this string value)
  88. {
  89. Enum.TryParse(value, out ServiceImageType type);
  90. return type;
  91. }
  92. /// <summary>
  93. /// 返回数据是验证-带message
  94. /// </summary>
  95. /// <param name="request"></param>
  96. /// <returns></returns>
  97. public static bool IsSuccessRequest(this string request, ref string msg)
  98. {
  99. bool result = false;
  100. if (request.IsNullOrEmpty()) return result;
  101. try
  102. {
  103. JObject jObject = JObject.Parse(request);
  104. result = jObject["Result"].ToString() == "success";
  105. msg = jObject["ResultMsg"].ToString();
  106. }
  107. catch (Exception e)
  108. {
  109. result = false;
  110. msg = e.StackTrace;
  111. }
  112. return result;
  113. }
  114. /// <summary>
  115. /// 返回数据是验证
  116. /// </summary>
  117. /// <param name="request"></param>
  118. /// <returns></returns>
  119. public static bool IsSuccessRequest(this string request)
  120. {
  121. bool result = false;
  122. if (request.IsNullOrEmpty()) return result;
  123. try
  124. {
  125. JObject jObject = JObject.Parse(request);
  126. result = jObject["Result"].ToString() == "success";
  127. }
  128. catch (Exception e)
  129. {
  130. result = false;
  131. }
  132. return result;
  133. }
  134. /// <summary>
  135. /// 返回数据是否正确
  136. /// 数据格式为result,message
  137. /// </summary>
  138. /// <param name="request"></param>
  139. /// <returns></returns>
  140. public static bool IsSuccessRequest2(this string request,ref string msg)
  141. {
  142. bool result = false;
  143. if (request.IsNullOrEmpty()) return result;
  144. try
  145. {
  146. JObject jObject = JObject.Parse(request);
  147. result = jObject["result"].ToString() == "success";
  148. msg = jObject["message"].ToString();
  149. }
  150. catch (Exception e)
  151. {
  152. result = false;
  153. }
  154. return result;
  155. }
  156. /// <summary>
  157. /// 返回的集合不为空集合
  158. /// </summary>
  159. /// <param name="request"></param>
  160. /// <returns></returns>
  161. public static bool IsRequestHasItem(this string request)
  162. {
  163. bool result = false;
  164. if (IsSuccessRequest(request))
  165. {
  166. try
  167. {
  168. JObject jObject = JObject.Parse(request);
  169. if (jObject.IsContainKeyEx("Count"))
  170. {
  171. result = jObject.GetValueEx("Count").ToInt() != 0;
  172. }
  173. else
  174. {
  175. if (jObject.IsContainKeyEx("Content"))
  176. result = ((JArray)jObject["Content"]).Count != 0;
  177. }
  178. }
  179. catch (Exception e)
  180. {
  181. result = false;
  182. }
  183. }
  184. return result;
  185. }
  186. }
  187. }