xwt
2025-10-16 349f2f66fdac1a2d64e0c99201b7f09579470714
StandardInterface/MES.Service/service/QC/SJService.cs
@@ -1028,4 +1028,127 @@
        return result;
    }
    /// <summary>
    /// 上传图片到检验项目的PICTURE字段
    /// </summary>
    /// <param name="id">检验项目ID</param>
    /// <param name="imageBytes">图片字节数组</param>
    /// <param name="fileName">原始文件名</param>
    /// <param name="createBy">创建人</param>
    /// <returns>操作结果</returns>
    public (int status, string message) UploadImageToPicture(decimal id, byte[] imageBytes, string fileName, string createBy)
    {
        try
        {
            if (imageBytes == null || imageBytes.Length == 0)
            {
                return (1, "图片数据为空");
            }
            if (string.IsNullOrEmpty(fileName))
            {
                return (1, "文件名为空");
            }
            // 验证图片格式
            var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp" };
            var extension = System.IO.Path.GetExtension(fileName).ToLower();
            if (!allowedExtensions.Contains(extension))
            {
                return (1, "不支持的图片格式,仅支持:jpg, jpeg, png, gif, bmp, webp");
            }
            // 验证图片大小(限制为5MB)
            if (imageBytes.Length > 5 * 1024 * 1024)
            {
                return (1, "图片大小不能超过5MB");
            }
            // 生成时间戳文件名,格式:1746945271304.jpg
            var timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var timestampFileName = $"{timestamp}{extension}";
            var result = SqlSugarHelper.UseTransactionWithOracle(db =>
            {
                // 检查检验项目是否存在
                var exists = db.Queryable<QsItemIpiItem>()
                    .Where(s => s.Id == id)
                    .Any();
                if (!exists)
                {
                    throw new Exception("检验项目不存在");
                }
                // 更新PICTURE字段(LONG RAW类型)和PICTURENAME字段(时间戳文件名)
                var updateResult = db.Updateable<QsItemIpiItem>()
                    .SetColumns(s => s.Picture == imageBytes)
                    .SetColumns(s => s.Picturename == timestampFileName)
                    .Where(s => s.Id == id)
                    .ExecuteCommand();
                return updateResult;
            });
            if (result > 0)
            {
                return (0, "图片保存成功");
            }
            else
            {
                return (1, "图片保存失败,未找到对应的检验项目");
            }
        }
        catch (Exception ex)
        {
            return (1, $"图片保存失败:{ex.Message}");
        }
    }
    /// <summary>
    /// 删除检验项目的图片
    /// </summary>
    /// <param name="id">检验项目ID</param>
    /// <returns>操作结果</returns>
    public (int status, string message) DeleteImageFromPicture(decimal id)
    {
        try
        {
            var result = SqlSugarHelper.UseTransactionWithOracle(db =>
            {
                // 检查检验项目是否存在
                var exists = db.Queryable<QsItemIpiItem>()
                    .Where(s => s.Id == id)
                    .Any();
                if (!exists)
                {
                    throw new Exception("检验项目不存在");
                }
                // 清空PICTURE字段和PICTURENAME字段
                var updateResult = db.Updateable<QsItemIpiItem>()
                    .SetColumns(s => s.Picture == null)
                    .SetColumns(s => s.Picturename == null)
                    .Where(s => s.Id == id)
                    .ExecuteCommand();
                return updateResult;
            });
            if (result > 0)
            {
                return (0, "图片删除成功");
            }
            else
            {
                return (1, "图片删除失败,未找到对应的检验项目");
            }
        }
        catch (Exception ex)
        {
            return (1, $"图片删除失败:{ex.Message}");
        }
    }
}