public DataTable GetSecretSetList(SecretAccountSetQuery sasq, out int totalCount)
 {
     try
     {
         return sasDao.GetSecretSetList(sasq, out totalCount);
     }
     catch (Exception ex)
     {
         throw new Exception("SecretAccountSetMgr-->GetSecretSetList" + ex.Message, ex);
     }
 }
 public HttpResponseBase GetSecretSetList()
 {
     string json = string.Empty;
     int totalCount = 0;
     uint result = 0;
     try
     {
         SecretAccountSetQuery query = new SecretAccountSetQuery();
         query.Start = Convert.ToInt32(Request.Params["start"] ?? "0");
         query.Limit = Convert.ToInt32(Request.Params["limit"] ?? "25");
         if (!string.IsNullOrEmpty(Request.Params["search_content"]))
         {
             if (uint.TryParse(Request.Params["search_content"], out result))
             {
                 query.user_id = result;
             }
             else
             {
                 query.user_username = Request.Params["search_content"];
             }
         }
         //判斷user_id  和ipfrom是否同時存在該賬號 若存在 則提示不能添加
         if (!string.IsNullOrEmpty(Request.Params["id"]))
         {
             query.id = int.Parse(Request.Params["id"]);
         }
         if (!string.IsNullOrEmpty(Request.Params["ipfrom"]))
         {
             query.ipfrom = Request.Params["ipfrom"];
         }
         if (!string.IsNullOrEmpty(Request.Params["ispage"]))
         {
             query.IsPage = false;
         }
         sasMgr = new SecretAccountSetMgr(mySqlConnectionString);
         DataTable dt = sasMgr.GetSecretSetList(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(dt, 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,totalCount:0,data:[]}";
     }
     this.Response.Clear();
     this.Response.Write(json);
     this.Response.End();
     return this.Response;
 }
        public HttpResponseBase SaveSecretSet()
        {
            string json = string.Empty;
            SecretAccountSet sas = new SecretAccountSet();
            try
            {
                sasMgr = new SecretAccountSetMgr(mySqlConnectionString);
                SecretAccountSetQuery sasq = new SecretAccountSetQuery();
                sasq.IsPage = false;
                bool issame = false;
                if (!string.IsNullOrEmpty(Request.Params["id"]))
                {
                    sas.id = int.Parse(Request.Params["id"]);
                    sasq.id = sas.id;
                }
                SecretAccountSet sasModel = sasMgr.Select(sasq);
                if (!string.IsNullOrEmpty(Request.Params["user_id"]))
                {
                    sas.user_id = uint.Parse(Request.Params["user_id"]);
                }
                string opassword = Request.Params["osecret_password"];
                string npassword = Request.Params["nsecret_password"];
                string password = string.Empty;
                string oldpwd = string.Empty;
                if (!string.IsNullOrEmpty(Request.Params["secret_limit"]))
                {
                    sas.secret_limit = Convert.ToInt32(Request.Params["secret_limit"]);
                }

                if (sasModel != null)
                {
                    sas.pwd_status = Convert.ToInt32(sasModel.pwd_status);
                }
                sas.updatedate = sas.createdate;
                //新密碼
                if (!string.IsNullOrEmpty(npassword))
                {
                    HashEncrypt hmd5 = new HashEncrypt();
                    password = hmd5.SHA256Encrypt(npassword);
                    sas.secret_pwd = password;
                    sas.pwd_status = 0;
                }
                if (string.IsNullOrEmpty(Request.Params["reset"]))
                {
                    //舊密碼
                    if (!string.IsNullOrEmpty(opassword))
                    {
                        HashEncrypt hmd5 = new HashEncrypt();
                        oldpwd = hmd5.SHA256Encrypt(opassword);
                    }
                    if (sasModel != null)
                    {
                        if (oldpwd == sasModel.secret_pwd)
                        {
                            issame = true;
                        }
                    }
                    IPAddress ip = new IPAddress(0);
                    if (IPAddress.TryParse(Request.Params["ipfrom"], out ip))
                    {
                        sas.ipfrom = ip.ToString();
                        if (!string.IsNullOrEmpty(Request.Params["id"]))
                        {
                            if (issame || Request.Params["nsecret_password"] == "")
                            {

                                if (sasMgr.Update(sas) > 0)
                                {
                                    json = "{success:true,msg:'修改成功!'}";
                                }
                                else
                                {
                                    json = "{success:false,msg:'修改失敗!'}";
                                }
                            }
                            else
                            {
                                json = "{success:false,msg:'原始密碼輸入錯誤!'}";
                            }
                        }
                        else
                        {
                            sas.secret_count = 0;
                            sas.user_login_attempts = 0;
                            sas.createdate = DateTime.Now;
                            sas.status = 0;
                            sas.pwd_status = 0;
                            if (sasMgr.SelectByUserIP(sas) == null)
                            {
                                if (sasMgr.Insert(sas) > 0)
                                {
                                    json = "{success:true,msg:'保存成功!'}";
                                }
                                else
                                {
                                    json = "{success:false,msg:'保存失敗!'}";
                                }
                            }
                            else
                            {
                                json = "{success:false,msg:'相同的用戶和IP不能重複添加!'}";
                            }
                        }
                    }
                    else
                    {
                        json = "{success:false,msg:'请输入正确的IP地址!'}";
                    }
                }
                else
                {
                    sas.pwd_status = 0;
                    if (sasMgr.Update(sas) > 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,msg:'操作失敗!'}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;
        }
        public DataTable GetSecretSetList(SecretAccountSetQuery sasq, out int totalCount)
        {
            StringBuilder sql = new StringBuilder();

            StringBuilder sqlfrom = new StringBuilder();
            StringBuilder sqlwhere = new StringBuilder();
            totalCount = 0;
            try
            {
                sasq.Replace4MySQL();

                sql.Append("SELECT sas.id,sas.user_id,sas.secret_pwd,sas.createdate,sas.updatedate, sas.`status`,sas.pwd_status, ");
                sql.Append("mu.user_email,mu.user_username,sas.user_login_attempts,sas.ipfrom ,sas.secret_limit,sas.secret_count ");

                sqlfrom.Append(" FROM secret_account_set sas ");
                sqlfrom.Append(" LEFT JOIN manage_user mu ON sas.user_id=mu.user_id  ");
                if (!string.IsNullOrEmpty(sasq.user_username))
                {
                    sqlwhere.AppendFormat(@" AND (mu.user_username like N'%{0}%' OR  mu.user_email like N'%{0}%' OR  sas.ipfrom like N'%{0}%' )", sasq.user_username);
                }
                if (sasq.user_id != 0)
                {
                    sqlwhere.AppendFormat(@" AND sas.user_id = '{0}'", sasq.user_id);
                }
                //判斷相同的用戶和IP不能重複添加
                if (!string.IsNullOrEmpty(sasq.ipfrom))
                {
                    sqlwhere.AppendFormat(@" AND sas.ipfrom = '{0}'", sasq.ipfrom);
                    if (sasq.id != 0)
                    {
                        sqlwhere.AppendFormat(@" AND sas.id != '{0}'", sasq.id);
                    }
                }
                else
                {
                    if (sasq.id != 0)
                    {
                        sqlwhere.AppendFormat(@" AND sas.id = '{0}'", sasq.id);
                    }
                }
                //  sqlwhere.Append(" ORDER BY sas.id DESC");
                if (sqlwhere.Length != 0)
                {
                    sqlfrom.Append(" WHERE " + sqlwhere.ToString().TrimStart().Remove(0, 3));
                }
                sqlfrom.Append(" ORDER BY sas.id DESC");
                if (sasq.IsPage)
                { 

                    DataTable dt = _access.getDataTable("SELECT  count(sas.id) as totalCount " + sqlfrom.ToString());
                    if (dt.Rows.Count > 0)
                    {
                        totalCount = Convert.ToInt32(dt.Rows[0]["totalCount"]);
                    }
                    sqlfrom.AppendFormat(" LIMIT {0},{1} ;", sasq.Start, sasq.Limit);
                }
                sql.Append(sqlfrom.ToString());
                return _access.getDataTable(sql.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception("SecretAccountSetDao-->GetSecretSetList" + ex.Message + sql.ToString(), ex);
            }

        }