GeneralMethod.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. using Microsoft.AspNetCore.Authentication;
  2. using Microsoft.AspNetCore.Authentication.Cookies;
  3. using Microsoft.AspNetCore.Mvc.TagHelpers;
  4. using Microsoft.International.Converters.PinYinConverter;
  5. using OASystem.API.OAMethodLib.JuHeAPI;
  6. using OASystem.Domain;
  7. using OASystem.Domain.Dtos.Financial;
  8. using OASystem.Domain.Entities.District;
  9. using OASystem.Domain.Entities.Financial;
  10. using OASystem.Domain.Entities.Groups;
  11. using OASystem.Domain.Entities.PersonnelModule;
  12. using OASystem.Domain.Entities.System;
  13. using OASystem.Domain.ViewModels.Financial;
  14. using OASystem.Domain.ViewModels.JuHeExchangeRate;
  15. using OASystem.Domain.ViewModels.PersonnelModule;
  16. using OASystem.Infrastructure.Repositories.Groups;
  17. using System.Collections.Generic;
  18. using System.IdentityModel.Tokens.Jwt;
  19. using System.Security.Claims;
  20. namespace OASystem.API.OAMethodLib
  21. {
  22. public static class GeneralMethod
  23. {
  24. //团组信息
  25. private readonly static DelegationInfoRepository _dirRep = AutofacIocManager.Instance.GetService<DelegationInfoRepository>();
  26. private readonly static TeamRateRepository _teamRateRep = AutofacIocManager.Instance.GetService<TeamRateRepository>();
  27. private readonly static IJuHeApiService _juHeApiService = AutofacIocManager.Instance.GetService<IJuHeApiService>();
  28. private readonly static SetDataRepository _setDataRep = AutofacIocManager.Instance.GetService<SetDataRepository>();
  29. private readonly static HttpContext _httpContext ;
  30. #region 消息
  31. #endregion
  32. #region md5 加密
  33. /// <summary>
  34. /// MD5加密,和动网上的16/32位MD5加密结果相同,
  35. /// 使用的UTF8编码
  36. /// </summary>
  37. /// <param name="source">待加密字串</param>
  38. /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
  39. /// <returns>加密后的字串</returns>
  40. public static string Encrypt(string source, int length = 32)
  41. {
  42. if (string.IsNullOrWhiteSpace(source))
  43. return string.Empty;
  44. HashAlgorithm hashAlgorithm = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
  45. byte[] bytes = Encoding.UTF8.GetBytes(source);
  46. byte[] hashValue = hashAlgorithm.ComputeHash(bytes);
  47. StringBuilder sb = new StringBuilder();
  48. switch (length)
  49. {
  50. case 16://16位密文是32位密文的9到24位字符
  51. for (int i = 4; i < 12; i++)
  52. {
  53. sb.Append(hashValue[i].ToString("x2"));
  54. }
  55. break;
  56. case 32:
  57. for (int i = 0; i < 16; i++)
  58. {
  59. sb.Append(hashValue[i].ToString("x2"));
  60. }
  61. break;
  62. default:
  63. for (int i = 0; i < hashValue.Length; i++)
  64. {
  65. sb.Append(hashValue[i].ToString("x2"));
  66. }
  67. break;
  68. }
  69. return sb.ToString();
  70. }
  71. #endregion
  72. #region jwt
  73. /// <summary>
  74. /// 获取token
  75. /// </summary>
  76. /// <param name="_config"></param>
  77. /// <param name="Number"></param>
  78. /// <param name="exp"></param>
  79. /// <returns></returns>
  80. public static async Task< string> GetToken(IConfiguration _config, string Number, int uId, string uName, DateTime exp)
  81. {
  82. string userId = Guid.NewGuid().ToString().Replace("-", "");
  83. var claims = new[] {
  84. new Claim(ClaimTypes.NameIdentifier, uName),
  85. new Claim(ClaimTypes.NameIdentifier, uId.ToString()),
  86. new Claim(ClaimTypes.NameIdentifier, userId),
  87. new Claim("Number",Number)
  88. };
  89. var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"]));
  90. var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
  91. var token = new JwtSecurityToken(
  92. issuer: "OASystem.com",
  93. audience: "OASystem.com",
  94. claims: claims,
  95. expires: exp,
  96. signingCredentials: creds);
  97. //var indentity = new ClaimsIdentity(claims, "formlogin");
  98. //var principal = new ClaimsPrincipal(indentity);
  99. // await _httpContext.SignInAsync (CookieAuthenticationDefaults.AuthenticationScheme, principal);
  100. return new JwtSecurityTokenHandler().WriteToken(token);
  101. }
  102. #endregion
  103. #region 数据类型转换
  104. /// <summary>
  105. /// object 转 Int
  106. /// </summary>
  107. /// <param name="obj"></param>
  108. /// <returns></returns>
  109. public static int GetInt(this object obj)
  110. {
  111. if (obj == null)
  112. return 0;
  113. int _number = 0;
  114. bool reslut = Int32.TryParse(obj.ToString(), out _number);
  115. return _number;
  116. }
  117. private static DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
  118. private static long longTime = 621355968000000000;
  119. private static int samllTime = 10000000;
  120. /// <summary>
  121. /// 时间戳 转 datetime
  122. /// </summary>
  123. /// <param name="timeStamp"></param>
  124. /// <returns></returns>
  125. public static DateTime GetTimeSpmpToDate(this object timeStamp)
  126. {
  127. if (timeStamp == null) return dateStart;
  128. DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime();
  129. return dateTime;
  130. }
  131. #endregion
  132. #region 用户页面操作功能 权限
  133. /// <summary>
  134. /// 用户页面操作功能(可使用)
  135. /// </summary>
  136. /// <param name="userId">用户Id</param>
  137. /// <param name="PageId">页面Id</param>
  138. /// <returns></returns>
  139. public static async Task<PageFunAuthViewBase> PostUserPageFuncDatas(int userId,int PageId)
  140. {
  141. PageFunAuthViewBase pageFunAuth = new PageFunAuthViewBase();
  142. List<UserPageFuncView> userPageFuncDatas = new List<UserPageFuncView>();
  143. string sql = string.Format(@"Select ua.UId As UserId, u.CnName As UserName, pa.ModuleId,pa.ModuleName,pa.PageId,pa.PageName,pa.PageIsEnable,
  144. pa.PagePhoneIsEnable,pa.FuncId,pa.FuncName,pa.FuncIsEnable
  145. From Sys_UserAuthority ua
  146. Left Join Sys_Users u On ua.UId = u.Id
  147. Left Join (
  148. Select sd.Id As ModuleId,sd.Name As ModuleName, smp.Id As PageId,smp.Name As PageName,smp.IsEnable As PageIsEnable,
  149. smp.phoneIsEnable As PagePhoneIsEnable,pfp.Id As FuncId,pfp.FunctionName As FuncName,pfp.IsEnable As FuncIsEnable
  150. From Sys_SystemMenuAndFunction smaf
  151. Left Join Sys_SystemMenuPermission smp On smaf.SmId = smp.Id
  152. Left Join Sys_SetData sd On sd.STid = 5 And smp.Mid = sd.Id
  153. Left Join Sys_PageFunctionPermission pfp On smaf.FId = pfp.Id
  154. Where smaf.IsDel = 0 And smp.IsDel = 0 And pfp.IsDel = 0 And sd.IsDel = 0 And smp.IsEnable = 1
  155. ) As pa On ua.SmId = pa.PageId And ua.FId = pa.FuncId
  156. Where ua.IsDel = 0 And ua.UId = {0} And pa.PageId = {1}
  157. Order By ModuleId,PageId,FuncId Asc", userId, PageId);
  158. userPageFuncDatas = await _dirRep._sqlSugar.SqlQueryable<UserPageFuncView>(sql).ToListAsync();
  159. if (userPageFuncDatas.Count <= 0)
  160. {
  161. return pageFunAuth;
  162. }
  163. UserPageFuncView userPageFunc = new UserPageFuncView();
  164. //查询 1
  165. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 1).FirstOrDefault();
  166. if (userPageFunc != null) pageFunAuth.CheckAuth = 1;
  167. //删除 2
  168. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 2).FirstOrDefault();
  169. if (userPageFunc != null) pageFunAuth.DeleteAuth = 1;
  170. //编辑 3
  171. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 3).FirstOrDefault();
  172. if (userPageFunc != null) pageFunAuth.EditAuth = 1;
  173. //下载 4
  174. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 4).FirstOrDefault();
  175. if (userPageFunc != null) pageFunAuth.FilesDownloadAuth = 1;
  176. //上传 5
  177. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 5).FirstOrDefault();
  178. if (userPageFunc != null) pageFunAuth.FilesUploadAuth = 1;
  179. //添加 11
  180. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 11).FirstOrDefault();
  181. if (userPageFunc != null) pageFunAuth.AddAuth = 1;
  182. //审核 12
  183. userPageFunc = userPageFuncDatas.Where(it => it.FuncId == 12).FirstOrDefault();
  184. if (userPageFunc != null) pageFunAuth.AuditAuth = 1;
  185. return pageFunAuth;
  186. }
  187. #endregion
  188. #region 业务模块 团组权限
  189. /// <summary>
  190. /// 业务模块 团组操作权限
  191. /// 验证
  192. /// </summary>
  193. /// <param name="diId">团组Id</param>
  194. /// <param name="userId">用户Id</param>
  195. /// <param name="CTable">业务模块Id</param>
  196. /// <returns></returns>
  197. public static async Task<Result> PostGroupOperationAuth(int diId, int userId, int CTable )
  198. {
  199. Result _result = new Result { Code = -1,Msg = "No Operation Authorty!" };
  200. if (CTable < 1)
  201. {
  202. _result.Msg = "请填写正确的用户Id!";
  203. return _result;
  204. }
  205. var data = await _dirRep._sqlSugar.Queryable<Grp_GroupsTaskAssignment>().Where(it => it.DIId == diId && it.UId == userId && it.CTId == CTable && it.IsDel == 0).FirstAsync();
  206. if (data == null)
  207. {
  208. _result.Msg = "你没有本团下的该业务模块操作,请联系主管分配操作权限!";
  209. }
  210. else
  211. {
  212. _result.Code = 0;
  213. }
  214. return _result;
  215. }
  216. #endregion
  217. #region 团组相关
  218. #region 建团按国家默认添加汇率 / 默认权限分配
  219. /// <summary>
  220. /// 团组汇率
  221. /// 建团时 添加基础汇率 CNY
  222. /// 按国家 添加 默认币种
  223. /// </summary>
  224. /// <param name="userId"></param>
  225. /// <param name="diId"></param>
  226. /// <returns></returns>
  227. public static async Task<Result> PostGroupRateAddInit(int userId, int diId)
  228. {
  229. Result result = new() { Code = -2 };
  230. if (userId < 0)
  231. {
  232. result.Msg = string.Format(@"请传入正确的userId");
  233. return result;
  234. }
  235. if (diId < 0)
  236. {
  237. result.Msg = string.Format(@"请传入正确的DiId");
  238. return result;
  239. }
  240. //美元(USD):1.0000|欧元(EUR):1.0000|墨西哥比索(MXN):1.0000
  241. string CNYInit = string.Format(@"人名币(CNY):1.0000");
  242. var gropInfo = await _dirRep._sqlSugar.Queryable<Grp_DelegationInfo>().Where( it => it.IsDel == 0 && it.Id == diId).FirstAsync();
  243. if (gropInfo != null)
  244. {
  245. if (!string.IsNullOrEmpty(gropInfo.VisitCountry))
  246. {
  247. var countryCueencyCodes = await _setDataRep.GetSetDataBySTId(_setDataRep, 66);
  248. string countrys = gropInfo.VisitCountry;
  249. string[] countryItems = new string[] { };
  250. if (countrys.Contains("|"))
  251. {
  252. countryItems = countrys.Split('|');
  253. }
  254. else
  255. {
  256. countryItems = new string[] { countrys };
  257. }
  258. var countryInfos = await _dirRep._sqlSugar.Queryable<Dis_Country>().Where(it => it.IsDel == 0).ToListAsync();
  259. List<string> currencyCodes = new List<string>();
  260. if (countryItems.Length > 0)
  261. {
  262. foreach (var item in countryItems)
  263. {
  264. Dis_Country country = new Dis_Country();
  265. country = countryInfos.Where(it => it.CnShortName.Equals(item)).FirstOrDefault();
  266. if (country != null)
  267. {
  268. if (!item.Equals("中国"))
  269. {
  270. currencyCodes.Add(country.CurrencyCode);
  271. }
  272. }
  273. }
  274. }
  275. if (currencyCodes.Count > 0)
  276. {
  277. List<ExchangeRateModel> exchangeRateModels = await _juHeApiService.PostItemRateAsync(currencyCodes.ToArray());
  278. if (exchangeRateModels.Count > 0)
  279. {
  280. var codes = await _dirRep._sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0 && it.IsDel == 0).ToListAsync();
  281. for (int i = 0; i < exchangeRateModels.Count; i++)
  282. {
  283. string currencyName = exchangeRateModels[i].Name;
  284. string code = "";
  285. var currencyData = codes.Where(it => it.IsDel == 0 && it.Remark == currencyName).FirstOrDefault();
  286. if (currencyData != null) {
  287. code = currencyData.Name;
  288. CNYInit += string.Format(@"|{0}({1}):{2}", exchangeRateModels[i].Name, code, exchangeRateModels[i].MSellPri);
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. List<Grp_TeamRate> grp_TeamRates = new List<Grp_TeamRate>()
  296. {
  297. new Grp_TeamRate(){ DiId = diId,CTable = 76,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //76 酒店预订
  298. new Grp_TeamRate(){ DiId = diId,CTable = 77,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //77 行程
  299. new Grp_TeamRate(){ DiId = diId,CTable = 79,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //79 车/导游地接
  300. new Grp_TeamRate(){ DiId = diId,CTable = 80,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //80 签证
  301. new Grp_TeamRate(){ DiId = diId,CTable = 82,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //82 团组客户保险
  302. new Grp_TeamRate(){ DiId = diId,CTable = 85,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //85 机票预订
  303. new Grp_TeamRate(){ DiId = diId,CTable = 98,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //98 其他款项
  304. new Grp_TeamRate(){ DiId = diId,CTable = 285,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //285 收款退还
  305. new Grp_TeamRate(){ DiId = diId,CTable = 751,Remark = CNYInit,CreateUserId = userId, IsDel = 0 }, //751 酒店早餐
  306. };
  307. var res = _teamRateRep._sqlSugar.Insertable(grp_TeamRates).ExecuteCommand();
  308. if (res < 0)
  309. {
  310. result.Msg = string.Format(@"添加失败!");
  311. return result;
  312. }
  313. result.Code = 0;
  314. result.Msg = string.Format(@"操作成功!");
  315. return result;
  316. }
  317. /// <summary>
  318. /// 团组汇率
  319. /// 建团时 默认按照岗位分配权限
  320. /// </summary>
  321. /// <param name="userId"></param>
  322. /// <param name="diId"></param>
  323. /// <param name="companyId"></param>
  324. /// <returns></returns>
  325. public static async Task<Result> PostGroupAuthAddInit(int userId, int diId, int companyId = 2)
  326. {
  327. Result result = new() { Code = -2 };
  328. if (userId < 0)
  329. {
  330. result.Msg = string.Format(@"请传入正确的userId");
  331. return result;
  332. }
  333. if (companyId < 0)
  334. {
  335. result.Msg = string.Format(@"请传入正确的companyId");
  336. return result;
  337. }
  338. if (diId < 0)
  339. {
  340. result.Msg = string.Format(@"请传入正确的DiId");
  341. return result;
  342. }
  343. //var depData = await _teamRateRep._sqlSugar.Queryable<Sys_Department>().Where(it => it.IsDel == 0 && it.CompanyId == companyId && it.DepName.Equals("国交部")).FirstAsync();
  344. //if (depData != null)
  345. //{
  346. // var userData = await _teamRateRep._sqlSugar.Queryable<Sys_Users>().Where(it => it.IsDel == 0 && it.CompanyId == companyId && it.DepId == depData.Id).ToListAsync();
  347. //}
  348. result.Code = 0;
  349. result.Msg = string.Format(@"操作成功!");
  350. return result;
  351. }
  352. #endregion
  353. #endregion
  354. #region 团组汇率
  355. /// <summary>
  356. /// 团组汇率
  357. /// 获取板块 币种 及 汇率
  358. /// 76 酒店预订 77 行程 79 车/导游地接
  359. /// 80 签证 82 团组客户保险 85 机票预订
  360. /// 98 其他款项 285 收款退还
  361. /// </summary>
  362. /// <param name="teamRateModels"></param>
  363. /// <param name="cTable"></param>
  364. /// <param name="currencyCode"></param>
  365. /// <returns>
  366. /// string
  367. /// eg: CNY 1.0000
  368. /// </returns>
  369. public static async Task<string> PostGroupRateByCTableAndCurrency(List<TeamRateModelView> teamRateModels,int cTable,List<string> currencyCodes)
  370. {
  371. string str = "";
  372. List<string> currencyRates = new List<string>();
  373. TeamRateModelView hotelRateData = teamRateModels.Where(it => it.CTableId == cTable).FirstOrDefault();
  374. if (hotelRateData != null)
  375. {
  376. var hotelRates = hotelRateData.TeamRates;
  377. foreach (var item in currencyCodes)
  378. {
  379. if (!string.IsNullOrEmpty(item))
  380. {
  381. var hotelRateInfo = hotelRates.Where(it => it.CurrencyCode.Equals(item)).FirstOrDefault();
  382. if (hotelRateInfo != null)
  383. {
  384. string str1 = string.Format("{0} {1}\r\n", hotelRateInfo.CurrencyCode, hotelRateInfo.Rate);
  385. currencyRates.Add(str1);
  386. }
  387. }
  388. }
  389. if (currencyRates != null || currencyRates.Count > 0)
  390. {
  391. currencyRates = currencyRates.Distinct().ToList();
  392. foreach (var item in currencyRates)
  393. {
  394. str += item;
  395. }
  396. }
  397. }
  398. return str;
  399. }
  400. #endregion
  401. #region 汉字转换拼音
  402. private static Dictionary<int, List<string>> GetTotalPingYinDictionary(string text)
  403. {
  404. var chs = text.ToCharArray();
  405. //记录每个汉字的全拼
  406. Dictionary<int, List<string>> totalPingYinList = new Dictionary<int, List<string>>();
  407. for (int i = 0; i < chs.Length; i++)
  408. {
  409. var pinyinList = new List<string>();
  410. //是否是有效的汉字
  411. if (ChineseChar.IsValidChar(chs[i]))
  412. {
  413. ChineseChar cc = new ChineseChar(chs[i]);
  414. pinyinList = cc.Pinyins.Where(p => !string.IsNullOrWhiteSpace(p)).ToList();
  415. }
  416. else
  417. {
  418. pinyinList.Add(chs[i].ToString());
  419. }
  420. //去除声调,转小写
  421. pinyinList = pinyinList.ConvertAll(p => Regex.Replace(p, @"\d", "").ToLower());
  422. //去重
  423. pinyinList = pinyinList.Where(p => !string.IsNullOrWhiteSpace(p)).Distinct().ToList();
  424. if (pinyinList.Any())
  425. {
  426. totalPingYinList[i] = pinyinList;
  427. }
  428. }
  429. return totalPingYinList;
  430. }
  431. /// <summary>
  432. /// 获取汉语拼音全拼
  433. /// </summary>
  434. /// <param name="text">The string.</param>
  435. /// <returns></returns>
  436. public static List<string> GetTotalPingYin(this string text)
  437. {
  438. var result = new List<string>();
  439. foreach (var pys in GetTotalPingYinDictionary(text))
  440. {
  441. var items = pys.Value;
  442. if (result.Count <= 0)
  443. {
  444. result = items;
  445. }
  446. else
  447. {
  448. //全拼循环匹配
  449. var newTotalPingYinList = new List<string>();
  450. foreach (var totalPingYin in result)
  451. {
  452. newTotalPingYinList.AddRange(items.Select(item => totalPingYin + item));
  453. }
  454. newTotalPingYinList = newTotalPingYinList.Distinct().ToList();
  455. result = newTotalPingYinList;
  456. }
  457. }
  458. return result;
  459. }
  460. /// <summary>
  461. /// 获取汉语拼音首字母
  462. /// </summary>
  463. /// <param name="text"></param>
  464. /// <returns></returns>
  465. public static List<string> GetFirstPingYin(this string text)
  466. {
  467. var result = new List<string>();
  468. foreach (var pys in GetTotalPingYinDictionary(text))
  469. {
  470. var items = pys.Value;
  471. if (result.Count <= 0)
  472. {
  473. result = items.ConvertAll(p => p.Substring(0, 1)).Distinct().ToList();
  474. }
  475. else
  476. {
  477. //首字母循环匹配
  478. var newFirstPingYinList = new List<string>();
  479. foreach (var firstPingYin in result)
  480. {
  481. newFirstPingYinList.AddRange(items.Select(item => firstPingYin + item.Substring(0, 1)));
  482. }
  483. newFirstPingYinList = newFirstPingYinList.Distinct().ToList();
  484. result = newFirstPingYinList;
  485. }
  486. }
  487. return result;
  488. }
  489. #endregion
  490. }
  491. }