public RptManageModule()
        {
            CurrentPrjInfo.CurrentEnvironment = MESEnvironment.MESReportServer;
            ProjectPathClass.ProjectPath      = AppDomain.CurrentDomain.BaseDirectory;
            Post ["/dirs"] = _ => {
                try {
                    if (this.Request.Body.Length <= 0)
                    {
                        return(string.Empty);
                    }

                    byte[] bs = new byte[this.Request.Body.Length];
                    this.Request.Body.Read(bs, 0, bs.Length);
                    var json = System.Text.Encoding.Default.GetString(bs);
                    log.InfoFormat("/dirs--request:json:{0}", json);
                    var obj = JsonConvert.DeserializeObject <Dictionary <string, string> > (json);
                    if (obj == null)
                    {
                        string str = GetResult(1, "请求参数为空");
                        return(this.Response.AsText(str));
                    }
                    var id      = string.Empty;
                    var dirName = string.Empty;
                    var parent  = string.Empty;
                    var flag    = string.Empty;
                    if (!obj.TryGetValue("id", out id))
                    {
                        id = string.Empty;
                    }
                    if (!obj.TryGetValue("name", out dirName))
                    {
                        dirName = string.Empty;
                    }
                    if (!obj.TryGetValue("parent", out parent))
                    {
                        parent = "$";
                    }
                    if (!obj.TryGetValue("flag", out flag))
                    {
                        flag = "0";
                    }

                    if (!string.IsNullOrEmpty(parent))
                    {
                        parent = parent.Replace('$', Path.DirectorySeparatorChar);
                    }

                    var dirPath = Path.Combine(parent, dirName);

                    if (flag == null)
                    {
                        string str = GetResult(1, "请求标志flag为空");
                        return(this.Response.AsText(str));
                    }

                    List <dynamic> files = null;
                    dynamic        res   = new ExpandoObject();
                    if (flag == "0")                      //string isDir = this.Request.Query.isdir;
                    {
                        string dir = string.Empty;
                        //log.InfoFormat ("dirs-json:{0}", json);
                        if (dirPath == null || string.IsNullOrEmpty(dirPath) || string.IsNullOrWhiteSpace(dirPath) || dirPath == "$")
                        {
                            dir = ProjectPathClass.UserCustomPath;
                        }
                        else
                        {
                            dir = Path.Combine(ProjectPathClass.UserCustomPath, dirPath.TrimStart(Path.DirectorySeparatorChar));
                        }
                        files = GetDirs(dir);

                        var dbDirs = RptDb.GetAllRptDirectory();
                        if (files != null && files.Count > 0 && dbDirs != null && dbDirs.Count > 0)
                        {
                            foreach (var d in files)
                            {
                                d.Id         = string.Empty;
                                d.CreateTime = string.Empty;
                                var dbdir = dbDirs.FirstOrDefault(dd => {
                                    //var dPath = Path.Combine (ProjectPathClass.UserCustomPath, dd.DirPath.TrimStart (Path.DirectorySeparatorChar));
                                    //log.DebugFormat("==dPath:{0},d.path:{1},DirName:{2},d.Name:{3}", dd.DirPath, d.Path, dd.DirName, d.Name);

                                    return(dd.DirName == d.Name && dd.DirPath == d.Path);
                                });

                                if (dbdir != null)
                                {
                                    d.Id         = dbdir.Id;
                                    d.CreateTime = dbdir.CreateTime;
                                }
                            }
                            List <dynamic> results = new List <dynamic> ();
                            if (dir == ProjectPathClass.UserCustomPath)
                            {
                                var root = dbDirs.FirstOrDefault((direct) => direct.DirName == "$");
                                if (root != null)
                                {
                                    dynamic rptRoot = new ExpandoObject();
                                    rptRoot.Name     = root.DirName;
                                    rptRoot.HasChild = 1;
                                    rptRoot.Id       = root.Id;
                                    results.Add(rptRoot);
                                }
                            }
                            files.Sort((x, y) => string.Compare(x.CreateTime, y.CreateTime));
                            foreach (var file in files)
                            {
                                dynamic rptPath = new ExpandoObject();
                                rptPath.Name     = file.Name;
                                rptPath.HasChild = file.HasChild;
                                rptPath.Id       = file.Id;
                                results.Add(rptPath);
                            }


                            res.list = results;
                        }
                    }
                    else if (flag == "1")                         // 设计时
                    {
                        if (string.IsNullOrEmpty(id))
                        {
                            string str = GetResult(1, "请求文件id为空");
                            return(this.Response.AsText(str));
                        }
                        var list = RptDb.GetRptDesignByDirId(id);
                        res.list = list;
                    }
                    else if (flag == "2")                         // 运行时
                    {
                        if (string.IsNullOrEmpty(id))
                        {
                            string str = GetResult(1, "请求文件id为空");
                            return(this.Response.AsText(str));
                        }
                        var designList = RptDb.GetRptDesignByDirId(id);
                        var list       = RptDb.GetRptRuntimeByDirId(id);
                        if (designList == null || designList.Count == 0 || list == null || list.Count == 0)
                        {
                            string str = GetResult(0, res);
                            return(this.Response.AsText(str));
                        }
                        List <dynamic> runList = new List <dynamic> ();
                        foreach (var run in list)
                        {
                            dynamic runtime = new ExpandoObject();
                            runtime.Id = run.Id;

                            runtime.QueryCount = run.QueryCount;

                            runtime.ExportCount = run.ExportCount;

                            runtime.LastQeuryPerson = run.LastQeuryPerson;
                            DateTime dt = DateTime.MinValue;
                            if (DateTime.TryParseExact(run.LastQueryTime, "yyyyMMddHHmmss", null, System.Globalization.DateTimeStyles.AssumeLocal, out dt))
                            {
                                runtime.LastQueryTime = dt.ToString("yyyy-MM-dd HH:mm:ss");
                            }

                            runtime.RptId = run.Id;
                            var design = designList.FirstOrDefault(d => d.Id == run.RptId);
                            if (design != null)
                            {
                                runtime.RptName = design.RptName;
                            }

                            runList.Add(runtime);
                        }
                        res.list = runList;
                    }

                    string result = GetResult(0, res);
                    return(this.Response.AsText(result));
                } catch (Exception ex) {
                    Message.Info(string.Format("message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

//			Get ["/rptruntime/dirid"] = _ => {
//
//			};
//
//			Get ["/rptdesign/dirid"] = _ => {
//
//			};

            Post ["/rptdesign/del"] = _ => {
                try {
                    if (this.Request.Body.Length <= 0)
                    {
                        return(string.Empty);
                    }

                    byte[] bs = new byte[this.Request.Body.Length];
                    this.Request.Body.Read(bs, 0, bs.Length);
                    var json = System.Text.Encoding.Default.GetString(bs);
                    log.InfoFormat("/rptdesign/del--request:json:{0}", json);
                    var obj = JsonConvert.DeserializeObject <Dictionary <string, List <string> > > (json);
                    if (obj == null)
                    {
                        string str = GetResult(1, "请求参数为空");
                        return(this.Response.AsText(str));
                    }
                    List <string> ids = null;
                    if (!obj.TryGetValue("ids", out ids))
                    {
                        string str = GetResult(1, "没有要删除的rptid.");
                        return(this.Response.AsText(str));
                    }
                    var designs = RptDb.GetRptDesignByIds(ids);
                    var flag    = RptDb.DeleteRptDesign(ids);
                    if (flag == true)
                    {
                        foreach (var design in designs)
                        {
                            var dir = RptDb.GetRptDirectoryById(design.DirId);
                            if (dir == null)
                            {
                                continue;
                            }
                            var file = Path.Combine(ProjectPathClass.UserCustomPath, dir.DirPath.TrimStart(Path.DirectorySeparatorChar), string.Format("{0}.rpt", design.RptName));
                            if (File.Exists(file))
                            {
                                File.Delete(file);
                            }
                        }
                        string str = GetResult(0, "删除成功");
                        return(this.Response.AsText(str));
                    }
                    else
                    {
                        string str = GetResult(1, "删除失败");
                        return(this.Response.AsText(str));
                    }
                } catch (Exception ex) {
                    Message.Info(string.Format("message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Post ["/dir/create"] = _ => {
                try {
                    if (this.Request.Body.Length <= 0)
                    {
                        return(string.Empty);
                    }

                    byte[] bs = new byte[this.Request.Body.Length];
                    this.Request.Body.Read(bs, 0, bs.Length);
                    var json = System.Text.Encoding.Default.GetString(bs);
                    log.InfoFormat("/dir/create--request:json:{0}", json);
                    var obj = JsonConvert.DeserializeObject <Dictionary <string, string> > (json);
                    if (obj == null)
                    {
                        string str = GetResult(1, "请求参数为空");
                        return(this.Response.AsText(str));
                    }
                    var dir = new RptDirectory();
                    //dir.Id = Guid.NewGuid ().ToString ("N");
                    var name = string.Empty;
                    if (obj.TryGetValue("name", out name))
                    {
                        dir.DirName = name;
                    }
                    //dir.DirName = obj ["name"];
                    var parent = string.Empty;
                    if (!obj.TryGetValue("parent", out parent))
                    {
                        parent = "$";
                    }
                    if (!string.IsNullOrEmpty(parent))
                    {
                        parent = parent.Replace('$', Path.DirectorySeparatorChar);
                    }
                    dir.DirPath = Path.Combine(parent, dir.DirName);
                    string user = string.Empty;
                    if (obj.TryGetValue("user", out user))
                    {
                        dir.LastModifyUser = user;
                    }
                    //dir.LastModifyUser = obj ["user"];
                    var     dirPath = Path.Combine(ProjectPathClass.UserCustomPath, dir.DirPath.TrimStart(Path.DirectorySeparatorChar));
                    var     flag    = RptDb.SaveDir(dir);
                    dynamic res     = new ExpandoObject();
                    if (flag == true)
                    {
                        if (Directory.Exists(dirPath))
                        {
                            Directory.Delete(dirPath);
                        }
                        Directory.CreateDirectory(dirPath);
                        res.id  = dir.Id;
                        res.msg = "创建成功";
                        string str = GetResult(0, res);
                        return(this.Response.AsText(str));
                    }
                    else
                    {
                        res.id  = string.Empty;
                        res.msg = "创建失败";
                        if (Directory.Exists(dirPath))
                        {
                            Directory.Delete(dirPath);
                        }
                        string str = GetResult(1, res);
                        return(this.Response.AsText(str));
                    }
                } catch (Exception ex) {
                    Message.Info(string.Format("/dir/create--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Post ["/dir/edit"] = _ => {
                try {
                    if (this.Request.Body.Length <= 0)
                    {
                        return(string.Empty);
                    }

                    byte[] bs = new byte[this.Request.Body.Length];
                    this.Request.Body.Read(bs, 0, bs.Length);
                    var json = System.Text.Encoding.Default.GetString(bs);
                    log.InfoFormat("/dir/edit--request:json:{0}", json);
                    var obj = JsonConvert.DeserializeObject <Dictionary <string, string> > (json);
                    if (obj == null)
                    {
                        string str = GetResult(1, "请求参数为空");
                        return(this.Response.AsText(str));
                    }
                    var    dir = new RptDirectory();
                    string id  = string.Empty;
                    if (obj.TryGetValue("id", out id))
                    {
                        dir.Id = id;
                    }
                    string name = string.Empty;
                    if (obj.TryGetValue("name", out name))
                    {
                        dir.DirName = name;
                    }
                    var parent = string.Empty;
                    if (!obj.TryGetValue("parent", out parent))
                    {
                        parent = "$";
                    }
                    if (!string.IsNullOrEmpty(parent))
                    {
                        parent = parent.Replace('$', Path.DirectorySeparatorChar);
                    }
                    dir.DirPath = Path.Combine(parent, dir.DirName);
                    string user = string.Empty;
                    if (obj.TryGetValue("user", out user))
                    {
                        dir.LastModifyUser = user;
                    }
                    var rptDir = RptDb.GetRptDirectoryById(dir.Id);
                    if (rptDir == null)
                    {
                        string str = GetResult(1, "没有");
                        return(this.Response.AsText(str));
                    }
                    var tempDirPath = dir.DirPath;
                    var flag        = RptDb.SaveDir(dir);
                    if (flag == true)
                    {
                        var dirPath    = Path.Combine(ProjectPathClass.UserCustomPath, rptDir.DirPath.TrimStart(Path.DirectorySeparatorChar));
                        var targetPath = Path.Combine(ProjectPathClass.UserCustomPath, tempDirPath.TrimStart(Path.DirectorySeparatorChar));
                        if (Directory.Exists(dirPath))
                        {
                            Directory.Move(dirPath, targetPath);
                        }
                        string str = GetResult(0, "修改成功");
                        return(this.Response.AsText(str));
                    }
                    else
                    {
                        string str = GetResult(1, "修改失败");
                        return(this.Response.AsText(str));
                    }
                } catch (Exception ex) {
                    Message.Info(string.Format("/dir/edit--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Post ["/dir/del"] = _ => {
                try {
                    if (this.Request.Body.Length <= 0)
                    {
                        return(string.Empty);
                    }

                    byte[] bs = new byte[this.Request.Body.Length];
                    this.Request.Body.Read(bs, 0, bs.Length);
                    var json = System.Text.Encoding.Default.GetString(bs);
                    log.InfoFormat("/dir/del--request:json:{0}", json);
                    var obj = JsonConvert.DeserializeObject <Dictionary <string, string> > (json);
                    if (obj == null)
                    {
                        string str = GetResult(1, "请求参数为空");
                        return(this.Response.AsText(str));
                    }
                    string id = string.Empty;
                    if (!obj.TryGetValue("id", out id))
                    {
                        string str = GetResult(1, "要删除的文件夹id为空");
                        return(this.Response.AsText(str));
                    }

                    var dir = RptDb.GetRptDirectoryById(id);
                    if (dir == null)
                    {
                        string str = GetResult(1, "没有要删除的文件夹");
                        return(this.Response.AsText(str));
                    }
                    if (!string.IsNullOrEmpty(dir.DirPath))
                    {
                        dir.DirPath = dir.DirPath.Replace('$', Path.DirectorySeparatorChar);
                    }
                    var flag = RptDb.DeleteRptDirectory(dir);
                    if (flag == true)
                    {
                        var dirPath = Path.Combine(ProjectPathClass.UserCustomPath, dir.DirPath.TrimStart(Path.DirectorySeparatorChar));
                        if (Directory.Exists(dirPath))
                        {
                            Directory.Delete(dirPath, true);
                        }
                        string str = GetResult(0, "删除成功");
                        return(this.Response.AsText(str));
                    }
                    else
                    {
                        string str = GetResult(1, "删除失败");
                        return(this.Response.AsText(str));
                    }
                } catch (Exception ex) {
                    Message.Info(string.Format("/dir/del--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Post ["/upload"] = _ => {
                try {
                    string user  = this.Request.Form.user;
                    string path  = this.Request.Form.path;
                    string dirId = this.Request.Form.dirId;
                    log.InfoFormat("/upload--request:Form-user:{0},path:{1},dirId:{2}", user ?? string.Empty, path ?? string.Empty, dirId ?? string.Empty);
                    if (string.IsNullOrEmpty(dirId))
                    {
                        string str = GetResult(1, "文件夹Id为空");
                        return(this.Response.AsText(str));
                    }
                    if (!string.IsNullOrEmpty(path))
                    {
                        path = path.Replace('$', Path.DirectorySeparatorChar);
                    }
                    if (string.IsNullOrEmpty(path))
                    {
                        path = ProjectPathClass.UserCustomPath;
                    }
                    else
                    {
                        path = Path.Combine(ProjectPathClass.UserCustomPath, path.TrimStart(Path.DirectorySeparatorChar));
                    }
                    if (this.Request.Files.Count() == 0)
                    {
                        string ss = GetResult(0, "没有要上传的文件");
                        return(this.Response.AsText(ss));
                    }
                    List <string> existsFiles = new List <string> ();
                    foreach (var file in this.Request.Files)
                    {
                        var filename = Path.Combine(path, file.Name);
                        using (FileStream fileStream = new FileStream(filename, FileMode.Create)) {
                            var design = new RptDesign()
                            {
                                RptName      = Path.GetFileNameWithoutExtension(filename),
                                UploadPerson = user,
                                UploadTime   = DateTime.Now.ToString("yyyyMMddHHmmss"),
                                DirId        = dirId
                            };
                            if (RptDb.ExistsRptDesignByName(design.RptName))
                            {
                                existsFiles.Add(file.Name);
                                continue;
                            }

                            file.Value.CopyTo(fileStream);
                            var flag = RptDb.SaveRptDesign(design);
                            if (flag)
                            {
                                RptDb.SaveRptRuntime(new RptRuntime()
                                {
                                    RptId       = design.Id,
                                    QueryCount  = 0,
                                    ExportCount = 0
                                });
                            }
                        }
                    }
                    if (existsFiles.Count == 0)
                    {
                        string ss = GetResult(0, "上传成功");
                        return(this.Response.AsText(ss));
                    }
                    else
                    {
                        dynamic result = new ExpandoObject();
                        result.msg  = "上传失败:要上传的文件在服务器上已经有同名文件,请修改后再上传";
                        result.list = existsFiles;
                        string ss = GetResult(1, result);
                        return(this.Response.AsText(ss));
                    }
                } catch (Exception ex) {
                    Message.Info(string.Format("/upload--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };
        }
        /// <summary>
        /// 扫描ProjectPathClass.UserCustomPath 目录 ,将未在数据库中的文件夹,放入数据库
        /// </summary>
        public static void DealWithDirs()
        {
            try {
                var isSyncWhenStart = ConfigurationManager.AppSettings.Get("isSyncWhenStart");
                if (isSyncWhenStart != "1")
                {
                    return;
                }
                var userCustomPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UserCustom");
                var dirs           = Directory.GetDirectories(userCustomPath, "*", SearchOption.AllDirectories);

                var tempDirs = new List <string> ();
                //文件夹 与数据库同步
                if (dirs != null)
                {
                    foreach (var d in dirs)
                    {
                        var relativePath = d.Replace(userCustomPath, "");
                        tempDirs.Add(relativePath);
                        var subDirs = relativePath.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
                        if (subDirs == null || subDirs.Length == 0)
                        {
                            continue;
                        }
                        List <string> subs = new List <string> (subDirs);
                        for (var i = 0; i < subDirs.Length; i++)
                        {
                            var p = Path.Combine(subs.ToArray());
                            if (!p.StartsWith(Path.DirectorySeparatorChar.ToString()))
                            {
                                p = Path.Combine(Path.DirectorySeparatorChar.ToString(), p);
                            }


                            //var time = DateTime.Now.ToString ("yyyyMMddHHmmss");
                            RptDb.InsertDir(new RptDirectory()
                            {
                                DirName = subs [subs.Count - 1],
                                DirPath = p,
                                //CreateTime = time,
                                //LastModifyTime = time,
                                LastModifyUser = "******"                                 //
                            });



                            subs.RemoveAt(subs.Count - 1);
                        }
                    }

                    var dbDirs = RptDb.GetAllRptDirectory();
                    if (dbDirs != null)
                    {
                        foreach (var dbDir in dbDirs)
                        {
                            if (dbDir.DirName == "$")
                            {
                                continue;
                            }
                            var dir = tempDirs.FirstOrDefault((d) => d == dbDir.DirPath);
                            if (dir == null)
                            {
                                RptDb.DeleteRptDirectory(dbDir);
                            }
                        }
                    }
                }
                // 根目录
                var rootList = RptDb.GetRptDirectoryByName("$");
                if (rootList == null || rootList.Count == 0)
                {
                    RptDb.SaveDir(new RptDirectory()
                    {
                        DirName = "$",
                        DirPath = Path.DirectorySeparatorChar.ToString(),
                        //CreateTime = time,
                        //LastModifyTime = time,
                        LastModifyUser = "******"                         //
                    });
                }

                var files   = Directory.GetFiles(userCustomPath, "*.rpt", SearchOption.AllDirectories);
                var dbfiles = RptDb.GetAllRptDesign();
                if (files != null)
                {
                    foreach (var file in files)
                    {
                        var fileName = Path.GetFileNameWithoutExtension(file);
                        if (string.IsNullOrEmpty(fileName))
                        {
                            continue;
                        }
                        var design = RptDb.GetRptDesignByName(fileName);
                        if (design == null)
                        {
                            var fileDir = Path.GetDirectoryName(file);
                            var dir     = fileDir.Replace(userCustomPath, "").Replace(Path.DirectorySeparatorChar, '$');
                            if (string.IsNullOrEmpty(dir))
                            {
                                dir = "$";
                            }
                            var rptDirs = RptDb.GetRptDirectoryByName(dir);

                            var rptDir    = rptDirs == null ? null : rptDirs.FirstOrDefault();
                            var rtpDesign = new RptDesign()
                            {
                                DirId        = rptDir == null ? string.Empty : rptDir.Id,
                                RptName      = fileName,
                                UploadPerson = "system",                                 //文件是复制过来的
                                UploadTime   = DateTime.Now.ToString("yyyyMMddHHmmss")
                            };
                            var flag = RptDb.SaveRptDesign(rtpDesign);
                            if (flag)
                            {
                                RptDb.SaveRptRuntime(new RptRuntime()
                                {
                                    RptId = rtpDesign.Id
                                });
                            }
                        }
                        else
                        {
                        }
                    }

                    var fileIds = new List <string> ();
                    if (dbfiles != null)
                    {
                        foreach (var dbfile in dbfiles)
                        {
                            var file = files.FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == dbfile.RptName);
                            if (file == null)
                            {
                                fileIds.Add(dbfile.Id);
                            }
                        }
                        if (fileIds.Count > 0)
                        {
                            RptDb.DeleteRptDesign(fileIds);
                        }
                    }
                }
                log.Info("DealWithDirs Ok..");
            } catch (IOException ex) {
                log.Error(string.Format("message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                //return false;
            }
        }
Exemple #3
0
        public ReportModule()
        {
            CurrentPrjInfo.CurrentEnvironment = MESEnvironment.MESReportServer;
            ProjectPathClass.ProjectPath      = AppDomain.CurrentDomain.BaseDirectory;
            ExportUrl = string.Format("{0}/{1}/{2}", BaseDomain, ProjectPathClass.MesReportServerServerWeb, ProjectPathClass.MesReportServerExport);
            //CurrentPrjInfo.CurrentLoginUserID = userName;
            // 查询
            Get ["/query/{clientId}/{queryId}/{reportName}"] = _ => {
                try {
                    string clientId   = _.clientId;
                    string queryId    = _.queryId;
                    string reportName = _.reportname;
                    string user       = this.Request.Query.user;
                    GetRptFullName(ref reportName);
                    string filter = string.Empty;
                    log.InfoFormat("query-cliendId:{0},queryId:{1},reportName:{2},filter:{3}", clientId, queryId, reportName, filter);
                    //var flag = Run (clientId, string.Format ("{0}{1}", clientId, queryId), reportName, ref filter, ReportViewType.Query);
                    var    flag = Run(clientId, queryId, reportName, ref filter, ReportViewType.Query, Path.GetFileName(reportName));
                    var    code = flag ? 0 : 1;
                    string str  = GetResult(code, filter);
                    RptDb.RecordRuntime(reportName, user, 0);
                    return(this.Response.AsText(str));
                } catch (Exception ex) {
                    Message.Info(string.Format("query--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };
            //query后 想导出 自定义导出文件夹名称
            Get ["/export/{clientId}/{queryId}/{reportname}/{reportdir}"] = _ => {
                try {
                    string clientId   = _.clientId;
                    string queryId    = _.queryId;
                    string reportdir  = _.reportdir;
                    string reportName = _.reportname;
                    GetRptFullName(ref reportName);
                    log.InfoFormat("export-cliendId:{0},queryId:{1},reportName:{2},reportdir:{3}", clientId, queryId, reportName, reportdir);
                    //var flag = Export (string.Format ("{0}{1}", clientId, queryId), reportdir, reportName);
                    var    flag = Export(queryId, reportdir, reportName);
                    int    code = flag ? 0 : 1;
                    string str  = GetResult(code, "");
                    return(this.Response.AsText(str));
                } catch (Exception ex) {
                    Message.Info(string.Format("export--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };
            //query后 想导出 默认rpt文件hashcode名称
            Get ["/export/{clientId}/{queryId}/{reportname}"] = _ => {
                try {
                    string clientId   = _.clientId;
                    string queryId    = _.queryId;
                    string repordir   = _.repordir;
                    string reportName = _.reportname;
                    GetRptFullName(ref reportName);
                    log.InfoFormat("export-cliendId:{0},queryId:{1},reportName:{2}", clientId, queryId, reportName);
                    //var flag = Export (string.Format ("{0}{1}", clientId, queryId), repordir, reportName);
                    var    flag = Export(queryId, repordir, reportName);
                    int    code = flag ? 0 : 1;
                    string str  = GetResult(code, "");
                    return(this.Response.AsText(str));
                } catch (Exception ex) {
                    Message.Info(string.Format("export--default--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };
            //导出
            Get ["/exportquery/{clientId}/{queryId}/{reportname}/{reportdir}"] = _ => {
                try {
                    string clientId   = _.clientId;
                    string queryId    = _.queryId;
                    string reportName = _.reportname;
                    string reportdir  = _.reportdir;
                    string user       = this.Request.Query.user;
                    GetRptFullName(ref reportName);
                    string filter = string.Empty;
                    log.InfoFormat("exportquery-cliendId:{0},queryId:{1},reportName:{2}", clientId, queryId, reportName);
                    var exportFileName = reportdir.TrimStart('$').Replace('$', Path.DirectorySeparatorChar);
                    //var flag = Run (clientId, string.Format ("{0}{1}", clientId, queryId), reportName, ref filter, ReportViewType.ExportQuery);
                    var    flag = Run(clientId, queryId, reportName, ref filter, ReportViewType.ExportQuery, exportFileName);
                    int    code = flag ? 0 : 1;
                    string str  = GetResult(code, filter);
                    RptDb.RecordRuntime(reportName, user, 1);
                    return(this.Response.AsText(str));
                } catch (Exception ex) {
                    Message.Info(string.Format("exportquery--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Get ["/cancel/{clientId}/{queryId}"] = _ => {
                //string clientId = _.clientId;
                try {
                    string clientId = _.clientId;
                    string queryId  = _.queryId;
                    log.InfoFormat("cancel-cliendId:{0},queryId:{1}", clientId, queryId);

                    //MESReportRun.Instance.CancelRunReportServer (string.Format ("{0}{1}", clientId, queryId));
                    MESReportRun.Instance.CancelRunReportServer(queryId);
                    string str = GetResult(0, "");
                    return(this.Response.AsText(str));
                } catch (Exception ex) {
                    Message.Info(string.Format("cancel--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            //rpt 文件列表
            Get ["/rptlist"] = _ => {
                try {
                    string dirName = this.Request.Query.dirname;
                    string isDir   = this.Request.Query.isdir;
                    log.InfoFormat("rptlist-dirName:{0},isDir:{1}", dirName, isDir);
                    if ((dirName == null || string.IsNullOrEmpty(dirName) || string.IsNullOrWhiteSpace(dirName)) &&
                        (isDir == null || string.IsNullOrEmpty(isDir) || string.IsNullOrWhiteSpace(isDir)))
                    {
                        var    files  = GetRptList(ProjectPathClass.UserCustomPath);
                        string result = GetResult(0, files);
                        return(this.Response.AsText(result));
                    }
                    else
                    {
                        if (isDir == "0")
                        {
                            string str = GetResult(0, "");
                            return(this.Response.AsText(str));
                        }
                        GetRptFullName(ref dirName);
                        var dir = Path.Combine(ProjectPathClass.UserCustomPath, dirName);
                        if (dirName == "/")
                        {
                            dir = ProjectPathClass.UserCustomPath;
                        }
                        var    files  = GetRptList(dir);
                        string result = GetResult(0, files);
                        return(this.Response.AsText(result));
                    }
                } catch (Exception ex) {
                    Message.Info(string.Format("rptlist--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };
            //已经导出文件列表
            Get ["/reportlist"] = _ => {
                try {
                    string dirName = this.Request.Query.dirname;
                    log.InfoFormat("reportlist-dirName:{0}", dirName);
                    if (dirName == null || string.IsNullOrEmpty(dirName) || string.IsNullOrWhiteSpace(dirName))
                    {
                        var    list   = GetReportList(ProjectPathClass.ServerWebExport);
                        string result = GetResult(0, list);
                        return(this.Response.AsText(result));
                    }
                    else
                    {
                        var dir  = Path.Combine(ProjectPathClass.ServerWebExport, dirName);
                        var flag = IsDirExisted(dir);
                        if (!flag)
                        {
                            dir  = Path.Combine(ProjectPathClass.ServerWebExport, Util.Hash_MD5_16(dirName, false));
                            flag = IsDirExisted(dir);
                            if (!flag)
                            {
                                string str = GetResult(1, "报表路径不存在");
                                return(this.Response.AsText(str));
                            }
                        }
                        var    list   = GetReportList(dir);
                        string result = GetResult(0, list);
                        return(this.Response.AsText(result));
                    }
                } catch (Exception ex) {
                    Message.Info(string.Format("reportlist--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            //查看已经导出的报表
//			Get ["/reportinfo"] = _ => {
//				try {
//					string dirName = this.Request.Query.dirname;
//					if (dirName == null || string.IsNullOrEmpty (dirName) || string.IsNullOrWhiteSpace (dirName)) {
//						string result = GetResult (1, "报表不存在!");
//						return this.Response.AsText (result);
//					} else {
//						var dir = Path.Combine (ProjectPathClass.ServerWebExport, dirName);
//						var flag = IsDirExisted (dir);
//						if (!flag) {
//							dir = Path.Combine (ProjectPathClass.ServerWebExport, Util.Hash_MD5_16 (dirName, false));
//							flag = IsDirExisted (dir);
//							if (!flag) {
//								string str = GetResult (1, "报表路径不存在");
//								return this.Response.AsText (str);
//							}
//						}
//						var url = string.Format ("{0}:{1}/{2}", CommonConst.Domain, CommonConst.Port, dir);
//
//						string result = GetResult (0, url);
//						return this.Response.AsText (result);
//					}
//				} catch (Exception ex) {
//					Message.Info (string.Format ("message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
//					string str = GetResult (1, ex.Message);
//					return this.Response.AsText (str);
//				}
//			};
            //获取
            Get ["/state/{clientId}/{queryId}"] = _ => {
                //string clientId = _.clientId;
                try {
                    string clientId = _.clientId;
                    string queryId  = _.queryId;
                    log.InfoFormat("state-clientId:{0},queryId:{1}", clientId, queryId);

                    var queryReport = MESReportRun.Instance.ReportQueryManager.Get(queryId);                     // (string.Format ("{0}{1}", clientId, queryId));

                    if (queryReport != null)
                    {
                        dynamic result = new ExpandoObject();
                        result.ClientId         = queryReport.ClientId;
                        result.ReportId         = queryReport.ReportId;
                        result.QueryID          = queryReport.QueryID;
                        result.FilterDialogFlag = queryReport.FilterDialogFlag;
                        result.IsCompleted      = queryReport.IsCompleted;
                        result.PageIndex        = queryReport.PageIndex;
                        result.PageCount        = queryReport.PageCount;
                        result.PageWidth        = queryReport.HtmlWidth;
                        result.PageHeight       = queryReport.HtmlHeight;
                        result.Url = string.Format("{0}/{1}", BaseDomain, queryReport.Url);
                        string str = GetResult(0, result);
                        log.InfoFormat("state-clientId:{0},queryId:{1},result:{2}", clientId, queryId, str);
                        //					if (result.IsCompleted == 1) {
                        //						MESReportRun.Instance.ReportQueryManager.Remove (queryId);
                        //					}
                        return(this.Response.AsText(str));
                    }
                    string resultStr = GetResult(1, "没有找到查询id。");
                    return(this.Response.AsText(resultStr));
                } catch (Exception ex) {
                    Message.Info(string.Format("state--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Post ["/login"] = _ => {
                try {
                    var user  = this.Bind <UserInfo> ();
                    var login = new ReportLogin();
                    log.InfoFormat("login-userName:{0}", user.UserName);
                    var flag = login.Login(user.UserName, user.Pwd);
                    if (flag)
                    {
                        CurrentPrjInfo.CurrentLoginUserID = user.UserName;
                        string str = GetResult(0, "");
                        return(this.Response.AsText(str));
                    }
                    else
                    {
                        string str = GetResult(1, "用户名或密码错误!");
                        return(this.Response.AsText(str));
                    }
                } catch (Exception ex) {
                    Message.Info(string.Format("login--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Post ["/logout"] = _ => {
                try {
                    //var user = this.Bind<UserInfo> ();
                    //var login = new ReportLogin ();
                    //var flag = login.Login (user.UserName, user.Pwd);
                    //if (flag) {
                    log.InfoFormat("logout-userName:{0}", CurrentPrjInfo.CurrentLoginUserID);
                    CurrentPrjInfo.CurrentLoginUserID = string.Empty;

                    string str = GetResult(0, "");
                    return(this.Response.AsText(str));
                    //} else {

                    /*		string str = GetResult (1, "");
                     *      return this.Response.AsText (str);
                     * }*/
                } catch (Exception ex) {
                    Message.Info(string.Format("logout--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Post ["/filterquery/{clientId}/{queryId}"] = _ => {
                try {
                    string clientId = _.clientId;
                    string queryId  = _.queryId;
                    var    filter   = GetPostFilter();
                    log.InfoFormat("filterquery-clientId:{0},queryId:{1},filter:{2}", clientId, queryId, filter);
                    //MESReportRun.Instance.ContinueRunReportServer (filter, string.Format ("{0}{1}", clientId, queryId));
                    MESReportRun.Instance.ContinueRunReportServer(filter, queryId);

                    string str = GetResult(0, "");
                    return(this.Response.AsText(str));
                } catch (Exception ex) {
                    Message.Info(string.Format("filterquery--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Post ["/queryreport/{clientId}/{queryId}/{reportName}"] = _ => {
                try {
                    string clientId   = _.clientId;
                    string queryId    = _.queryId;
                    string reportName = _.reportname;
                    GetRptFullName(ref reportName);
                    string filter = GetPostFilter();
                    log.InfoFormat("queryreport-cliendId:{0},queryId:{1},reportName:{2},filter:{3}", clientId, queryId, reportName, filter);
                    Run(clientId, queryId, reportName, filter, ReportViewType.ExportQuery);
                    string str = GetResult(0, string.Empty);
                    return(this.Response.AsText(str));
                } catch (Exception ex) {
                    Message.Info(string.Format("queryreport--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };

            Get ["/exportedDirs"] = _ => {
                try {
                    dynamic root = GetAllDirs();
                    string  str  = GetResult(0, root);
                    return(this.Response.AsText(str));
                } catch (Exception ex) {
                    Message.Info(string.Format("exportedDirs--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace));
                    string str = GetResult(1, ex.Message);
                    return(this.Response.AsText(str));
                }
            };
        }