272 lines
10 KiB
C#
272 lines
10 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using ZR.Model.Business.Dto;
|
||
using ZR.Model.Business;
|
||
using ZR.Service.Business.IBusinessService;
|
||
using MiniExcelLibs.Picture;
|
||
using SKIT.FlurlHttpClient.Wechat.Api.Models;
|
||
using Microsoft.Extensions.Options;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using MiniExcelLibs;
|
||
using System.IO;
|
||
using IOFile = System.IO.File;
|
||
using Org.BouncyCastle.Utilities;
|
||
using Microsoft.Extensions.Configuration.UserSecrets;
|
||
using MiniExcelLibs.OpenXml;
|
||
using SixLabors.ImageSharp;
|
||
using SixLabors.ImageSharp.Formats.Jpeg;
|
||
using ZR.Infrastructure.Helper;
|
||
using OfficeOpenXml;
|
||
using OfficeOpenXml.Drawing;
|
||
//创建时间:2025-08-18
|
||
namespace ZR.Admin.WebApi.Controllers.Business
|
||
{
|
||
/// <summary>
|
||
/// 工作记录
|
||
/// </summary>
|
||
[Route("business/CamWorkrecord")]
|
||
public class CamWorkrecordController : BaseController
|
||
{
|
||
/// <summary>
|
||
/// 工作记录接口
|
||
/// </summary>
|
||
private readonly ICamWorkrecordService _CamWorkrecordService;
|
||
|
||
private OptionsSetting OptionsSetting;
|
||
private IWebHostEnvironment WebHostEnvironment;
|
||
private readonly ICamWorkerService _CamWorkerService;
|
||
|
||
public CamWorkrecordController(ICamWorkrecordService CamWorkrecordService, IOptions<OptionsSetting> options, IWebHostEnvironment webHostEnvironment,
|
||
ICamWorkerService camWorkerService)
|
||
{
|
||
_CamWorkrecordService = CamWorkrecordService;
|
||
OptionsSetting = options.Value;
|
||
WebHostEnvironment = webHostEnvironment;
|
||
_CamWorkerService = camWorkerService;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询工作记录列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("list")]
|
||
[ActionPermissionFilter(Permission = "camworkrecord:list")]
|
||
public IActionResult QueryCamWorkrecord([FromQuery] CamWorkrecordQueryDto parm)
|
||
{
|
||
var response = _CamWorkrecordService.GetList(parm);
|
||
return SUCCESS(response);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 查询工作记录详情
|
||
/// </summary>
|
||
/// <param name="Id"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("{Id}")]
|
||
[ActionPermissionFilter(Permission = "camworkrecord:query")]
|
||
public IActionResult GetCamWorkrecord(int Id)
|
||
{
|
||
var response = _CamWorkrecordService.GetInfo(Id);
|
||
|
||
var info = response.Adapt<CamWorkrecordDto>();
|
||
if (info != null)
|
||
{
|
||
var names = _CamWorkerService.AsQueryable().Where(it => it.WorkrecordId == info.Id).Select(it => it.WorkerName).ToList();
|
||
info.Worker = names != null && names.Count > 0 ? string.Join(",", names) : "";
|
||
}
|
||
return SUCCESS(info);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加工作记录
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ActionPermissionFilter(Permission = "camworkrecord:add")]
|
||
[Log(Title = "工作记录", BusinessType = BusinessType.INSERT)]
|
||
public IActionResult AddCamWorkrecord([FromBody] CamWorkrecordDto parm)
|
||
{
|
||
var modal = parm.Adapt<CamWorkrecord>().ToCreate(HttpContext);
|
||
modal.CreateTime = DateTime.Now;
|
||
modal.UpdateTime = DateTime.Now;
|
||
var response = _CamWorkrecordService.AddCamWorkrecord(modal);
|
||
|
||
return SUCCESS(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新工作记录
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPut]
|
||
[ActionPermissionFilter(Permission = "camworkrecord:edit")]
|
||
[Log(Title = "工作记录", BusinessType = BusinessType.UPDATE)]
|
||
public IActionResult UpdateCamWorkrecord([FromBody] CamWorkrecordDto parm)
|
||
{
|
||
var modal = parm.Adapt<CamWorkrecord>().ToUpdate(HttpContext);
|
||
modal.UpdateTime = DateTime.Now;
|
||
var response = _CamWorkrecordService.UpdateCamWorkrecord(modal);
|
||
|
||
return ToResponse(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除工作记录
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost("delete/{ids}")]
|
||
[ActionPermissionFilter(Permission = "camworkrecord:delete")]
|
||
[Log(Title = "工作记录", BusinessType = BusinessType.DELETE)]
|
||
public IActionResult DeleteCamWorkrecord([FromRoute] string ids)
|
||
{
|
||
var idArr = Tools.SplitAndConvert<int>(ids);
|
||
|
||
return ToResponse(_CamWorkrecordService.Delete(idArr, "删除工作记录"));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 导出工作记录
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[Log(Title = "工作记录", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
|
||
[HttpGet("export")]
|
||
[ActionPermissionFilter(Permission = "camworkrecord:export")]
|
||
public IActionResult Export([FromQuery] CamWorkrecordQueryDto parm)
|
||
{
|
||
var list = _CamWorkrecordService.ExportList(parm).Result;
|
||
if (list == null || list.Count <= 0)
|
||
{
|
||
return ToResponse(ResultCode.FAIL, "没有要导出的数据");
|
||
}
|
||
var url = OptionsSetting.Upload.UploadUrl;
|
||
string savePath = Path.Combine(WebHostEnvironment.WebRootPath);
|
||
int i = 1;
|
||
list.ForEach(it =>
|
||
{
|
||
var temp = string.IsNullOrEmpty(it.ImageUrl) ? "" : it.ImageUrl;
|
||
if (temp.IndexOf("http") > -1)
|
||
{
|
||
var image = temp.Replace(url, "");
|
||
temp = savePath + image;
|
||
}
|
||
it.Index = i;
|
||
it.ImageUrl = temp;
|
||
//it.Image = CompressImage(temp, 50);
|
||
i++;
|
||
});
|
||
ExcelHelper excelHelper = new ExcelHelper(WebHostEnvironment);
|
||
ExcelPackage.License.SetNonCommercialPersonal("pnaa");
|
||
var result = ExportWorkRecordExcel(list, "工作记录", 200, 70, 60);
|
||
//ExportExcelMini(list, "工作记录", "工作记录");
|
||
//ExportExcelWithImagesInBatches(result.Item2, "工作记录", urlList, 10);
|
||
//loadExcel();
|
||
return ExportExcel(result.Item2, result.Item1);
|
||
}
|
||
|
||
/// <summary>
|
||
public (string fileName, string fullPath) ExportWorkRecordExcel(List<CamWorkrecordExcelDto> list, string fileName, int imageWidth = 100, int imageHeight = 60, int imageQuality = 50)
|
||
{
|
||
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||
string sFileName = $"{fileName}_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";
|
||
string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
|
||
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
|
||
|
||
using var package = new ExcelPackage();
|
||
|
||
var ws = package.Workbook.Worksheets.Add("工作记录");
|
||
|
||
// 写表头
|
||
var headers = new[]
|
||
{
|
||
"序号","部门名称","拍照时间","经度","纬度","位置","工作内容","施工人员","状态","施工图片","创建时间","更新时间"
|
||
};
|
||
for (int i = 0; i < headers.Length; i++)
|
||
{
|
||
ws.Cells[1, i + 1].Value = headers[i];
|
||
ws.Cells[1, i + 1].Style.Font.Bold = true;
|
||
//ws.Column(i).Width = 120;
|
||
}
|
||
|
||
//ws.Column(10).Width = 40;
|
||
|
||
int row = 2;
|
||
foreach (var item in list)
|
||
{
|
||
ws.Cells[row, 1].Value = item.Index;
|
||
ws.Cells[row, 2].Value = item.DeptName;
|
||
ws.Cells[row, 3].Value = item.RecordTime?.ToString("yyyy-MM-dd HH:mm:ss");
|
||
ws.Cells[row, 4].Value = item.Longitude;
|
||
ws.Cells[row, 5].Value = item.Latitude;
|
||
ws.Cells[row, 6].Value = item.Address;
|
||
ws.Cells[row, 7].Value = item.Content;
|
||
ws.Cells[row, 8].Value = item.Worker;
|
||
ws.Cells[row, 9].Value = item.StatusName;
|
||
ws.Cells[row, 11].Value = item.CreateTime?.ToString("yyyy-MM-dd HH:mm:ss");
|
||
ws.Cells[row, 12].Value = item.UpdateTime?.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
||
// 如果 ImageUrl 有值,则压缩后插入 Excel
|
||
if (!string.IsNullOrEmpty(item.ImageUrl) && IOFile.Exists(item.ImageUrl))
|
||
{
|
||
using var image = SixLabors.ImageSharp.Image.Load(item.ImageUrl);
|
||
var encoder = new JpegEncoder { Quality = imageQuality };
|
||
using var ms = new MemoryStream();
|
||
image.Save(ms, encoder);
|
||
ms.Position = 0;
|
||
|
||
var pic = ws.Drawings.AddPicture($"Pic_{row}_10", ms);
|
||
pic.SetPosition(row - 1, 5, 9, 5); // 第10列
|
||
pic.SetSize(imageWidth, imageHeight);
|
||
}
|
||
ws.Row(row).Height = imageHeight;
|
||
row++;
|
||
}
|
||
|
||
ws.Cells[ws.Dimension.Address].AutoFitColumns();
|
||
ws.Cells[ws.Dimension.Address].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
|
||
ws.Column(10).Width = 40;
|
||
package.SaveAs(new FileInfo(fullPath));
|
||
|
||
return (sFileName, fullPath);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public byte[] CompressImage(string imagePath, int quality)
|
||
{
|
||
// 使用 using 语句确保资源被释放
|
||
using (var image = Image.Load(imagePath))
|
||
{
|
||
var encoder = new JpegEncoder { Quality = quality };
|
||
|
||
using (var outputStream = new MemoryStream())
|
||
{
|
||
image.Save(outputStream, encoder);
|
||
return outputStream.ToArray();
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 根据图片文件扩展名映射为 ContentType,避免类型不匹配导致 Excel 修复。
|
||
/// </summary>
|
||
private static string GetContentTypeByExtension(string filePath)
|
||
{
|
||
var ext = Path.GetExtension(filePath)?.ToLowerInvariant();
|
||
return ext switch
|
||
{
|
||
".png" => "image/png",
|
||
".jpg" => "image/jpeg",
|
||
".jpeg" => "image/jpeg",
|
||
".gif" => "image/gif",
|
||
".bmp" => "image/bmp",
|
||
".webp" => "image/webp", // 只有新版本 Excel 兼容,谨慎使用
|
||
_ => "image/png" // 默认按 png 处理
|
||
};
|
||
}
|
||
}
|
||
} |