Example #1
0
        /// <summary>
        /// 通过id查询文件
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public NGPResponse <SingleFileResponse> QueryFileById(NGPSingleRequest request)
        {
            // 参数验证
            if (request == null || string.IsNullOrWhiteSpace(request.RequestData))
            {
                return(new NGPResponse <SingleFileResponse>
                {
                    ErrorCode = ErrorCode.ParamEmpty,
                    Message = CommonResource.ParameterError,
                    Status = OperateStatus.Error
                });
            }

            var file = _unitRepository.FindById <Sys_File_Info>(request.RequestData);

            return(new NGPResponse <SingleFileResponse>
            {
                Message = CommonResource.OperatorSuccess,
                Status = OperateStatus.Success,
                Data = new SingleFileResponse
                {
                    FileId = file.Id,
                    FileName = file.FileName,
                    FilePath = file.FilePath,
                    Extension = file.ExtensionName,
                    Size = file.Size,
                    Url = NGPFileExtend.FileUrl(file.FilePath, file.FileName)
                }
            });
        }
Example #2
0
        public NGPResponse <List <SingleFileResponse> > UploadFiles(UploadFileRequest fileRequest)
        {
            if (fileRequest == null ||
                fileRequest.Files.IsNullOrEmpty())
            {
                return(new NGPResponse <List <SingleFileResponse> >
                {
                    ErrorCode = ErrorCode.ParamEmpty,
                    Message = CommonResource.ParameterError,
                    Status = OperateStatus.Error
                });
            }

            // 根据区域和平台划分文件夹
            var virtualPath = GlobalConst.__AttachmentFilesPath;

            // 创建划分的文件夹
            var folderPath = _fileProvider.MapPath(virtualPath);

            _fileProvider.CreateDirectory(folderPath);

            var insertList = new List <Sys_File_Info>();

            // 循环文件
            foreach (var file in fileRequest.Files)
            {
                var spitFileNames = file.FileName.Split('.');
                var extension     = string.Empty;
                if (spitFileNames.Length > 0)
                {
                    extension = "." + spitFileNames[spitFileNames.Length - 1];
                }

                // 添加时间戳的文件名称
                var fileName = string.Format("{0}_{1}", DateTime.Now.ToString(GlobalConst.DateFormatConst.__DateTimeFormatNotBar),
                                             file.FileName);

                // 插入数据库对象
                var insertFile = new Sys_File_Info
                {
                    ExtensionName = extension,
                    FileName      = fileName,
                    FilePath      = virtualPath,
                    Size          = file.Length,
                };
                insertFile.InitAddDefaultFields(_workContext);
                insertList.Add(insertFile);

                // 完整路径
                var fullPath = Path.Combine(folderPath, fileName);

                // 保存文件
                using (var bits = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(bits);
                }
            }

            // 插入数据库
            _unitRepository.Insert(insertList);

            // 转换返回对象
            var result = insertList.Select(s => new SingleFileResponse
            {
                FileId    = s.Id,
                FileName  = s.FileName,
                FilePath  = s.FilePath,
                Extension = s.ExtensionName,
                Size      = s.Size,
                Url       = NGPFileExtend.FileUrl(s.FilePath, s.FileName)
            }).ToList();

            return(new NGPResponse <List <SingleFileResponse> >
            {
                AffectedRows = insertList.Count,
                Message = CommonResource.OperatorSuccess,
                Status = OperateStatus.Success,
                Data = result
            });
        }
        /// <summary>
        /// 执行上下文
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public override bool Process(QueryResolveContext ctx)
        {
            if (ctx.Response.Data == null)
            {
                return(false);
            }

            // 没有数据需要扩展赋值
            if (ctx.GenerateContext.GenerateNameFields.IsNullOrEmpty() && ctx.GenerateContext.AttachmentFields.IsNullOrEmpty())
            {
                return(true);
            }

            // 扩展设定字段列表
            var getSetFields = ctx.GenerateContext.GenerateNameFields.Select(s => new
            {
                GetPropertyName = s.FieldKey,
                SetPropertyName = AppConfigExtend.GetFieldNameKey(s.FieldKey),
                s.FieldKey,
                s.DbConfig.IsMulti,
                FieldType       = s.FieldType.ToEnum <FieldType>(),
                FieldColumnType = s.DbConfig.ColumnType.ToEnum <FieldColumnType>()
            }).ToList();

            getSetFields.AddRange(ctx.GenerateContext.AttachmentFields.Select(s => new
            {
                GetPropertyName = s.FieldKey,
                SetPropertyName = AppConfigExtend.GetFieldNameKey(s.FieldKey),
                s.FieldKey,
                s.DbConfig.IsMulti,
                FieldType       = s.FieldType.ToEnum <FieldType>(),
                FieldColumnType = s.DbConfig.ColumnType.ToEnum <FieldColumnType>()
            }));

            // 设定名称处理
            Action <dynamic> setNameAction = (item) =>
            {
                var dicItem = item as IDictionary <string, object>;
                // 单条数据赋值
                foreach (var field in getSetFields)
                {
                    // 获取key值
                    dynamic getValue = dicItem[field.GetPropertyName];
                    var     value    = string.Empty;
                    if (string.IsNullOrWhiteSpace(Convert.ToString(getValue)))
                    {
                        dicItem[field.SetPropertyName] = value;
                        continue;
                    }

                    // 根据类型筛选
                    switch (field.FieldType)
                    {
                    case FieldType.GroupType:
                    {
                        App_Config_GroupType groupItem = null;
                        // 多选的场景
                        if (field.IsMulti == true)
                        {
                            var listValue = new List <string>();
                            var getValues = getValue.Split(',');
                            foreach (var keyItem in getValues)
                            {
                                groupItem = ctx.AssociatedContext.GroupTypes.FirstOrDefault(s => s.TypeKey == keyItem);
                                if (groupItem != null)
                                {
                                    listValue.Add(groupItem.TypeValue);
                                }
                            }
                            // 设定name值
                            dicItem[field.SetPropertyName] = listValue;
                            break;
                        }
                        groupItem = ctx.AssociatedContext.GroupTypes.FirstOrDefault(s => s.TypeKey == getValue);
                        if (groupItem != null)
                        {
                            value = groupItem.TypeValue;
                        }
                        // 设定name值
                        dicItem[field.SetPropertyName] = value;
                        break;
                    }

                    case FieldType.EmployeeType:
                    {
                        Sys_Org_Employee accountItem = null;
                        // 多选的场景
                        if (field.IsMulti == true)
                        {
                            var listValue = new List <string>();
                            var keys      = getValue.Split(',');
                            foreach (var keyItem in keys)
                            {
                                accountItem = ctx.AssociatedContext.Employees.FirstOrDefault(s => s.Id == keyItem);
                                if (accountItem != null)
                                {
                                    listValue.Add(accountItem.EmplName);
                                }
                            }
                            // 设定name值
                            dicItem[field.SetPropertyName] = listValue;
                            break;
                        }
                        accountItem = ctx.AssociatedContext.Employees.FirstOrDefault(s => s.Id == getValue);
                        if (accountItem != null)
                        {
                            value = accountItem.EmplName;
                        }
                        // 设定name值
                        dicItem[field.SetPropertyName] = value;
                        break;
                    }

                    case FieldType.DeptType:
                    {
                        Sys_Org_Department deptItem = null;
                        // 多选的场景
                        if (field.IsMulti == true)
                        {
                            var listValue = new List <string>();
                            var keys      = getValue.Split(',');
                            foreach (var keyItem in keys)
                            {
                                deptItem = ctx.AssociatedContext.Departments.FirstOrDefault(s => s.Id == keyItem);
                                if (deptItem != null)
                                {
                                    listValue.Add(deptItem.DeptName);
                                }
                            }
                            // 设定name值
                            dicItem[field.SetPropertyName] = listValue;
                            break;
                        }
                        deptItem = ctx.AssociatedContext.Departments.FirstOrDefault(s => s.Id == getValue);
                        if (deptItem != null)
                        {
                            value = deptItem.DeptName;
                        }
                        // 设定name值
                        dicItem[field.SetPropertyName] = value;
                        break;
                    }

                    case FieldType.FormType:
                        // 如果是附件
                        if (field.FieldColumnType == FieldColumnType.Attachment)
                        {
                            dicItem[field.SetPropertyName] = NGPFileExtend.FileUrl(GlobalConst.__AttachmentFilesPath, getValue);
                        }
                        break;

                    case FieldType.RelationType:
                    default:
                        break;
                    }
                }
            };

            // 如果是列表
            if (ctx.Response.Data is IList && ctx.Response.Data.GetType().IsGenericType)
            {
                foreach (var item in ctx.Response.Data)
                {
                    setNameAction(item);
                }
                return(true);
            }

            // 如果是单条
            setNameAction(ctx.Response.Data);

            return(true);
        }