GeneralMethod.cs 16 KB

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