SettingController.cs
6.91 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
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
using HHECS.DAQWebClient.Dtos;
using HHECS.DAQWebClient.Models;
using HHECS.DAQWebClient.Services;
using HHECS.DAQWebClient.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace HHECS.DAQWebClient.Controllers
{
[Authorize]
public class SettingController : Controller
{
private readonly IFreeSql _freeSql;
private readonly CommonService _commonService;
private readonly IOTCloudService _cloudService;
private readonly ILogger<SettingController> _logger;
public SettingController(IFreeSql freeSql, CommonService commonService, IOTCloudService cloudService, ILogger<SettingController> logger)
{
_freeSql = freeSql;
_commonService = commonService;
_cloudService = cloudService;
_logger = logger;
}
public IActionResult Index()
{
try
{
var codes = new List<ConfigType>
{
ConfigType.ClientId,
ConfigType.CommitCount,
ConfigType.Area,
ConfigType.IsProductionEnvironment,
ConfigType.IOTCloundAPI,
ConfigType.IOTCloundDevelopmentAPI,
ConfigType.MqttWebSocketServer,
ConfigType.MqttWebSocketDevelopmentServer,
}.Select(x => x.ToString()).ToList();
var configs = _freeSql.Queryable<LocalConfig>().Where(x => codes.Contains(x.Code)).ToList(x => new LocalConfig
{
Code = x.Code,
Value = x.Value,
});
var clientIdValue = configs.Where(x => x.Code == ConfigType.ClientId.ToString()).Select(x => x.Value).FirstOrDefault();
var commitCountValue = configs.Where(x => x.Code == ConfigType.CommitCount.ToString()).Select(x => x.Value).FirstOrDefault();
var areaValue = configs.Where(x => x.Code == ConfigType.Area.ToString()).Select(x => x.Value).FirstOrDefault();
var isProductionEnvironmentValue = configs.Where(x => x.Code == ConfigType.IsProductionEnvironment.ToString()).Select(x => x.Value).FirstOrDefault();
var iotCloundAPIValue = configs.Where(x => x.Code == ConfigType.IOTCloundAPI.ToString()).Select(x => x.Value).FirstOrDefault();
var iotCloundDevelopmentAPIValue = configs.Where(x => x.Code == ConfigType.IOTCloundDevelopmentAPI.ToString()).Select(x => x.Value).FirstOrDefault();
var mqttWebSocketServerValue = configs.Where(x => x.Code == ConfigType.MqttWebSocketServer.ToString()).Select(x => x.Value).FirstOrDefault();
var mqttWebSocketDevelopmentServerValue = configs.Where(x => x.Code == ConfigType.MqttWebSocketDevelopmentServer.ToString()).Select(x => x.Value).FirstOrDefault();
_ = Guid.TryParse(clientIdValue, out var clientId);
_ = int.TryParse(commitCountValue, out var commitCount);
_ = bool.TryParse(isProductionEnvironmentValue, out var isProductionEnvironment);
var model = new SettingModel
{
ClientId = clientId,
CommitCount = commitCount,
Area = areaValue ?? string.Empty,
IsProductionEnvironment = isProductionEnvironment,
IOTCloundAPI = iotCloundAPIValue ?? string.Empty,
IOTCloundDevelopmentAPI = iotCloundDevelopmentAPIValue ?? string.Empty,
MqttWebSocketServer = mqttWebSocketServerValue ?? string.Empty,
MqttWebSocketDevelopmentServer = mqttWebSocketDevelopmentServerValue ?? string.Empty
};
return View(model);
}
catch (Exception)
{
return View();
}
}
[HttpPost]
[Authorize(Roles = $"{nameof(RoleType.Admin)}")]
public IActionResult UpdateConfig(LocalConfig model)
{
try
{
if (string.IsNullOrWhiteSpace(model.Code))
{
return Ok(new ResultDto
{
Code = 500,
Msg = $"Code不能为空"
});
}
var user = HttpContext.User.Identity?.Name ?? string.Empty;
var config = _freeSql.Queryable<LocalConfig>().Where(x => x.Code == model.Code).First();
if (config == null)
{
config = new LocalConfig
{
Code = model.Code,
Name = model.Name,
Value = model.Value,
Remark = model.Remark,
Created = DateTime.Now,
CreatedBy = user,
};
_freeSql.Insert(config).ExecuteAffrows();
}
else
{
_freeSql.Update<LocalConfig>(config.Id).Set(x => new LocalConfig
{
Value = model.Value,
Updated = DateTime.Now,
UpdatedBy = user,
}).ExecuteAffrows();
}
var msg = $"更新成功";
_logger.LogInformation(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
catch (Exception ex)
{
var msg = $"更新失败:{ex.Message}";
_logger.LogError(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
}
[HttpPost]
[Authorize(Roles = $"{nameof(RoleType.Admin)}")]
public IActionResult UpdateEnvironment(bool isProductionEnvironment)
{
try
{
var result = _commonService.UpdateEnvironment(isProductionEnvironment);
if (!result.Success)
{
_logger.LogError(result.Msg);
return Ok(new ResultDto
{
Code = 500,
Msg = result.Msg
});
}
var msg = $"更新成功";
_logger.LogInformation(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
catch (Exception ex)
{
var msg = $"更新失败:{ex.Message}";
_logger.LogError(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
}
}
}