/// <summary>
 /// 查詢出序號兌換列表
 /// </summary>
 /// <returns>序號兌換列表</returns>
 public HttpResponseBase PromotionsBonuslist()
 {
     List<PromotionsBonusQuery> stores = new List<PromotionsBonusQuery>();
     _promBnus = new PromotionsBonusMgr(mySqlConnectionString);
     string json = string.Empty;
     try
     {
         PromotionsBonusQuery query = new PromotionsBonusQuery();
         query.Start = Convert.ToInt32(Request["Start"] ?? "0");
         if (!string.IsNullOrEmpty(Request["Limit"]))
         {
             query.Limit = Convert.ToInt32(Request["Limit"]);
         }
         if (!string.IsNullOrEmpty(Request.Params["ddlSel"]))
         {
             query.expired = Convert.ToInt32(Request.Params["ddlSel"]);
         }
         int totalCount = 0;
         stores = _promBnus.Query(query, out totalCount);
         IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
         //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式     
         timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
         //listUser是准备转换的对象
         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:true,totalCount:0,data:[]}";
     }
     this.Response.Clear();
     this.Response.Write(json);
     this.Response.End();
     return this.Response;
 }
        //public HttpResponseBase GetInsertNum()
        //{
        //    string jsonStr = "";
        //    try
        //    {
        //        //StreamReader sr = new StreamReader(@"D:\\2.txt", Encoding.UTF8);
        //        //string result = "";
        //        //string re = sr.ReadToEnd();
        //        //if (re.Length > 0)
        //        //{
        //        //    result = re.Trim().TrimEnd(',');
        //        //}
        //        //string[] ge = result.Split(',');
        //        //string a = ge[(ge.Count() - 1)];


        //        //jsonStr = "{success:true,msg:" + a + "}";//返回json數據
           
        //        jsonStr = "{success:true,msg:" + SuccessNum + "}";//返回json數據
        //       // sr.Close();
               

        //    }
        //    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);
        //        jsonStr = "{success:false}";
        //    }
        //    this.Response.Clear();
        //    this.Response.Write(jsonStr.ToString());
        //    this.Response.End();
        //    return this.Response;

        //}
        #endregion
        #endregion

        #region 序號兌換 更改活動使用狀態+JsonResult UpdateActive()
        /// <summary>
        /// 更改活動使用狀態
        /// </summary>
        /// <returns>數據庫操作結果</returns>
        public JsonResult UpdateActive()
        {
            string currentUser = (System.Web.HttpContext.Current.Session["caller"] as Caller).user_id.ToString();
            string muser = string.Empty;
            int activeValue = Convert.ToInt32(Request.Params["active"]);
            if (!string.IsNullOrEmpty(Request.Params["muser"]))
            {
                muser = (Request.Params["muser"]);
            }
            if (currentUser == muser && activeValue == 1)
            {
                return Json(new { success = "stop" });
            }
            _promBnus = new PromotionsBonusMgr(mySqlConnectionString);
            int id = Convert.ToInt32(Request.Params["id"]);
            PromotionsBonus model = new PromotionsBonus();
            model = _promBnus.GetModel(id);
            model.active = Convert.ToBoolean(activeValue);
            model.muser =int.Parse(currentUser);
            model.modified = DateTime.Now;
            if (_promBnus.UpdateActive(model) > 0)
            {
                return Json(new { success = "true", msg = "" });
            }
            else
            {
                return Json(new { success = "false", msg = "" });
            }
        }
        /// <summary>
        /// 保存前臺新增或者修改的數據
        /// </summary>
        /// <returns></returns>
        public HttpResponseBase Save()
        {
            PromotionsBonus model = new PromotionsBonus();
            model.muser=int.Parse((System.Web.HttpContext.Current.Session["caller"] as Caller).user_id.ToString());
            //判斷是否能夠獲取到rowid
            if (String.IsNullOrEmpty(Request.Params["rowid"]))  //新增
            {
                model.modified = DateTime.Now;
                model.created = DateTime.Now;
                model.kuser = model.muser;
                //todo:對model進行賦值
                try
                {
                    model.amount = Convert.ToInt32(Request.Params["amount"]);
                }
                catch (Exception)
                {
                    model.amount = 0;
                }
                if (Request.Params["group_id"].ToString() != "")
                {
                    try//group_id
                    {
                        model.group_id = Convert.ToInt32(Request.Params["group_id"].ToString());
                    }
                    catch (Exception)
                    {
                        model.group_id = 0;
                    }
                    model.condition_id = 0;
                }
                else if (Request.Params["condition_id"].ToString() != "")
                {
                    try//condition_id
                    {
                        model.condition_id = Convert.ToInt32(Request.Params["condition_id"].ToString());
                    }
                    catch (Exception)
                    {
                        model.condition_id = 0;
                    }
                    model.group_id = 0;
                }

                try
                {
                    model.end = Convert.ToDateTime(Request.Params["end"]);
                }
                catch (Exception)
                {
                    model.end = DateTime.Now;
                }

                try
                {
                    model.start = Convert.ToDateTime(Request.Params["startbegin"]);
                }
                catch (Exception)
                {
                    model.start = DateTime.Now;
                }
                try
                {
                    model.name = Request.Params["name"];
                }
                catch (Exception)
                {
                    model.name = "";
                }
                try
                {
                    model.type = Convert.ToInt32(Request.Params["bonus_type"]);
                }
                catch (Exception)
                {
                    model.type = 0;
                }
                try
                {
                    if (Convert.ToInt32(Request.Params["hmcfsy"]) == 1)
                    {
                        model.repeat = true;
                    }
                    else
                    {
                        model.repeat = false;
                    }
                }
                catch (Exception)
                {
                    model.repeat = false;
                }
                try
                {
                    if (Convert.ToInt32(Request.Params["sydzxh"]) == 1)
                    {
                        model.multiple = true;
                    }
                    else
                    {
                        model.multiple = false;
                    }
                }
                catch (Exception)
                {
                    model.multiple = false;
                }
                //添加使用期限
                int day;
                if (Int32.TryParse(Request.Params["days"], out day))
                {
                    model.days = day;
                }
                return InsertPromotionsBonus(model);//如果獲取不到則進行新增

            }
            else    //修改
            {
                _promBnus = new PromotionsBonusMgr(mySqlConnectionString);
                model.id = Convert.ToInt32(Request.Params["rowid"]);
                PromotionsBonus PB = _promBnus.GetModel(model.id);
                model.modified = DateTime.Now;
                try
                {
                    model.amount = Convert.ToInt32(Request.Params["amount"]);
                }
                catch (Exception)
                {
                    model.amount = PB.amount;
                }
                try
                {
                    model.type = Convert.ToInt32(Request.Params["bonus_type"]);
                }
                catch (Exception)
                {
                    model.type = PB.type;
                }
                if (Request.Params["group_id"].ToString() != "")
                {
                    try//group_id
                    {
                        model.group_id = Convert.ToInt32(Request.Params["group_id"].ToString());
                    }
                    catch (Exception)
                    {
                        model.group_id = PB.group_id;
                    }
                    model.condition_id = 0;
                }
                else if (Request.Params["condition_id"].ToString() != "")
                {
                    try//condition_id
                    {
                        model.condition_id = Convert.ToInt32(Request.Params["condition_id"].ToString());
                    }
                    catch (Exception)
                    {
                        model.condition_id = PB.condition_id;
                    }
                    model.group_id = 0;
                }
                try
                {
                    model.end = Convert.ToDateTime(Request.Params["end"]);
                }
                catch (Exception)
                {
                    model.end = PB.end;
                }
                try
                {
                    model.start = Convert.ToDateTime(Request.Params["startbegin"]);
                }
                catch (Exception)
                {
                    model.start = PB.start;
                }
                try
                {
                    model.name = Request.Params["name"];
                }
                catch (Exception)
                {
                    model.name = PB.name;
                }
                try
                {
                    if ("1" == Request.Params["hmcfsy"].ToString())
                    {
                        model.repeat = true;
                    }
                    else
                    {
                        model.repeat = false;
                    }
                }
                catch (Exception)
                {
                    model.repeat = PB.repeat;
                }
                try
                {
                    if ("1" == Request.Params["sydzxh"].ToString())
                    {
                        model.multiple = true;
                    }
                    else
                    {
                        model.multiple = false;
                    }
                }
                catch (Exception)
                {
                    model.multiple = PB.multiple;
                }
                model.active = false;
                int day;
                if (Int32.TryParse(Request.Params["days"], out day))
                {
                    model.days = day;
                }
                //todo:對model進行賦值
                return UpdatePromotionsBonus(model);//如果可以獲取到rowid則進行修改
            }
        }
        /// <summary>
        /// 新增功能 PromotionsBonus
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        protected HttpResponseBase InsertPromotionsBonus(PromotionsBonus model)
        {
            string json = string.Empty;
            try
            {
                _promBnus = new PromotionsBonusMgr(mySqlConnectionString);
                _promBnus.Save(model);
                json = "{success:true}";//返回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;
        }
        /// <summary>
        /// 刪除數據
        /// </summary>
        /// <returns>數據庫操作結果</returns>
        public HttpResponseBase Delete()
        {
            string jsonStr = String.Empty;
            _promBnus = new PromotionsBonusMgr(mySqlConnectionString);
            PromotionsBonus query = new PromotionsBonus();
            if (!String.IsNullOrEmpty(Request.Params["rowid"]))
            {
                try
                {
                    foreach (string rid in Request.Params["rowid"].ToString().Split('|'))
                    {
                        if (!string.IsNullOrEmpty(rid))
                        {
                            query.id = Convert.ToInt32(rid);
                            query.muser=int.Parse((System.Web.HttpContext.Current.Session["caller"] as Caller).user_id.ToString());
                            query.modified = DateTime.Now;
                            _promBnus.Delete(query);
                        }
                    }
                    jsonStr = "{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);
                    jsonStr = "{success:false}";
                }
            }

            this.Response.Clear();
            this.Response.Write(jsonStr);
            this.Response.End();
            return this.Response;
        }