using Infrastructure.Extensions; using JinianNet.JNTemplate; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using MiniExcelLibs; using Model.DBModel; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SqlSugar; using System.Text.Json; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; using ZR.Model.MES.pro; using ZR.Service.mes.pro; using ZR.Service.mes.pro.IService; namespace ZR.Admin.WebApi.Controllers.mes.pro { [Route("mes/pro/workorder_v2")] public class ProWorkorderV2Controller : BaseController { private readonly IProWorkorderServiceV2 proWorkorderService; public ProWorkorderV2Controller(IProWorkorderServiceV2 proWorkorderService) { this.proWorkorderService = proWorkorderService; } [HttpGet("getWorkoderList")] public IActionResult GetWorkorderList(int pageNum, int pageSize, int year = -1, int week = -1, int date = -1) { (List, int) data = proWorkorderService.GetWorkorderList(pageNum, pageSize, year, week, date, 0); return ToResponse(new ApiResult(200, "success", data)); } // [HttpGet("getWorkoderList_piliang")] public IActionResult GetWorkorderList_Piliang(int pageNum, int pageSize, int year = -1, int week = -1, int date = -1) { (List, int) data = proWorkorderService.GetWorkorderList_Piliang(pageNum, pageSize, year, week, date, 0); 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); } /// /// 导入 /// /// 使用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, "workorder", sFileName); int year = 0; int week = 0; int date = 0; if (!Directory.Exists(target)) { // 如果目录不存在就创建 Directory.CreateDirectory(target); } 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().Skip(1).First(); year = Convert.ToInt32(row.A); week = Convert.ToInt32(row.B); date = Convert.ToInt32(row.C); var list = stream.Query(sheetName: "Sheet1", startCell: "A3").ToList(); ; foreach (ProWorkorder_v2 item in list) { if (item.BlankNumber == null) item.BlankNumber = ""; if (item.FinishedPartNumber == null) item.FinishedPartNumber = ""; if (item.ProductDescription == null) item.ProductDescription = ""; if (item.Colour == null) item.Colour = ""; if (item.Specifications == null) item.Specifications = ""; if (item.CylinderNumber == null) item.CylinderNumber = ""; if (item.Remark1 == null) item.Remark1 = ""; if (item.Remark2 == null) item.Remark2 = ""; if (item.Remark3 == null) item.Remark3 = ""; if (item.Remark4 == null) item.Remark4 = ""; if (item.ClientWorkorder == null) item.ClientWorkorder = ""; item.ToCreate(HttpContext); item.Year = year; item.Week = week; item.Date = date; } var final_list = list.Where(it => !it.BlankNumber.Contains("圈数")) .Where(it => !(it.BlankNumber == "" && it.FinishedPartNumber == "" && it.ProductDescription == "" && it.Specifications == "" && it.CylinderNumber == "" && it.Remark1 == "" && it.Remark2== "" && it.Remark3 == "" && it.Remark4== ""&&it.ClientWorkorder=="")) .ToList(); string result = proWorkorderService.ImportExceldata(final_list); return SUCCESS(result); } catch (Exception ex) { return ToResponse(ResultCode.GLOBAL_ERROR, "内容错误,请仔细检测格式,并联系管理员" + ex.Message); } } return SUCCESS(null); } /// /// 浏览器下载 生产工单 /// /// /// [HttpGet("downloadWorkorder")] [Log(Title = "下载生产工单", BusinessType = BusinessType.EXPORT)] public IActionResult UserExport(int? year, int? week, int? date) { if (year == null || week == null || date == null) { return SUCCESS(0); } var result = proWorkorderService.ExportExceldata((int)year, (int)week, (int)date); return ExportExcel(result.Item2, result.Item1); } /// /// 删除本周所有计划 /// /// /// [HttpGet("deleteAll")] public IActionResult DeleteAllItem(int? year, int? week, int? date) { int data = 0; if (week != null && week > 0) { if (year != null && year > 0) data = proWorkorderService.DeleteAllWorkorder((int)year, (int)week, (int)date); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 新增生产工单 /// /// 生产工单对象 /// [HttpPost("addworkorder")] public IActionResult AddWorkOrder([FromBody] ProWorkorder_v2 proWorkorder) { int data = 0; if (proWorkorder != null) { proWorkorder.ToCreate(HttpContext); data = proWorkorderService.AddWorkOrder(proWorkorder); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 删除生产工单 /// /// 工单ID /// [HttpGet("deleteitem/{id}")] public IActionResult DeleteItem(string id) { int data = 0; if (!string.IsNullOrEmpty(id)) { data = proWorkorderService.DeleteWorkOrder(id); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 更新生产计划 /// /// 生产计划对象 /// [HttpPost("updateworkorder")] public IActionResult UpdateWorkOrder([FromBody] ProWorkorder_v2 proWorkorder) { int data = 0; if (proWorkorder != null) { proWorkorder.ToUpdate(HttpContext); data = proWorkorderService.UpdateWorkOrder(proWorkorder); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 删除本周所有计划 /// /// /// [HttpGet("updateSort")] public IActionResult UpdateSort(string id, int? sort) { int data = 0; if (!string.IsNullOrEmpty(id)) { data = proWorkorderService.UpdateworkorderSort(id, (int)sort); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 工单开始上线 /// /// 工单ID /// [HttpGet("startOnline/{id}")] public IActionResult StartOnline(string id) { int data = 0; if (!string.IsNullOrEmpty(id)) { data = proWorkorderService.StartWorkOrder(id); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 工单下线 /// /// 工单ID /// [HttpGet("cancelOnline/{id}")] public IActionResult CancelOnline(string id) { int data = 0; if (!string.IsNullOrEmpty(id)) { data = proWorkorderService.CancelWorkOrder(id); } return ToResponse(new ApiResult(200, "success", data)); } /// /// 生成工单号 /// /// /// [HttpGet("generateWorkorder")] public IActionResult GenerateWorkorder(int? year, int? week, int? date) { int data = 0; data = proWorkorderService.GenerateWorkorder((int)year, (int)week, (int)date); return ToResponse(new ApiResult(200, "success", data)); } } }