using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Ropin.Inspection.Api.Common; using Ropin.Inspection.Api.Helper; using Ropin.Inspection.Model; using Ropin.Inspection.Model.SearchModel; using Ropin.Inspection.Model.ViewModel; using Ropin.Inspection.Service.Interface; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace Ropin.Inspection.Api.Controllers { /// <summary> /// 巡检点 /// </summary> public class TispSpotController : BaseController { private readonly ILogger<TispSpotController> _logger; private readonly ITispSpotService _tispSpotService; private readonly ITispSpotContentService _tispSpotContentService; /// <summary> /// 构造函数 /// </summary> /// <param name="tispSpotService"></param> /// <param name="tispSpotContentService"></param> /// <param name="logger"></param> public TispSpotController(ITispSpotService tispSpotService, ITispSpotContentService tispSpotContentService, ILogger<TispSpotController> logger) { _tispSpotService = tispSpotService; _tispSpotContentService = tispSpotContentService; _logger = logger; } /// <summary> /// 通过ID获取巡检点信息 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet("GetSpotAsync/{id}")] public async Task<ApiResult> GetSpotAsync(Guid id) { if (Guid.Empty == id) { return new ApiResult(ReturnCode.GeneralError); } try { var spot = await _tispSpotService.GetByIdAsync(id); return new ApiResult<TispSpotViewModel>(spot); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 获取所有巡检点 /// </summary> /// <returns></returns> [HttpGet("GetSpotsAsync")] public async Task<ApiResult> GetSpotsAsync() { try { var spotList = await _tispSpotService.GetAllAsync(); return new ApiResult<IEnumerable<TispSpotViewModel>>(spotList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 条件查询巡检点 /// </summary> /// <param name="searchModel"></param> /// <returns></returns> [HttpPost("GetSpotsConditionAsync")] public async Task<ApiResult> GetSpotsConditionAsync(TispSpotSearchModel searchModel) { if (null == searchModel || string.IsNullOrEmpty(searchModel.C_StoreCode)) { return new ApiResult(ReturnCode.GeneralError); } try { var spotList = await _tispSpotService.GetSpotsConditionAsync(searchModel); return new ApiResult<PagesModel<TispSpotViewModel>>(new PagesModel<TispSpotViewModel>(spotList?.ToList(), searchModel)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 条件查询巡检点【设备运维点配置-排除业主设备运维点重复的】 /// </summary> /// <param name="searchModel"></param> /// <returns></returns> [HttpPost("GetSpotsConditionNoDevSpotRepeatAsync")] public async Task<ApiResult> GetSpotsConditionNoDevSpotRepeatAsync(TispSpotSearchModel searchModel) { if (null == searchModel || string.IsNullOrEmpty(searchModel.C_StoreCode)) { return new ApiResult(ReturnCode.GeneralError); } try { var spotList = await _tispSpotService.GetSpotsConditionNoDevSpotRepeatAsync(searchModel); return new ApiResult<PagesModel<TispSpotViewModel>>(new PagesModel<TispSpotViewModel>(spotList?.ToList(), searchModel)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 通过二维码获取巡检点 /// </summary> /// <param name="qRCode"></param> /// <param name="storeCode"></param> /// <returns></returns> [HttpGet("GetSpotByQRCodeTAsync/{qRCode}/{storeCode}")] public async Task<ApiResult> GetSpotByQRCodeTAsync(string qRCode,string storeCode) { try { if (string.IsNullOrEmpty(qRCode)|| string.IsNullOrEmpty(storeCode)) { return new ApiResult(ReturnCode.GeneralError); } var spot = await _tispSpotService.GetSpotByQRCodeAsync(qRCode, storeCode); if (null == spot) { return new ApiResult<TispSpotViewModel>(spot, ReturnCode.ResultError, "没找到此二维码对应的巡检点,请联系管理员!"); } return new ApiResult<TispSpotViewModel>(spot); } catch (Exception ex) { if (ex.Message == "此巡检点没有分配给您!") { return new ApiResult(ReturnCode.ResultError, ex.Message); } return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 通过二维码获取巡检点器具 /// </summary> /// <param name="qRCode"></param> /// <param name="storeCode"></param> /// <returns></returns> [HttpGet("GetSpotProductByQRCodeTAsync/{qRCode}/{storeCode}")] public async Task<ApiResult> GetSpotProductByQRCodeTAsync(string qRCode, string storeCode) { try { if (string.IsNullOrEmpty(qRCode) || string.IsNullOrEmpty(storeCode)) { return new ApiResult(ReturnCode.GeneralError); } var spot = await _tispSpotService.GetSpotProductByQRCodeTAsync(qRCode, storeCode); if (null == spot) { return new ApiResult<TispSpotViewModel>(spot, ReturnCode.ResultError, "没找到此二维码对应的巡检点,请联系管理员!"); } return new ApiResult<TispSpotViewModel>(spot); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 通过二维码获取点及设备信息 /// </summary> /// <param name="qRCode"></param> /// <param name="storeCode"></param> /// <returns></returns> [HttpGet("GetDevStoreByQRCodeAsync/{qRCode}/{storeCode}")] public async Task<ApiResult> GetDevStoreByQRCodeAsync(string qRCode, string storeCode) { try { if (string.IsNullOrEmpty(qRCode) || string.IsNullOrEmpty(storeCode)) { return new ApiResult(ReturnCode.GeneralError); } var spot = await _tispSpotService.GetDevStoreByQRCodeAsync(qRCode, storeCode); if (null == spot) { return new ApiResult<TispSpotDevStoreViewModel>(spot, ReturnCode.ResultError, "没找到此二维码对应的点,请联系管理员!"); } return new ApiResult<TispSpotDevStoreViewModel>(spot); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// <summary> /// 创建巡检点 /// </summary> /// <param name="spot">巡检点</param> /// <returns></returns> //public async Task<ApiResult> CreateSpotAsync([FromQuery] TispSpotCreateViewModel spot, [FromQuery] List<Guid> contentList) [Route("CreateSpotAsync")] [HttpPost] public async Task<ApiResult> CreateSpotAsync(TispSpotCreateViewModel spot) { if (spot == null) { return new ApiResult(ReturnCode.ArgsError); } try { Guid spotId = Guid.NewGuid(); spot.C_Code = spotId; await _tispSpotService.CreateOneAsync(spot); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 创建设备巡检运维点 /// </summary> /// <param name="spot"></param> /// <returns></returns> [Route("CreateOneByDevAsync")] [HttpPost] public async Task<ApiResult> CreateOneByDevAsync(TispSpotByDevCreateViewModel spot) { if (spot == null) { return new ApiResult(ReturnCode.ArgsError); } try { Guid spotId = Guid.NewGuid(); spot.C_Code = spotId; await _tispSpotService.CreateOneByDevAsync(spot); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 删除巡检点 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete("DeleteSpotAsync/{id}")] public async Task<ApiResult> DeleteSpotAsync(Guid id) { if (Guid.Empty == id) { return new ApiResult(ReturnCode.GeneralError); } try { await _tispSpotService.DeleteAsync(id); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 更新巡检点 /// </summary> /// <param name="id"></param> /// <param name="updateModel"></param> /// <returns></returns> [HttpPut("UpdateSpotAsync/{id}")] public async Task<ApiResult> UpdateSpotAsync(Guid id, TispSpotUpdateViewModel updateModel) { if (Guid.Empty == id) { return new ApiResult(ReturnCode.GeneralError); } try { await _tispSpotService.UpdateAsync(id, updateModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 扫码绑定,包括二维码,GPS,照片 /// </summary> /// <param name="id"></param> /// <param name="updateModel"></param> /// <returns></returns> [HttpPut("UpdateConfigureSpotAsync/{id}")] public async Task<ApiResult> UpdateConfigureSpotAsync(Guid id, TispSpotConfigureUpdateViewModel updateModel) { if (Guid.Empty == id) { return new ApiResult(ReturnCode.GeneralError); } try { await _tispSpotService.UpdateConfigureSpotAsync(id, updateModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// <summary> /// 更新巡检点图片 /// </summary> /// <param name="spotId"></param> /// <param name="Files"></param> /// <returns></returns> [HttpPost("UploadSpotImageAsync/{spotId}")] public async Task<ApiResult> UploadSpotImageAsync(Guid spotId, IFormCollection Files) { try { if (Guid.Empty.Equals(spotId)|| null == Files) { return new ApiResult(ReturnCode.ArgsError); } Hashtable hash = new Hashtable(); IFormFileCollection cols = Request.Form.Files; if (cols == null || cols.Count == 0) { return new ApiResult(ReturnCode.GeneralError, "没有上传文件"); } foreach (IFormFile file in cols) { //定义图片数组后缀格式 string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" }; //获取图片后缀是否存在数组中 string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper(); if (LimitPictureType.Contains(currentPictureExtension)) { var new_path = Path.Combine("uploads/images/spots/", file.FileName); var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", new_path); using (var stream = new FileStream(path, FileMode.Create)) { await Task.Run(() => { file.CopyTo(stream); hash.Add("file", "/" + new_path); }); } } else { return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的图片"); } } return new ApiResult<Hashtable>(new Hashtable(hash)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException); } } } }