颐和api

PLCIPManageController.cs 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MadRunFabric.Common;
  6. using MadRunFabric.Model.PLCAutomationApi;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Logging;
  10. using MongoDB.Driver;
  11. using PLCAutomationApi.IRepositories;
  12. namespace PLCAutomationApi.Controllers
  13. {
  14. /// <summary>
  15. /// 访问接口ip管理
  16. /// </summary>
  17. [Authorize]
  18. [ApiVersion("6.0")]
  19. [Produces("application/json")]
  20. [Route("api/[controller]")]
  21. public class PLCIPManageController : BaseController
  22. {
  23. private readonly ILogger<PLCIPManageController> _logger;
  24. private readonly IPLC_IPManageRepository _ipmanageRepository;
  25. public PLCIPManageController(ILogger<PLCIPManageController> logger, IPLC_IPManageRepository ipmanageRepository)
  26. {
  27. _logger = logger;
  28. _ipmanageRepository = ipmanageRepository;
  29. }
  30. public IActionResult Index()
  31. {
  32. return View();
  33. }
  34. //[Authorize]
  35. [HttpGet("getlistbypage")]
  36. public async Task<IActionResult> GetListsByPageAsync (string ipaddress, string stime, string etime, string macaddress, int pageindex = 1, int pagesize = 10)
  37. {
  38. try
  39. {
  40. //排序字段
  41. var sort = Builders<PLC_IPManage>.Sort.Descending("createtime");
  42. //根据条件查询集合
  43. var flist = new List<FilterDefinition<PLC_IPManage>>();
  44. flist.Add(Builders<PLC_IPManage>.Filter.Eq("deleteflag", 0));
  45. if (!string.IsNullOrWhiteSpace(ipaddress))
  46. flist.Add(Builders<PLC_IPManage>.Filter.Where(s => s.ipaddress .Equals(ipaddress)));
  47. if (!string.IsNullOrWhiteSpace(macaddress))
  48. flist.Add(Builders<PLC_IPManage>.Filter.Where(s => s.macaddress .Equals(macaddress)));
  49. if (!string.IsNullOrWhiteSpace(stime))
  50. {
  51. DateTime dt2 = new DateTime();
  52. if (DateTime.TryParse(stime.Trim(), out dt2))
  53. flist.Add(Builders<PLC_IPManage>.Filter.Gt("createtime", stime + " 00:00:00"));
  54. }
  55. if (!string.IsNullOrWhiteSpace(etime))
  56. {
  57. DateTime dt2 = new DateTime();
  58. if (DateTime.TryParse(etime.Trim(), out dt2))
  59. flist.Add(Builders<PLC_IPManage>.Filter.Lt("createtime", etime + " 23:59:59"));
  60. }
  61. var filter = Builders<PLC_IPManage>.Filter.And(flist);
  62. var list = await _ipmanageRepository.GetByPage(filter, pageindex, pagesize, sort);
  63. var count = await _ipmanageRepository.CountAsync(filter);
  64. var obj = new
  65. {
  66. state = "success",
  67. message = "根据条件获取分页数据成功",
  68. rows = list,
  69. total = count,
  70. };
  71. return Content(obj.ToJson());
  72. }
  73. catch (Exception ex)
  74. {
  75. _logger.LogError(ex, "根据条件获取分页数据异常");
  76. return Error("根据条件获取分页数据异常");
  77. }
  78. }
  79. /// <summary>
  80. /// 添加信息
  81. /// </summary>
  82. /// <param name="input"></param>
  83. /// <returns></returns>
  84. [HttpPost("add")]
  85. public async Task<IActionResult> AddAsync(string ipaddress,string macaddress)
  86. {
  87. if (string.IsNullOrEmpty(ipaddress))
  88. return Error("ip地址不能为空");
  89. if (string.IsNullOrEmpty(macaddress))
  90. return Error("mac地址不能为空");
  91. long n = await _ipmanageRepository.Count(x => x.ipaddress == ipaddress && x.macaddress == macaddress&&x.deleteflag ==0);
  92. if (n > 0)
  93. return Error("此ip信息已存在");
  94. var model = new PLC_IPManage();
  95. model.ipaddress = ipaddress;
  96. model.macaddress = macaddress;
  97. model.createtime = DateTime .Now;
  98. model.forbidread = 0;
  99. model.forbidwrite = 0;
  100. model.deleteflag = 0;
  101. bool b = await _ipmanageRepository.Add(model);
  102. if (b)
  103. return Success("添加成功");
  104. return Error("添加失败");
  105. }
  106. [HttpPost("delete")]
  107. public async Task<IActionResult> Remove(string[] ids)
  108. {
  109. //使用逻辑删除
  110. //物理删除的数据无法恢复
  111. var res = 0;
  112. if (ids != null && ids.Length > 0)
  113. {
  114. foreach (var item in ids)
  115. {
  116. var ml = await _ipmanageRepository.GetSingle(item);
  117. ml.deleteflag = 1;
  118. if (await _ipmanageRepository.Update(ml))
  119. res += 1;
  120. }
  121. if (res == ids.Length)
  122. return Success("删除成功");
  123. else if (res > 0 && res < ids.Length)
  124. return Error("部分删除失败,请查看后重新操作");
  125. else
  126. return Error("删除失败,请查看后重新操作");
  127. }
  128. else
  129. return Error("请选择要删除的记录");
  130. }
  131. [HttpPost("update")]
  132. public async Task<IActionResult> UpdateData(string id, int? forbidread, int? forbidwrite,int? deleteflag)//(string dataname, string datavalue,string inputdate)
  133. {
  134. try
  135. {
  136. PLC_IPManage comodel = new PLC_IPManage();
  137. var list = new List<FilterDefinition<PLC_IPManage>>();
  138. var filterBuilder = Builders<PLC_IPManage>.Filter;
  139. list.Add(filterBuilder.Eq("id", id));
  140. var filter = Builders<PLC_IPManage>.Filter.And(list);
  141. var count = await _ipmanageRepository.CountAsync(filter);
  142. if (count > 0)
  143. {
  144. var dModel = await _ipmanageRepository.GetSingle(s => s.id.Equals(id) & s.deleteflag == 0, null);
  145. if (forbidread != null)
  146. {
  147. dModel.forbidread = (int)forbidread;
  148. }
  149. if (forbidwrite != null)
  150. {
  151. dModel.forbidwrite = (int)forbidwrite;
  152. }
  153. if (deleteflag != null)
  154. {
  155. dModel.deleteflag = (int)deleteflag;
  156. }
  157. var tt = await _ipmanageRepository.Update(dModel);
  158. if (tt)
  159. return Success("修改数据成功");
  160. }
  161. return Error("修改数据失败");
  162. }
  163. catch (Exception ex)
  164. {
  165. _logger.LogError(ex, "修改数据异常");
  166. return Error("修改数据失败");
  167. }
  168. }
  169. [HttpGet("getsingle")]
  170. public async Task<IActionResult> GetSingleAsync(string id)
  171. {
  172. if (id != null && id.Trim() != "")
  173. {
  174. try
  175. {
  176. var dModel = await _ipmanageRepository.GetSingle(id.Trim());
  177. if (dModel != null)
  178. {
  179. return Success("获取参数成功", dModel);
  180. }
  181. else
  182. {
  183. return Error("获取参数失败");
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. _logger.LogError(ex, "获取参数实体异常");
  189. return Error("获取参数失败");
  190. }
  191. }
  192. else
  193. {
  194. return Error("获取参数失败");
  195. }
  196. }
  197. [HttpPost("getauthority")]
  198. public async Task<IActionResult> GetAuthority(string ipaddress, string macaddress)
  199. {
  200. try
  201. {
  202. PLC_IPManage comodel = new PLC_IPManage();
  203. var list = new List<FilterDefinition<PLC_IPManage>>();
  204. var filterBuilder = Builders<PLC_IPManage>.Filter;
  205. list.Add(filterBuilder.Eq("ipaddress", ipaddress));
  206. list.Add(filterBuilder.Eq("macaddress", macaddress));
  207. var filter = Builders<PLC_IPManage>.Filter.And(list);
  208. var count = await _ipmanageRepository.CountAsync(filter);
  209. if (count > 0)
  210. {
  211. var dModel = await _ipmanageRepository.GetSingle(s => s.ipaddress.Equals(ipaddress) & s.macaddress == macaddress & s.deleteflag == 0, null);
  212. var forbidread = dModel.forbidread;
  213. var forbidwrite = dModel.forbidwrite;
  214. //var tt = await _ipmanageRepository.Update(dModel);
  215. var obj = new
  216. {
  217. state = "success",
  218. message = "根据条件获取权限成功",
  219. forbidread,
  220. forbidwrite
  221. };
  222. return Content(obj.ToJson());
  223. }
  224. return Error("获取失败");
  225. }
  226. catch (Exception ex)
  227. {
  228. _logger.LogError(ex, "获取权限数据异常");
  229. return Error("获取权限数据失败");
  230. }
  231. }
  232. }
  233. }