新框架PC后端代码(祈禧6月初版本)
南骏 池
7 天以前 72449a1b8699b65712e57fba8abce5a8240e9465
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Dynamic;
using System.Text;
using Gs.Sys.Models;
using Gs.Toolbox;
using Gs.Toolbox.ApiCore.Abstract.Mvc;
using Gs.Toolbox.ApiCore.Common.Mvc;
using Gs.Toolbox.ApiCore.Group;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
 
namespace Gs.Sys.Services;
 
[ApiGroup(ApiGroupNames.Sys)]
public class MesFileController : Repository<MesFile>, IRomteService
{
    private readonly IHttpContextAccessor _http;
    private readonly string _userCode, _userGuid, _orgFids;
 
    public MesFileController(IHttpContextAccessor httpContextAccessor)
    {
        _http = httpContextAccessor;
        (_userCode, _userGuid, _orgFids) =
            UtilityHelper.GetUserGuidAndOrgGuid(_http);
    }
 
    /// <summary>
    ///     文件上传
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public async Task<ReturnDto<ExpandoObject>> UploadFile(IFormFile file)
    {
        dynamic m = new ExpandoObject();
        if (file == null)
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success,
                "上传失败,请重试!");
        var _ary = file.FileName.Split("~");
        var _parentGuid = _ary[0];
        var _fileName = Guid.NewGuid() + _ary[1];
        var savePath = AppContext.BaseDirectory +
                       AppSettingsHelper.getValueByKey("UploadPath");
        if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
 
        if (file == null || file.Length == 0)
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                "请选择文件");
        var _fullName = Path.Combine(savePath, _fileName);
        using (var stream = new FileStream(_fullName, FileMode.Create))
        {
            await file.CopyToAsync(stream);
            m.urlPath = _fileName;
        }
 
        if (_ary.Length > 1)
        {
            var model = new MesFile();
            model.Guid = Guid.NewGuid();
            model.ParentGuid = Guid.Parse(_ary[0]);
            model.FileTitle = _ary[1];
            model.UrlPath = _fileName;
            model.CreateBy = _userCode;
            model.CreateDate = DateTime.Now;
            model.FileType = _ary[1].Split('.')[1];
            if (_ary.Length > 2)
                model.fileGroup = _ary[2];
            else
                model.fileGroup = "";
            var _bl = base.Insert(model);
        }
 
        return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "上传成功");
    }
 
    /// <summary>
    ///     查询列表,支持分页
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<PageList<MesFile>> GetListPage(PageQuery query)
    {
        var pageList = new PageList<MesFile>();
        try
        {
            var _sbWhere = new StringBuilder(" 1=1" + query.keyWhere);
            var _sbBy =
                new StringBuilder(query.sortName + " " + query.sortOrder);
            var totalCount = 0;
            var itemsList = Db.Queryable<MesFile>()
                .Where(_sbWhere.ToString())
                .OrderBy(_sbBy.ToString())
                .ToPageList(query.currentPage, query.everyPageSize,
                    ref totalCount);
 
            pageList = new PageList<MesFile>(itemsList, totalCount,
                query.everyPageSize);
            return ReturnDto<PageList<MesFile>>.QuickReturn(pageList,
                ReturnCode.Success, "读取成功");
        }
        catch (Exception ex)
        {
            return ReturnDto<PageList<MesFile>>.QuickReturn(pageList,
                ReturnCode.Default, ex.Message);
        }
    }
 
 
    /// <summary>
    ///     删除实体,支持批量删除
    /// </summary>
    /// <param name="guid"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<int?> DeleteModel([FromBody] JArray guidList)
    {
        var intArray = guidList.ToObject<string[]>();
        int? rtnInt = (int)ReturnCode.Default;
        rtnInt = base.DeleteById(intArray) ? guidList.Count : 0;
        if (rtnInt > 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Success,
                "操作成功,共删除" + rtnInt + "条数据!");
        return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception,
            "删除失败,请重试!");
    }
}