public HttpResponseBase GetSystemKeyWord()
        {
            int totalCount = 0;
            string json = string.Empty;
            List<SphinxKeywordQuery> stores = new List<SphinxKeywordQuery>();
            SphinxKeywordQuery query = new SphinxKeywordQuery();
            swMgr = new SystemKeyWordMgr(SqlConnectionString);
            DateTime time;
            try
            {
                query.Start = Convert.ToInt32(Request.Params["Start"] ?? "0");
                if (!string.IsNullOrEmpty(Request.Params["Limit"]))
                {
                    query.Limit = Convert.ToInt32(Request.Params["Limit"]);
                }
                if (!string.IsNullOrEmpty(Request.Params["searchKey"]))
                {
                    query.searchKey = Request.Params["searchKey"];
                }
                if (DateTime.TryParse(Request.Params["startTime"], out time))
                {
                    query.startTime = time;
                }
                if (DateTime.TryParse(Request.Params["endTime"], out time))
                {
                    query.endTime = time;
                }
                stores = swMgr.GetSystemKeyWord(query, out totalCount);
                IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
                //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式     
                timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                json = "{success:true,totalCount:" + totalCount + ",data:" + JsonConvert.SerializeObject(stores, Formatting.Indented, timeConverter) + "}";//返回json數據
            }
            catch (Exception ex)
            {
                Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
                logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
                logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                log.Error(logMessage);
                json = "{success:false}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;

        }
 public HttpResponseBase KeyWordUploadExcel()
 {
     string newName = string.Empty;
     string json = string.Empty;
     List<SphinxKeywordQuery> store = new List<SphinxKeywordQuery>();
     HashEncrypt hashpt = new HashEncrypt();
     try
     {
         DTIlocExcel.Clear();
         DTIlocExcel.Columns.Clear();
         DTIlocExcel.Columns.Add("keyword", typeof(String));
         DTIlocExcel.Columns.Add("foodkeyword(1:是食安關鍵字,0不是食安關鍵字)", typeof(String));
         DTIlocExcel.Columns.Add("failureMessage", typeof(String));
         int result = 0;
         int count = 0;//總匯入數
         //int entercount = 0;//插入失敗個數
         int errorcount = 0;//數據異常個數
         int repeat = 0;//數據已存在個數
         string create_user = (Session["caller"] as Caller).user_username;
         if (Request.Files["ImportExcelFile"] != null && Request.Files["ImportExcelFile"].ContentLength > 0)
         {
             HttpPostedFileBase excelFile = Request.Files["ImportExcelFile"];
             newName = Server.MapPath(excelPath) + excelFile.FileName;
             excelFile.SaveAs(newName);
             DataTable dt = new DataTable();
             NPOI4ExcelHelper helper = new NPOI4ExcelHelper(newName);
             dt = helper.SheetData();
             if (dt.Rows.Count > 0)
             {
                 swMgr = new SystemKeyWordMgr(SqlConnectionString);
                 int i = 0;
                 foreach (DataRow dr in dt.Rows)
                 {
                     StringBuilder strsql = new StringBuilder();
                     SphinxKeywordQuery query = new BLL.gigade.Model.Query.SphinxKeywordQuery();
                     i++;
                     try
                     {
                         if (!string.IsNullOrEmpty(dr[0].ToString()) && !string.IsNullOrEmpty(dr[1].ToString()))
                         {
                             int row_id_exsit = swMgr.CheckKeyWordExsit(dr[0].ToString());//判斷關鍵字是否存在
                             query.user_name = create_user;
                             query.key_word = dr[0].ToString();
                             query.flag = dr[1].ToString();
                             query.operateType = 1;
                             if (row_id_exsit > 0)
                             {
                                 DataRow drtwo = DTIlocExcel.NewRow();
                                 drtwo[0] = dr[0].ToString();
                                 drtwo[1] = dr[1].ToString();
                                 drtwo[2] = "系統關鍵字已存在";
                                 DTIlocExcel.Rows.Add(drtwo);
                                 repeat++;
                                 continue;
                             }
                             else//關鍵字不存在
                             {
                                 if (query.flag == "0" || query.flag == "1")
                                 {
                                     byte[] strBt = Encoding.Unicode.GetBytes(query.key_word);
                                     //關鍵字最多為25個中文字或50個英文字
                                     if (strBt.Length <= 50)
                                     {
                                         result = swMgr.SaveSystemKeyWord(query);
                                         if (result > 0)
                                         {
                                             count++;
                                             continue;
                                         }
                                         else
                                         {
                                             DataRow drtwo = DTIlocExcel.NewRow();
                                             drtwo[0] = dr[0].ToString();
                                             drtwo[1] = dr[1].ToString();
                                             drtwo[2] = "關鍵字插入數據庫時失敗";
                                             DTIlocExcel.Rows.Add(drtwo);
                                             errorcount++;
                                             continue;
                                         }
                                     }
                                     else
                                     {
                                         DataRow drtwo = DTIlocExcel.NewRow();
                                         drtwo[0] = dr[0].ToString();
                                         drtwo[1] = dr[1].ToString();
                                         drtwo[2] = "關鍵字最多為25個中文字或50個英文字";
                                         DTIlocExcel.Rows.Add(drtwo);
                                         errorcount++;
                                         continue;
                                     }
                                 }
                                 else
                                 {
                                     DataRow drtwo = DTIlocExcel.NewRow();
                                     drtwo[0] = dr[0].ToString();
                                     drtwo[1] = dr[1].ToString();
                                     drtwo[2] = "'foodkeyword的值只能為0或1'";
                                     DTIlocExcel.Rows.Add(drtwo);
                                     errorcount++;
                                     continue;
                                 }
                             }
                         }
                         else
                         {
                             DataRow drtwo = DTIlocExcel.NewRow();
                             drtwo[0] = dr[0].ToString();
                             drtwo[1] = dr[1].ToString();
                             drtwo[2] = "keyword或foodkeyword不符合格式";
                             DTIlocExcel.Rows.Add(drtwo);
                             errorcount++;
                             continue;
                         }
                     }
                     catch
                     {
                         DataRow drtwo = DTIlocExcel.NewRow();
                         drtwo[0] = dr[0].ToString();
                         drtwo[1] = dr[1].ToString();
                         drtwo[2] = "數據異常";
                         DTIlocExcel.Rows.Add(drtwo);
                         errorcount++;
                         continue;
                     }
                 }
                 if (count > 0)
                 {
                     json = "{success:true,total:" + count + ",error:" + errorcount + ",repeat:" + repeat+"}";
                 }
                 else
                 {
                     json = "{success:true,total:" + 0 + ",error:" + errorcount + ",repeat:" + repeat+"}";
                 }
             }
             else
             {
                 json = "{success:true,total:" + 0 + ",error:" + 0 + ",repeat" + repeat + "}";
             }
         }
     }
     catch (Exception ex)
     {
         Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
         logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
         logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
         log.Error(logMessage);
         json = "{success:false,data:" + "" + "}";
     }
     this.Response.Clear();
     this.Response.Write(json);
     this.Response.End();
     return this.Response;
 }
 public HttpResponseBase CheckKeyWordExsit()
 {
     string json = string.Empty;
     SphinxKeywordQuery query = new SphinxKeywordQuery();
     swMgr = new SystemKeyWordMgr(SqlConnectionString);
     int msg = 0;
     try
     {
         if (!string.IsNullOrEmpty(Request.Params["key_word"]))
         {
             query.key_word = Request.Params["key_word"].Trim();
         }
         msg = swMgr.CheckKeyWordExsit(query.key_word);
         json = "{success:true,msg:" + msg + "}";//返回json數據
     }
     catch (Exception ex)
     {
         Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
         logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
         logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
         log.Error(logMessage);
         json = "{success:false}";
     }
     this.Response.Clear();
     this.Response.Write(json);
     this.Response.End();
     return this.Response;
 } 
        public void KeyWordExcelList()
        {
            string json = string.Empty;
            SphinxKeywordQuery skQuery = new SphinxKeywordQuery();
            DataTable dtExcel = new DataTable();
            List<SphinxKeywordQuery> store = new List<SphinxKeywordQuery>();
            try
            {
                DateTime time;
                if (!string.IsNullOrEmpty(Request.Params["searchKey"].Trim()))
                {
                    skQuery.searchKey = Request.Params["searchKey"].ToString();
                }
                if (DateTime.TryParse(Request.Params["startTime"].ToString(), out time))
                {
                    skQuery.startTime = time;
                }
                if (DateTime.TryParse(Request.Params["endtime"].ToString(), out time))
                {
                    skQuery.endTime = time;
                }

                swMgr = new SystemKeyWordMgr(SqlConnectionString);
                store = swMgr.GetKeyWordExportList(skQuery);
                dtExcel.Columns.Add("keyword", typeof(String));
                dtExcel.Columns.Add("foodkeyword(1:是食安關鍵字,0不是食安關鍵字)", typeof(String));
                for (int i = 0; i < store.Count; i++)
                {
                    DataRow newRow = dtExcel.NewRow();
                    newRow[0] = store[i].key_word.ToString();
                    newRow[1] = store[i].flag.ToString();
                    dtExcel.Rows.Add(newRow);
                }
                if (dtExcel.Rows.Count > 0)
                {
                    string fileName = "系統關鍵字_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                    MemoryStream ms = ExcelHelperXhf.ExportDT(dtExcel, "");
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                    Response.BinaryWrite(ms.ToArray());
                }
                else
                {
                    Response.Write("匯出數據不存在");
                }
            }
            catch (Exception ex)
            {
                Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
                logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
                logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                log.Error(logMessage);
            }
        }
        public HttpResponseBase DelSystemKeyWord()
        {
            string json = string.Empty;
            ///返回的状态
            int state = 0;
            List<SphinxKeywordQuery> stores = new List<SphinxKeywordQuery>();
            SphinxKeywordQuery query = new SphinxKeywordQuery();
            swMgr = new SystemKeyWordMgr(SqlConnectionString);
            try
            {
                if (!string.IsNullOrEmpty(Request.Params["row_id"]))
                {
                    string[] strId = Request.Params["row_id"].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    query.ArrId = strId;
                }
                query.user_name = (System.Web.HttpContext.Current.Session["caller"] as Caller).user_username;
                state = swMgr.DelSystemKeyWord(query);
                IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
                //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式     
                timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                json = "{success:true,state:" + state + "}";//返回json數據
            }
            catch (Exception ex)
            {
                Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
                logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
                logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                log.Error(logMessage);
                json = "{success:false}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;

        }