using Aliyun.OSS; using AutoMapper.Configuration.Conventions; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting.Internal; using MimeKit; using MiniExcelLibs; using MiniExcelLibs.OpenXml; using Model.DBModel; using Org.BouncyCastle.Crypto.IO; using System.IO; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; using ZR.Model; using ZR.Model.mes.pro; using ZR.Model.MES.pro.DTO; using ZR.Model.System; using ZR.Model.System.Dto; using ZR.Service.mes.pro; using ZR.Service.mes.pro.IService; namespace ZR.Admin.WebApi.Controllers.mes.pro { [Route("mes/pro/workplan_v2")] public class ProWorkplanV2Controller : BaseController { private readonly IProWorkplanServiceV2 proWorkplanService; public ProWorkplanV2Controller(IProWorkplanServiceV2 proWorkplanService) { this.proWorkplanService = proWorkplanService; } /// /// 获取生产计划列表 /// /// 页编号 /// 页大小 /// 年份 /// 周数 /// 零件号 /// 颜色 /// [HttpGet("list")] public IActionResult List(int pageNum, int pageSize, int year = -1, int week = -1, string partNumber = "", string color = "") { (List, int) data = proWorkplanService.GetAllData(pageNum, pageSize, year, week, partNumber, color); return ToResponse(new ApiResult(200, "success", data)); } /// /// 新增生产计划 /// /// 生产计划对象 /// [HttpPost("addworkplan")] public IActionResult AddWorkPlan([FromBody] ProWorklplan_v2 proWorkplan) { int data = 0; if (proWorkplan != null) { proWorkplan.ToCreate(HttpContext); data = proWorkplanService.AddWorkPlan(proWorkplan); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 删除生产计划 /// /// 计划ID /// [HttpGet("deleteitem/{id}")] public IActionResult DeleteItem(string id) { int data = 0; if (!string.IsNullOrEmpty(id)) { data = proWorkplanService.DeleteWorkPlan(id); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 删除本周所有计划 /// /// /// [HttpGet("deleteAll")] public IActionResult DeleteAllItem(int? year,int? week) { int data = 0; if (week!=null&&week>0) { if (year != null && year > 0) data = proWorkplanService.DeleteAllWorkPlan((int)year,(int)week); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 更新生产计划 /// /// 生产计划对象 /// [HttpPost("updateworkplan")] public IActionResult UpdateWorkPlan([FromBody] ProWorklplan_v2 proWorkplan) { int data = 0; if (proWorkplan != null) { proWorkplan.ToUpdate(HttpContext); data = proWorkplanService.UpdateWorkPlan(proWorkplan); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 生产计划模板下载 /// /// [HttpGet("importTemplate")] [Log(Title = "生产计划模板模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)] [AllowAnonymous] //不需要授权 就可以访问 public IActionResult ImportTemplateExcel() { (string, string) result = DownloadImportTemplate("周计划标准模板");//返回文件名和路径 return ExportExcel(result.Item2, result.Item1); } /// /// 获取周汇总 /// /// /// /// [HttpGet("getWeekSummary")] public IActionResult GetWeekSummary(int? year, int? week) { if (year == null && week == null) { return SUCCESS(0); } WorkplanSummaryDto workplanSummaryDto = proWorkplanService.GetWeekSummary((int)year, (int)week); return SUCCESS(workplanSummaryDto); } /// /// 导入 /// /// 使用IFromFile必须使用name属性否则获取不到文件 /// [HttpPost("importData")] [Log(Title = "生产计划导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)] public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile, bool updateSupport) { //1.0 读取excel 文件 保存在指定位置 IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment)); string sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + formFile.FileName; string target = Path.Combine(webHostEnvironment.WebRootPath, "workplan", sFileName); if (!Directory.Exists(Path.Combine(webHostEnvironment.WebRootPath, "workplan"))) { Directory.CreateDirectory(Path.Combine(webHostEnvironment.WebRootPath, "workplan")); } int year = 0; int week = 0; 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); } //读取列表数据 try { //2.0 解析excel //读取第一行 解析 年和月 var row = stream.Query().Take(1).First(); year = Convert.ToInt32(row.A); week = Convert.ToInt32(row.B); var list = stream.Query(sheetName: "Sheet1", startCell: "B3") .Where(it => it.Partnumber != null) .Where(it => !it.Partnumber.Contains("合计")) .Where(it => it.RequireNum > 0) .ToList(); list.ForEach(it => { it.ToCreate(HttpContext); it.Year = year; it.Week = week; it.NoSchedule = it.RequireNum; }); string result = proWorkplanService.ImportExceldata(list); return SUCCESS(result); } catch (Exception ex) { return ToResponse(ResultCode.GLOBAL_ERROR,"内容错误,请仔细检测格式,并联系管理员"+ex.Message); } } return SUCCESS(null); } /// /// 浏览器下载 生产计划 /// /// /// [HttpGet("downloadWorkplan")] [Log(Title = "下载生产计划", BusinessType = BusinessType.EXPORT)] public IActionResult UserExport(int? year, int? week) { if (year == null || week == null) { return SUCCESS(0); } var result = proWorkplanService.ExportExceldata((int)year, (int)week); return ExportExcel(result.Item2, result.Item1); } /// /// 生产计划检查 /// /// 生产计划对象 /// [HttpPost("checkWorkPlan")] public IActionResult CheckWorkPlan([FromBody] ProWorklplan_v2 proWorkplan) { try { int result = proWorkplanService.CheckWorkPlan(proWorkplan); return ToResponse(new ApiResult(200, "success", result)); } catch (Exception ex) { return ToResponse(new ApiResult(500, ex.Message, 0)); } } } }