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 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 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 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 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 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; //} } }