GeneralMethod.cs 19 KB

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