PublicFunction.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Text.RegularExpressions;
  6. using MySystem.Models;
  7. using Library;
  8. using NPOI.SS.UserModel;
  9. using NPOI.XSSF.UserModel;
  10. using System.IO;
  11. namespace MySystem
  12. {
  13. public class PublicFunction
  14. {
  15. public WebCMSEntities db = new WebCMSEntities();
  16. public BsModels.WebCMSEntities bsdb = new BsModels.WebCMSEntities();
  17. public OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  18. #region 获取权限JSON数据
  19. public string GetRightJson()
  20. {
  21. string result = "[";
  22. List<BsModels.RightDic> list = bsdb.RightDic.ToList();
  23. List<BsModels.RightDic> Level1 = list.Where(m => m.RightLevel == 1).ToList();
  24. foreach (BsModels.RightDic sub1 in Level1)
  25. {
  26. result += "{title: '" + sub1.Name + "', id: '" + sub1.Id + "', spread: false, children: [";
  27. List<BsModels.RightDic> Level2 = list.Where(m => m.RightLevel == 2 && m.Id.StartsWith(sub1.Id)).ToList();
  28. if (Level2.Count > 0)
  29. {
  30. foreach (BsModels.RightDic sub2 in Level2)
  31. {
  32. result += "{title: '" + sub2.Name + "', id: '" + sub2.Id + "', spread: false, children: [";
  33. List<BsModels.RightDic> Level3 = list.Where(m => m.RightLevel == 3 && m.Id.StartsWith(sub2.Id)).ToList();
  34. if (Level3.Count > 0)
  35. {
  36. foreach (BsModels.RightDic sub3 in Level3)
  37. {
  38. result += "{title: '" + sub3.Name + "', id: '" + sub3.Id + "', spread: false, children: [";
  39. string rightString = ForOperateRight(sub3.Id);
  40. if (!string.IsNullOrEmpty(rightString))
  41. {
  42. result += rightString;
  43. }
  44. else
  45. {
  46. result += "{title: '基本权限', id: '" + sub3.Id + "_base'}";
  47. }
  48. result += "]},";
  49. }
  50. result = result.TrimEnd(',');
  51. }
  52. else
  53. {
  54. string rightString = ForOperateRight(sub2.Id);
  55. if (!string.IsNullOrEmpty(rightString))
  56. {
  57. result += rightString;
  58. }
  59. else
  60. {
  61. result += "{title: '基本权限', id: '" + sub2.Id + "_base'}";
  62. }
  63. }
  64. result += "]},";
  65. }
  66. result = result.TrimEnd(',');
  67. }
  68. else
  69. {
  70. result += ForOperateRight(sub1.Id);
  71. string rightString = ForOperateRight(sub1.Id);
  72. if (!string.IsNullOrEmpty(rightString))
  73. {
  74. result += rightString;
  75. }
  76. else
  77. {
  78. result += "{title: '基本权限', id: '" + sub1.Id + "_base'}";
  79. }
  80. }
  81. result += "]},";
  82. }
  83. result = result.TrimEnd(',');
  84. result += "]";
  85. return result;
  86. }
  87. //读取当前菜单有哪些权限
  88. public string ForOperateRight(string Id)
  89. {
  90. string result = "";
  91. List<BsModels.MenuRight> rights = bsdb.MenuRight.Where(m => m.MenuId.StartsWith(Id)).OrderBy(m => m.MenuId).ToList();
  92. foreach (BsModels.MenuRight right in rights)
  93. {
  94. result += "{title: '" + right.Name + "', id: '" + right.MenuId + "'},";
  95. }
  96. BsModels.RightDic rightDic = bsdb.RightDic.FirstOrDefault(m => m.Id == Id) ?? new BsModels.RightDic();
  97. if (!string.IsNullOrEmpty(rightDic.OtherRight))
  98. {
  99. string[] OtherRightList = rightDic.OtherRight.Split('\n');
  100. foreach (string SubOtherRight in OtherRightList)
  101. {
  102. string[] SubOtherRightData = SubOtherRight.Split('_');
  103. result += "{title: '" + SubOtherRightData[1] + "', id: '" + Id + "_" + SubOtherRightData[0] + "'},";
  104. }
  105. }
  106. result = result.TrimEnd(',');
  107. return result;
  108. }
  109. public string TranslateRightString(string RightString)
  110. {
  111. string result = "";
  112. if (!string.IsNullOrEmpty(RightString))
  113. {
  114. string[] RightList = RightString.Split(',');
  115. foreach (string sub in RightList)
  116. {
  117. string EndString = sub.Substring(sub.LastIndexOf("_") + 1);
  118. if (EndString.Length > 1 && !function.IsInt(EndString))
  119. {
  120. result += sub + ",";
  121. }
  122. }
  123. }
  124. return result.TrimEnd(',');
  125. }
  126. #endregion
  127. #region 中译英
  128. public string TranslateZHToEn(string Source)
  129. {
  130. string result = function.GetWebRequest("http://fanyi.youdao.com/translate?&doctype=json&type=ZH_CN2EN&i=" + Source);
  131. //{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":2,"translateResult":[[{"src":"大事件","tgt":"The big event"}]]}
  132. Match match = Regex.Match(result, "\"tgt\":\".*?\"");
  133. if (match.Success)
  134. {
  135. return match.Value.Replace("\"tgt\":", "").Trim('"');
  136. }
  137. return "";
  138. }
  139. #endregion
  140. #region 解析编辑器里的视频代码
  141. public string CheckMediaFromHtml(string content)
  142. {
  143. if (string.IsNullOrEmpty(content))
  144. {
  145. return "";
  146. }
  147. string result = content;
  148. MatchCollection mc = Regex.Matches(content, "<embed.*?/>");
  149. foreach (Match submc in mc)
  150. {
  151. string info = submc.Value;
  152. Match match = Regex.Match(info, "src=\".*?\"");
  153. if (match.Success)
  154. {
  155. string path = match.Value;
  156. path = path.Replace("src=\"", "");
  157. path = path.TrimEnd(new char[] { '"' });
  158. string html = "";
  159. string width = "600";
  160. string height = "300";
  161. Match width_match = Regex.Match(info, "width=\".*?\"");
  162. if (width_match.Success)
  163. {
  164. width = width_match.Value.Replace("width=", "").Trim('"');
  165. }
  166. Match height_match = Regex.Match(info, "height=\".*?\"");
  167. if (height_match.Success)
  168. {
  169. height = height_match.Value.Replace("height=", "").Trim('"');
  170. }
  171. if (path.EndsWith(".mp4"))
  172. {
  173. if (string.IsNullOrEmpty(html))
  174. {
  175. html = "<video controls=\"controls\" autoplay=\"autoplay\" poster=\"\" onplay=\"true\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
  176. "<source src=\"" + path + "\">" +
  177. "<source src=\"" + path + "\" type=\"video/mp4\">" +
  178. "<source src=\"" + path + "\" type=\"video/webm\">" +
  179. "<source src=\"" + path + "\" type=\"video/ogg\">" +
  180. "</video>";
  181. }
  182. }
  183. else if (path.EndsWith(".mp3"))
  184. {
  185. html = "<audio controls=\"controls\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
  186. "<source src=\"" + path + "\" type=\"audio/mp3\" />" +
  187. "<embed src=\"" + path + "\" height=\"100\" width=\"100\" />" +
  188. "</audio>";
  189. }
  190. result = result.Replace(info, html);
  191. }
  192. }
  193. return result;
  194. }
  195. #endregion
  196. #region 对象转Json字符串
  197. public static string ObjectToJsonString(object obj)
  198. {
  199. return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  200. }
  201. #endregion
  202. #region Json字符串转对象
  203. public static T DeserializeJSON<T>(string json)
  204. {
  205. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
  206. }
  207. #endregion
  208. #region 删除文件
  209. public static bool DeleteFile(string VirtualPath)
  210. {
  211. string FilePath = function.getPath(VirtualPath);
  212. if (System.IO.File.Exists(FilePath))
  213. {
  214. System.IO.File.Delete(FilePath);
  215. return true;
  216. }
  217. return false;
  218. }
  219. #endregion
  220. #region 两点距离
  221. public static double GetDistanceNumber(string start, string end)
  222. {
  223. if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
  224. {
  225. string[] startpos = start.Split(',');
  226. string[] endpos = end.Split(',');
  227. double lng1 = double.Parse(startpos[0]);
  228. double lat1 = double.Parse(startpos[1]);
  229. double lng2 = double.Parse(endpos[0]);
  230. double lat2 = double.Parse(endpos[1]);
  231. double radLat1 = rad(lat1);
  232. double radLat2 = rad(lat2);
  233. double a = radLat1 - radLat2;
  234. double b = rad(lng1) - rad(lng2);
  235. double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
  236. s = s * EARTH_RADIUS;
  237. s = Math.Round(s * 10000) / 10000;
  238. return s;
  239. }
  240. return 10000000;
  241. }
  242. private static double rad(double d)
  243. {
  244. return d * Math.PI / 180.0;
  245. }
  246. private static double EARTH_RADIUS = 6378.137;
  247. #endregion
  248. #region 短信条数
  249. public int SMSCount()
  250. {
  251. SystemSet check = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  252. return check.QueryCount;
  253. }
  254. #endregion
  255. #region 短信消耗
  256. public void UseSMS()
  257. {
  258. SystemSet check = db.SystemSet.FirstOrDefault();
  259. if (check != null)
  260. {
  261. check.QueryCount -= 1;
  262. db.SaveChanges();
  263. }
  264. }
  265. #endregion
  266. #region 获取OSS开关
  267. public string GetOssStatus()
  268. {
  269. SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  270. return set.UploadOss == 1 ? "-oss" : "";
  271. }
  272. #endregion
  273. #region 获取上传文件参数
  274. public string GetUploadParam(string buttonId)
  275. {
  276. string tag = function.MD5_16(buttonId);
  277. string param = RedisDbconn.Instance.Get<string>("btn:" + tag);
  278. if (!string.IsNullOrEmpty(param))
  279. {
  280. return param;
  281. }
  282. SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  283. return "{width:" + set.UploadAutoZoomWidth + ",height:" + set.UploadAutoZoomHeight + ",quality:" + set.UploadAutoZoomQuality + "},{max_file_size:" + set.UploadMaxSize + "}";
  284. }
  285. #endregion
  286. #region 获取上传文件提示文字
  287. public string GetUploadHint(string buttonId, bool ispic = true)
  288. {
  289. string tag = function.MD5_16(buttonId);
  290. string param = RedisDbconn.Instance.Get<string>("btn:" + tag);
  291. if (string.IsNullOrEmpty(param))
  292. {
  293. SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  294. param = "{width:" + set.UploadAutoZoomWidth + ",height:" + set.UploadAutoZoomHeight + ",quality:" + set.UploadAutoZoomQuality + "},{max_file_size:" + set.UploadMaxSize + "}";
  295. }
  296. string[] json = param.Replace(" ", "").Replace("},{", "}#cut#{").Split(new string[] { "#cut#" }, StringSplitOptions.None);
  297. Dictionary<string, string> resize = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json[0]);
  298. Dictionary<string, string> size = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json[1]);
  299. int max_file_size = int.Parse(size["max_file_size"]) / 1024;
  300. string unit = "KB";
  301. if (max_file_size / 1024 > 0)
  302. {
  303. max_file_size = max_file_size / 1024;
  304. unit = "MB";
  305. }
  306. if (max_file_size / 1024 > 0)
  307. {
  308. max_file_size = max_file_size / 1024;
  309. unit = "GB";
  310. }
  311. if (ispic)
  312. {
  313. return "建议尺寸" + resize["width"] + "x" + resize["height"] + "," + max_file_size + unit + "以内";
  314. }
  315. return "建议" + max_file_size + "以内";
  316. }
  317. #endregion
  318. #region 通过表名、文本、值获得字典数据
  319. public Dictionary<string, string> GetDictionaryByTableName(string tableEnName, string Text, string Value)
  320. {
  321. Text = Text.Split('_')[0];
  322. Value = Value.Split('_')[0];
  323. Dictionary<string, string> dic = new Dictionary<string, string>();
  324. DataTable dt = dbconn.dtable("select " + Text + "," + Value + " from " + tableEnName + " order by Sort desc,Id asc");
  325. foreach (DataRow dr in dt.Rows)
  326. {
  327. dic.Add(dr[1].ToString(), dr[0].ToString());
  328. }
  329. return dic;
  330. }
  331. #endregion
  332. #region 获取Excel文件内容
  333. public DataTable ExcelToDataTable(string filepath)
  334. {
  335. List<string> result = new List<string>();
  336. DataTable dtTable = new DataTable();
  337. List<string> rowList = new List<string>();
  338. try
  339. {
  340. ISheet sheet;
  341. using (var stream = new FileStream(filepath, FileMode.Open))
  342. {
  343. stream.Position = 0;
  344. XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream);
  345. sheet = xssWorkbook.GetSheetAt(0);
  346. IRow headerRow = sheet.GetRow(0);
  347. int cellCount = headerRow.LastCellNum;
  348. for (int j = 0; j < cellCount; j++)
  349. {
  350. ICell cell = headerRow.GetCell(j);
  351. if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
  352. {
  353. dtTable.Columns.Add(cell.ToString());
  354. }
  355. }
  356. for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
  357. {
  358. IRow row = sheet.GetRow(i);
  359. if (row == null) continue;
  360. if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
  361. for (int j = row.FirstCellNum; j < cellCount; j++)
  362. {
  363. if (row.GetCell(j) != null)
  364. {
  365. if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
  366. {
  367. rowList.Add(row.GetCell(j).ToString());
  368. }
  369. }
  370. }
  371. if (rowList.Count > 0)
  372. dtTable.Rows.Add(rowList.ToArray());
  373. rowList.Clear();
  374. }
  375. }
  376. }
  377. catch (Exception ex)
  378. {
  379. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "获取Excel文件内容异常");
  380. }
  381. return dtTable;
  382. }
  383. #endregion
  384. #region 重置创客机具数量
  385. public static void SycnMachineCount(int Id = 0, int BrandId = 0)
  386. {
  387. WebCMSEntities dbnew = new WebCMSEntities();
  388. string IdBrand = Id + "_" + BrandId;
  389. UserMachineData machineData = dbnew.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  390. if (machineData == null)
  391. {
  392. machineData = dbnew.UserMachineData.Add(new UserMachineData()
  393. {
  394. IdBrand = IdBrand
  395. }).Entity;
  396. dbnew.SaveChanges();
  397. }
  398. machineData.BindCount = dbnew.PosMachinesTwo.Count(m => m.Status > -1 && m.BuyUserId == Id && m.BrandId == BrandId && m.BindingState == 1);
  399. machineData.UnBindCount = dbnew.PosMachinesTwo.Count(m => m.Status > -1 && m.BuyUserId == Id && m.BrandId == BrandId && m.BindingState == 0);
  400. machineData.TotalMachineCount = machineData.BindCount + machineData.UnBindCount;
  401. dbnew.SaveChanges();
  402. dbnew.Dispose();
  403. }
  404. #endregion
  405. #region 统计机具绑定数据
  406. public static void StatUserMachineData(int UserId, int BrandId, int Count)
  407. {
  408. using (WebCMSEntities db = new WebCMSEntities())
  409. {
  410. string IdBrand = UserId + "_" + BrandId;
  411. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  412. if (MachineData == null)
  413. {
  414. MachineData = db.UserMachineData.Add(new UserMachineData()
  415. {
  416. IdBrand = IdBrand,
  417. }).Entity;
  418. db.SaveChanges();
  419. }
  420. if(Count < 0)
  421. {
  422. MachineData.TotalMachineCount -= Math.Abs(Count);
  423. MachineData.UnBindCount -= Math.Abs(Count);
  424. }
  425. else
  426. {
  427. MachineData.TotalMachineCount += Count;
  428. MachineData.UnBindCount += Count;
  429. }
  430. db.SaveChanges();
  431. }
  432. }
  433. #endregion
  434. #region 退回仓库,清除历史记录
  435. public static void ClearPosHistory(WebCMSEntities db, string PosSn)
  436. {
  437. PreSendStockDetail prepos = db.PreSendStockDetail.FirstOrDefault(m => m.SnNo == PosSn && m.Status == 1);
  438. if(prepos != null)
  439. {
  440. prepos.Status = -1;
  441. prepos.ApplyFlag = 0;
  442. // db.SaveChanges();
  443. }
  444. MachineApply apply = db.MachineApply.FirstOrDefault(m => m.SwapSnExpand.Contains(PosSn));
  445. if(apply != null)
  446. {
  447. apply.SwapSnExpand = apply.SwapSnExpand.Replace(PosSn, PosSn.Substring(0, 3) + "d" + PosSn.Substring(4));
  448. // db.SaveChanges();
  449. }
  450. }
  451. #endregion
  452. #region 测试创客Id
  453. public static int[] testids = { };
  454. #endregion
  455. #region 数据库结构
  456. public static Dictionary<string, Dictionary<string, string>> MainTables = new Dictionary<string, Dictionary<string, string>>();
  457. public static Dictionary<string, Dictionary<string, string>> CashTables = new Dictionary<string, Dictionary<string, string>>();
  458. public static Dictionary<string, Dictionary<string, string>> BsTables = new Dictionary<string, Dictionary<string, string>>();
  459. public static Dictionary<string, Dictionary<string, string>> JobTables = new Dictionary<string, Dictionary<string, string>>();
  460. public static Dictionary<string, Dictionary<string, string>> SpTables = new Dictionary<string, Dictionary<string, string>>();
  461. public static Dictionary<string, Dictionary<string, string>> OpTables = new Dictionary<string, Dictionary<string, string>>();//运营中心数据模型
  462. #endregion
  463. }
  464. }