TdevDevStoreController.cs 19 KB

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