GeneralMethod.cs 19 KB

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