| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- using Common;
- using Database;
- using Database.Database;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- //using static Common.Request;
- //using static Common.Response;
- namespace Common.Logic
- {
- public class Connector
- {
- private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Connector));
- // public static async Task<bool> AddBonusToUser(
- // IConfiguration configuration,
- // ModelContext dbContext,
- // PredictTerm predictTerm,
- // PredictWinner predictWinner,
- // decimal userId,
- // Prize prize
- // )
- // {
- // try
- // {
- // log.Debug("Add bonus " + prize.Name + " to " + userId);
- // var account = dbContext.AccountUsers.Where(x => x.Id == userId).ToList();
- // if (account.Count == 0)
- // {
- // log.Error("No user existed");
- // return false;
- // }
- // // get language from config
- // string content = "";
- // string contentEN = "";
- // string contentMY = "";
- // List<Config> configs = dbContext
- // .Configs.Where(
- // x =>
- // x.ParamName == "ADD_WEEKLY_PRIZE"
- // || x.ParamName == "ADD_MONTHLY_PRIZE"
- // || x.ParamName == "SMS_LANG"
- // )
- // .ToList();
- // if (configs != null && configs.Count > 0)
- // {
- // var lang = configs.Find(x => x.ParamName == "SMS_LANG");
- // var monthlyPrize = configs.Find(x => x.ParamName == "ADD_MONTHLY_PRIZE");
- // var weeklyPrize = configs.Find(x => x.ParamName == "ADD_WEEKLY_PRIZE");
- // if (lang != null && lang.ParamValue == "MY")
- // {
- // if (prize.PackgeType == PackageCode.AddData)
- // {
- // if (weeklyPrize != null)
- // {
- // content = weeklyPrize
- // .ParamValueFr!.Replace(
- // "%fromtime%",
- // predictTerm.FromDate.ToString("dd/MM/yyyy")
- // )
- // .Replace("%totime%", predictTerm.ToDate.ToString("dd/MM/yyyy"))
- // .Replace(
- // "%prize%",
- // prize.Value == "750"
- // ? "၇၅၀"
- // : prize.Value == "500"
- // ? "၅၀၀"
- // : prize.Value == "400"
- // ? "၄၀၀"
- // : prize.Value == "250"
- // ? "၂၅၀"
- // : "၁၅၀"
- // )
- // .Replace(
- // "%position%",
- // prize.Priority == 1
- // ? "ပထမဆု"
- // : prize.Priority == 2
- // ? "ဒုတိယဆု"
- // : prize.Priority == 3
- // ? "တတိယဆု"
- // : prize.Priority == 4
- // ? "စတုတ္ထဆု"
- // : "ပဉ္စမဆု"
- // );
- // }
- // }
- // else
- // {
- // if (monthlyPrize != null)
- // {
- // content = monthlyPrize
- // .ParamValueFr!.Replace(
- // "%time%",
- // predictTerm.FromDate.ToString("MMMM")
- // )
- // .Replace(
- // "%prize%",
- // prize.Priority == 1
- // ? "၁ဝသိန်းကျပ်"
- // : prize.Priority == 2
- // ? "၅သိန်းကျပ်"
- // : prize.Priority == 3
- // ? "၅သောင်းကျပ်"
- // : "၂သောင်းကျပ် "
- // )
- // .Replace(
- // "%position%",
- // prize.Priority == 1
- // ? "ပထမဆု"
- // : prize.Priority == 2
- // ? "ဒုတိယဆု"
- // : prize.Priority == 3
- // ? "တတိယဆု"
- // : prize.Priority == 4
- // ? "စတုတ္ထဆု"
- // : "ပဉ္စမဆု"
- // );
- // }
- // }
- // }
- // else if (lang != null && lang.ParamValue == "EN")
- // {
- // if (prize.PackgeType == PackageCode.AddData)
- // {
- // if (weeklyPrize != null)
- // {
- // content = weeklyPrize
- // .ParamValue!.Replace(
- // "%fromtime%",
- // predictTerm.FromDate.ToString("dd/MM/yyyy")
- // )
- // .Replace("%totime%", predictTerm.ToDate.ToString("dd/MM/yyyy"))
- // .Replace("%prize%", prize.Value)
- // .Replace(
- // "%position%",
- // prize.Priority
- // + (
- // prize.Priority == 1
- // ? "st"
- // : prize.Priority == 2
- // ? "nd"
- // : prize.Priority == 3
- // ? "rd"
- // : "th"
- // )
- // );
- // ;
- // }
- // }
- // else
- // {
- // if (monthlyPrize != null)
- // {
- // content = monthlyPrize
- // .ParamValue!.Replace(
- // "%time%",
- // predictTerm.FromDate.ToString("MMMM")
- // )
- // .Replace("%prize%", prize.Value)
- // .Replace(
- // "%position%",
- // prize.Priority
- // + (
- // prize.Priority == 1
- // ? "st"
- // : prize.Priority == 2
- // ? "nd"
- // : prize.Priority == 3
- // ? "rd"
- // : "th"
- // )
- // );
- // ;
- // }
- // }
- // }
- // else
- // {
- // // send both MY and EN
- // if (prize.PackgeType == PackageCode.AddData)
- // {
- // if (weeklyPrize != null)
- // {
- // contentEN = weeklyPrize
- // .ParamValue!.Replace(
- // "%fromtime%",
- // predictTerm.FromDate.ToString("dd/MM/yyyy")
- // )
- // .Replace("%totime%", predictTerm.ToDate.ToString("dd/MM/yyyy"))
- // .Replace("%prize%", prize.Value)
- // .Replace(
- // "%position%",
- // prize.Priority
- // + (
- // prize.Priority == 1
- // ? "st"
- // : prize.Priority == 2
- // ? "nd"
- // : prize.Priority == 3
- // ? "rd"
- // : "th"
- // )
- // );
- // ;
- // contentMY = weeklyPrize
- // .ParamValueFr!.Replace(
- // "%fromtime%",
- // predictTerm.FromDate.ToString("dd/MM/yyyy")
- // )
- // .Replace("%totime%", predictTerm.ToDate.ToString("dd/MM/yyyy"))
- // .Replace(
- // "%prize%",
- // prize.Value == "750"
- // ? "၇၅၀"
- // : prize.Value == "500"
- // ? "၅၀၀"
- // : prize.Value == "400"
- // ? "၄၀၀"
- // : prize.Value == "250"
- // ? "၂၅၀"
- // : "၁၅၀"
- // )
- // .Replace(
- // "%position%",
- // prize.Priority == 1
- // ? "ပထမဆု"
- // : prize.Priority == 2
- // ? "ဒုတိယဆု"
- // : prize.Priority == 3
- // ? "တတိယဆု"
- // : prize.Priority == 4
- // ? "စတုတ္ထဆု"
- // : "ပဉ္စမဆု"
- // );
- // }
- // }
- // else
- // {
- // if (monthlyPrize != null)
- // {
- // contentEN = monthlyPrize
- // .ParamValue!.Replace(
- // "%time%",
- // predictTerm.FromDate.ToString("MMMM")
- // )
- // .Replace("%prize%", prize.Value)
- // .Replace(
- // "%position%",
- // prize.Priority
- // + (
- // prize.Priority == 1
- // ? "st"
- // : prize.Priority == 2
- // ? "nd"
- // : prize.Priority == 3
- // ? "rd"
- // : "th"
- // )
- // );
- // ;
- // contentMY = monthlyPrize
- // .ParamValueFr!.Replace(
- // "%time%",
- // predictTerm.FromDate.ToString("MMMM")
- // )
- // .Replace(
- // "%prize%",
- // prize.Priority == 1
- // ? "၁ဝသိန်းကျပ်"
- // : prize.Priority == 2
- // ? "၅သိန်းကျပ်"
- // : prize.Priority == 3
- // ? "၅သောင်းကျပ်"
- // : "၂သောင်းကျပ် "
- // )
- // .Replace(
- // "%position%",
- // prize.Priority == 1
- // ? "ပထမဆု"
- // : prize.Priority == 2
- // ? "ဒုတိယဆု"
- // : prize.Priority == 3
- // ? "တတိယဆု"
- // : prize.Priority == 4
- // ? "စတုတ္ထဆု"
- // : "ပဉ္စမဆု"
- // );
- // }
- // }
- // }
- // }
- // bool prizeStatus = false;
- // if (prize.PackgeType == PackageCode.AddData)
- // {
- // string? transactionLogId = CommonLogic.GenerateUUID();
- // // load result
- // AddDataReq request = new AddDataReq
- // {
- // refTrans = transactionLogId,
- // msisdn = account[0].Msisdn,
- // packageCode = prize.PackgeCode
- // };
- // string bodyString = JsonConvert.SerializeObject(request);
- // TransactionRelog transactionLog = new TransactionRelog
- // {
- // Channel = configuration.GetSection("UrlAddData").Value ?? "",
- // Command = "Add_data_" + prize.PackgeCode,
- // Msisdn = account[0].Msisdn!,
- // Request = bodyString,
- // RequestTime = DateTime.Now,
- // TransactionId = transactionLogId,
- // Id = (decimal)await DbLogic.GenIdAsync(dbContext, "transaction_relog_seq")
- // };
- // string key = configuration.GetSection("Hmac256KeyAddData").Value ?? "";
- // string? signature = CommonLogic.HashHMACHex(key, bodyString).ToString();
- // log.Info("body hmac: " + bodyString);
- // log.Info("signature hmac: " + signature);
- // string? res = await CommonLogic.SendPostWithSignatureAsync(
- // log,
- // configuration.GetSection("UrlAddData").Value ?? "",
- // request,
- // signature,
- // configuration.GetSection("UsernameAddData").Value,
- // configuration.GetSection("PasswordAddData").Value
- // );
- // // 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\"}";
- // transactionLog.Response = JsonConvert.SerializeObject(res);
- // transactionLog.ResponseTime = DateTime.Now;
- // if (res != null)
- // {
- // AddDataRes response = new AddDataRes(res);
- // transactionLog.ErrorCode = response.success == true ? "0" : "-1";
- // log.Warn(
- // "Add prize weekly for user: "
- // + account[0].Msisdn
- // + " "
- // + (response.success == true ? "0" : "-1")
- // );
- // if (response.success == true)
- // {
- // prizeStatus = true;
- // // save history
- // predictWinner.Status = CommonConstants.Claimed;
- // predictWinner.ReceiveTime = DateTime.Now;
- // predictWinner.TransactionLogId = transactionLogId;
- // predictWinner.TransactionBonusId = response.result!.id;
- // dbContext.PredictWinners.Update(predictWinner);
- // await dbContext.SaveChangesAsync();
- // }
- // transactionLog.ErrorCode = response.success == true ? "0" : "-1";
- // }
- // else
- // {
- // log.Error(
- // "An error was happened to add point to user " + account[0].Msisdn
- // );
- // }
- // dbContext.TransactionRelogs.Add(transactionLog);
- // await dbContext.SaveChangesAsync();
- // }
- // else if (prize.PackgeType == PackageCode.AddCode)
- // {
- // // save to code prize info
- // // get prize code
- // // get code prize which is not claimed
- // List<CodePrizeInfo> codePrizeInfos = dbContext
- // .CodePrizeInfos.Where(
- // x =>
- // x.PredictTermId == predictTerm.Id
- // && x.PrizeId == prize.Id
- // && x.Status == CommonConstants.Active
- // && x.UserClaimed == null
- // && x.TimeClaimed == null
- // )
- // .ToList();
- // if (codePrizeInfos == null || codePrizeInfos.Count == 0)
- // {
- // log.Warn(
- // "NO code prize set for prize: " + prize.Id + " term: " + predictTerm.Id
- // );
- // }
- // // else if (codePrizeInfos.Count > 1)
- // // {
- // // log.Warn(">1 code prize set for prize: " + prize.Id + " term: " + predictTerm.Id);
- // // }
- // else
- // {
- // // add code pirze log
- // CodePrizeInfo codePrize = codePrizeInfos[0];
- // codePrize.Status = CommonConstants.Claimed;
- // codePrize.TimeProcess = DateTime.Now;
- // codePrize.UserClaimed = account[0].Msisdn;
- // codePrize.TimeClaimed = DateTime.Now;
- // dbContext.CodePrizeInfos.Update(codePrize);
- // // save winner
- // predictWinner.Status = CommonConstants.Claimed;
- // predictWinner.ReceiveTime = DateTime.Now;
- // dbContext.PredictWinners.Update(predictWinner);
- // await dbContext.SaveChangesAsync();
- // log.Warn(
- // "set code prize set for prize: "
- // + prize.Id
- // + " term: "
- // + predictTerm.Id
- // + " for: "
- // + account[0].Msisdn
- // );
- // content = content.Replace("%code%", codePrizeInfos[0].Value);
- // prizeStatus = true;
- // }
- // }
- // // prizeStatus = false;
- // if (prizeStatus)
- // {
- // _ = SentMtAsync(dbContext, account[0].Msisdn!, content);
- // if (content != "")
- // {
- // log.Debug("content is not null => send MT to: " + content);
- // }
- // if (contentEN != "")
- // {
- // log.Debug("contentEN is not null => send MT to: " + contentEN);
- // }
- // if (contentMY != "")
- // {
- // log.Debug("contentMY is not null => send MT to: " + contentMY);
- // }
- // return true;
- // }
- // else
- // {
- // log.Error("Can not add data to user, so not send sms winner to user");
- // }
- // }
- // catch (Exception ex)
- // {
- // log.Error("Exception: ", ex);
- // }
- // return false;
- // }
- // public static async Task<AddDataRes?> AddPointToUserAsync(
- // ModelContext dbContext,
- // IConfiguration configuration,
- // string msisdn,
- // string packageCode
- // )
- // {
- // try
- // {
- // string? transactionLogId = CommonLogic.GenerateUUID();
- // // load result
- // AddDataReq request = new AddDataReq
- // {
- // refTrans = transactionLogId,
- // msisdn = msisdn,
- // packageCode = packageCode
- // };
- // string bodyString = JsonConvert.SerializeObject(request);
- // TransactionRelog transactionLog = new TransactionRelog
- // {
- // Channel = configuration.GetSection("UrlAddData").Value ?? "",
- // Command = "Add_data_" + packageCode,
- // Msisdn = msisdn,
- // Request = bodyString,
- // RequestTime = DateTime.Now,
- // TransactionId = transactionLogId,
- // Id = (decimal)await DbLogic.GenIdAsync(dbContext, "transaction_relog_seq")
- // };
- // string key = configuration.GetSection("Hmac256KeyAddData").Value ?? "";
- // string? signature = CommonLogic.HashHMACHex(key, bodyString).ToString();
- // log.Info("body hmac: " + bodyString);
- // log.Info("signature hmac: " + signature);
- // string? res = await CommonLogic.SendPostWithSignatureAsync(
- // log,
- // configuration.GetSection("UrlAddData").Value ?? "",
- // request,
- // signature,
- // configuration.GetSection("UsernameAddData").Value,
- // configuration.GetSection("PasswordAddData").Value
- // );
- // transactionLog.Response = JsonConvert.SerializeObject(res);
- // transactionLog.ResponseTime = DateTime.Now;
- // transactionLog.ErrorCode = "-1";
- // AddDataRes? response = null;
- // if (res != null)
- // {
- // response = new AddDataRes(res);
- // transactionLog.ErrorCode = response.success == true ? "0" : "-1";
- // log.Warn(
- // "Add prize weekly for user: "
- // + msisdn
- // + " "
- // + (response.success == true ? "0" : "-1")
- // );
- // transactionLog.ErrorCode = response.success == true ? "0" : "-1";
- // }
- // else
- // {
- // log.Error("An error was happened to add point to user " + msisdn);
- // }
- // dbContext.TransactionRelogs.Add(transactionLog);
- // await dbContext.SaveChangesAsync();
- // return response;
- // }
- // catch (Exception exception)
- // {
- // log.Error("Exception: ", exception);
- // }
- // return null;
- // }
- //public static async Task SentMtAsync(ModelContext dbContext, string msisdn, string content)
- //{
- // await CommonLogic.SendMtToUserAsync(dbContext, msisdn!, content);
- //}
- //public static async Task<Mt> CreateMtAsync(
- // ModelContext dbContext,
- // string msisdn,
- // string content
- //)
- //{
- // // add mt
- // decimal seq = (decimal)await Database.DbLogic.GenIdAsync(dbContext, "mt_seq");
- // Mt mt = new Mt
- // {
- // MtId = seq,
- // MoHisId = (int)seq,
- // ReceiveTime = DateTime.Now,
- // Channel = CommonConstants.Channel,
- // Message = content,
- // Msisdn = msisdn
- // };
- // return mt;
- //}
- }
- }
|