using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using WIDESEA_Core;
using WIDESEA_Core.BaseController;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Inbound;
using WIDESEA_IInboundService;
using WIDESEA_IOutboundService;
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
namespace WIDESEA_WMSServer.Controllers
{
///
/// PDA 控制器 - 提供PDA应用下载、上传和版本检查功能
///
[Route("api/PDA")]
[Authorize, ApiController]
public class PDAController : Controller
{
private readonly IInboundService _inboundService;
private readonly IOutboundService _outboundService;
private readonly ITaskService _taskService;
///
/// 构造函数
///
public PDAController(ITaskService taskService, IInboundService inboundService, IOutboundService outboundService)
{
_inboundService = inboundService;
_outboundService = outboundService;
_taskService = taskService;
}
///
/// 下载PDA应用安装包
///
/// APK文件或错误信息
[HttpPost, HttpGet, Route("DownLoadApp"), AllowAnonymous]
public virtual ActionResult DownLoadApp()
{
const string appDirectory = "Upload/App/";
const string appFileName = "WMS-PDA.apk";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, appDirectory, appFileName);
if (!System.IO.File.Exists(path))
{
return Json(WebResponseContent.Instance.Error("未找到安装包"));
}
byte[] fileBytes = System.IO.File.ReadAllBytes(path);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, appFileName);
}
///
/// 上传PDA应用安装包
///
/// 上传的文件
/// 上传结果
[HttpPost, HttpGet, Route("UploadApp"), AllowAnonymous]
[Consumes("multipart/form-data")]
public WebResponseContent UploadApp(IEnumerable fileInput)
{
try
{
List files = fileInput.ToList();
if (files == null || files.Count == 0)
{
return new WebResponseContent { Status = false, Message = "请选择上传的文件" };
}
IFormFile formFile = files[0];
const string appDirectory = "Upload/App/";
const string appFileName = "WMS-PDA.apk";
string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, appDirectory);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
string backupPath = Path.Combine(directoryPath, $"WMS-PDA{DateTime.Now:yyyyMMddhhmmss}.apk");
string targetPath = Path.Combine(directoryPath, appFileName);
// 备份现有文件
if (System.IO.File.Exists(targetPath))
{
System.IO.File.Move(targetPath, backupPath);
}
// 保存新文件
using (var stream = new FileStream(targetPath, FileMode.Create))
{
formFile.CopyTo(stream);
}
return new WebResponseContent { Status = true, Message = "文件上传成功" };
}
catch (Exception ex)
{
return WebResponseContent.Instance.Error($"文件上传失败:{ex.Message}");
}
}
///
/// 获取PDA版本号并检查是否需要更新
///
/// 当前PDA版本号
/// 是否需要更新的标志
[HttpPost, HttpGet, Route("GetPDAVersion"), AllowAnonymous]
public WebResponseContent GetPDAVersion(string version)
{
try
{
if (string.IsNullOrEmpty(version))
{
return WebResponseContent.Instance.Error("版本号不能为空");
}
string serverVersion = AppSettings.Get("PDAVersion");
if (int.TryParse(serverVersion, out int serverVersionNum) && int.TryParse(version, out int clientVersionNum))
{
bool needUpdate = serverVersionNum > clientVersionNum;
return WebResponseContent.Instance.OK(data: needUpdate);
}
return WebResponseContent.Instance.Error("版本号格式错误");
}
catch (Exception ex)
{
return WebResponseContent.Instance.Error($"版本检查失败:{ex.Message}");
}
}
}
}