using Infrastructure; using Infrastructure.Attribute; using Mapster.Utils; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Routing.Template; using Microsoft.Extensions.DependencyInjection; using MiniExcelLibs; using Model.DBModel; using SqlSugar; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using ZR.Model.mes.pro; using ZR.Model.MES.pro.DTO; using ZR.Service.mes.pro.IService; using ZR.Service.MES.md.IService; namespace ZR.Service.mes.pro { [AppService(ServiceType = typeof(IProWorkplanServiceV2), ServiceLifetime = LifeTime.Transient)] public class ProWorkplanServiceV2 : BaseService, IProWorkplanServiceV2 { public (List, int) GetAllData(int pageNum, int pageSize, int year, int week, string partNumber, string color) { var predicate = Expressionable.Create() .AndIF(year > 0, it => it.Year == year) .AndIF(week > 0, it => it.Week == week) .AndIF(!string.IsNullOrEmpty(partNumber), it => it.Partnumber.Contains(partNumber)) .AndIF(!string.IsNullOrEmpty(color), it => it.ColorCode.Contains(color)) .ToExpression(); int totalCount = 0; List proWorkplanList = Context.Queryable().Where(predicate).OrderBy(it => it.Id).ToPageList(pageNum, pageSize, ref totalCount); return (proWorkplanList, totalCount); } public List GetProWorkplanById(string id) { return Context.Queryable().Where(it => it.Id == id).ToList(); } public int AddWorkPlan(ProWorklplan_v2 proWorkplan) { proWorkplan.Id = "MP" + DateTime.Now.ToString("yyyyMMdd") + Getworkplanid_max().ToString("000"); proWorkplan.Remark = "手动插入"; return Context.Insertable(proWorkplan).ExecuteCommand(); } /// /// 获取生产计划id /// /// private int Getworkplanid_max() { ProWorklplan_v2 max_workplan = Context.Queryable().OrderBy(it => it.Id, OrderByType.Desc).First(); if (max_workplan != null && !string.IsNullOrEmpty(max_workplan.Id) && max_workplan.Id.Substring(2, 8) == DateTime.Now.ToString("yyyyMMdd")) { int num = Convert.ToInt32(max_workplan.Id.Substring(10)) + 1; return num; } else { return 0; } } // 修改 public int UpdateWorkPlan(ProWorklplan_v2 proWorkplan) { return Context.Updateable() .SetColumns(it => new ProWorklplan_v2() { BlankNum = proWorkplan.BlankNum, Partnumber = proWorkplan.Partnumber, ProductName = proWorkplan.ProductName, Specification = proWorkplan.Specification, ColorCode = proWorkplan.ColorCode, RequireNum = proWorkplan.RequireNum, EveryHangerNum = proWorkplan.EveryHangerNum, ProductionBeat = proWorkplan.ProductionBeat, AllHangerNum = proWorkplan.AllHangerNum, TurnNumber = proWorkplan.TurnNumber, ProductTime = proWorkplan.ProductTime, RequireHanger = proWorkplan.RequireHanger, }) .Where(it => it.Id == proWorkplan.Id) .ExecuteCommand(); } public int DeleteWorkPlan(string id) { return Context.Deleteable().In(id).ExecuteCommand(); } public string ImportExceldata(List worklplanList) { int max_id = Getworkplanid_max(); worklplanList.ForEach(it => { it.Id = "MP" + DateTime.Now.ToString("yyyyMMdd") + max_id.ToString("000"); it.Remark = "Excel导入"; max_id++; }); var x = Context.Storageable(worklplanList) .SplitUpdate(it => it.Any())//存在更新 .SplitInsert(it => true)//否则插入(更新优先级大于插入) .WhereColumns(it => new { it.Year, it.Week, it.Partnumber })//如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2}) .ToStorage(); x.AsInsertable.ExecuteCommand();//插入可插入部分; x.AsUpdateable.IgnoreColumns(it => it.Id).ExecuteCommand();//存在更新 string msg = string.Format(" 插入{0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4} 总共{5}", x.InsertList.Count, x.UpdateList.Count, x.ErrorList.Count, x.IgnoreList.Count, x.DeleteList.Count, x.TotalList.Count); return msg; //插入可插入部分 } /// /// 下载 /// /// /// /// public (string, string) ExportExceldata(int year, int week) { //1.0 读取表数据 var list = Queryable().Where(it => it.Year == year && it.Week == week).ToList(); //2.0 保存为excel IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment)); string sFileName = $"{year}年{week}周计划-{DateTime.Now:MM-dd-HHmmss}.xlsx"; string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName); Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); var Sheet1 = new { year = year, week = week, title = $"{year}年车镜实业涂装事业{week}周生产滚动表", workplan = list }; string templatePath = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", "周计划标准模板1.xlsx"); MiniExcel.SaveAsByTemplate(fullPath, templatePath, Sheet1); // MiniExcel.SaveAs(fullPath, list); //3.0 返回路径和文件名 return (sFileName, fullPath); } public List GetWorkorderListByPlanId(string id) { return Context.Queryable().Where(it => it.FkProPlanId == id).OrderBy("priority desc ").ToList(); } public List GetWorkorderListById(string id) { return Context.Queryable().Where(it => it.Id == id).ToList(); } public int AddWorkorder(ProWorkorder proWorkorder) { return Context.Insertable(proWorkorder).ExecuteCommand(); } public int UpdateWorkorder(ProWorkorder proWorkorder) { return Context.Updateable(proWorkorder).ExecuteCommand(); } public int DeleteWorkorder(string id) { return Context.Deleteable().In(id).ExecuteCommand(); } /// /// 周计划汇总 /// /// /// public WorkplanSummaryDto GetWeekSummary(int year, int week) { return Context.Queryable().Where(it => it.Year == year && it.Week == week) .GroupBy(it => new { it.Week }) .Select(it => new WorkplanSummaryDto { requireNum = SqlFunc.AggregateSum(it.RequireNum), requireHanger = SqlFunc.AggregateSum(it.RequireHanger), turnnum = SqlFunc.AggregateSum(it.TurnNumber), productiontime = SqlFunc.AggregateSum(it.ProductTime), }) .First(); } /// /// 删除周计划全部 /// /// /// /// public int DeleteAllWorkPlan(int year,int week) { return Context.Deleteable().Where(it => it.Year == year && it.Week == week).ExecuteCommand(); } } }