public HttpResponseBase PromoShareConditionList()
 {
     PromoShareConditionQuery query = new PromoShareConditionQuery();
     List<PromoShareConditionQuery> store = new List<PromoShareConditionQuery>();
     string json = string.Empty;
     try
     {
         if (!string.IsNullOrEmpty(Request.Params["promo_id"]))
         {
             query.promo_id = Convert.ToInt32(Request.Params["promo_id"]);
         }
         PshareConMgr = new PromoShareConditionMgr(mySqlConnectionString);
         store = PshareConMgr.GetList(query);
         if (store.Count>0)
         {
             for (int i = 0; i < store.Count; i++)
             {
                 //處理數據使"條件名稱"與"對應值"顯示對應描述
                 //store[i].
             }   
         }
         IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
         timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
         json = "{success:true,data:" + JsonConvert.SerializeObject(store, Formatting.Indented, timeConverter) + "}";
     }
     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:true,data:[]}";
     }
     this.Response.Clear();
     this.Response.Write(json);
     this.Response.End();
     return this.Response;
 }
 public HttpResponseBase GetEditData()
 {
    // List<PromoShareCondition> store = new List<PromoShareCondition>();
     PromoShareCondition query = new PromoShareCondition();
     string json = string.Empty;
     try
     {
         if (!string.IsNullOrEmpty(Request.Params["PromoId"]))
         {
             query.promo_id = Convert.ToInt32(Request.Params["PromoId"]);
         }
         PshareConMgr = new PromoShareConditionMgr(mySqlConnectionString);
         DataTable _dt=PshareConMgr.Get(condition, query);
     //    _dt.Columns.Add("promo_event_id",typeof (string));
      //   string strPromoId = "SH";
         //if (_dt.Rows[0]["promo_id"].ToString().Length < 6)
         //{
         //    for (int i = 0; i <6-_dt.Rows[0]["promo_id"].ToString().Length; i++)
         //    {
         //        strPromoId += "0";
         //    }
         //}
         //strPromoId += _dt.Rows[0]["promo_id"].ToString();
         //_dt.Rows[0]["promo_event_id"] = strPromoId;
         json = "{success:true,data:" + JsonConvert.SerializeObject(_dt, Formatting.Indented) + "}";
     }
     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);
     }
     this.Response.Clear();
     this.Response.Write(json);
     this.Response.End();
     return this.Response;
 }
        //public HttpResponseBase EditPromoShareMaster()
        //{
        //    PromoShareMaster query = new PromoShareMaster();
        //    string json = string.Empty;
        //    try
        //    {
        //        if (!string.IsNullOrEmpty(Request.Params["Epromo_id"]))
        //        {
        //            query.promo_id = Convert.ToInt32(Request.Params["Epromo_id"]);
        //        }
        //        if (!string.IsNullOrEmpty(Request.Params["Epromo_name"]))
        //        {
        //            query.promo_name = Request.Params["Epromo_name"];
        //        }
        //        if (!string.IsNullOrEmpty(Request.Params["Epromo_desc"]))
        //        {
        //            query.promo_desc = Request.Params["Epromo_desc"];
        //        }
        //        if (!string.IsNullOrEmpty(Request.Params["Epromo_start"]))
        //        {
        //            query.promo_start = Convert.ToDateTime(Request.Params["Epromo_start"]);
        //        }
        //        if (!string.IsNullOrEmpty(Request.Params["Epromo_end"]))
        //        {
        //            query.promo_end = Convert.ToDateTime(Request.Params["Epromo_end"]);
        //        }
        //        PshareMgr = new PromoShareMasterMgr(mySqlConnectionString);
        //        if (PshareMgr.Update(query) > 0)
        //        {
        //            json = "{success:true}";
        //        }
        //        else
        //        {
        //            json = "{success:false}";
        //        }
        //    }
        //    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;
        //}
        #endregion

        #region 編輯
        public HttpResponseBase updatePromoShareCon()
        {
            PromoShareMaster psmQuery = new PromoShareMaster();
              PromoShareCondition pscQuery = new PromoShareCondition();
            PshareMgr = new PromoShareMasterMgr(mySqlConnectionString);
            PshareConMgr = new PromoShareConditionMgr(mySqlConnectionString);
            string json = string.Empty;
            try
            {
            if (!string.IsNullOrEmpty(Request.Params["promo_name"]))
            {
                psmQuery.promo_name = Request.Params["promo_name"];
            }
            if (!string.IsNullOrEmpty(Request.Params["promo_desc"]))
            {
                psmQuery.promo_desc = Request.Params["promo_desc"];
            }
            if (!string.IsNullOrEmpty(Request.Params["promo_start"]))
            {
                psmQuery.promo_start = Convert.ToDateTime(Request.Params["promo_start"]);
            }
            if (!string.IsNullOrEmpty(Request.Params["promo_end"]))
            {
                psmQuery.promo_end = Convert.ToDateTime(Request.Params["promo_end"]);
            }
            if (!string.IsNullOrEmpty(Request.Params["promo_id"]))
            {
                psmQuery.promo_id = Convert.ToInt32(Request.Params["promo_id"]);
            }

            //判斷如果condition表內無此編號的數據則insert,如果有此編號的數據則update
            if (PshareMgr.PromoCon(psmQuery) == 0)
            {//執行新增
                InsertIntoPromoShareCon(pscQuery);//插入promo_share_condition;新增入第二個面板
            }
            else
            {//執行編輯
                //編輯第一個面板
                PshareMgr.Update(psmQuery);
                //1編輯第二個面板 promo_share_condition, 
                //2將promo_share_condition表中對應promo_id數據刪除 
                //3然後再次insert
                PshareConMgr.Delete(psmQuery.promo_id);
                InsertIntoPromoShareCon(pscQuery);
                //PshareConMgr.Update(pscQuery);
            }
            json = "{success:true}";
        }
               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 GetPromoShareConditionCount()
        {
            PromoShareCondition query = new PromoShareCondition();
            string json = string.Empty;
            int count = 0;
            try
            {
                if (!string.IsNullOrEmpty(Request.Params["promo_id"]))
                {
                    query.promo_id = Convert.ToInt32(Request.Params["promo_id"]);
                }
                PshareConMgr = new PromoShareConditionMgr(mySqlConnectionString);
                count = PshareConMgr.GetPromoShareConditionCount(query);
               
                json = "{success:true,Count:" + count + "}";
            }

            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,Count:0}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;
        }
        public void InsertIntoPromoShareCon(PromoShareCondition query)
        {
            List<PromoShareCondition> list = new List<PromoShareCondition>();
            PshareConMgr = new PromoShareConditionMgr(mySqlConnectionString);
            string json = string.Empty;
             DataTable _dt=new DataTable();
            try
            {
                for (int i = 0; i < condition.Length; i++)
                {
                    if (!string.IsNullOrEmpty(Request.Params[condition[i]]))
                    {
                        query = new PromoShareConditionQuery();
                        query.promo_id = Convert.ToInt32(Request.Params["promo_id"]);
                        query.condition_name = condition[i];
                        query.condition_value = Request.Params[ condition[i]];
                        if (query.condition_name == "picture")
                        {
                            #region 上傳圖片
                            string ErrorMsg = string.Empty;
                            string path = Server.MapPath(xmlPath);
                            SiteConfigMgr _siteConfigMgr = new SiteConfigMgr(path);
                            SiteConfig extention_config = _siteConfigMgr.GetConfigByName("PIC_Extention_Format");
                            SiteConfig minValue_config = _siteConfigMgr.GetConfigByName("PIC_Length_Min_Element");
                            SiteConfig maxValue_config = _siteConfigMgr.GetConfigByName("PIC_Length_MaxValue");
                            SiteConfig admin_userName = _siteConfigMgr.GetConfigByName("ADMIN_USERNAME");
                            SiteConfig admin_passwd = _siteConfigMgr.GetConfigByName("ADMIN_PASSWD");
                            //擴展名、最小值、最大值
                            string extention = extention_config.Value == "" ? extention_config.DefaultValue : extention_config.Value;
                            string minValue = minValue_config.Value == "" ? minValue_config.DefaultValue : minValue_config.Value;
                            string maxValue = maxValue_config.Value == "" ? maxValue_config.DefaultValue : maxValue_config.Value;
                            string localPromoPath = imgLocalPath + promoPath;//圖片存儲地址
                            FileManagement fileLoad = new FileManagement();
                            if (Request.Files.Count > 0)
                            {
                                HttpPostedFileBase file = Request.Files[0];
                                string fileName = string.Empty;//當前文件名
                                string fileExtention = string.Empty;//當前文件的擴展名
                                fileName = fileLoad.NewFileName(file.FileName);
                                if (fileName != "")
                                {
                                    fileName = fileName.Substring(0, fileName.LastIndexOf("."));
                                    fileExtention = file.FileName.Substring(file.FileName.LastIndexOf('.')).ToLower().ToString();
                                    string NewFileName = string.Empty;
                                    HashEncrypt hash = new HashEncrypt();
                                    NewFileName = hash.Md5Encrypt(fileName, "32");
                                    string ServerPath = string.Empty;
                                    FTP f_cf = new FTP();
                                    f_cf.MakeMultiDirectory(localPromoPath.Substring(0, localPromoPath.Length - promoPath.Length + 1), promoPath.Substring(1, promoPath.Length - 2).Split('/'), ftpuser, ftppwd);
                                    fileName = NewFileName + fileExtention;
                                    NewFileName = localPromoPath + NewFileName + fileExtention;//絕對路徑
                                    ServerPath = Server.MapPath(imgLocalServerPath + promoPath);

                                    //上傳之前刪除已有的圖片
                                    if (query.promo_id != 0)
                                    {
                                        _dt = PshareConMgr.Get(condition, query);
                                        if (_dt.Rows[0]["picture"].ToString() != "")
                                        {
                                            string oldFileName = _dt.Rows[0]["picture"].ToString();
                                            CommonFunction.DeletePicFile(ServerPath + oldFileName);//刪除本地圖片
                                            FTP ftp = new FTP(localPromoPath, ftpuser, ftppwd);
                                            List<string> tem = ftp.GetFileList();
                                            if (tem.Contains(oldFileName))
                                            {
                                                FTP ftps = new FTP(localPromoPath + oldFileName, ftpuser, ftppwd);
                                                ftps.DeleteFile(localPromoPath + oldFileName);
                                            }
                                        }
                                    }
                                    try
                                    {
                                        Resource.CoreMessage = new CoreResource("Product");
                                        bool result = fileLoad.UpLoadFile(file, ServerPath, NewFileName, extention, int.Parse(maxValue), int.Parse(minValue), ref ErrorMsg, ftpuser, ftppwd);
                                        if (result)
                                        {
                                            query.condition_value = fileName;
                                        }

                                    }
                                    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);
                                    }
                                    if (!string.IsNullOrEmpty(ErrorMsg))
                                    {
                                        string jsonStr = string.Empty;
                                        json = "{success:true,msg:\"" + ErrorMsg + "\"}";
                                        this.Response.Clear();
                                        this.Response.Write(json);
                                        this.Response.End();
                                    }

                                }
                            }
                            else
                            {
                                query.condition_value = _dt.Rows[0]["picture"].ToString();
                            }
                            #endregion
                        }
                        list.Add(query);
                    }
                }
             
                PshareConMgr.AddSql(list);
            }
            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}";
            }
        }