TdevDevStoreController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using Newtonsoft.Json;
  5. using Ropin.Inspection.Api.Common;
  6. using Ropin.Inspection.Api.Controllers;
  7. using Ropin.Inspection.Model;
  8. using Ropin.Inspection.Service;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. using Ropin.Core.Extensions.Redis;
  14. using Ropin.Core.Common;
  15. using Ropin.Inspection.Service.DEV.Interface;
  16. using Ropin.Inspection.Model.ViewModel.DEV;
  17. using Newtonsoft.Json.Linq;
  18. using Ropin.Inspection.Model.SearchModel.DEV;
  19. using System.Text.RegularExpressions;
  20. using Ropin.Inspection.Common.Helper;
  21. using Microsoft.Extensions.FileSystemGlobbing.Internal;
  22. using K4os.Compression.LZ4.Internal;
  23. namespace Ropin.Inspection.Api
  24. {
  25. public class TdevDevStoreController : BaseController
  26. {
  27. public ILogger<TdevDevStoreController> _logger { get; }
  28. private readonly ITdevDevStoreService _TdevDevStoreService;
  29. private readonly IPushMsgService _pushMsgService;
  30. private readonly IRedisBasketRepository _redisBasketRepository;
  31. private readonly ITdevDevOpeAccountService _tdevDevOpeAccountService;
  32. private readonly IDevDevOpeAccountConfigService _devDevOpeAccountConfigService;
  33. /// <summary>
  34. /// 构造函数
  35. /// </summary>
  36. /// <param name="TdevDevStoreService"></param>
  37. /// <param name="logger"></param>
  38. public TdevDevStoreController(ITdevDevStoreService TdevDevStoreService, ITdevDevOpeAccountService tdevDevOpeAccountService,IRedisBasketRepository redisBasketRepository, IPushMsgService pushMsgService, ILogger<TdevDevStoreController> logger, IDevDevOpeAccountConfigService devDevOpeAccountConfigService)
  39. {
  40. _TdevDevStoreService = TdevDevStoreService;
  41. _logger = logger;
  42. _pushMsgService = pushMsgService;
  43. _redisBasketRepository = redisBasketRepository;
  44. _tdevDevOpeAccountService = tdevDevOpeAccountService;
  45. _devDevOpeAccountConfigService = devDevOpeAccountConfigService;
  46. }
  47. /// <summary>
  48. /// 通过id获取业主设备信息
  49. /// </summary>
  50. /// <param name="id"></param>
  51. /// <returns></returns>
  52. [HttpGet("GetDevStoreAsync/{id}")]
  53. [AllowAnonymous]
  54. public async Task<ApiResult> GetDevStoreAsync(string id)
  55. {
  56. if (string.IsNullOrEmpty(id))
  57. {
  58. return new ApiResult(ReturnCode.GeneralError);
  59. }
  60. try
  61. {
  62. var content = await _TdevDevStoreService.GetConditionAsync(new TdevDevStoreSearchModel { C_ID = id, type= "details" });
  63. var dev = content.FirstOrDefault();
  64. var user = await _TdevDevStoreService.GetUserByDevStoreIdAndRoleNameAsync(id,"设备管理员");
  65. if(dev != null)
  66. dev.DevManager = user?.C_Name;
  67. return new ApiResult<TdevDevStoreViewModel>(dev);
  68. }
  69. catch (Exception ex)
  70. {
  71. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  72. }
  73. }
  74. /// <summary>
  75. /// 通过id获取业主设备二维码地址信息
  76. /// </summary>
  77. /// <param name="id"></param>
  78. /// <returns></returns>
  79. [HttpGet("GetDevStoreQRCodeAsync/{id}")]
  80. public async Task<ApiResult> GetDevStoreQRCodeAsync(string id)
  81. {
  82. if (string.IsNullOrEmpty(id))
  83. {
  84. return new ApiResult(ReturnCode.GeneralError);
  85. }
  86. try
  87. {
  88. var content = await _TdevDevStoreService.GetDevStoreQRCodeAsync(id);
  89. return new ApiResult<string>(content);
  90. }
  91. catch (Exception ex)
  92. {
  93. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  94. }
  95. }
  96. /// <summary>
  97. /// 获取所有业主设备
  98. /// </summary>
  99. /// <returns></returns>
  100. [HttpGet("GetDevStoresAsync")]
  101. public async Task<ApiResult> GetDevStoresAsync()
  102. {
  103. try
  104. {
  105. var contentList = await _TdevDevStoreService.GetAllAsync();
  106. return new ApiResult<IEnumerable<TdevDevStoreViewModel>>(contentList);
  107. }
  108. catch (Exception ex)
  109. {
  110. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  111. }
  112. }
  113. /// <summary>
  114. /// 通过业主设备名称条件查询
  115. /// </summary>
  116. /// <param name="searchModel"></param>
  117. /// <returns></returns>
  118. [HttpPost("GetDevStoresByAsync")]
  119. public async Task<ApiResult> GetDevStoresByAsync(TdevDevStoreSearchModel searchModel)
  120. {
  121. if (searchModel == null)
  122. {
  123. return new ApiResult(ReturnCode.ArgsError);
  124. }
  125. //searchModel.IsPagination = false;
  126. try
  127. {
  128. var contentList = await _TdevDevStoreService.GetConditionAsync(searchModel);
  129. PagesModel<TdevDevStoreViewModel> pages = new PagesModel<TdevDevStoreViewModel>(searchModel.IsPagination ? contentList.Skip((searchModel.PageIndex - 1) * searchModel.PageSize).Take(searchModel.PageSize) : contentList, searchModel);
  130. return new ApiResult<PagesModel<TdevDevStoreViewModel>>(pages);
  131. }
  132. catch (Exception ex)
  133. {
  134. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  135. }
  136. }
  137. /// <summary>
  138. /// 通过业主设备名称条件查询(数据包时间)
  139. /// </summary>
  140. /// <param name="searchModel"></param>
  141. /// <returns></returns>
  142. [HttpPost("GetDevStoresByAsync1")]
  143. public async Task<ApiResult> GetDevStoresByAsync1(TdevDevStoreSearchModel searchModel)
  144. {
  145. if (searchModel == null)
  146. {
  147. return new ApiResult(ReturnCode.ArgsError);
  148. }
  149. //searchModel.IsPagination = false;
  150. try
  151. {
  152. var contentList = await _TdevDevStoreService.GetConditionAsync(searchModel);
  153. foreach (var item in contentList)
  154. {
  155. if (item!=null)
  156. {
  157. var content = await _redisBasketRepository.GetValue(RedisKey.Fanyibox_DevStore_ + item.C_ID);
  158. if (content!=null)
  159. {
  160. try
  161. {
  162. JObject jsonObj = JObject.Parse(content);
  163. if (jsonObj != null&& jsonObj["package"].Any())
  164. {
  165. JObject address = (JObject)jsonObj["package"];
  166. item.packageTime = address["time"].ToString();
  167. }
  168. }
  169. catch
  170. {
  171. }
  172. }
  173. }
  174. }
  175. PagesModel<TdevDevStoreViewModel> pages = new PagesModel<TdevDevStoreViewModel>(searchModel.IsPagination ? contentList.Skip((searchModel.PageIndex - 1) * searchModel.PageSize).Take(searchModel.PageSize) : contentList, searchModel);
  176. return new ApiResult<PagesModel<TdevDevStoreViewModel>>(pages);
  177. }
  178. catch (Exception ex)
  179. {
  180. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  181. }
  182. }
  183. /// <summary>
  184. /// 业主设备状态统计
  185. /// </summary>
  186. /// <param name="searchModel"></param>
  187. /// <returns></returns>
  188. [HttpPost("GetDevStoreStatusCount")]
  189. public async Task<ApiResult> GetDevStoreStatusCount(TdevDevStoreSearchModel searchModel)
  190. {
  191. if (searchModel == null)
  192. {
  193. return new ApiResult(ReturnCode.ArgsError);
  194. }
  195. try
  196. {
  197. var contentList = await _TdevDevStoreService.GetDevStoreStatusCount(searchModel);
  198. PagesModel<DevStoreStatusGroup> pages = new PagesModel<DevStoreStatusGroup>(searchModel.IsPagination ? contentList.Skip((searchModel.PageIndex - 1) * searchModel.PageSize).Take(searchModel.PageSize) : contentList, searchModel);
  199. return new ApiResult<PagesModel<DevStoreStatusGroup>>(pages);
  200. }
  201. catch (Exception ex)
  202. {
  203. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  204. }
  205. }
  206. /// <summary>
  207. /// 创建业主设备
  208. /// </summary>
  209. /// <param name="content"></param>
  210. /// <returns></returns>
  211. [HttpPost("CreateDevStoreAsync")]
  212. public async Task<ApiResult> CreateDevStoreAsync(TdevDevStoreViewModel content)
  213. {
  214. if (content == null)
  215. {
  216. return new ApiResult(ReturnCode.ArgsError);
  217. }
  218. try
  219. {
  220. await _TdevDevStoreService.CreateOneAsync(content);
  221. }
  222. catch (Exception ex)
  223. {
  224. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  225. }
  226. return new ApiResult(ReturnCode.Success);
  227. }
  228. /// <summary>
  229. /// 删除业主设备
  230. /// </summary>
  231. /// <param name="id"></param>
  232. /// <returns></returns>
  233. [HttpDelete("DeleteDevStoreAsync/{id}")]
  234. public async Task<ApiResult> DeleteDevStoreAsync(string id)
  235. {
  236. if (string.IsNullOrEmpty(id))
  237. {
  238. return new ApiResult(ReturnCode.GeneralError);
  239. }
  240. try
  241. {
  242. await _TdevDevStoreService.DeleteAsync(id);
  243. }
  244. catch (Exception ex)
  245. {
  246. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  247. }
  248. return new ApiResult(ReturnCode.Success);
  249. }
  250. /// <summary>
  251. /// 更新业主设备
  252. /// </summary>
  253. /// <param name="id"></param>
  254. /// <param name="updateModel"></param>
  255. /// <returns></returns>
  256. [HttpPut("UpdateDevStoreAsync/{id}")]
  257. [AllowAnonymous]
  258. public async Task<ApiResult> UpdateDevStoreAsync(string id, TdevDevStoreUpdateModel updateModel)
  259. {
  260. if (string.IsNullOrEmpty(id))
  261. {
  262. return new ApiResult(ReturnCode.GeneralError);
  263. }
  264. try
  265. {
  266. await _TdevDevStoreService.UpdateAsync(id, updateModel);
  267. }
  268. catch (Exception ex)
  269. {
  270. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  271. }
  272. return new ApiResult(ReturnCode.Success);
  273. }
  274. /// <summary>
  275. /// 通过二维码获取点及设备信息
  276. /// </summary>
  277. /// <param name="qRCode"></param>
  278. /// <param name="storeCode"></param>
  279. /// <returns></returns>
  280. [HttpGet("GetDevStoreByQRCodeAsync/{qRCode}/{storeCode}")]
  281. [AllowAnonymous]
  282. public async Task<ApiResult> GetDevStoreByQRCodeAsync(string qRCode, string storeCode)
  283. {
  284. try
  285. {
  286. if (string.IsNullOrEmpty(qRCode) || string.IsNullOrEmpty(storeCode))
  287. {
  288. return new ApiResult(ReturnCode.GeneralError);
  289. }
  290. var spot = await _TdevDevStoreService.GetDevStoreByQRCodeAsync(qRCode, storeCode);
  291. if (null == spot)
  292. {
  293. return new ApiResult<TdevDevStoreDetailViewModel>(spot, ReturnCode.ResultError, "没找到此二维码对应的点,请联系管理员!");
  294. }
  295. return new ApiResult<TdevDevStoreDetailViewModel>(spot);
  296. }
  297. catch (Exception ex)
  298. {
  299. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  300. }
  301. }
  302. /// <summary>
  303. /// 操作设备
  304. /// </summary>
  305. /// <param name="mode"></param>
  306. /// <returns></returns>
  307. [HttpPost("DevOperateByQRCodeAsync")]
  308. [AllowAnonymous]
  309. public async Task<ApiResult> DevOperateByQRCodeAsync(DevOperateCreateModel mode)
  310. {
  311. if (mode == null)
  312. {
  313. return new ApiResult(ReturnCode.ArgsError);
  314. }
  315. try
  316. {
  317. await _TdevDevStoreService.DevOperateByQRCodeAsync(mode);
  318. //await _redisBasketRepository.Set(RedisKey.Fanyibox_Ope_DevStore + mode.C_DevStoreCode, mode.C_Type, TimeSpan.FromDays(7));
  319. await _pushMsgService.PushAlarmMsgAsync(new TpushMsgModel
  320. {
  321. C_DevStoreCode = mode.C_DevStoreCode,
  322. C_MsgTypeCode = "MSG_TYPE_011",
  323. Msg = mode.C_Remark,
  324. Subject = "设备启停 " + mode.C_LogMsg,
  325. DevNumber = String.Empty,
  326. DevName =String.Empty,
  327. GenerationType = 2,
  328. msgStatus = 4,
  329. }, "设备启停");
  330. //string strContent = "";
  331. //await _tdevDevOpeAccountService.CreateOneAsync(new TdevDevOpeAccountViewModel{ C_DevStoreCode = mode.C_DevStoreCode,C_Content = strContent });
  332. }
  333. catch (Exception ex)
  334. {
  335. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  336. }
  337. return new ApiResult(ReturnCode.Success);
  338. }
  339. /// <summary>
  340. /// 获取设备运行点配置
  341. /// </summary>
  342. /// <param name="id"></param>
  343. /// <returns></returns>
  344. [HttpGet("GetDevStoreRunSpotConfigAsync/{id}")]
  345. [AllowAnonymous]
  346. public async Task<ApiResult> GetDevStoreRunSpotConfigAsync(string id)
  347. {
  348. if (string.IsNullOrEmpty(id))
  349. {
  350. return new ApiResult(ReturnCode.GeneralError);
  351. }
  352. try
  353. {
  354. //TDEV_DevRunSpot
  355. //var content = await _TdevDevStoreService.GetConditionAsync(new TdevDevStoreSearchModel { C_ID = id });
  356. //TdevDevStoreRunSpotConfigViewModel mode = new TdevDevStoreRunSpotConfigViewModel();
  357. ////RunSpotConfig f1 = new RunSpotConfig() { Name = "排污口编号",Value = "325465465" };
  358. ////RunSpotConfig f2 = new RunSpotConfig() { Name = "设施名称",Value = "VOCs治理设施" };
  359. ////mode.RunSpotConfigList = new List<RunSpotConfig>() { f1,f2};
  360. //if (content.FirstOrDefault().C_RunSpotConfig == null)
  361. // return new ApiResult<IEnumerable<string>>(new List<string>());
  362. //else
  363. //return new ApiResult<TdevDevStoreRunSpotConfigViewModel>(JsonConvert.DeserializeObject<TdevDevStoreRunSpotConfigViewModel>(content.FirstOrDefault().C_RunSpotConfig));
  364. var content = await _devDevOpeAccountConfigService.GetDevOpeAccountConfigModelAsync(id);
  365. if (content == null|| string.IsNullOrEmpty(content.C_Config))
  366. {
  367. return new ApiResult<IEnumerable<string>>(new List<string>());
  368. }
  369. else
  370. {
  371. return new ApiResult<TdevDevOpeAccountConfigViewModel>(JsonConvert.DeserializeObject<TdevDevOpeAccountConfigViewModel>(content.C_Config));
  372. }
  373. }
  374. catch (Exception ex)
  375. {
  376. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  377. }
  378. }
  379. /// <summary>
  380. /// 更新设备运行点配置
  381. /// </summary>
  382. /// <param name="id"></param>
  383. /// <param name="updateModel"></param>
  384. /// <returns></returns>
  385. [HttpPut("UpdateDevStoreRunSpotConfigAsync/{id}")]
  386. public async Task<ApiResult> UpdateDevStoreRunSpotConfigAsync(string id, TdevDevStoreRunSpotConfigViewModel updateModel)
  387. {
  388. if (string.IsNullOrEmpty(id)|| updateModel == null || updateModel.RunSpotConfigList == null || updateModel.RunSpotConfigList.Count() == 0)
  389. {
  390. return new ApiResult(ReturnCode.ArgsError);
  391. }
  392. try
  393. {
  394. string pattern = @"[^a-zA-Z0-9\u4e00-\u9fa5]";// 定义正则表达式模式,匹配非字母、数字和汉字的字符
  395. foreach (var item in updateModel.RunSpotConfigList)
  396. {
  397. if (string.IsNullOrEmpty(item.Name))
  398. {
  399. string LabPInYIn = ChineseAndEnglishHelper.GetLedgerConfigurationEnglishTab(item.Label);
  400. int cut = updateModel.RunSpotConfigList.Where(t => t.Name == LabPInYIn).ToList().Count();
  401. if (cut > 0)
  402. {
  403. LabPInYIn = LabPInYIn + cut;
  404. }
  405. item.Name = LabPInYIn;
  406. }
  407. else
  408. {
  409. string vals = Regex.Replace(item.Name, pattern, ""); // 替换匹配到的字符为空
  410. item.Name = vals;
  411. }
  412. }
  413. await _TdevDevStoreService.UpdateDevStoreRunSpotConfigAsync(id, updateModel);
  414. }
  415. catch (Exception ex)
  416. {
  417. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  418. }
  419. return new ApiResult(ReturnCode.Success);
  420. }
  421. /// <summary>
  422. /// 更新设备用户配置
  423. /// </summary>
  424. /// <param name="id"></param>
  425. /// <param name="updateModel"></param>
  426. /// <returns></returns>
  427. [HttpPut("UpdateDevStoreUserConfigAsync/{id}")]
  428. [AllowAnonymous]
  429. public async Task<ApiResult> UpdateDevStoreUserConfigAsync(string id, TdevDevStoreUserConfigViewModel updateModel)
  430. {
  431. if (string.IsNullOrEmpty(id))
  432. {
  433. return new ApiResult(ReturnCode.GeneralError);
  434. }
  435. try
  436. {
  437. await _TdevDevStoreService.UpdateDevStoreUserConfigAsync(id, updateModel);
  438. }
  439. catch (Exception ex)
  440. {
  441. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  442. }
  443. return new ApiResult(ReturnCode.Success);
  444. }
  445. /// <summary>
  446. /// 获取设备用户配置
  447. /// </summary>
  448. /// <param name="id"></param>
  449. /// <returns></returns>
  450. [HttpGet("GetDevStoreUserConfigAsync/{id}")]
  451. [AllowAnonymous]
  452. public async Task<ApiResult> GetDevStoreUserConfigAsync(string id)
  453. {
  454. if (string.IsNullOrEmpty(id))
  455. {
  456. return new ApiResult(ReturnCode.GeneralError);
  457. }
  458. try
  459. {
  460. var content = await _TdevDevStoreService.GetConditionAsync(new TdevDevStoreSearchModel { C_ID = id });
  461. if(content.FirstOrDefault().C_UserConfig == null)
  462. {
  463. return new ApiResult<IEnumerable<DevStoreUser>>(new List<DevStoreUser>());
  464. }
  465. else
  466. {
  467. IEnumerable<DevStoreUser> users = JsonConvert.DeserializeObject<IEnumerable<DevStoreUser>>(content.FirstOrDefault().C_UserConfig).OrderBy(t=>t.Sort);
  468. return new ApiResult<IEnumerable<DevStoreUser>>(users);
  469. }
  470. }
  471. catch (Exception ex)
  472. {
  473. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  474. }
  475. }
  476. /// <summary>
  477. /// 拷贝业主设备信息
  478. /// </summary>
  479. /// <param name="id"></param>
  480. /// <returns></returns>
  481. [HttpPost("CopyDevStoreInfo")]
  482. [AllowAnonymous]
  483. public async Task<ApiResult> CopyDevStoreInfo(string id)
  484. {
  485. if (string.IsNullOrEmpty(id))
  486. {
  487. return new ApiResult(ReturnCode.GeneralError);
  488. }
  489. try
  490. {
  491. await _TdevDevStoreService.CopyCreateOneAsync(id);
  492. return new ApiResult(ReturnCode.Success);
  493. }
  494. catch (Exception ex)
  495. {
  496. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  497. }
  498. }
  499. }
  500. }