using System; using System.Data; using System.Linq; using KART_API.Dto; using KART_API.Utils; namespace KART_API.Services { public class URIService { static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(URIService)); /// /// 단축 URL 반환 /// /// 실제 URL /// 단축 URL public static string GetURIKey(string realURI, int mode, string skey, string projectTitle, string okey) { string returnString = string.Empty; MSDAL msdal = new MSDAL(); var connection = msdal.ConnectReport(); try { DataTable urlKeyTable = msdal.GetDataTable("select * from REPORT_URL with(nolock) where REAL_URL = '" + realURI + "' and SKEY ='"+ skey + "' and URL_OWNER ='" + okey + "' and IS_PAGE_ONLY="+mode, connection); if (urlKeyTable.Rows.Count > 0) { returnString = urlKeyTable.Rows[0]["URL_KEY"].ToString(); } else { DataTable urlKeysTable = msdal.GetDataTable("select URL_KEY from REPORT_URL with(nolock) ", connection); string randomString = string.Empty; do { randomString = Common.GetRandomString(); } while (urlKeysTable.AsEnumerable().Where(dr => dr.Field("URL_KEY") == randomString).ToList().Count > 0); //while ((from DataRow dr in urlKeysTable.Rows where dr.Field("URL_KEY") == randomString select dr).ToList().Count > 0); string insertString = "insert into REPORT_URL(URL_KEY, REAL_URL, IS_PAGE_ONLY, SKEY, PROJECT_TITLE, URL_OWNER, LANDING_CNT) values ('" + randomString + "','" + realURI + "'," + mode + ",'" + skey + "','" + projectTitle + "','" + okey + "', 0)"; msdal.ExecuteSQL(insertString, connection); returnString = randomString; } } catch (Exception ex) { logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message); } finally { msdal.Disconnect(connection); } return returnString; } /// /// 실제 URL 반환 /// /// 단축 URL의 Key값 /// 카운트 Update 여부 /// 실제 URL public static DataTable GetRealURL(string urlKey, bool isCount) { DataTable urlTable = new DataTable(); MSDAL msdal = new MSDAL(); var connection = msdal.ConnectReport(); try { urlTable = GetUriInfo(urlKey); if (urlTable.Rows.Count > 0) { if (isCount) { string insertString = "update a set a.LANDING_CNT = a.LANDING_CNT + 1 from REPORT_URL a where URL_KEY = '" + urlKey + "'"; msdal.ExecuteSQL(insertString, connection); } } } catch (Exception ex) { logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message); } finally { msdal.Disconnect(connection); } return urlTable; } public static DataTable GetUriInfo(string urlKey) { DataTable uriInfo = new DataTable(); MSDAL msdal = new MSDAL(); var connection = msdal.ConnectReport(); try { uriInfo = msdal.GetDataTable("SELECT [URL_KEY],[REAL_URL],[IS_PAGE_ONLY],[SKEY], '' as [VER],[PROJECT_TITLE],[URL_OWNER] FROM REPORT_URL WHERE [URL_KEY] collate Korean_Wansung_CS_AS = '" + urlKey + "'", connection); if (uriInfo.Rows.Count > 0) { projectCodeInfo projectCodeInfo = new projectCodeInfo(uriInfo.Rows[0]["SKEY"].ToString()); Common.SetEnableDataTableForEdit(ref uriInfo,0); uriInfo.Columns["VER"].MaxLength = 50; uriInfo.Rows[0]["VER"] = projectCodeInfo.ver; } } catch (Exception ex) { logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message); } finally { msdal.Disconnect(connection); } return uriInfo; } } }