SysAppService.cs
4.82 KB
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
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.IO;
namespace Hh.Mes.Service.WebService.Base
{
/// <summary>
/// app管理服务
/// </summary>
public class SysAppService : RepositorySqlSugar<sys_app>
{
#region app管理
/// <summary>
/// //获取列表
/// </summary>
public Response Load(PageReq pageRequest, sys_app model)
{
int total = 0;
var sysdata = Context.Queryable<sys_app>().OrderBy(x => x.id, OrderByType.Desc).ToOffsetPage(pageRequest.page, pageRequest.limit, ref total);
return new Response()
{
Result = sysdata,
Count = total
};
}
/// <summary>
/// //获取列表
/// </summary>
public string Download()
{
var filePath = Context.Queryable<sys_app>().OrderBy(x => x.id, OrderByType.Desc).Select(x => x.filePath).First();
return filePath != null ? filePath : "";
}
/// <summary>
/// 删除
/// </summary>
public dynamic DelByIds(int[] ids)
{
var response = new Response();
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = Context.Deleteable<sys_app>().In(ids).ExecuteCommand() > 0;
if (!result)
{
response.Message = "删除失败";
}
return response;
});
}
/// <summary>
/// 导入 文件格式:xxx-v1.apk
/// </summary>
public dynamic Import(string appType, string http, int port, IFormFileCollection files, IHostingEnvironment environment)
{
var response = new Response();
return ExceptionsHelp.Instance.ExecuteT(() =>
{
#region before
if (files.Count != 1)
{
response.Code = 500;
response.Status = false;
response.Message = "请选择上传一个apk文件";
return response;
}
var fileName = Path.GetFileName(files[0].FileName);
if (fileName.IndexOf(".apk", StringComparison.Ordinal) == -1 || fileName.IndexOf("-v", StringComparison.Ordinal) == -1)
{
response.Code = 500;
response.Status = false;
response.Message = "请选择.apk文件的正确格式:xxx-v1.apk";
return response;
}
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(files[0].FileName);// 没有扩展名的文件名 “Default”
var ver = double.Parse(fileNameWithoutExtension.Split("-")[1].Replace("v", ""));
var isExtFile = Context.Queryable<sys_app>().Any(x => x.fileName == fileName && x.ver >= ver);
if (isExtFile)
{
response.Code = 500;
response.Status = false;
response.Message = $"文件名:{fileName},已存在该版本或者不能小于历史版本号!";
return response;
}
#endregion
#region apk 文件上传
var upFloder = Path.Combine(environment.ContentRootPath + "\\wwwroot", uploadFolder);
if (!Directory.Exists(upFloder)) Directory.CreateDirectory(upFloder);
var suffix = Path.GetExtension(files[0].FileName);
var newFileName = Guid.NewGuid().ToString("N") + suffix;
var filePath = Path.Combine(upFloder, newFileName);
using (var fs = File.Create(filePath))
{
files[0].CopyTo(fs);
fs.Flush();
}
#endregion
#region 数据保存到数据库
var url = http + "://" + base.GetDictionaryDictValue("UrlApp", "GetUrl") + ":" + port;
sys_app model = new sys_app
{
appType = appType,
filePath = url + "/UploadFile/" + newFileName,
ver = ver,
fileName = fileName,
keys = Guid.NewGuid(),
createBy = sysWebUser?.Account,
createTime = DateTime.Now
};
response.Status = base.Insert(model);
response.Code = response.Status ? 200 : 500;
#endregion
return response;
});
}
#endregion
}
}