Connector.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using Common;
  5. using Database;
  6. using Database.Database;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.Configuration;
  9. using Newtonsoft.Json;
  10. //using static Common.Request;
  11. //using static Common.Response;
  12. namespace Common.Logic
  13. {
  14. public class Connector
  15. {
  16. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Connector));
  17. // public static async Task<bool> AddBonusToUser(
  18. // IConfiguration configuration,
  19. // ModelContext dbContext,
  20. // PredictTerm predictTerm,
  21. // PredictWinner predictWinner,
  22. // decimal userId,
  23. // Prize prize
  24. // )
  25. // {
  26. // try
  27. // {
  28. // log.Debug("Add bonus " + prize.Name + " to " + userId);
  29. // var account = dbContext.AccountUsers.Where(x => x.Id == userId).ToList();
  30. // if (account.Count == 0)
  31. // {
  32. // log.Error("No user existed");
  33. // return false;
  34. // }
  35. // // get language from config
  36. // string content = "";
  37. // string contentEN = "";
  38. // string contentMY = "";
  39. // List<Config> configs = dbContext
  40. // .Configs.Where(
  41. // x =>
  42. // x.ParamName == "ADD_WEEKLY_PRIZE"
  43. // || x.ParamName == "ADD_MONTHLY_PRIZE"
  44. // || x.ParamName == "SMS_LANG"
  45. // )
  46. // .ToList();
  47. // if (configs != null && configs.Count > 0)
  48. // {
  49. // var lang = configs.Find(x => x.ParamName == "SMS_LANG");
  50. // var monthlyPrize = configs.Find(x => x.ParamName == "ADD_MONTHLY_PRIZE");
  51. // var weeklyPrize = configs.Find(x => x.ParamName == "ADD_WEEKLY_PRIZE");
  52. // if (lang != null && lang.ParamValue == "MY")
  53. // {
  54. // if (prize.PackgeType == PackageCode.AddData)
  55. // {
  56. // if (weeklyPrize != null)
  57. // {
  58. // content = weeklyPrize
  59. // .ParamValueFr!.Replace(
  60. // "%fromtime%",
  61. // predictTerm.FromDate.ToString("dd/MM/yyyy")
  62. // )
  63. // .Replace("%totime%", predictTerm.ToDate.ToString("dd/MM/yyyy"))
  64. // .Replace(
  65. // "%prize%",
  66. // prize.Value == "750"
  67. // ? "၇၅၀"
  68. // : prize.Value == "500"
  69. // ? "၅၀၀"
  70. // : prize.Value == "400"
  71. // ? "၄၀၀"
  72. // : prize.Value == "250"
  73. // ? "၂၅၀"
  74. // : "၁၅၀"
  75. // )
  76. // .Replace(
  77. // "%position%",
  78. // prize.Priority == 1
  79. // ? "ပထမဆု"
  80. // : prize.Priority == 2
  81. // ? "ဒုတိယဆု"
  82. // : prize.Priority == 3
  83. // ? "တတိယဆု"
  84. // : prize.Priority == 4
  85. // ? "စတုတ္ထဆု"
  86. // : "ပဉ္စမဆု"
  87. // );
  88. // }
  89. // }
  90. // else
  91. // {
  92. // if (monthlyPrize != null)
  93. // {
  94. // content = monthlyPrize
  95. // .ParamValueFr!.Replace(
  96. // "%time%",
  97. // predictTerm.FromDate.ToString("MMMM")
  98. // )
  99. // .Replace(
  100. // "%prize%",
  101. // prize.Priority == 1
  102. // ? "၁ဝသိန်းကျပ်"
  103. // : prize.Priority == 2
  104. // ? "၅သိန်းကျပ်"
  105. // : prize.Priority == 3
  106. // ? "၅သောင်းကျပ်"
  107. // : "၂သောင်းကျပ် "
  108. // )
  109. // .Replace(
  110. // "%position%",
  111. // prize.Priority == 1
  112. // ? "ပထမဆု"
  113. // : prize.Priority == 2
  114. // ? "ဒုတိယဆု"
  115. // : prize.Priority == 3
  116. // ? "တတိယဆု"
  117. // : prize.Priority == 4
  118. // ? "စတုတ္ထဆု"
  119. // : "ပဉ္စမဆု"
  120. // );
  121. // }
  122. // }
  123. // }
  124. // else if (lang != null && lang.ParamValue == "EN")
  125. // {
  126. // if (prize.PackgeType == PackageCode.AddData)
  127. // {
  128. // if (weeklyPrize != null)
  129. // {
  130. // content = weeklyPrize
  131. // .ParamValue!.Replace(
  132. // "%fromtime%",
  133. // predictTerm.FromDate.ToString("dd/MM/yyyy")
  134. // )
  135. // .Replace("%totime%", predictTerm.ToDate.ToString("dd/MM/yyyy"))
  136. // .Replace("%prize%", prize.Value)
  137. // .Replace(
  138. // "%position%",
  139. // prize.Priority
  140. // + (
  141. // prize.Priority == 1
  142. // ? "st"
  143. // : prize.Priority == 2
  144. // ? "nd"
  145. // : prize.Priority == 3
  146. // ? "rd"
  147. // : "th"
  148. // )
  149. // );
  150. // ;
  151. // }
  152. // }
  153. // else
  154. // {
  155. // if (monthlyPrize != null)
  156. // {
  157. // content = monthlyPrize
  158. // .ParamValue!.Replace(
  159. // "%time%",
  160. // predictTerm.FromDate.ToString("MMMM")
  161. // )
  162. // .Replace("%prize%", prize.Value)
  163. // .Replace(
  164. // "%position%",
  165. // prize.Priority
  166. // + (
  167. // prize.Priority == 1
  168. // ? "st"
  169. // : prize.Priority == 2
  170. // ? "nd"
  171. // : prize.Priority == 3
  172. // ? "rd"
  173. // : "th"
  174. // )
  175. // );
  176. // ;
  177. // }
  178. // }
  179. // }
  180. // else
  181. // {
  182. // // send both MY and EN
  183. // if (prize.PackgeType == PackageCode.AddData)
  184. // {
  185. // if (weeklyPrize != null)
  186. // {
  187. // contentEN = weeklyPrize
  188. // .ParamValue!.Replace(
  189. // "%fromtime%",
  190. // predictTerm.FromDate.ToString("dd/MM/yyyy")
  191. // )
  192. // .Replace("%totime%", predictTerm.ToDate.ToString("dd/MM/yyyy"))
  193. // .Replace("%prize%", prize.Value)
  194. // .Replace(
  195. // "%position%",
  196. // prize.Priority
  197. // + (
  198. // prize.Priority == 1
  199. // ? "st"
  200. // : prize.Priority == 2
  201. // ? "nd"
  202. // : prize.Priority == 3
  203. // ? "rd"
  204. // : "th"
  205. // )
  206. // );
  207. // ;
  208. // contentMY = weeklyPrize
  209. // .ParamValueFr!.Replace(
  210. // "%fromtime%",
  211. // predictTerm.FromDate.ToString("dd/MM/yyyy")
  212. // )
  213. // .Replace("%totime%", predictTerm.ToDate.ToString("dd/MM/yyyy"))
  214. // .Replace(
  215. // "%prize%",
  216. // prize.Value == "750"
  217. // ? "၇၅၀"
  218. // : prize.Value == "500"
  219. // ? "၅၀၀"
  220. // : prize.Value == "400"
  221. // ? "၄၀၀"
  222. // : prize.Value == "250"
  223. // ? "၂၅၀"
  224. // : "၁၅၀"
  225. // )
  226. // .Replace(
  227. // "%position%",
  228. // prize.Priority == 1
  229. // ? "ပထမဆု"
  230. // : prize.Priority == 2
  231. // ? "ဒုတိယဆု"
  232. // : prize.Priority == 3
  233. // ? "တတိယဆု"
  234. // : prize.Priority == 4
  235. // ? "စတုတ္ထဆု"
  236. // : "ပဉ္စမဆု"
  237. // );
  238. // }
  239. // }
  240. // else
  241. // {
  242. // if (monthlyPrize != null)
  243. // {
  244. // contentEN = monthlyPrize
  245. // .ParamValue!.Replace(
  246. // "%time%",
  247. // predictTerm.FromDate.ToString("MMMM")
  248. // )
  249. // .Replace("%prize%", prize.Value)
  250. // .Replace(
  251. // "%position%",
  252. // prize.Priority
  253. // + (
  254. // prize.Priority == 1
  255. // ? "st"
  256. // : prize.Priority == 2
  257. // ? "nd"
  258. // : prize.Priority == 3
  259. // ? "rd"
  260. // : "th"
  261. // )
  262. // );
  263. // ;
  264. // contentMY = monthlyPrize
  265. // .ParamValueFr!.Replace(
  266. // "%time%",
  267. // predictTerm.FromDate.ToString("MMMM")
  268. // )
  269. // .Replace(
  270. // "%prize%",
  271. // prize.Priority == 1
  272. // ? "၁ဝသိန်းကျပ်"
  273. // : prize.Priority == 2
  274. // ? "၅သိန်းကျပ်"
  275. // : prize.Priority == 3
  276. // ? "၅သောင်းကျပ်"
  277. // : "၂သောင်းကျပ် "
  278. // )
  279. // .Replace(
  280. // "%position%",
  281. // prize.Priority == 1
  282. // ? "ပထမဆု"
  283. // : prize.Priority == 2
  284. // ? "ဒုတိယဆု"
  285. // : prize.Priority == 3
  286. // ? "တတိယဆု"
  287. // : prize.Priority == 4
  288. // ? "စတုတ္ထဆု"
  289. // : "ပဉ္စမဆု"
  290. // );
  291. // }
  292. // }
  293. // }
  294. // }
  295. // bool prizeStatus = false;
  296. // if (prize.PackgeType == PackageCode.AddData)
  297. // {
  298. // string? transactionLogId = CommonLogic.GenerateUUID();
  299. // // load result
  300. // AddDataReq request = new AddDataReq
  301. // {
  302. // refTrans = transactionLogId,
  303. // msisdn = account[0].Msisdn,
  304. // packageCode = prize.PackgeCode
  305. // };
  306. // string bodyString = JsonConvert.SerializeObject(request);
  307. // TransactionRelog transactionLog = new TransactionRelog
  308. // {
  309. // Channel = configuration.GetSection("UrlAddData").Value ?? "",
  310. // Command = "Add_data_" + prize.PackgeCode,
  311. // Msisdn = account[0].Msisdn!,
  312. // Request = bodyString,
  313. // RequestTime = DateTime.Now,
  314. // TransactionId = transactionLogId,
  315. // Id = (decimal)await DbLogic.GenIdAsync(dbContext, "transaction_relog_seq")
  316. // };
  317. // string key = configuration.GetSection("Hmac256KeyAddData").Value ?? "";
  318. // string? signature = CommonLogic.HashHMACHex(key, bodyString).ToString();
  319. // log.Info("body hmac: " + bodyString);
  320. // log.Info("signature hmac: " + signature);
  321. // string? res = await CommonLogic.SendPostWithSignatureAsync(
  322. // log,
  323. // configuration.GetSection("UrlAddData").Value ?? "",
  324. // request,
  325. // signature,
  326. // configuration.GetSection("UsernameAddData").Value,
  327. // configuration.GetSection("PasswordAddData").Value
  328. // );
  329. // // string? res = " {\"success\":true,\"result\":{\"id\":\"9448dc64-7bfb-4c59-935a-6db169fcf968\",\"partnerCode\":\"MY_KAN_KAUNG\",\"msisdn\":\"959685064698\",\"type\":\"LOYALTY\",\"amount\":750.0,\"unit\":\"POINT\",\"ocsId\":null,\"packageCode\":\"LOYALTY_750\",\"status\":\"SUCCESS\",\"refTransId\":\"b78c3be9-8d84-48ea-a46e-c0e7668ec160\",\"errMessage\":\"Successful\",\"note\":\"Add Success\",\"refLoyaltyId\":\"11808868349\",\"createdAt\":\"2023-11-15 10:21:22\",\"updatedAt\":\"2023-11-15 10:21:22\"},\"code\":\"SUCCESS\",\"message\":\"Add successfully\"}";
  330. // transactionLog.Response = JsonConvert.SerializeObject(res);
  331. // transactionLog.ResponseTime = DateTime.Now;
  332. // if (res != null)
  333. // {
  334. // AddDataRes response = new AddDataRes(res);
  335. // transactionLog.ErrorCode = response.success == true ? "0" : "-1";
  336. // log.Warn(
  337. // "Add prize weekly for user: "
  338. // + account[0].Msisdn
  339. // + " "
  340. // + (response.success == true ? "0" : "-1")
  341. // );
  342. // if (response.success == true)
  343. // {
  344. // prizeStatus = true;
  345. // // save history
  346. // predictWinner.Status = CommonConstants.Claimed;
  347. // predictWinner.ReceiveTime = DateTime.Now;
  348. // predictWinner.TransactionLogId = transactionLogId;
  349. // predictWinner.TransactionBonusId = response.result!.id;
  350. // dbContext.PredictWinners.Update(predictWinner);
  351. // await dbContext.SaveChangesAsync();
  352. // }
  353. // transactionLog.ErrorCode = response.success == true ? "0" : "-1";
  354. // }
  355. // else
  356. // {
  357. // log.Error(
  358. // "An error was happened to add point to user " + account[0].Msisdn
  359. // );
  360. // }
  361. // dbContext.TransactionRelogs.Add(transactionLog);
  362. // await dbContext.SaveChangesAsync();
  363. // }
  364. // else if (prize.PackgeType == PackageCode.AddCode)
  365. // {
  366. // // save to code prize info
  367. // // get prize code
  368. // // get code prize which is not claimed
  369. // List<CodePrizeInfo> codePrizeInfos = dbContext
  370. // .CodePrizeInfos.Where(
  371. // x =>
  372. // x.PredictTermId == predictTerm.Id
  373. // && x.PrizeId == prize.Id
  374. // && x.Status == CommonConstants.Active
  375. // && x.UserClaimed == null
  376. // && x.TimeClaimed == null
  377. // )
  378. // .ToList();
  379. // if (codePrizeInfos == null || codePrizeInfos.Count == 0)
  380. // {
  381. // log.Warn(
  382. // "NO code prize set for prize: " + prize.Id + " term: " + predictTerm.Id
  383. // );
  384. // }
  385. // // else if (codePrizeInfos.Count > 1)
  386. // // {
  387. // // log.Warn(">1 code prize set for prize: " + prize.Id + " term: " + predictTerm.Id);
  388. // // }
  389. // else
  390. // {
  391. // // add code pirze log
  392. // CodePrizeInfo codePrize = codePrizeInfos[0];
  393. // codePrize.Status = CommonConstants.Claimed;
  394. // codePrize.TimeProcess = DateTime.Now;
  395. // codePrize.UserClaimed = account[0].Msisdn;
  396. // codePrize.TimeClaimed = DateTime.Now;
  397. // dbContext.CodePrizeInfos.Update(codePrize);
  398. // // save winner
  399. // predictWinner.Status = CommonConstants.Claimed;
  400. // predictWinner.ReceiveTime = DateTime.Now;
  401. // dbContext.PredictWinners.Update(predictWinner);
  402. // await dbContext.SaveChangesAsync();
  403. // log.Warn(
  404. // "set code prize set for prize: "
  405. // + prize.Id
  406. // + " term: "
  407. // + predictTerm.Id
  408. // + " for: "
  409. // + account[0].Msisdn
  410. // );
  411. // content = content.Replace("%code%", codePrizeInfos[0].Value);
  412. // prizeStatus = true;
  413. // }
  414. // }
  415. // // prizeStatus = false;
  416. // if (prizeStatus)
  417. // {
  418. // _ = SentMtAsync(dbContext, account[0].Msisdn!, content);
  419. // if (content != "")
  420. // {
  421. // log.Debug("content is not null => send MT to: " + content);
  422. // }
  423. // if (contentEN != "")
  424. // {
  425. // log.Debug("contentEN is not null => send MT to: " + contentEN);
  426. // }
  427. // if (contentMY != "")
  428. // {
  429. // log.Debug("contentMY is not null => send MT to: " + contentMY);
  430. // }
  431. // return true;
  432. // }
  433. // else
  434. // {
  435. // log.Error("Can not add data to user, so not send sms winner to user");
  436. // }
  437. // }
  438. // catch (Exception ex)
  439. // {
  440. // log.Error("Exception: ", ex);
  441. // }
  442. // return false;
  443. // }
  444. // public static async Task<AddDataRes?> AddPointToUserAsync(
  445. // ModelContext dbContext,
  446. // IConfiguration configuration,
  447. // string msisdn,
  448. // string packageCode
  449. // )
  450. // {
  451. // try
  452. // {
  453. // string? transactionLogId = CommonLogic.GenerateUUID();
  454. // // load result
  455. // AddDataReq request = new AddDataReq
  456. // {
  457. // refTrans = transactionLogId,
  458. // msisdn = msisdn,
  459. // packageCode = packageCode
  460. // };
  461. // string bodyString = JsonConvert.SerializeObject(request);
  462. // TransactionRelog transactionLog = new TransactionRelog
  463. // {
  464. // Channel = configuration.GetSection("UrlAddData").Value ?? "",
  465. // Command = "Add_data_" + packageCode,
  466. // Msisdn = msisdn,
  467. // Request = bodyString,
  468. // RequestTime = DateTime.Now,
  469. // TransactionId = transactionLogId,
  470. // Id = (decimal)await DbLogic.GenIdAsync(dbContext, "transaction_relog_seq")
  471. // };
  472. // string key = configuration.GetSection("Hmac256KeyAddData").Value ?? "";
  473. // string? signature = CommonLogic.HashHMACHex(key, bodyString).ToString();
  474. // log.Info("body hmac: " + bodyString);
  475. // log.Info("signature hmac: " + signature);
  476. // string? res = await CommonLogic.SendPostWithSignatureAsync(
  477. // log,
  478. // configuration.GetSection("UrlAddData").Value ?? "",
  479. // request,
  480. // signature,
  481. // configuration.GetSection("UsernameAddData").Value,
  482. // configuration.GetSection("PasswordAddData").Value
  483. // );
  484. // transactionLog.Response = JsonConvert.SerializeObject(res);
  485. // transactionLog.ResponseTime = DateTime.Now;
  486. // transactionLog.ErrorCode = "-1";
  487. // AddDataRes? response = null;
  488. // if (res != null)
  489. // {
  490. // response = new AddDataRes(res);
  491. // transactionLog.ErrorCode = response.success == true ? "0" : "-1";
  492. // log.Warn(
  493. // "Add prize weekly for user: "
  494. // + msisdn
  495. // + " "
  496. // + (response.success == true ? "0" : "-1")
  497. // );
  498. // transactionLog.ErrorCode = response.success == true ? "0" : "-1";
  499. // }
  500. // else
  501. // {
  502. // log.Error("An error was happened to add point to user " + msisdn);
  503. // }
  504. // dbContext.TransactionRelogs.Add(transactionLog);
  505. // await dbContext.SaveChangesAsync();
  506. // return response;
  507. // }
  508. // catch (Exception exception)
  509. // {
  510. // log.Error("Exception: ", exception);
  511. // }
  512. // return null;
  513. // }
  514. //public static async Task SentMtAsync(ModelContext dbContext, string msisdn, string content)
  515. //{
  516. // await CommonLogic.SendMtToUserAsync(dbContext, msisdn!, content);
  517. //}
  518. //public static async Task<Mt> CreateMtAsync(
  519. // ModelContext dbContext,
  520. // string msisdn,
  521. // string content
  522. //)
  523. //{
  524. // // add mt
  525. // decimal seq = (decimal)await Database.DbLogic.GenIdAsync(dbContext, "mt_seq");
  526. // Mt mt = new Mt
  527. // {
  528. // MtId = seq,
  529. // MoHisId = (int)seq,
  530. // ReceiveTime = DateTime.Now,
  531. // Channel = CommonConstants.Channel,
  532. // Message = content,
  533. // Msisdn = msisdn
  534. // };
  535. // return mt;
  536. //}
  537. }
  538. }