sjz
2025-05-09 597f8e08e6264b2143454e40a7be553d1e8b6df7
StandardInterface/MESApplication/Controllers/PLM/PLMController.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,125 @@
using ConsoleApp1;
using MES.Service.service.PLM;
using MES.Service.util;
using Microsoft.AspNetCore.Mvc;
using System.Dynamic;
using System.IO;
namespace MESApplication.Controllers.PLM;
[ApiController]
[Route("api/PLM")]
public class PLMController : ControllerBase
{
    private readonly PLMManager m = new();
    private readonly WarehouseDownloadDoc wdd = new();
    //RetrieveDrawings  è°ƒå–图纸
    [HttpPost("RetrieveDrawings")]
    public ResponseResult RetrieveDrawings(string ItemNo)
    {
        try
        {
            dynamic resultInfos = new ExpandoObject();
            resultInfos = m.RetrieveDrawings(ItemNo);
            return new ResponseResult
            {
                status = 0,
                message = "OK",
                data = resultInfos
            };
        }
            catch (Exception ex)
        {
            return ResponseResult.ResponseError(ex);
        }
    }
    //RetrieveImageFile  è°ƒå–上传到服务器的拍摄文件
    [HttpGet("GetImageNames")]
    public IActionResult GetImageNames(string releaseNo)
    {
        // æ‹¼æŽ¥æœ¬åœ°æ–‡ä»¶ç³»ç»Ÿè·¯å¾„
        string basePath = @"D:\MES_FTP\IQC\"; // æœåС噍D盘下的基础路径
        string folderPath = Path.Combine(basePath, releaseNo); // åŠ¨æ€æ‹¼æŽ¥å•å·å¯¹åº”çš„æ–‡ä»¶å¤¹è·¯å¾„
        // æ£€æŸ¥æ–‡ä»¶å¤¹æ˜¯å¦å­˜åœ¨
        if (!Directory.Exists(folderPath))
        {
            return NotFound($"文件夹 {folderPath} ä¸å­˜åœ¨ã€‚请检查路径是否正确。");
        }
        // èŽ·å–æ–‡ä»¶å¤¹ä¸­çš„æ‰€æœ‰å›¾ç‰‡æ–‡ä»¶åç§°
        string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
        // èŽ·å–æ–‡ä»¶çš„å®Œæ•´è·¯å¾„ï¼Œå¹¶è¿”å›žä¸€ä¸ªåŒ…å«æ–‡ä»¶åå’Œå®Œæ•´è·¯å¾„çš„å¯¹è±¡
        var imageFiles = Directory.GetFiles(folderPath)
            .Where(file => imageExtensions.Any(ext => file.ToLower().EndsWith(ext)))
            .Select(file => new
            {
                FileName = Path.GetFileName(file), // æ–‡ä»¶å
                FilePath = file,
                FileBasePath = ConvertFileToBase64(file) // æ–‡ä»¶çš„ Base64 æ•°æ® // æ–‡ä»¶çš„完整路径
            });
        return Ok(new { success = true, data = imageFiles });
    }
    private string ConvertFileToBase64(string filePath)
    {
        byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
        return Convert.ToBase64String(fileBytes);
    }
    [HttpPost("DeleteImageFile")]
    public IActionResult DeleteImageFile([FromQuery] string filePath)
    {
        // æ£€æŸ¥æ–‡ä»¶è·¯å¾„是否为空
        if (string.IsNullOrWhiteSpace(filePath))
        {
            return BadRequest("文件路径不能为空");
        }
        // ç¡®ä¿è·¯å¾„是绝对路径(可以根据需要调整)
        filePath = Path.GetFullPath(filePath);
        // æ£€æŸ¥æ–‡ä»¶æ˜¯å¦å­˜åœ¨
        if (!System.IO.File.Exists(filePath))
        {
            return NotFound("文件不存在");
        }
        try
        {
            // åˆ é™¤æ–‡ä»¶
            System.IO.File.Delete(filePath);
            return Ok("文件删除成功");
        }
        catch (System.Exception ex)
        {
            // æ•获异常并返回错误信息
            return StatusCode(500, $"删除文件时发生错误: {ex.Message}");
        }
    }
    //RetrieveDrawings  è°ƒå–图纸
    [HttpPost("OpenDrawings")]
    public IActionResult OpenDrawings(string fileId,string fName)
    {
        try
        {
            var resultInfos = wdd.SendRequest("Download", fileId);
            return File(resultInfos, "application/octet-stream", fName);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new ResponseResult
            {
                status = 1,
                message = ex.Message,
                data = null
            });
        }
    }
}