using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using MiniExcelLibs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using DOAN.Admin.WebApi.Extensions;
using DOAN.Admin.WebApi.Filters;
using DOAN.Model;
using DOAN.Model.MES.upload;
using DOAN.Model.System;
using DOAN.Service.quality.upload;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace DOAN.WebApi.Controllers.MES.quality.upload
{
[Verify]
[Route("mes/quality/upload")]
public class UploadController : BaseController
{
IUploadServive uploadservcie;
public UploadController(IUploadServive uploadservcie)
{
this.uploadservcie = uploadservcie;
}
///
/// 获取上传数据列表
///
///
///
///
///
///
[HttpGet("list")]
public IActionResult List(string partnumber, string workstation, string paramter, [FromQuery] PagerInfo pager)
{
var list = uploadservcie.SelectExcelList(partnumber, workstation, paramter, pager);
return SUCCESS(list);
}
///
/// 导入
///
/// 使用IFromFile必须使用name属性否则获取不到文件
///
[HttpPost("importData")]
[Log(Title = "云端数据导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
{
//1.0 读取excel 文件 保存在指定位置
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
string target = Path.Combine(webHostEnvironment.WebRootPath, "cloudUpload");
if (!Directory.Exists(target))
{
// 如果目录不存在就创建
Directory.CreateDirectory(target);
}
string sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + formFile.FileName;
target = Path.Combine(target, sFileName);
using (var stream = formFile.OpenReadStream())
{
FileStream targetFileStream = new FileStream(target, FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
targetFileStream.Write(buffer, 0, bytesRead);
}
//2.0 解析excel
try
{
var uploadList = stream.Query(sheetName: "数据上传宜搭计划", startCell: "A3").ToList();
int data = uploadservcie.GOcloud(uploadList, HttpContext.GetName());
return SUCCESS(data);
}
catch (Exception ex)
{
return ToResponse(ResultCode.GLOBAL_ERROR, "内容错误,请仔细检测格式,并联系管理员" + ex.Message);
}
}
}
}
}