qwj
2023-08-21 fab35da3e1218a329e4acbe71e8d333c1dcbcadd
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using DocumentServiceAPI.Application.DocManage.Dtos;
using DocumentServiceAPI.Application.DocManage.Services;
using DocumentServiceAPI.Application.System.Services;
using DocumentServiceAPI.Model.cyDocumentModel;
using DocumentServiceAPI.Utility;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Linq.Expressions;
 
namespace DocumentServiceAPI.Application.DocManage
{
    /// <summary>
    /// 资料管理
    /// </summary>
    public class DocumentManageAppService : IDynamicApiController
    {
        private readonly DocClassificationService _classificationService;
        private readonly DocumentManageService _docManageService;
        private readonly OrganizationService _organizationService;
        private readonly FileManageService _fileManageService;
 
        public DocumentManageAppService(DocClassificationService classService, DocumentManageService docService, OrganizationService orgService,FileManageService fileManageService)
        {
            _classificationService = classService;
            _docManageService = docService;
            _organizationService = orgService;
            _fileManageService = fileManageService;
        }
 
        /// <summary>
        /// 检查名称重复
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> CheckNameDuplicate(Document_Submit_Dto model)
        {
            var msg = false;
            Expression<Func<Doc_Info, bool>> expression = t => true;
            expression = expression.And(c => c.tenant_code == model.tenant_id && c.doc_name == model.name && c.doc_code == model.code );
            if (model.class_id.HasValue && model.class_id!=0)
            {
                expression = expression.And(t => t.classification_id == model.class_id);
            }
            if (model.id > 0)
            {
                expression = expression.And(t => t.id != model.id);
            }
            var ck =await _docManageService.GetFirstAsync(expression);
            if (ck != null)
            {
                msg = true;
            }
            return msg;
        }
 
        /// <summary>
        /// 根据分页条件查询分页数据
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostListPage(DocumentPageSearch page)
        {
            PageResult<Document_List_Dto> result = new PageResult<Document_List_Dto>();
 
            try
            {
                Expression<Func<Doc_Info, bool>> expression = t => true;
 
                expression = expression.And(t =>t.tenant_code==page.TenantID && t.doc_code == page.Code && t.is_del==false);
                if (page.ClassCode.HasValue)
                {
                    expression = expression.And(t => t.classification_id == page.ClassCode);
                }
                if (!string.IsNullOrEmpty(page.Name))
                {
                    expression = expression.And(t => t.doc_name.Contains(page.Name));
                }
                if (page.Status.HasValue)
                {
                    expression = expression.And(t => t.status == page.Status);
                }
                if (page.OrganizationCode.HasValue)
                {
                    expression = expression.And(t => t.org_id == page.OrganizationCode);
                }
                RefAsync<int> total = 0;
                var data = await _docManageService.Context.Queryable<Doc_Info>()
                        .Includes(c => c.DocClassification)
                        .Includes(c=>c.DocOrganization)
                        .Where(expression)
                        .ToPageListAsync(page.PageIndex, page.PageSize, total);
 
                if (data != null && total > 0)
                {
                    result.Items = data.Select(c => new Document_List_Dto()
                    {
                        add_time = c.add_time,
                        class_id = c.classification_id,
                        class_name = c.DocClassification?.doc_classification,
                        doc_name = c.doc_name,
                        id = c.id,
                        org_id = c.org_id,
                        org_name = c.DocOrganization?.UnitName,
                        project_id = c.project_id,
                        project_name = "",
                        status = c.status
                    }).ToList();
 
                }
                result.PageIndex = page.PageIndex;
                result.PageSize = page.PageSize;
                result.TotalCount = total;
            }
            catch (Exception er)
            {
                Log.Error(er.Message, er);
            }
            
            return new JsonResult(result);
        }
 
        /// <summary>
        /// 根据文档号查询
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostDocumentInfo(DocumentSearch search)
        {
            var data = await _docManageService.GetFirstAsync(c => c.is_del == false && c.id == search.ID && c.tenant_code==search.TenantID);
            if (data != null)
            {
                var list = await _fileManageService.GetListAsync(c =>c.status==1 && c.doc_id == search.ID);
                Document_Info_Dto info = new Document_Info_Dto(data, list.Select(c => new FileInfo_List_Dto(c)).OrderBy(c => c.sort).ToList());
                return new JsonResult(info);
            }
 
            return null;
        }
 
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<bool> PostAddDocumentInfo(Document_Submit_Dto info)
        {
            bool msg = false;
 
            Doc_Info doc = new Doc_Info();            
            doc.add_time = DateTime.Now;
            doc.classification_id = info.class_id;
            doc.doc_code = info.code;
            doc.doc_name = info.name;
            doc.is_del = false;
            doc.org_id = info.org_id;
            doc.status = info.status;
            doc.tenant_code = info.tenant_id;
            
            var id =await _docManageService.InsertReturnIdentityAsync(doc);
            if (id > 0)
            {
                if(info.newfiles!=null&& info.newfiles.Count > 0)
                {
                    foreach (var item in info.newfiles)
                    {
                        File_Info file = new File_Info();
                        file.doc_id = id;                        
                        file.filenewname = item.filenewname.Length > 40 ? item.filenewname.Substring(item.filenewname.Length - 40) : item.filenewname;
                        file.fileoldname = item.fileoldname.Length > 40 ? item.fileoldname.Substring(item.fileoldname.Length - 40) : item.fileoldname;
                        file.filepath = item.filepath;
                        file.fileservername = item.fileservername;
                        file.filesize = item.filesize;
                        file.filetype = item.filetype;
                        file.status = 1;
                        file.suffix = item.suffix;
                        file.uptime = DateTime.Now;
                        file.up_userid = item.up_userid;
                        file.up_username = item.up_username;
                        file.sort = item.sort;
                        await _fileManageService.InsertAsync(file);
                    }
                }
                msg = true;
            }
            return msg;
        }
 
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<bool> PostEdtDocumentInfo(Document_Submit_Dto info)
        {
            bool msg = false;
 
            var doc= _docManageService.GetById(info.id);
            if (info.tenant_id == doc.tenant_code && doc.is_del==false)
            {
                doc.add_time = DateTime.Now;
                doc.classification_id = info.class_id;                
                doc.doc_name = info.name;
                doc.org_id = info.org_id;
                doc.status = info.status;
 
                if (await _docManageService.UpdateAsync(doc))
                {
                    //新提交的文件
                    if (info.newfiles != null && info.newfiles.Count > 0)
                    {
                        foreach (var item in info.newfiles)
                        {
                            File_Info file = new File_Info();
                            file.doc_id = doc.id;
                            file.filenewname = item.filenewname.Length > 40 ? item.filenewname.Substring(item.filenewname.Length - 40) : item.filenewname;
                            file.fileoldname = item.fileoldname.Length > 40 ? item.fileoldname.Substring(item.fileoldname.Length - 40) : item.fileoldname;
                            file.filepath = item.filepath;
                            file.fileservername = item.fileservername;
                            file.filesize = item.filesize;
                            file.filetype = item.filetype;
                            file.status = 1;
                            file.suffix = item.suffix;
                            file.uptime = DateTime.Now;
                            file.up_userid = item.up_userid;
                            file.up_username = item.up_username;
                            file.sort = item.sort;
                            await _fileManageService.InsertAsync(file);
                        }
                    }
 
                    //修改资源名称的文件
                    if (info.editfiles != null && info.editfiles.Count > 0)
                    {
                        foreach (var item in info.editfiles)
                        {
                            var file= _fileManageService.GetById(item.id);
                            if (file != null && file.doc_id==doc.id)
                            {
                                file.fileoldname = file.filenewname;
                                file.filenewname = item.filenewname.Length > 40 ? item.filenewname.Substring(item.filenewname.Length - 40) : item.filenewname;                                
                                file.uptime = DateTime.Now;
                                file.sort = item.sort;
                                await _fileManageService.UpdateAsync(file);
                            }                            
                        }
                    }
 
                    //删除资源
                    if (info.delfiles != null && info.delfiles.Count > 0)
                    {
                        foreach (var item in info.delfiles)
                        {
                            var file = _fileManageService.GetById(item);
                            if (file != null && file.doc_id == doc.id)
                            {
                                file.status = 0;
                                file.uptime = DateTime.Now;
                                await _fileManageService.UpdateAsync(file);
                            }                            
                        }
                    }
 
                    msg = true;
                }
            }
            
            return msg;
        }
 
        /// <summary>
        /// 删除文档数据
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostDelInfo(Document_Submit_Dto doc)
        {
            bool msg = false;
            //假删除
            var model = await _docManageService.GetByIdAsync(doc.id);
            if (model != null)
            {
                if(model.tenant_code==doc.tenant_id && !model.is_del)
                {
                    model.is_del = true;
                    msg = await _docManageService.UpdateAsync(model);
                }                
            }
            return new JsonResult(msg);
        }
 
        #region 文档的文件
 
        /// <summary>
        /// 根据文档号查询文件数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetDocumentFilesList(int id)
        {
            var data = await _fileManageService.GetListAsync(c => c.status == 1 && c.doc_id == id);
            var list = data.Select(c => new FileInfo_List_Dto(c)).OrderBy(c => c.sort).ToList();
 
            return new JsonResult(list);
        }
 
        #endregion
    }
}