TdevDevStoreController.cs 20 KB

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