CommunicationController.cs
5.84 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
175
176
177
178
179
180
181
182
183
184
185
using HHECS.DAQWebClient.Dtos;
using HHECS.DAQWebClient.Model;
using HHECS.DAQWebClient.Models;
using HHECS.DAQWebClient.ViewModel;
using LinqKit;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
namespace HHECS.DAQWebClient.Controllers
{
[Authorize]
public class CommunicationController : Controller
{
private readonly IFreeSql _freeSql;
private readonly ILogger<CommunicationController> _logger;
public CommunicationController(IFreeSql freeSql, ILogger<CommunicationController> logger)
{
_freeSql = freeSql;
_logger = logger;
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public async Task<IActionResult> Load(CommunicationQueryModel model, int page, int limit)
{
try
{
var query = _freeSql.Queryable<CommunicationConfig>().Where(GetFilter(model));
var result = await query.Skip((page - 1) * limit).Take(limit).ToListAsync();
var total = await query.CountAsync();
return Ok(new ResultDto<List<CommunicationConfig>>
{
Code = 0,
Count = total,
Data = result
});
}
catch (Exception ex)
{
return Ok(new ResultDto<List<CommunicationConfig>>
{
Code = 500,
Msg = ex.Message
});
}
}
private static Expression<Func<CommunicationConfig, bool>> GetFilter(CommunicationQueryModel model)
{
var filter = PredicateBuilder.New<CommunicationConfig>(true);
if (!string.IsNullOrWhiteSpace(model.Code))
{
filter = filter.And(x => x.Code.Contains(model.Code));
}
if (!string.IsNullOrWhiteSpace(model.Name))
{
filter = filter.And(x => x.Name.Contains(model.Name));
}
if (model.CommunicationType != null)
{
filter = filter.And(x => x.CommunicationType == model.CommunicationType);
}
return filter;
}
[HttpPost]
[Authorize(Roles = $"{nameof(RoleType.Admin)}")]
public async Task<IActionResult> Add(CommunicationConfig model)
{
try
{
model.CreatedBy = HttpContext.User.Identity?.Name ?? string.Empty;
model.Created = DateTime.Now;
await _freeSql.Insert<CommunicationConfig>(model).ExecuteAffrowsAsync();
return Ok(new ResultDto
{
Code = 0,
Msg = "新增成功"
});
}
catch (Exception ex)
{
return Ok(new ResultDto
{
Code = 500,
Msg = $"新增失败:{ex.Message}"
});
}
}
[HttpPost]
public async Task<IActionResult> UpdateStatus(CommunicationConfig model)
{
try
{
var user = HttpContext.User.Identity?.Name ?? string.Empty;
await _freeSql.Update<CommunicationConfig>(model.Id).Set(x => new CommunicationConfig
{
Disable = model.Disable,
UpdatedBy = user,
Updated = DateTime.Now
}).ExecuteAffrowsAsync();
return Ok(new ResultDto
{
Code = 0,
Msg = $"更新状态成功"
});
}
catch (Exception ex)
{
return Ok(new ResultDto
{
Code = 500,
Msg = $"更新状态失败:{ex.Message}"
});
}
}
[HttpPost]
[Authorize(Roles = $"{nameof(RoleType.Admin)}")]
public async Task<IActionResult> Update(CommunicationConfig model)
{
try
{
var user = HttpContext.User.Identity?.Name ?? string.Empty;
await _freeSql.Update<CommunicationConfig>(model.Id).Set(x => new CommunicationConfig
{
Code = model.Code,
Name = model.Name,
CommunicationType = model.CommunicationType,
IpAddress = model.IpAddress,
Port = model.Port,
Disable = model.Disable,
Remark = model.Remark,
UpdatedBy = user,
Updated = DateTime.Now
}).ExecuteAffrowsAsync();
return Ok(new ResultDto
{
Code = 0,
Msg = $"更新成功"
});
}
catch (Exception ex)
{
return Ok(new ResultDto
{
Code = 500,
Msg = $"更新失败:{ex.Message}"
});
}
}
[HttpPost]
[Authorize(Roles = $"{nameof(RoleType.Admin)}")]
public async Task<IActionResult> Delete(int[] ids)
{
try
{
await _freeSql.Delete<CommunicationConfig>().Where(x => ids.Contains(x.Id)).ExecuteAffrowsAsync();
return Ok(new ResultDto
{
Code = 0,
Msg = "删除成功"
});
}
catch (Exception ex)
{
return Ok(new ResultDto
{
Code = 500,
Msg = $"删除失败:{ex.Message}"
});
}
}
}
}