TispSpotController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using Ropin.Inspection.Api.Common;
  5. using Ropin.Inspection.Api.Helper;
  6. using Ropin.Inspection.Model;
  7. using Ropin.Inspection.Model.SearchModel;
  8. using Ropin.Inspection.Model.ViewModel;
  9. using Ropin.Inspection.Service.Interface;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading.Tasks;
  16. namespace Ropin.Inspection.Api.Controllers
  17. {
  18. /// <summary>
  19. /// 巡检点
  20. /// </summary>
  21. public class TispSpotController : BaseController
  22. {
  23. private readonly ILogger<TispSpotController> _logger;
  24. private readonly ITispSpotService _tispSpotService;
  25. private readonly ITispSpotContentService _tispSpotContentService;
  26. /// <summary>
  27. /// 构造函数
  28. /// </summary>
  29. /// <param name="tispSpotService"></param>
  30. /// <param name="tispSpotContentService"></param>
  31. /// <param name="logger"></param>
  32. public TispSpotController(ITispSpotService tispSpotService, ITispSpotContentService tispSpotContentService, ILogger<TispSpotController> logger)
  33. {
  34. _tispSpotService = tispSpotService;
  35. _tispSpotContentService = tispSpotContentService;
  36. _logger = logger;
  37. }
  38. /// <summary>
  39. /// 通过ID获取巡检点信息
  40. /// </summary>
  41. /// <param name="id"></param>
  42. /// <returns></returns>
  43. [HttpGet("GetSpotAsync/{id}")]
  44. public async Task<ApiResult> GetSpotAsync(Guid id)
  45. {
  46. if (Guid.Empty == id)
  47. {
  48. return new ApiResult(ReturnCode.GeneralError);
  49. }
  50. try
  51. {
  52. var spot = await _tispSpotService.GetByIdAsync(id);
  53. return new ApiResult<TispSpotViewModel>(spot);
  54. }
  55. catch (Exception ex)
  56. {
  57. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  58. }
  59. }
  60. /// <summary>
  61. /// 获取所有巡检点
  62. /// </summary>
  63. /// <returns></returns>
  64. [HttpGet("GetSpotsAsync")]
  65. public async Task<ApiResult> GetSpotsAsync()
  66. {
  67. try
  68. {
  69. var spotList = await _tispSpotService.GetAllAsync();
  70. return new ApiResult<IEnumerable<TispSpotViewModel>>(spotList);
  71. }
  72. catch (Exception ex)
  73. {
  74. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  75. }
  76. }
  77. /// <summary>
  78. /// 条件查询巡检点
  79. /// </summary>
  80. /// <param name="searchModel"></param>
  81. /// <returns></returns>
  82. [HttpPost("GetSpotsConditionAsync")]
  83. public async Task<ApiResult> GetSpotsConditionAsync(TispSpotSearchModel searchModel)
  84. {
  85. if (null == searchModel || string.IsNullOrEmpty(searchModel.C_StoreCode))
  86. {
  87. return new ApiResult(ReturnCode.GeneralError);
  88. }
  89. try
  90. {
  91. var spotList = await _tispSpotService.GetSpotsConditionAsync(searchModel);
  92. return new ApiResult<PagesModel<TispSpotViewModel>>(new PagesModel<TispSpotViewModel>(spotList?.ToList(), searchModel));
  93. }
  94. catch (Exception ex)
  95. {
  96. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  97. }
  98. }
  99. /// <summary>
  100. /// 条件查询巡检点【设备运维点配置-排除业主设备运维点重复的】
  101. /// </summary>
  102. /// <param name="searchModel"></param>
  103. /// <returns></returns>
  104. [HttpPost("GetSpotsConditionNoDevSpotRepeatAsync")]
  105. public async Task<ApiResult> GetSpotsConditionNoDevSpotRepeatAsync(TispSpotSearchModel searchModel)
  106. {
  107. if (null == searchModel || string.IsNullOrEmpty(searchModel.C_StoreCode))
  108. {
  109. return new ApiResult(ReturnCode.GeneralError);
  110. }
  111. try
  112. {
  113. var spotList = await _tispSpotService.GetSpotsConditionNoDevSpotRepeatAsync(searchModel);
  114. return new ApiResult<PagesModel<TispSpotViewModel>>(new PagesModel<TispSpotViewModel>(spotList?.ToList(), searchModel));
  115. }
  116. catch (Exception ex)
  117. {
  118. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  119. }
  120. }
  121. /// <summary>
  122. /// 通过二维码获取巡检点
  123. /// </summary>
  124. /// <param name="qRCode"></param>
  125. /// <param name="storeCode"></param>
  126. /// <returns></returns>
  127. [HttpGet("GetSpotByQRCodeTAsync/{qRCode}/{storeCode}")]
  128. public async Task<ApiResult> GetSpotByQRCodeTAsync(string qRCode,string storeCode)
  129. {
  130. try
  131. {
  132. if (string.IsNullOrEmpty(qRCode)|| string.IsNullOrEmpty(storeCode))
  133. {
  134. return new ApiResult(ReturnCode.GeneralError);
  135. }
  136. var spot = await _tispSpotService.GetSpotByQRCodeAsync(qRCode, storeCode);
  137. if (null == spot)
  138. {
  139. return new ApiResult<TispSpotViewModel>(spot, ReturnCode.ResultError, "没找到此二维码对应的巡检点,请联系管理员!");
  140. }
  141. return new ApiResult<TispSpotViewModel>(spot);
  142. }
  143. catch (Exception ex)
  144. {
  145. if (ex.Message == "此巡检点没有分配给您!")
  146. {
  147. return new ApiResult(ReturnCode.ResultError, ex.Message);
  148. }
  149. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  150. }
  151. }
  152. /// <summary>
  153. /// 通过二维码获取巡检点器具
  154. /// </summary>
  155. /// <param name="qRCode"></param>
  156. /// <param name="storeCode"></param>
  157. /// <returns></returns>
  158. [HttpGet("GetSpotProductByQRCodeTAsync/{qRCode}/{storeCode}")]
  159. public async Task<ApiResult> GetSpotProductByQRCodeTAsync(string qRCode, string storeCode)
  160. {
  161. try
  162. {
  163. if (string.IsNullOrEmpty(qRCode) || string.IsNullOrEmpty(storeCode))
  164. {
  165. return new ApiResult(ReturnCode.GeneralError);
  166. }
  167. var spot = await _tispSpotService.GetSpotProductByQRCodeTAsync(qRCode, storeCode);
  168. if (null == spot)
  169. {
  170. return new ApiResult<TispSpotViewModel>(spot, ReturnCode.ResultError, "没找到此二维码对应的巡检点,请联系管理员!");
  171. }
  172. return new ApiResult<TispSpotViewModel>(spot);
  173. }
  174. catch (Exception ex)
  175. {
  176. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  177. }
  178. }
  179. /// <summary>
  180. /// 通过二维码获取点及设备信息
  181. /// </summary>
  182. /// <param name="qRCode"></param>
  183. /// <param name="storeCode"></param>
  184. /// <returns></returns>
  185. [HttpGet("GetDevStoreByQRCodeAsync/{qRCode}/{storeCode}")]
  186. public async Task<ApiResult> GetDevStoreByQRCodeAsync(string qRCode, string storeCode)
  187. {
  188. try
  189. {
  190. if (string.IsNullOrEmpty(qRCode) || string.IsNullOrEmpty(storeCode))
  191. {
  192. return new ApiResult(ReturnCode.GeneralError);
  193. }
  194. var spot = await _tispSpotService.GetDevStoreByQRCodeAsync(qRCode, storeCode);
  195. if (null == spot)
  196. {
  197. return new ApiResult<TispSpotDevStoreViewModel>(spot, ReturnCode.ResultError, "没找到此二维码对应的点,请联系管理员!");
  198. }
  199. return new ApiResult<TispSpotDevStoreViewModel>(spot);
  200. }
  201. catch (Exception ex)
  202. {
  203. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  204. }
  205. }
  206. /// <summary>
  207. /// 创建巡检点
  208. /// </summary>
  209. /// <param name="spot">巡检点</param>
  210. /// <returns></returns>
  211. //public async Task<ApiResult> CreateSpotAsync([FromQuery] TispSpotCreateViewModel spot, [FromQuery] List<Guid> contentList)
  212. [Route("CreateSpotAsync")]
  213. [HttpPost]
  214. public async Task<ApiResult> CreateSpotAsync(TispSpotCreateViewModel spot)
  215. {
  216. if (spot == null)
  217. {
  218. return new ApiResult(ReturnCode.ArgsError);
  219. }
  220. try
  221. {
  222. Guid spotId = Guid.NewGuid();
  223. spot.C_Code = spotId;
  224. await _tispSpotService.CreateOneAsync(spot);
  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="spot"></param>
  236. /// <returns></returns>
  237. [Route("CreateOneByDevAsync")]
  238. [HttpPost]
  239. public async Task<ApiResult> CreateOneByDevAsync(TispSpotByDevCreateViewModel spot)
  240. {
  241. if (spot == null)
  242. {
  243. return new ApiResult(ReturnCode.ArgsError);
  244. }
  245. try
  246. {
  247. Guid spotId = Guid.NewGuid();
  248. spot.C_Code = spotId;
  249. await _tispSpotService.CreateOneByDevAsync(spot);
  250. }
  251. catch (Exception ex)
  252. {
  253. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  254. }
  255. return new ApiResult(ReturnCode.Success);
  256. }
  257. /// <summary>
  258. /// 删除巡检点
  259. /// </summary>
  260. /// <param name="id"></param>
  261. /// <returns></returns>
  262. [HttpDelete("DeleteSpotAsync/{id}")]
  263. public async Task<ApiResult> DeleteSpotAsync(Guid id)
  264. {
  265. if (Guid.Empty == id)
  266. {
  267. return new ApiResult(ReturnCode.GeneralError);
  268. }
  269. try
  270. {
  271. await _tispSpotService.DeleteAsync(id);
  272. }
  273. catch (Exception ex)
  274. {
  275. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  276. }
  277. return new ApiResult(ReturnCode.Success);
  278. }
  279. /// <summary>
  280. /// 更新巡检点
  281. /// </summary>
  282. /// <param name="id"></param>
  283. /// <param name="updateModel"></param>
  284. /// <returns></returns>
  285. [HttpPut("UpdateSpotAsync/{id}")]
  286. public async Task<ApiResult> UpdateSpotAsync(Guid id, TispSpotUpdateViewModel updateModel)
  287. {
  288. if (Guid.Empty == id)
  289. {
  290. return new ApiResult(ReturnCode.GeneralError);
  291. }
  292. try
  293. {
  294. await _tispSpotService.UpdateAsync(id, updateModel);
  295. }
  296. catch (Exception ex)
  297. {
  298. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  299. }
  300. return new ApiResult(ReturnCode.Success);
  301. }
  302. /// <summary>
  303. /// 扫码绑定,包括二维码,GPS,照片
  304. /// </summary>
  305. /// <param name="id"></param>
  306. /// <param name="updateModel"></param>
  307. /// <returns></returns>
  308. [HttpPut("UpdateConfigureSpotAsync/{id}")]
  309. public async Task<ApiResult> UpdateConfigureSpotAsync(Guid id, TispSpotConfigureUpdateViewModel updateModel)
  310. {
  311. if (Guid.Empty == id)
  312. {
  313. return new ApiResult(ReturnCode.GeneralError);
  314. }
  315. try
  316. {
  317. await _tispSpotService.UpdateConfigureSpotAsync(id, updateModel);
  318. }
  319. catch (Exception ex)
  320. {
  321. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  322. }
  323. return new ApiResult(ReturnCode.Success);
  324. }
  325. /// <summary>
  326. /// 更新巡检点图片
  327. /// </summary>
  328. /// <param name="spotId"></param>
  329. /// <param name="Files"></param>
  330. /// <returns></returns>
  331. [HttpPost("UploadSpotImageAsync/{spotId}")]
  332. public async Task<ApiResult> UploadSpotImageAsync(Guid spotId, IFormCollection Files)
  333. {
  334. try
  335. {
  336. if (Guid.Empty.Equals(spotId)|| null == Files)
  337. {
  338. return new ApiResult(ReturnCode.ArgsError);
  339. }
  340. Hashtable hash = new Hashtable();
  341. IFormFileCollection cols = Request.Form.Files;
  342. if (cols == null || cols.Count == 0)
  343. {
  344. return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
  345. }
  346. foreach (IFormFile file in cols)
  347. {
  348. //定义图片数组后缀格式
  349. string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
  350. //获取图片后缀是否存在数组中
  351. string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
  352. if (LimitPictureType.Contains(currentPictureExtension))
  353. {
  354. var new_path = Path.Combine("uploads/images/spots/", file.FileName);
  355. var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", new_path);
  356. using (var stream = new FileStream(path, FileMode.Create))
  357. {
  358. await Task.Run(() => { file.CopyTo(stream); hash.Add("file", "/" + new_path); });
  359. }
  360. }
  361. else
  362. {
  363. return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的图片");
  364. }
  365. }
  366. return new ApiResult<Hashtable>(new Hashtable(hash));
  367. }
  368. catch (Exception ex)
  369. {
  370. return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
  371. }
  372. }
  373. }
  374. }