using System; using System.Data; using System.Linq; namespace REPORT_API_ver6.Services { public class URI { static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(URI)); /// /// 단축 URL 반환 /// /// 실제 URL /// 단축 URL public static string GetURIKey(string realURI, int mode, string skey, string projectTitle, string okey) { string returnString = string.Empty; DAL dal = new DAL(); var connection = dal.ConnectReport(); try { DataTable urlKeyTable = dal.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 = dal.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)"; dal.ExecuteSQL(insertString, connection); returnString = randomString; } } catch (Exception ex) { logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message); } finally { dal.Disconnect(connection); } return returnString; } /// /// 실제 URL 반환 /// /// 단축 URL의 Key값 /// 카운트 Update 여부 /// 실제 URL public static DataTable GetRealURL(string urlKey, bool isCount) { DataTable urlTable = new DataTable(); DAL dal = new DAL(); var connection = dal.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 + "'"; dal.ExecuteSQL(insertString, connection); } } } catch (Exception ex) { logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message); } finally { dal.Disconnect(connection); } return urlTable; } public static DataTable GetUriInfo(string urlKey) { DataTable uriInfo = new DataTable(); DAL dal = new DAL(); var connection = dal.ConnectReport(); try { uriInfo = dal.GetDataTable("SELECT [URL_KEY],[REAL_URL],[IS_PAGE_ONLY],[SKEY],[PROJECT_TITLE],[URL_OWNER] FROM REPORT_URL WHERE [URL_KEY] collate Korean_Wansung_CS_AS = '" + urlKey + "'", connection); } catch (Exception ex) { logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message); } finally { dal.Disconnect(connection); } return uriInfo; } } }