TdevDevStoreController.cs 21 KB

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