948 lines
26 KiB
C#
948 lines
26 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using OfficeOpenXml;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Web;
|
|
|
|
namespace REPORT_API_ver6.Services
|
|
{
|
|
public class Common
|
|
{
|
|
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Common));
|
|
|
|
public static IConfiguration GetConfiguration()
|
|
{
|
|
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
|
return builder.Build();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Request 변수값 반환
|
|
/// </summary>
|
|
/// <param name="fieldName">필드명</param>
|
|
/// <returns>Request 변수값</returns>
|
|
public static string GetRequestValue(string fieldName)
|
|
{
|
|
HttpContextAccessor hcAccessor = new HttpContextAccessor();
|
|
string returnValue = string.Empty;
|
|
|
|
try
|
|
{
|
|
returnValue = hcAccessor.HttpContext.Request.Query[fieldName].ToString();
|
|
|
|
if (String.IsNullOrEmpty(returnValue))
|
|
{
|
|
returnValue = hcAccessor.HttpContext.Request.Form[fieldName].ToString();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Request 변수값 반환 없으면 0
|
|
/// </summary>
|
|
/// <param name="fieldName">필드명</param>
|
|
/// <returns>Request 변수값</returns>
|
|
public static string GetRequestValueZero(string fieldName)
|
|
{
|
|
HttpContextAccessor hcAccessor = new HttpContextAccessor();
|
|
var returnValue = "";
|
|
|
|
try
|
|
{
|
|
returnValue = hcAccessor.HttpContext.Request.Query[fieldName].ToString();
|
|
|
|
if (String.IsNullOrEmpty(returnValue))
|
|
{
|
|
returnValue = hcAccessor.HttpContext.Request.Form[fieldName].ToString();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
if (String.IsNullOrEmpty(returnValue) == true)
|
|
{
|
|
returnValue = "0";
|
|
}
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// URL 파라미터 값 반환
|
|
/// </summary>
|
|
/// <param name="url">URL</param>
|
|
/// <param name="key">필드명</param>
|
|
/// <returns>파라미터 값</returns>
|
|
public static string getParamInURL(string url, string key)
|
|
{
|
|
string returnString = string.Empty;
|
|
|
|
Uri uri = new Uri(url);
|
|
var queryDictionary = HttpUtility.ParseQueryString(uri.Query);
|
|
returnString = GetNullString(queryDictionary[key]);
|
|
|
|
return returnString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// NULL 값을 0으로 변환
|
|
/// </summary>
|
|
/// <param name="value">값</param>
|
|
/// <returns>입력 값</returns>
|
|
public static string GetNullToZero(object value)
|
|
{
|
|
var returnValue = "0";
|
|
|
|
if (value != null && value.ToString() != "")
|
|
{
|
|
returnValue = value.ToString();
|
|
}
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
public static string ReplaceUnicodeEmpty(string inputString)
|
|
{
|
|
return Regex.Replace(inputString, @"(\\U\d*)", string.Empty);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// string을 JObject로 변환
|
|
/// </summary>
|
|
/// <param name="jsonString">JSON 형식 문자열</param>
|
|
/// <returns>JObject</returns>
|
|
public static JObject GetJObjectFromString(string jsonString)
|
|
{
|
|
JObject pJson = new JObject();
|
|
|
|
try
|
|
{
|
|
if (String.IsNullOrEmpty(jsonString) == false)
|
|
{
|
|
pJson = JObject.Parse(jsonString);
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
return pJson;
|
|
}
|
|
|
|
/// <summary>
|
|
/// string을 JArray로 변환
|
|
/// </summary>
|
|
/// <param name="jsonString">JSON 형식 문자열</param>
|
|
/// <returns>JArray</returns>
|
|
public static JArray GetJArrayFromString(string jsonString)
|
|
{
|
|
JArray pJson = new JArray();
|
|
|
|
try
|
|
{
|
|
if (String.IsNullOrEmpty(jsonString) == false)
|
|
{
|
|
pJson = JArray.Parse(jsonString);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
return pJson;
|
|
}
|
|
|
|
/// <summary>
|
|
/// JArray 머지
|
|
/// </summary>
|
|
/// <param name="oldArray">기존 JArray</param>
|
|
/// <param name="newArray">추가할 JArray</param>
|
|
/// <returns>JArray</returns>
|
|
public static JArray mergeJarray(JArray oldArray, JArray newArray)
|
|
{
|
|
JArray returnArray = oldArray;
|
|
|
|
if (oldArray != null)
|
|
{
|
|
/*
|
|
IDKEY를 기준으로
|
|
for (int i = 0; i < newArray.Count; i++)
|
|
{
|
|
if (returnArray.Where(x => x.Value<string>("IDKEY") == newArray[i]["IDKEY"].ToString()).Count() < 1)
|
|
{
|
|
returnArray.Add(newArray[i]);
|
|
}
|
|
}
|
|
*/
|
|
returnArray.Merge(newArray);
|
|
}
|
|
else
|
|
{
|
|
returnArray = newArray;
|
|
}
|
|
return returnArray;
|
|
}
|
|
|
|
/// <summary>
|
|
/// JSON여부 판단
|
|
/// </summary>
|
|
/// <param name="jsonString">JSON 형식 문자열</param>
|
|
/// <returns>true / false</returns>
|
|
public static bool IsJson(string jsonString)
|
|
{
|
|
bool isJson = false;
|
|
|
|
try
|
|
{
|
|
if (jsonString.StartsWith("{") && jsonString.EndsWith("}"))
|
|
{
|
|
JObject.Parse(jsonString);
|
|
isJson = true;
|
|
}
|
|
else if (jsonString.StartsWith("[") && jsonString.EndsWith("]"))
|
|
{
|
|
JArray.Parse(jsonString);
|
|
isJson = true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
return isJson;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Javascript Block 생성
|
|
/// </summary>
|
|
/// <param name="script">Javascript 내용</param>
|
|
/// <returns>Javascript 문자열</returns>
|
|
public static string GetJavascriptBlock(string script)
|
|
{
|
|
var scriptBlock = new StringBuilder(200);
|
|
scriptBlock.Append("<script language=\"javascript\" type=\"text/javascript\">" + Environment.NewLine);
|
|
scriptBlock.Append(script);
|
|
scriptBlock.Append("</script>" + Environment.NewLine);
|
|
return scriptBlock.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable로 부터 JSON 형식 생성
|
|
/// </summary>
|
|
/// <param name="dataTable">Data</param>
|
|
/// <returns>JSON 형식 문자열</returns>
|
|
public static string GetJsonFromObject(object Data)
|
|
{
|
|
return JsonConvert.SerializeObject(Data, Formatting.Indented);
|
|
}
|
|
|
|
|
|
public static string GetStringFromJson(JObject jobject)
|
|
{
|
|
string returnString = jobject.ToString().Replace(Environment.NewLine, "");
|
|
|
|
while (returnString.IndexOf(" ") > -1)
|
|
{
|
|
returnString = returnString.Replace(" ", "");
|
|
}
|
|
return returnString;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// JArray를 DataTable로 변환
|
|
/// </summary>
|
|
/// <param name="jArray">DataTable로 변환할 JArray</param>
|
|
/// <returns>변환된 DataTable</returns>
|
|
public static DataTable GetDataTableFromJArray(JArray jArray)
|
|
{
|
|
var result = new DataTable();
|
|
//Initialize the columns, If you know the row type, replace this
|
|
foreach (var row in jArray)
|
|
{
|
|
foreach (var jToken in row)
|
|
{
|
|
var jproperty = jToken as JProperty;
|
|
if (jproperty == null) continue;
|
|
if (result.Columns[jproperty.Name] == null)
|
|
result.Columns.Add(jproperty.Name, typeof(string));
|
|
}
|
|
}
|
|
foreach (var row in jArray)
|
|
{
|
|
var datarow = result.NewRow();
|
|
foreach (var jToken in row)
|
|
{
|
|
var jProperty = jToken as JProperty;
|
|
if (jProperty == null) continue;
|
|
datarow[jProperty.Name] = jProperty.Value.ToString();
|
|
}
|
|
result.Rows.Add(datarow);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 지정된 경로와 파일명을 전달하며 고유한 파일명 반환
|
|
/// </summary>
|
|
/// <param name="uploadFolder">업로드 경로</param>
|
|
/// <param name="fileName">파일명</param>
|
|
/// <returns>고유한 파일명</returns>
|
|
public static string GetUniqueFileName(string uploadFolder, string fileName)
|
|
{
|
|
var uploadFile = new FileInfo(uploadFolder + fileName);
|
|
var fileIndex = 0;
|
|
|
|
while (uploadFile.Exists)
|
|
{
|
|
string[] filearray = uploadFile.Name.Split(' ');
|
|
if (filearray.Length > 1)
|
|
{
|
|
fileIndex = Int32.Parse(Regex.Replace(filearray[1], @"[\-()" + uploadFile.Extension + "]", string.Empty));
|
|
}
|
|
fileIndex++;
|
|
var file = String.Format("{0}({1}){2}", filearray[0].Replace(uploadFile.Extension, String.Empty), fileIndex.ToString(), uploadFile.Extension);
|
|
return GetUniqueFileName(uploadFolder, file);
|
|
}
|
|
return uploadFolder + fileName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 파일 경로에서 파일명만 추출
|
|
/// </summary>
|
|
/// <param name="fileFullName">파일 경로</param>
|
|
/// <returns>경로가 제거된 파일명</returns>
|
|
public static string GetOnlyFileName(string fileFullName)
|
|
{
|
|
if (fileFullName.Contains("\\") == true)
|
|
{
|
|
fileFullName = fileFullName.Substring(fileFullName.LastIndexOf("\\") + 1);
|
|
}
|
|
|
|
return fileFullName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// upload_data 디렉토리에 파일 업로드
|
|
/// </summary>
|
|
/// <param name="fileName">파일명</param>
|
|
/// <param name="sourceFile">파일</param>
|
|
/// <returns>저장된 파일 경로</returns>
|
|
public static string uploadFile(string fileName, IFormFile sourceFile)
|
|
{
|
|
fileName = Common.GetOnlyFileName(fileName);
|
|
string outFileName = MapPath("upload_data") + "\\" + fileName;
|
|
|
|
using (FileStream output = System.IO.File.Create(outFileName))
|
|
{
|
|
sourceFile.CopyTo(output);
|
|
}
|
|
|
|
return outFileName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Text(탭으로 분리) OR CSV형태의 파일을 DataTable로 변환
|
|
/// </summary>
|
|
/// <param name="filepath">파일 경로</param>
|
|
/// <param name="extension">확장자</param>
|
|
/// <param name="header">헤더 목록</param>
|
|
/// <returns>변환된 DataTable</returns>
|
|
public static DataTable textToDataTable(string filepath, string extension = ".txt", string[] header = null)
|
|
{
|
|
DataTable returnTable = new DataTable();
|
|
|
|
StreamReader uploadStream = new StreamReader(filepath, Encoding.GetEncoding("euc-kr"));
|
|
try
|
|
{
|
|
string sep = "\t";
|
|
if (extension.ToLower() == ".csv")
|
|
{
|
|
sep = ",";
|
|
}
|
|
|
|
if (header != null)
|
|
{
|
|
Common.tableAddColumn(returnTable, header);
|
|
}
|
|
else
|
|
{
|
|
string headerString = uploadStream.ReadLine();
|
|
Common.tableAddColumn(returnTable, headerString.Split(sep.ToCharArray()));
|
|
}
|
|
|
|
string readString = String.Empty;
|
|
while ((readString = uploadStream.ReadLine()) != null)
|
|
{
|
|
string[] valueArray = readString.Split(sep.ToCharArray());
|
|
returnTable.Rows.Add(valueArray);
|
|
}
|
|
uploadStream.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
|
|
returnTable = new DataTable();
|
|
Common.tableAddColumn(returnTable, new string[] { "error" });
|
|
returnTable.Rows.Add(new string[] { ex.Message });
|
|
}
|
|
finally
|
|
{
|
|
uploadStream.Close();
|
|
}
|
|
|
|
return returnTable;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 엑셀 파일을 DataTable로
|
|
/// </summary>
|
|
/// <param name="filepath">파일 경로</param>
|
|
/// <param name="sheetName">변환할 Sheet Name</param>
|
|
/// <param name="header">특별한 header가 있으면 설정</param>
|
|
/// <returns>변환된 DataTable</returns>
|
|
public static DataTable ExceltoDataTable(string filepath, string sheetName = "", string[] header = null)
|
|
{
|
|
DataTable returnTable = new DataTable();
|
|
try
|
|
{
|
|
FileInfo importFile = new FileInfo(filepath);
|
|
using (var excelPackage = new ExcelPackage(importFile))
|
|
{
|
|
using (var stream = File.OpenRead(filepath))
|
|
{
|
|
excelPackage.Load(stream);
|
|
}
|
|
|
|
if (String.IsNullOrEmpty(sheetName))
|
|
{
|
|
sheetName = excelPackage.Workbook.Worksheets.First().Name;
|
|
}
|
|
|
|
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[sheetName];
|
|
|
|
if (header != null)
|
|
{
|
|
Common.tableAddColumn(returnTable, header);
|
|
}
|
|
else
|
|
{
|
|
foreach (var firstRowCell in worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column])
|
|
{
|
|
returnTable.Columns.Add(firstRowCell.Text);
|
|
}
|
|
}
|
|
|
|
for (int rowNum = 2; rowNum <= worksheet.Dimension.End.Row; rowNum++)
|
|
{
|
|
var wsRow = worksheet.Cells[rowNum, 1, rowNum, worksheet.Dimension.End.Column];
|
|
DataRow row = returnTable.Rows.Add();
|
|
foreach (var cell in wsRow)
|
|
{
|
|
row[cell.Start.Column - 1] = cell.Text;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
|
|
returnTable = new DataTable();
|
|
Common.tableAddColumn(returnTable, new string[] { "error" });
|
|
returnTable.Rows.Add(new string[] { ex.Message });
|
|
}
|
|
|
|
return returnTable;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable을 Excel로 변환
|
|
/// </summary>
|
|
/// <param name="excelTable">변환할 DataTable</param>
|
|
/// <param name="sheetName">만들 Sheet Name / 기본값 : Sheet1</param>
|
|
/// <returns>Excel파일</returns>
|
|
public static byte[] DataTableToExcel(DataTable excelTable, string sheetName = "Sheet1")
|
|
{
|
|
ExcelPackage excelPackage = new ExcelPackage();
|
|
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add(sheetName);
|
|
worksheet.Cells["A1"].LoadFromDataTable(excelTable, true);
|
|
|
|
return excelPackage.GetAsByteArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable을 CSV로 변환
|
|
/// </summary>
|
|
/// <param name="csvTable">변환할 DataTable</param>
|
|
/// <returns>FileByteArray</returns>
|
|
public static byte[] DataTableToCSV(DataTable csvTable)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
IEnumerable<string> columnNames = csvTable.Columns.Cast<DataColumn>().
|
|
Select(column => column.ColumnName);
|
|
sb.AppendLine(string.Join(",", columnNames));
|
|
|
|
foreach (DataRow row in csvTable.Rows)
|
|
{
|
|
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
|
|
sb.AppendLine(string.Join(",", fields));
|
|
}
|
|
|
|
string tempFileName = GetRandomString() + "_" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".csv";
|
|
File.WriteAllText(Common.MapPath("temp_data/"+ tempFileName), sb.ToString());
|
|
byte[] fileByteArray = File.ReadAllBytes(Common.MapPath("temp_data/"+ tempFileName));
|
|
File.Delete(Common.MapPath("temp_data/" + tempFileName));
|
|
|
|
return fileByteArray;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Null 이 아닌경우 String 으로 변환 Null은 공백
|
|
/// </summary>
|
|
/// <param name="inputObject">입력값</param>
|
|
/// <returns>변환된 String</returns>
|
|
public static string GetNullString(object inputObject)
|
|
{
|
|
var returnString = string.Empty;
|
|
|
|
if (inputObject != null)
|
|
{
|
|
returnString = inputObject.ToString();
|
|
}
|
|
return returnString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 특수 문자열
|
|
/// </summary>
|
|
/// <param name="strLen">만들 문자 길이 / 기본값 : 8</param>
|
|
/// <returns></returns>
|
|
public static string GetRandomString(int strLen = 8)
|
|
{
|
|
Random rand = new Random();
|
|
string returnString = string.Empty;
|
|
|
|
string sourceString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
for (int i = 0; i < strLen; i++)
|
|
{
|
|
int randNumber = rand.Next(sourceString.Length);
|
|
returnString += sourceString.Substring(randNumber, 1);
|
|
}
|
|
|
|
return returnString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 이미지 태그 제거
|
|
/// </summary>
|
|
/// <param name="text">문자열</param>
|
|
/// <returns>이미지 태그가 제거된 문자열</returns>
|
|
public static string StripImgTag(string text)
|
|
{
|
|
return Regex.Replace(text, @"<img\s[^>]*>(?:\s*?</img>)?", "", RegexOptions.IgnoreCase);
|
|
}
|
|
|
|
/// <summary>
|
|
/// HTML 태그 제거
|
|
/// </summary>
|
|
/// <param name="input">문자열</param>
|
|
/// <returns>HTML 태크가 제거된 문자열</returns>
|
|
public static string StripHTML(string input)
|
|
{
|
|
input = StripHTMLChracters(input);
|
|
return Regex.Replace(input, "<[a-zA-Z/].*?>", String.Empty);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// HTML 엔티티 치환
|
|
/// </summary>
|
|
/// <param name="input">문자열</param>
|
|
/// <returns>HTML 엔티티 치환된 문자열</returns>
|
|
public static string StripHTMLChracters(string input)
|
|
{
|
|
return HttpUtility.HtmlDecode(input);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 접속 IP 반환
|
|
/// </summary>
|
|
/// <returns>접속IP</returns>
|
|
public static string GetRemoteIP()
|
|
{
|
|
HttpContextAccessor hcAccessor = new HttpContextAccessor();
|
|
var ip = hcAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
|
|
ip = ip.Split(',').Last().Trim();
|
|
|
|
return ip;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 숫자 여부 판단
|
|
/// </summary>
|
|
/// <param name="s">문자열</param>
|
|
/// <returns>true / false</returns>
|
|
public static bool IsNumeric(string s)
|
|
{
|
|
float output;
|
|
return float.TryParse(s, out output);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 반올림 형식 반환
|
|
/// </summary>
|
|
/// <param name="roundPointer">반올림 위치</param>
|
|
/// <returns>반올림 형식 문자열</returns>
|
|
public static string makeRoundFormat(int roundPointer)
|
|
{
|
|
string formatString = "#0";
|
|
|
|
for (int i = 0; i < roundPointer; i++)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
formatString += ".";
|
|
}
|
|
formatString += "0";
|
|
}
|
|
|
|
return formatString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 반올림
|
|
/// </summary>
|
|
/// <param name="inString">반올림 할 숫자(문자열)</param>
|
|
/// <param name="roundPointer">반올림 위치</param>
|
|
/// <returns>반올림 된 문자열</returns>
|
|
public static string RoundValue(string inString, int roundPointer)
|
|
{
|
|
if (IsNumeric(inString) && roundPointer > -1)
|
|
{
|
|
string formatString = makeRoundFormat(roundPointer);
|
|
double pointConstant = 1;
|
|
|
|
for (int i = 0; i < roundPointer; i++)
|
|
{
|
|
pointConstant = pointConstant * 10;
|
|
}
|
|
|
|
inString = (Math.Round(Convert.ToDouble(inString) * pointConstant, MidpointRounding.AwayFromZero) / pointConstant).ToString(formatString);
|
|
}
|
|
|
|
return inString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 반올림
|
|
/// </summary>
|
|
/// <param name="inDouble">반올림 할 숫자(실수)</param>
|
|
/// <param name="roundPointer">반올림 위치</param>
|
|
/// <returns>반올림 된 문자열</returns>
|
|
public static string RoundValue(double inDouble, int roundPointer)
|
|
{
|
|
string returnValue = inDouble.ToString();
|
|
if (roundPointer > -1)
|
|
{
|
|
string formatString = makeRoundFormat(roundPointer);
|
|
double pointConstant = 1;
|
|
|
|
for (int i = 0; i < roundPointer; i++)
|
|
{
|
|
pointConstant = pointConstant * 10;
|
|
}
|
|
|
|
returnValue = (Math.Round(inDouble * pointConstant, MidpointRounding.AwayFromZero) / pointConstant).ToString(formatString);
|
|
}
|
|
return returnValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 초를 시분초로 변경한 값
|
|
/// </summary>
|
|
/// <param name="secondString">초</param>
|
|
/// <returns>시:분:초</returns>
|
|
public static string SecondToTime(string secondString)
|
|
{
|
|
string timeString = string.Empty;
|
|
|
|
try
|
|
{
|
|
if (IsNumeric(secondString))
|
|
{
|
|
int num = Convert.ToInt32(secondString);
|
|
int hours = 0;
|
|
int minute = 0;
|
|
int second = 0;
|
|
|
|
//시간공식
|
|
hours = num / 3600;//시 공식
|
|
minute = num % 3600 / 60;//분을 구하기위해서 입력되고 남은값에서 또 60을 나눈다.
|
|
second = num % 3600 % 60;//마지막 남은 시간에서 분을 뺀 나머지 시간을 초로 계산함
|
|
|
|
timeString = hours.ToString("#00") + ":" + minute.ToString("00") + ":" + second.ToString("00");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
return timeString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 문자열 배열을 정수형 배열로 변환
|
|
/// </summary>
|
|
/// <param name="inputArray">문자열 배열</param>
|
|
/// <returns>정수형 배열</returns>
|
|
public static int[] ConvertIntArray(string[] inputArray)
|
|
{
|
|
List<int> intList = new List<int>();
|
|
int[] returnArray = new int[0];
|
|
|
|
try
|
|
{
|
|
for (int i = 0; i < inputArray.Length; i++)
|
|
{
|
|
if (Common.IsNumeric(inputArray[i]))
|
|
{
|
|
intList.Add(Convert.ToInt32(inputArray[i]));
|
|
}
|
|
}
|
|
|
|
returnArray = intList.Distinct().ToArray();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
return returnArray;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 문자열 배열을 실수형 배열로 변환
|
|
/// </summary>
|
|
/// <param name="inputArray">문자열 배열</param>
|
|
/// <returns>정수형 배열</returns>
|
|
public static double[] ConvertDoubleArray(string[] inputArray)
|
|
{
|
|
List<double> doubleList = new List<double>();
|
|
double[] returnArray = new double[0];
|
|
|
|
try
|
|
{
|
|
for (int i = 0; i < inputArray.Length; i++)
|
|
{
|
|
if (Common.IsNumeric(inputArray[i]))
|
|
{
|
|
doubleList.Add(Convert.ToDouble(inputArray[i]));
|
|
}
|
|
}
|
|
|
|
returnArray = doubleList.Distinct().ToArray();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
return returnArray;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable Row 위치 옮기기
|
|
/// </summary>
|
|
/// <param name="listTable">Row 위치를 수정할 DataTable</param>
|
|
/// <param name="query">Row를 찾을 Query</param>
|
|
/// <param name="rowOrder">옮길 Row 위치</param>
|
|
public static void ReorderDataTableRow(ref DataTable listTable, string query, int rowOrder)
|
|
{
|
|
if (rowOrder < 0)
|
|
{
|
|
rowOrder = listTable.Rows.Count + rowOrder;
|
|
}
|
|
DataRow selectRow = listTable.Select(query)[0];
|
|
DataRow insertRow = listTable.NewRow();
|
|
insertRow.ItemArray = selectRow.ItemArray;
|
|
listTable.Rows.Remove(selectRow);
|
|
listTable.Rows.InsertAt(insertRow, rowOrder);
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable을 수정 가능한 형태로 변경
|
|
/// </summary>
|
|
/// <param name="listTable">변경할 DataTable</param>
|
|
/// <returns></returns>
|
|
public static DataTable SetEnableDataTableForEdit(DataTable listTable)
|
|
{
|
|
foreach (DataColumn col in listTable.Columns)
|
|
{
|
|
col.DataType = typeof(String);
|
|
col.ReadOnly = false;
|
|
}
|
|
|
|
return listTable;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable을 수정 가능한 형태로 변경
|
|
/// </summary>
|
|
/// <param name="listTable">변경할 DataTable</param>
|
|
/// <param name="changeDataType">1인 경우 모든 칼럼을 String으로 변경</param>
|
|
public static void SetEnableDataTableForEdit(ref DataTable listTable, short changeDataType)
|
|
{
|
|
foreach (DataColumn col in listTable.Columns)
|
|
{
|
|
if (changeDataType == 1)
|
|
{
|
|
col.DataType = typeof(String);
|
|
}
|
|
col.ReadOnly = false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable 생성
|
|
/// </summary>
|
|
/// <param name="columnName">Column Name들</param>
|
|
/// <returns>columnName을 Header로 생성된 DataTable</returns>
|
|
public static DataTable makeDataTable(string[] columnName)
|
|
{
|
|
DataTable returnTable = new DataTable();
|
|
|
|
for (int i = 0; i < columnName.Length; i++)
|
|
{
|
|
returnTable.Columns.Add(columnName[i], typeof(String));
|
|
}
|
|
|
|
return returnTable;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable에 Column추가(string)
|
|
/// </summary>
|
|
/// <param name="returnTable">Column을 추가할 DataTable</param>
|
|
/// <param name="columnName">추가할 Column Name들</param>
|
|
public static void tableAddColumn(DataTable returnTable, string[] columnName)
|
|
{
|
|
for (int i = 0; i < columnName.Length; i++)
|
|
{
|
|
returnTable.Columns.Add(columnName[i], typeof(String));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable에서 컬럼 제거
|
|
/// </summary>
|
|
/// <param name="returnTable">Column을 제거할 DataTable</param>
|
|
/// <param name="columnName">제거할 Column Name들</param>
|
|
public static void tableRemoveColumn(DataTable returnTable, string[] columnName)
|
|
{
|
|
for (int i = 0; i < columnName.Length; i++)
|
|
{
|
|
returnTable.Columns.Remove(columnName[i]);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataTable의 Column Name들 반환
|
|
/// </summary>
|
|
/// <param name="dataTable">Column Name을 반환할 Table</param>
|
|
/// <returns>문자열 배열 형태의 Column Name들</returns>
|
|
public static string[] getColumnNames(DataTable dataTable)
|
|
{
|
|
string[] returnArray = new string[dataTable.Columns.Count];
|
|
|
|
for (int i = 0; i < dataTable.Columns.Count; i++)
|
|
{
|
|
returnArray[i] = dataTable.Columns[i].ColumnName;
|
|
}
|
|
|
|
return returnArray;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 서버 로컬 Path 반환
|
|
/// </summary>
|
|
/// <param name="path">서버안의 Path</param>
|
|
/// <returns>서버 Path</returns>
|
|
public static string MapPath(string path)
|
|
{
|
|
return Path.Combine((string)AppDomain.CurrentDomain.GetData("ContentRootPath"), path);
|
|
}
|
|
|
|
public static string callWebRequest(string requestMethod, string reuqestURL, JObject headerJson, JObject bodyJson)
|
|
{
|
|
string responseFromServer = string.Empty;
|
|
|
|
try
|
|
{
|
|
|
|
WebRequest request = WebRequest.Create(reuqestURL);
|
|
request.Method = requestMethod.ToUpper();
|
|
request.ContentType = "application/json";
|
|
|
|
foreach(var jsonElement in headerJson)
|
|
{
|
|
request.Headers[jsonElement.Key] = jsonElement.Value.ToString();
|
|
}
|
|
|
|
byte[] bytes = new byte[0];
|
|
if(bodyJson != null)
|
|
{
|
|
bytes = Encoding.ASCII.GetBytes(GetStringFromJson(bodyJson));
|
|
request.ContentLength = bytes.Length; // 바이트수 지정
|
|
|
|
using (Stream reqStream = request.GetRequestStream())
|
|
{
|
|
reqStream.Write(bytes, 0, bytes.Length);
|
|
}
|
|
}
|
|
|
|
using (WebResponse response = request.GetResponse())
|
|
using (Stream dataStream = response.GetResponseStream())
|
|
using (StreamReader reader = new StreamReader(dataStream))
|
|
{
|
|
responseFromServer = reader.ReadToEnd();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Fatal("Common" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
|
|
}
|
|
|
|
return responseFromServer;
|
|
}
|
|
}
|
|
}
|