Ejemplo n.º 1
0
 /// <summary>
 /// 保存参数
 /// </summary>
 /// <param name="cfg"></param>
 /// <param name="msg"></param>
 /// <returns></returns>
 public static int SaveCfg(Syscfg cfg, out string msg)
 {
     try
     {
         using (IDAL dal = DALBuilder.CreateDAL(ConfigurationManager.ConnectionStrings["SYSDB"].ConnectionString, 0))
         {
             int i;
             dal.BeginTran();
             dal.Execute("UPDATE dbo.tSysCfg SET OptionValue=@OptionValue WHERE OptionName=@OptionName", out i,
                         dal.CreateParameter("@OptionValue", cfg.Company),
                         dal.CreateParameter("@OptionName", "Company"));
             dal.Execute("UPDATE dbo.tSysCfg SET OptionValue=@OptionValue WHERE OptionName=@OptionName", out i,
                         dal.CreateParameter("@OptionValue", cfg.License),
                         dal.CreateParameter("@OptionName", "License"));
             dal.CommitTran();
             msg = "success";
             return(1);
         }
     }
     catch (System.Exception ex)
     {
         msg = ex.Message;
         return(-1);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 读取参数
        /// </summary>
        /// <param name="cfg"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static Syscfg LoadCfg(out string msg)
        {
            try
            {
                using (IDAL dal = DALBuilder.CreateDAL(ConfigurationManager.ConnectionStrings["SYSDB"].ConnectionString, 0))
                {
                    dal.OpenReader("Select Section,OptionName,OptionValue From tSyscfg");
                    Syscfg cfg = new Syscfg();
                    while (dal.DataReader.Read())
                    {
                        switch (Convert.ToString(dal.DataReader["OptionName"]).TrimEnd())
                        {
                        case "Company":
                            cfg.Company = Convert.ToString(dal.DataReader["OptionValue"]).TrimEnd();
                            break;

                        case "License":
                            cfg.License = Convert.ToString(dal.DataReader["OptionValue"]).TrimEnd();
                            break;
                        }
                    }
                    cfg.SerialNo = LicenseClass.GetMacByNetworkInterface();
                    if (string.IsNullOrEmpty(cfg.SerialNo))
                    {
                        msg = "获取序列号失败";
                        return(null);
                    }
                    if (cfg.License != null && cfg.License.Length == 48)
                    {
                        LicenseClass.AnalyzeLisense(cfg.License, cfg.SerialNo, out cfg.ExpDate, out cfg.ReportNumber);
                        if (cfg.ExpDate == "99999999")
                        {
                            cfg.ExpDate = "永久";
                        }
                        else
                        {
                            StringBuilder temp = new StringBuilder(32);
                            temp.AppendFormat("{0}-{1}-{2}", cfg.ExpDate.Substring(0, 4), cfg.ExpDate.Substring(4, 2), cfg.ExpDate.Substring(6, 2));
                            cfg.ExpDate = temp.ToString();
                        }
                        if (cfg.ReportNumber == "999")
                        {
                            cfg.ReportNumber = "无限";
                        }
                    }
                    else
                    {
                        cfg.ExpDate      = "无效";
                        cfg.ReportNumber = "无效";
                    }
                    msg = "success";
                    return(cfg);
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return(null);
            }
        }
Ejemplo n.º 3
0
        // post api/syscfg/5
        public void Post(Syscfg value)
        {
            User user = HttpContext.Current.Session["SigninedUser"] as User;

            if (user == null || !user.IsAdmin)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }
            string ResultMessage;
            int    ResultStatus = SyscfgBLL.SaveCfg(value, out ResultMessage);

            if (ResultStatus == 0)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
            }
            else if (ResultStatus == -1)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
        }
Ejemplo n.º 4
0
        const string PARAM_BETWEEN_BOTH_PARAM_NULL  = @"\b\w+\s+between\s+null\s+and\s+null\b";                                                    //匹配between 两侧侧参数为null
        /// <summary>
        /// 执行报表
        /// </summary>
        /// <param name="id">报表ID</param>
        /// <param name="request">请求信息</param>
        /// <returns></returns>
        public static int QueryReport(ReportRequest request, out ReportResult result, out string msg)
        {
            try
            {
                #region 授权检测
                Syscfg cfg = SyscfgBLL.LoadCfg(out msg);
                if (cfg == null)
                {
                    msg    = "系统参数错误,请联系管理员";
                    result = null;
                    return(-99);
                }
                if (cfg.ExpDate == "无效" || (cfg.ExpDate != "永久" && DateTime.Now.CompareTo(DateTime.Parse(cfg.ExpDate)) > 0))
                {
                    //过期
                    msg    = "授权已过期";
                    result = null;
                    return(-99);
                }
                int rptCount;
                if (cfg.ReportNumber == "无效" || !int.TryParse(cfg.ReportNumber, out rptCount))
                {
                    throw new Exception("报表授权数获取失败");
                }
                if (rptCount < ReportCount())
                {
                    //超过报表数
                    msg    = "报表数已超过授权数量";
                    result = null;
                    return(-99);
                }
                #endregion

                Report rpt = GetReport(request.ReportID);//获取报表
                result = new ReportResult()
                {
                    TotalCount = 0,
                    CurPage    = request.Page,
                    PageSize   = rpt.PageSize
                };
                string connectionString;
                short  dbType;
                int    i = -1;
                i = DatabaseBLL.GetConnectionString(rpt.DBID, out connectionString, out dbType, out msg);
                if (i != 1)
                {
                    //获取数据库连接参数失败
                    result = null;
                    return(i);
                }

                //开始组装sql
                StringBuilder sql = new StringBuilder(256);
                sql.Append(rpt.SqlCommand);
                var orderBy = new StringBuilder();
                if (!rpt.CommandHasOrderby && !string.IsNullOrEmpty(request.SortColumn))
                {
                    //排序请求
                    orderBy.AppendFormat(" Order By {0} {1}",
                                         request.SortColumn,
                                         request.Desc?"Desc":string.Empty
                                         );
                }
                string    finalSql = sql.ToString();
                DataTable rstTable;
                using (IDAL dal = DALBuilder.CreateDAL(connectionString, dbType))
                {
                    //组成parameter
                    List <IDbDataParameter> pList = new List <IDbDataParameter>();
                    if (request.Params != null)
                    {
                        //替换所有的
                        for (int index = 0; index < request.Params.Length; index++)
                        {
                            if (request.Params[index].ParamValue == null)
                            {
                                //将所有值为null的参数 替换为null
                                finalSql = Regex.Replace(finalSql, PARAM_REGEX.Replace("参数名", request.Params[index].ParamCode), " null ");
                            }
                        }
                        finalSql = Regex.Replace(finalSql, PARAM_SIG_OPERATOR, " 1=1 ");             //将所有 参数【单操作符】null 的项 替换为1=1
                        finalSql = Regex.Replace(finalSql, PARAM_MUL_OPERATOR, " 1=1 ");             //将所有参数【多操作符】null 的项替换为1=1
                        finalSql = Regex.Replace(finalSql, PARAM_LIKE, " 1=1 ");                     //将所有like null的项替换为1=1
                        finalSql = Regex.Replace(finalSql, PARAM_BETWEEN_BOTH_PARAM_NULL, "1=1");    //将所有between null and null 的项替换为1=1
                        MatchCollection mc = Regex.Matches(finalSql, PARAM_BETWEEN_LEFT_PARAM_NULL); //匹配between null and 参数 的项
                        if (mc.Count > 0)
                        {
                            foreach (Match m in mc)
                            {
                                string tmp = " <= " + (dbType == 0 ? "@" : ":") + Regex.Match(m.Value, @"(?<=@)\w+\b").Value + " ";
                                finalSql = finalSql.Replace(m.Value, tmp);
                            }
                        }
                        mc = Regex.Matches(finalSql, PARAM_BETWEEN_RIGHT_PARAM_NULL);//匹配between 参数 and null
                        if (mc.Count > 0)
                        {
                            foreach (Match m in mc)
                            {
                                string tmp = " >=" + (dbType == 0 ? "@" : ":") + Regex.Match(m.Value, @"(?<=@)\w+\b").Value + " ";
                                finalSql = finalSql.Replace(m.Value, tmp);
                            }
                        }
                        foreach (ReportParam p in request.Params)
                        {
                            IDbDataParameter dbp = null;
                            if (p.ParamValue == null)
                            {
                                continue;//值为null的参数不加入参数列表
                            }
                            switch (p.ParamType)
                            {
                            case 0:
                                dbp       = dal.CreateParameter(p.ParamCode, DbType.String);
                                dbp.Value = string.IsNullOrEmpty(p.ParamValue) ? string.Empty : p.ParamValue;
                                break;

                            case 1:
                                decimal v;
                                dbp = dal.CreateParameter(p.ParamCode, DbType.Decimal);
                                if (decimal.TryParse(p.ParamValue, out v))
                                {
                                    dbp.Value = v;
                                }
                                else
                                {
                                    dbp.Value = null;
                                }
                                break;

                            case 2:
                                DateTime d;
                                dbp = dal.CreateParameter(p.ParamCode, DbType.DateTime);
                                if (DateTime.TryParse(p.ParamValue, out d))
                                {
                                    dbp.Value = d;
                                }
                                else
                                {
                                    dbp.Value = null;
                                }
                                break;
                            }
                            if (dbp == null)
                            {
                                throw new Exception("参数错误");
                            }
                            pList.Add(dbp);
                        }
                    }
                    if (rpt.Pagingabled)
                    {
                        //分页请求
                        rstTable = dal.Select(finalSql.Replace('\r', ' ').Replace('\n', ' ') + orderBy.ToString(), rpt.PageSize * (request.Page - 1), rpt.PageSize, out i, pList.ToArray());
                    }
                    else
                    {
                        rstTable = dal.Select(finalSql.Replace('\r', ' ').Replace('\n', ' ') + orderBy.ToString(), out i, pList.ToArray());
                    }

                    if (rpt.PageSumabled)
                    {
                        //页合计请求
                        DataRow row = rstTable.NewRow();
                        foreach (ReportColumn c in rpt.Columns)
                        {
                            if (!c.Sumabled)
                            {
                                continue;
                            }
                            row[c.ColumnCode] = rstTable.Compute("sum(" + c.ColumnCode + ")", "");
                        }
                        rstTable.Rows.InsertAt(row, rstTable.Rows.Count);
                    }
                    sql.Clear();
                    sql.Append(" Select ");
                    if (rpt.AllSumabled)
                    {
                        //总合计请求
                        foreach (ReportColumn c in rpt.Columns)
                        {
                            if (c.Sumabled)
                            {
                                sql.AppendFormat(" Sum({0}) AS {0}, ",
                                                 c.ColumnCode
                                                 );
                            }
                            else
                            {
                                sql.AppendFormat(" null AS {0}, ",
                                                 c.ColumnCode
                                                 );
                            }
                        }
                    }
                    sql.Append(" Count(*) AS TotalCount ");
                    sql.AppendFormat(" From {0} ", Regex.Match(finalSql.Replace('\r', ' ').Replace('\n', ' '), ALLSUM_FROM_REGEX, RegexOptions.IgnoreCase).Value);

                    IDbDataParameter[] pList2 = new IDbDataParameter[pList.Count];
                    for (int j = 0; j < pList.Count; j++)
                    {
                        pList2[j] = dal.CloneParameter(pList[j]);
                    }

                    dal.OpenReader(sql.ToString(), pList2);
                    if (dal.DataReader.Read() && rpt.AllSumabled)
                    {
                        DataRow newrow = rstTable.NewRow();
                        foreach (ReportColumn c in rpt.Columns)
                        {
                            if (!c.Sumabled)
                            {
                                continue;
                            }
                            newrow[c.ColumnCode] = dal.DataReader[c.ColumnCode];
                        }
                        rstTable.Rows.Add(newrow);
                    }
                    result.TotalCount = Convert.ToInt32(dal.DataReader["TotalCount"]);
                    dal.DataReader.Close();
                }
                result.ReportData = JsonHelper.DatatableToJson(rstTable);
                return(1);
            }
            catch (Exception ex)
            {
                result = null;
                msg    = ex.Message;
                return(-1);
            }
        }