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
{
///
/// 巡检点
///
public class TispSpotController : BaseController
{
private readonly ILogger _logger;
private readonly ITispSpotService _tispSpotService;
private readonly ITispSpotContentService _tispSpotContentService;
///
/// 构造函数
///
///
///
///
public TispSpotController(ITispSpotService tispSpotService, ITispSpotContentService tispSpotContentService, ILogger logger)
{
_tispSpotService = tispSpotService;
_tispSpotContentService = tispSpotContentService;
_logger = logger;
}
///
/// 通过ID获取巡检点信息
///
///
///
[HttpGet("GetSpotAsync/{id}")]
public async Task GetSpotAsync(Guid id)
{
if (Guid.Empty == id)
{
return new ApiResult(ReturnCode.GeneralError);
}
try
{
var spot = await _tispSpotService.GetByIdAsync(id);
return new ApiResult(spot);
}
catch (Exception ex)
{
return new ApiResult(ReturnCode.GeneralError, ex.Message);
}
}
///
/// 获取所有巡检点
///
///
[HttpGet("GetSpotsAsync")]
public async Task GetSpotsAsync()
{
try
{
var spotList = await _tispSpotService.GetAllAsync();
return new ApiResult>(spotList);
}
catch (Exception ex)
{
return new ApiResult(ReturnCode.GeneralError, ex.Message);
}
}
///
/// 条件查询巡检点
///
///
///
[HttpPost("GetSpotsConditionAsync")]
public async Task 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>(new PagesModel(spotList?.ToList(), searchModel));
}
catch (Exception ex)
{
return new ApiResult(ReturnCode.GeneralError, ex.Message);
}
}
///
/// 条件查询巡检点【设备运维点配置-排除业主设备运维点重复的】
///
///
///
[HttpPost("GetSpotsConditionNoDevSpotRepeatAsync")]
public async Task 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>(new PagesModel(spotList?.ToList(), searchModel));
}
catch (Exception ex)
{
return new ApiResult(ReturnCode.GeneralError, ex.Message);
}
}
///
/// 通过二维码获取巡检点
///
///
///
///
[HttpGet("GetSpotByQRCodeTAsync/{qRCode}/{storeCode}")]
public async Task 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(spot, ReturnCode.ResultError, "没找到此二维码对应的巡检点,请联系管理员!");
}
return new ApiResult(spot);
}
catch (Exception ex)
{
if (ex.Message == "此巡检点没有分配给您!")
{
return new ApiResult(ReturnCode.ResultError, ex.Message);
}
return new ApiResult(ReturnCode.GeneralError, ex.Message);
}
}
///
/// 通过二维码获取巡检点器具
///
///
///
///
[HttpGet("GetSpotProductByQRCodeTAsync/{qRCode}/{storeCode}")]
public async Task 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(spot, ReturnCode.ResultError, "没找到此二维码对应的巡检点,请联系管理员!");
}
return new ApiResult(spot);
}
catch (Exception ex)
{
return new ApiResult(ReturnCode.GeneralError, ex.Message);
}
}
///
/// 通过二维码获取点及设备信息
///
///
///
///
[HttpGet("GetDevStoreByQRCodeAsync/{qRCode}/{storeCode}")]
public async Task 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(spot, ReturnCode.ResultError, "没找到此二维码对应的点,请联系管理员!");
}
return new ApiResult(spot);
}
catch (Exception ex)
{
return new ApiResult(ReturnCode.GeneralError, ex.Message);
}
}
///
/// 创建巡检点
///
/// 巡检点
///
//public async Task CreateSpotAsync([FromQuery] TispSpotCreateViewModel spot, [FromQuery] List contentList)
[Route("CreateSpotAsync")]
[HttpPost]
public async Task 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);
}
///
/// 创建设备巡检运维点
///
///
///
[Route("CreateOneByDevAsync")]
[HttpPost]
public async Task 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);
}
///
/// 删除巡检点
///
///
///
[HttpDelete("DeleteSpotAsync/{id}")]
public async Task 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);
}
///
/// 更新巡检点
///
///
///
///
[HttpPut("UpdateSpotAsync/{id}")]
public async Task 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);
}
///
/// 扫码绑定,包括二维码,GPS,照片
///
///
///
///
[HttpPut("UpdateConfigureSpotAsync/{id}")]
public async Task 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);
}
///
/// 更新巡检点图片
///
///
///
///
[HttpPost("UploadSpotImageAsync/{spotId}")]
public async Task 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(new Hashtable(hash));
}
catch (Exception ex)
{
return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
}
}
}
}