using EmitMapper; using FineUIPro; using Microsoft.SqlServer.Dts.Runtime; using MiniExcelLibs; using Model; using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; namespace BLL { public static class TwInOutplanmasterService { #region 获取列表 /// /// 记录数 /// public static int Count { get; set; } public static IQueryable GetModle(Model.Tw_InOutMasterOutput table) { var q = from x in Funs.DB.Tw_InOutPlanMaster join y in Funs.DB.HJGL_WeldTask on x.WeldTaskId equals y.WeldTaskId into yy from y in yy.DefaultIfEmpty() join person in Funs.DB.Person_Persons on x.CreateMan equals person.PersonId into persons from person in persons.DefaultIfEmpty() join auditperson in Funs.DB.Person_Persons on x.AuditMan equals auditperson.PersonId into auditpersons from auditperson in auditpersons.DefaultIfEmpty() join auditperson2 in Funs.DB.Person_Persons on x.AuditMan2 equals auditperson2.PersonId into auditpersons2 from auditperson2 in auditpersons2.DefaultIfEmpty() join warehouseperson in Funs.DB.Person_Persons on x.WarehouseMan equals warehouseperson.PersonId into warehousepersons from warehouseperson in warehousepersons.DefaultIfEmpty() join unit in Funs.DB.Base_Unit on x.ReqUnitId equals unit.UnitId into units from unit in units.DefaultIfEmpty() orderby x.CreateDate descending where (string.IsNullOrEmpty(table.Id) || x.Id.Contains(table.Id)) && (string.IsNullOrEmpty(table.ProjectId) || x.ProjectId.Contains(table.ProjectId)) && (string.IsNullOrEmpty(table.CusBillCode) || x.CusBillCode.Contains(table.CusBillCode)) && (string.IsNullOrEmpty(table.WarehouseCode) || x.WarehouseCode.Contains(table.WarehouseCode)) && (string.IsNullOrEmpty(table.CreateMan) || x.CreateMan.Contains(table.CreateMan)) && (string.IsNullOrEmpty(table.OutputMasterId) || x.OutputMasterId.Contains(table.OutputMasterId)) && (string.IsNullOrEmpty(table.ReqUnitId) || x.ReqUnitId.Contains(table.ReqUnitId)) && (string.IsNullOrEmpty(table.UnitWorkId) || x.WeldTaskId.Contains(table.UnitWorkId)) && (string.IsNullOrEmpty(table.WeldTaskId) || x.WeldTaskId.Contains(table.WeldTaskId)) && (table.InOutType == null || x.InOutType == table.InOutType) && (table.TypeInt == null || x.TypeInt == table.TypeInt) && (table.Category == null || x.Category == table.Category) && (table.State == null || x.State == table.State) select new Model.Tw_InOutMasterOutput { Id = x.Id, ProjectId = x.ProjectId, CusBillCode = x.CusBillCode, WarehouseCode = x.WarehouseCode, Source = x.Source, InOutType = x.InOutType, TypeInt = x.TypeInt, State = x.State, Category = x.Category, CreateMan = x.CreateMan, CreateManName = person.PersonName, CreateDate = x.CreateDate, OutputMasterId = x.OutputMasterId, WeldTaskId = x.WeldTaskId, ReqUnitId = x.ReqUnitId, ReqUnitName = unit.UnitName, UnitWorkId = y.UnitWorkId, WeldTaskCode = y.TaskCode, AuditMan = x.AuditMan, AuditManName = auditperson.PersonName, AuditDate = x.AuditDate, Remark = x.Remark, AuditMan2 = x.AuditMan2, AuditManName2 = auditperson2.PersonName, AuditDate2 = x.AuditDate2, WarehouseMan = x.WarehouseMan, WarehouseDate = x.WarehouseDate, WarehouseManName = warehouseperson.PersonName, PipeLineSortIndex = y.PipeLineSortIndex } ; return q; } /// /// 获取分页列表 /// /// /// /// public static List GetListData(Model.Tw_InOutMasterOutput table) { var q = GetModle(table); Count = q.Count(); if (Count == 0) { return null; } var result = q.ToList(); // q = SortConditionHelper.SortingAndPaging(q, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize); return (from x in result select new Model.Tw_InOutMasterOutput { Id = x.Id, ProjectId = x.ProjectId, CusBillCode = x.CusBillCode, WarehouseCode = x.WarehouseCode, Source = x.Source, InOutType = x.InOutType, TypeInt = x.TypeInt, Category = x.Category, State = x.State, CreateMan = x.CreateMan, CreateManName = x.CreateManName, CreateDate = x.CreateDate, OutputMasterId = x.OutputMasterId, WeldTaskId = x.WeldTaskId, ReqUnitId = x.ReqUnitId, ReqUnitName = x.ReqUnitName, UnitWorkId = x.UnitWorkId, WeldTaskCode = x.WeldTaskId != null ? x.WeldTaskId.Contains('|') ? Funs.DB.HJGL_WeldTask.FirstOrDefault(e => e.UnitWorkId == x.WeldTaskId.Split('|')[0].ToString() && e.UnitId == x.WeldTaskId.Split('|')[1].ToString() && e.TaskDate.Value.Date == DateTime.ParseExact(x.WeldTaskId.Split('|')[2].ToString(), "yyyyMMdd", null).Date)?.TaskCode : "" : "", CategoryString = TwConst.CategoryMap.FirstOrDefault(y => y.Value == x.Category).Key, TypeString = TwConst.TypeIntMap.FirstOrDefault(y => y.Value == x.TypeInt).Key, StateString = TwConst.StateMap.FirstOrDefault(y => y.Value == x.State).Key, AuditMan = x.AuditMan, AuditManName = x.AuditManName, AuditDate = x.AuditDate, Remark = x.Remark, AuditMan2 = x.AuditMan2, AuditManName2 = x.AuditManName2, AuditDate2 = x.AuditDate2, WarehouseMan = x.WarehouseMan, WarehouseDate = x.WarehouseDate, WarehouseManName = x.WarehouseManName, PipeLineSortIndex = x.PipeLineSortIndex, }).ToList(); } public static IEnumerable GetListData(Model.Tw_InOutMasterOutput table, Grid grid1) { var q = GetModle(table); Count = q.Count(); if (Count == 0) { return null; } var result = q.Skip(grid1.PageSize * grid1.PageIndex).Take(grid1.PageSize).ToList(); // q = SortConditionHelper.SortingAndPaging(q, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize); return from x in result select new Model.Tw_InOutMasterOutput { Id = x.Id, ProjectId = x.ProjectId, CusBillCode = x.CusBillCode, WarehouseCode = x.WarehouseCode, Source = x.Source, InOutType = x.InOutType, TypeInt = x.TypeInt, Category = x.Category, State = x.State, CreateMan = x.CreateMan, CreateManName = x.CreateManName, CreateDate = x.CreateDate, OutputMasterId = x.OutputMasterId, WeldTaskId = x.WeldTaskId, ReqUnitId = x.ReqUnitId, ReqUnitName = x.ReqUnitName, UnitWorkId = x.UnitWorkId, WeldTaskCode = GetWeldTaskCode(x.WeldTaskId), CategoryString = TwConst.CategoryMap.FirstOrDefault(y => y.Value == x.Category).Key, TypeString = TwConst.TypeIntMap.FirstOrDefault(y => y.Value == x.TypeInt).Key, StateString = TwConst.StateMap.FirstOrDefault(y => y.Value == x.State).Key, AuditMan = x.AuditMan, AuditManName = x.AuditManName, AuditDate = x.AuditDate, Remark = x.Remark, AuditMan2 = x.AuditMan2, AuditManName2 = x.AuditManName2, AuditDate2 = x.AuditDate2, WarehouseMan = x.WarehouseMan, WarehouseDate = x.WarehouseDate, WarehouseManName = x.WarehouseManName }; } public static string GetWeldTaskCode(string WeldTaskId) { string result = String.Empty; if (string.IsNullOrEmpty(WeldTaskId)) return result; if (WeldTaskId.Split('|').Length == 3) //用于处理历史数据 { result = (from x in Funs.DB.HJGL_WeldTask where x.UnitWorkId == WeldTaskId.Split('|')[0].ToString() && x.UnitId == WeldTaskId.Split('|')[1].ToString() && x.TaskDate.Value.Date == DateTime.ParseExact(WeldTaskId.Split('|')[2].ToString(), "yyyyMMdd", null) select x.TaskCode).FirstOrDefault() ?.ToString(); } else if (WeldTaskId.Split('|').Length == 4) { result = (from x in Funs.DB.HJGL_WeldTask where x.UnitWorkId == WeldTaskId.Split('|')[0].ToString() && x.UnitId == WeldTaskId.Split('|')[1].ToString() && x.TaskDate.Value.Date == DateTime.ParseExact(WeldTaskId.Split('|')[2].ToString(), "yyyyMMdd", null) && x.SerialNumber == WeldTaskId.Split('|')[3] select x.TaskCode).FirstOrDefault() ?.ToString(); } return result; } #endregion /// /// 导入数据 /// /// /// /// /// public static ResponeData ImportData(string OriFileName, string path, string projectid, string creatUserId) { var responeData = new ResponeData(); List temeplateDtoIns; try { temeplateDtoIns = MiniExcel.Query(path, startCell: "A1").ToList(); } catch (Exception ex) { responeData.code = 0; responeData.message = "模板错误:" + ex.ToString(); return responeData; } #region 数据校验 if (temeplateDtoIns.Count == 0) { responeData.code = 0; responeData.message = "导入数据为空!"; return responeData; } var warehouseCodeList = temeplateDtoIns.Select(x => x.WarehouseCode).Distinct().ToList(); //获取导入文件的仓库编号 string errorWarehouseCode = ""; foreach (var item in warehouseCodeList) { if (!Base_WarehouseService.GetWarehouseList(projectid).Select(x => x.WarehouseName == item).Any()) { errorWarehouseCode += item + ","; } } if (!string.IsNullOrEmpty(errorWarehouseCode)) { //if (temeplateDtoIns.Count == 0) //{ responeData.code = 0; responeData.message = errorWarehouseCode + "仓库不存在!"; return responeData; //} } var typeString = temeplateDtoIns.Select(x => x.TypeString).Distinct().ToList(); //获取导入文件的类型 if (typeString.Where(x => string.IsNullOrEmpty(x)).Count() > 0) { responeData.code = 0; responeData.message = "导入数据中类型列不能为空!"; return responeData; } if (typeString.Contains("采购入库") && typeString.Contains("其他入库")) { responeData.code = 0; responeData.message = errorWarehouseCode + "每次只能导入一种类型!"; return responeData; } var dateTypeString = temeplateDtoIns.Select(x => x.DeliveryDate).Distinct().ToList(); //获取导入文件的类型 foreach (var item in dateTypeString) { try { DateTime date = Convert.ToDateTime(item); } catch (Exception) { responeData.code = 0; responeData.message = errorWarehouseCode + "到货时间[" + item + "]格式错误!"; return responeData; } } //var materialCodeList = temeplateDtoIns.Select(x => x.MaterialCode).Distinct().ToList(); //获取导入文件的材料编码 //var IsExitMaterialCode = // from x in Funs.DB.HJGL_MaterialCodeLib // where materialCodeList.Contains(x.MaterialCode) // select x.MaterialCode; //if (IsExitMaterialCode.Count() < materialCodeList.Count) //{ // var errorMaterialCode = materialCodeList.Except(IsExitMaterialCode.ToList()); // if (errorMaterialCode.Count() > 0) // { // responeData.code = 0; // responeData.message = string.Join(",", errorMaterialCode) + "材料不存在!"; // return responeData; // } //} string cusbilcode = GetDataInCusBillCode(projectid, UnitService.GetUnitCodeByUnitId(Person_PersonsService.GetPerson_PersonsById(creatUserId).UnitId), temeplateDtoIns.FirstOrDefault().TypeString); foreach (var item in temeplateDtoIns) { item.CusBillCode = cusbilcode; } /*var queryAll = new Tw_InOutMasterOutput() { ProjectId = projectid }; var queryAllresult = GetModle(queryAll).Select(x => x.CusBillCode).Distinct().ToList();//查询所有的编号 var CusBillCodeList = temeplateDtoIns.Select(x => x.CusBillCode).Distinct().ToList(); //获取导入文件的编号 var IsContain = CusBillCodeList.Intersect(queryAllresult).ToList(); //判断导入文件中是有已存在的编号 if (IsContain.Count>0) { responeData.code = 0; responeData.message = string.Join(",", IsContain)+ "编号已存在!"; return responeData; }*/ var CusBillCodeList = temeplateDtoIns.Select(x => x.CusBillCode).Distinct().ToList(); //获取导入文件的编号 #endregion //根据申请单编号分组插入数据 foreach (var CusBillCode in CusBillCodeList) { var CusBillCodeDtoIns = temeplateDtoIns.Where(x => x.CusBillCode == CusBillCode).ToList(); var FirstCusBillCodeDtoIns = temeplateDtoIns.FirstOrDefault(x => x.CusBillCode == CusBillCode); var mapper = ObjectMapperManager.DefaultInstance.GetMapper, List>(); var mapperDetail = ObjectMapperManager.DefaultInstance.GetMapper, List>(); //通过映射实体赋值 var twInOutPlanMaster = mapper.Map(CusBillCodeDtoIns).FirstOrDefault(); var twInOutPlanDetails = mapperDetail.Map(CusBillCodeDtoIns.Where(x => !string.IsNullOrEmpty(x.MaterialCode) && !string.IsNullOrEmpty(x.PlanNum)).ToList()); twInOutPlanMaster.Id = SQLHelper.GetNewID(); twInOutPlanMaster.InOutType = (int)TwConst.InOutType.入库; twInOutPlanMaster.State = (int)TwConst.State.待审核; if ("采购入库".Equals(FirstCusBillCodeDtoIns.TypeString)) { twInOutPlanMaster.TypeInt = (int)TwConst.TypeInt.采购入库; } else if ("其他入库".Equals(FirstCusBillCodeDtoIns.TypeString)) { twInOutPlanMaster.TypeInt = (int)TwConst.TypeInt.其他入库; } else { twInOutPlanMaster.TypeInt = (int)TwConst.TypeInt.其他入库; } twInOutPlanMaster.Category = (int)TwConst.Category.全部; twInOutPlanMaster.ProjectId = projectid; twInOutPlanMaster.CreateMan = creatUserId; twInOutPlanMaster.CreateDate = DateTime.Now; twInOutPlanMaster.Remark = OriFileName; Add(twInOutPlanMaster); MaterialCodeLibService.AddList(temeplateDtoIns); //插入材料编码库 TwInOutplandetailService.AddList(twInOutPlanDetails, twInOutPlanMaster.Id); //插入入库明细 } return responeData; } /// /// 导入历史出库数据,直接生成出库申请单和出库单并扣减库存 /// public static ResponeData ImportOutHistoryData(string oriFileName, string path, string projectId, string createUserId, string unitWorkId) { var responeData = new ResponeData(); if (string.IsNullOrEmpty(unitWorkId)) { responeData.code = 0; responeData.message = "请选择单位工程!"; return responeData; } List importRows; try { importRows = MiniExcel.Query(path, startCell: "A1").ToList(); } catch (Exception ex) { responeData.code = 0; responeData.message = "模板错误:" + ex.Message; return responeData; } if (importRows == null || importRows.Count == 0) { responeData.code = 0; responeData.message = "导入数据为空!"; return responeData; } var warehouseList = Base_WarehouseService.GetWarehouseList(projectId); var allStock = TwMaterialstockService.GetTw_MaterialStockByModle(new Tw_MaterialStockOutput { ProjectId = projectId }); var errors = new List(); var detailRows = new List(); for (int i = 0; i < importRows.Count; i++) { var row = importRows[i]; string rowWarehouseCode = CleanImportText(row.WarehouseCode); string materialCode = CleanImportText(row.MaterialCode); string heatNo = CleanImportText(row.HeatNo); string batchNo = CleanImportText(row.BatchNo); string numText = CleanImportText(row.Num); string outputDateText = CleanImportDateText(row.OutputDate); string reqUnitName = CleanImportText(row.ReqUnitName); int rowIndex = i + 2; if (string.IsNullOrEmpty(rowWarehouseCode) && string.IsNullOrEmpty(materialCode) && string.IsNullOrEmpty(heatNo) && string.IsNullOrEmpty(batchNo) && string.IsNullOrEmpty(numText) && string.IsNullOrEmpty(outputDateText) && string.IsNullOrEmpty(reqUnitName)) { continue; } if (string.IsNullOrEmpty(rowWarehouseCode)) { errors.Add("第" + rowIndex + "行,仓库,此项为必填项!"); continue; } if (!warehouseList.Any(x => x.WarehouseName == rowWarehouseCode)) { errors.Add("第" + rowIndex + "行,仓库[" + rowWarehouseCode + "]不存在!"); continue; } if (string.IsNullOrEmpty(materialCode)) { errors.Add("第" + rowIndex + "行,材料编码,此项为必填项!"); continue; } if (string.IsNullOrEmpty(heatNo)) { errors.Add("第" + rowIndex + "行,炉号,此项为必填项!"); continue; } if (string.IsNullOrEmpty(batchNo)) { errors.Add("第" + rowIndex + "行,批号,此项为必填项!"); continue; } decimal num; if (!decimal.TryParse(numText, out num) || num <= 0) { errors.Add("第" + rowIndex + "行,数量,请输入大于0的数字!"); continue; } DateTime outputDate; if (!TryParseImportDate(outputDateText, out outputDate)) { errors.Add("第" + rowIndex + "行,出库时间,格式错误!"); continue; } var reqUnit = UnitService.getUnitByUnitName(reqUnitName); if (reqUnit == null) { errors.Add("第" + rowIndex + "行,领用单位[" + reqUnitName + "]不存在!"); continue; } var stock = allStock.FirstOrDefault(x => x.WarehouseCode == rowWarehouseCode && x.Code == materialCode && x.HeatNo == heatNo && x.BatchNo == batchNo); if (stock == null) { errors.Add("第" + rowIndex + "行,仓库[" + rowWarehouseCode + "]库存中不存在此材料编码/炉号/批号-" + materialCode + "/" + heatNo + "/" + batchNo); continue; } detailRows.Add(new Tw_OutHistoryImportRow { WarehouseCode = stock.WarehouseCode, MaterialCode = stock.PipeLineMatCode, Code = stock.Code, HeatNo = stock.HeatNo, BatchNo = stock.BatchNo, MaterialUnit = stock.MaterialUnit, Num = num, OutputDate = outputDate, ReqUnitId = reqUnit.UnitId }); } if (errors.Count > 0) { responeData.code = 0; responeData.message = string.Join("|", errors.Distinct()); return responeData; } if (detailRows.Count == 0) { responeData.code = 0; responeData.message = "导入数据为空!"; return responeData; } var stockErrors = detailRows.GroupBy(x => new { x.WarehouseCode, x.MaterialCode }) .Select(x => new { x.Key.WarehouseCode, x.Key.MaterialCode, Num = x.Sum(y => y.Num), Stock = allStock.FirstOrDefault(y => y.WarehouseCode == x.Key.WarehouseCode && y.PipeLineMatCode == x.Key.MaterialCode) }) .Where(x => x.Stock == null || (x.Stock.StockNum ?? 0) < x.Num) .Select(x => "仓库[" + x.WarehouseCode + "]" + (x.Stock == null ? x.MaterialCode : x.Stock.Code + "/" + x.Stock.HeatNo + "/" + x.Stock.BatchNo) + "库存不足,库存:" + (x.Stock?.StockNum ?? 0) + ",导入数量:" + x.Num) .ToList(); if (stockErrors.Count > 0) { responeData.code = 0; responeData.message = string.Join("|", stockErrors); return responeData; } int planCount = 0; foreach (var group in detailRows.GroupBy(x => new { x.WarehouseCode, x.ReqUnitId, OutputDate = x.OutputDate.Date, Category = IsPipeSectionUnit(x.MaterialUnit) ? (int)TwConst.Category.管段 : (int)TwConst.Category.管件 })) { var planId = SQLHelper.GetNewID(); string cusBillCode = GetDataInCusBillCode(projectId, UnitService.GetUnitCodeByUnitId(group.Key.ReqUnitId), TwConst.TypeInt.其他出库.ToString()); var planMaster = new Tw_InOutPlanMaster { Id = planId, ProjectId = projectId, CusBillCode = cusBillCode, WarehouseCode = group.Key.WarehouseCode, WeldTaskId = unitWorkId, Source = 2, InOutType = (int)TwConst.InOutType.出库, TypeInt = (int)TwConst.TypeInt.其他出库, State = (int)TwConst.State.已审核, Category = group.Key.Category, CreateMan = createUserId, CreateDate = group.Key.OutputDate, ReqUnitId = group.Key.ReqUnitId, AuditMan = createUserId, AuditDate = group.Key.OutputDate, AuditMan2 = createUserId, AuditDate2 = group.Key.OutputDate, WarehouseMan = createUserId, WarehouseDate = group.Key.OutputDate, Remark = oriFileName }; Add(planMaster); var outputDetails = new List(); int sortIndex = 1; foreach (var detailGroup in group.GroupBy(x => x.MaterialCode)) { decimal num = detailGroup.Sum(x => x.Num); TwInOutplandetailService.Add(new Tw_InOutPlanDetail { Id = SQLHelper.GetNewID(), InOutPlanMasterId = planId, MaterialCode = detailGroup.Key, PlanNum = num, ActNum = num, SortIndex = sortIndex }); outputDetails.Add(new Tw_OutputDetail { MaterialCode = detailGroup.Key, PlanNum = num, ActNum = num }); sortIndex++; } TwOutputmasterService.GenOutMasterByPlanId(planId, outputDetails); planCount++; } responeData.code = 1; responeData.message = "导入成功,生成出库申请单/出库单" + planCount + "张。"; return responeData; } private static bool IsPipeSectionUnit(string materialUnit) { return CleanImportText(materialUnit) == "米"; } private static string CleanImportText(string value) { return Convert.ToString(value).Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "").Trim(); } private static string CleanImportDateText(string value) { return Convert.ToString(value).Replace("\n", "").Replace("\t", " ").Replace("\r", "").Trim(); } private static bool TryParseImportDate(string value, out DateTime date) { date = default(DateTime); if (string.IsNullOrEmpty(value)) { return false; } double oaDate; if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out oaDate) && oaDate > 0) { try { date = DateTime.FromOADate(oaDate); return true; } catch { return false; } } return DateTime.TryParse(value, CultureInfo.GetCultureInfo("zh-CN"), DateTimeStyles.None, out date) || DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out date); } public static Model.Tw_InOutPlanMaster GetById(string Id) { return Funs.DB.Tw_InOutPlanMaster.FirstOrDefault(x => x.Id == Id); } public static void Add(Model.Tw_InOutPlanMaster newtable) { Model.Tw_InOutPlanMaster table = new Model.Tw_InOutPlanMaster { Id = newtable.Id, ProjectId = newtable.ProjectId, CusBillCode = newtable.CusBillCode, WarehouseCode = newtable.WarehouseCode, Source = newtable.Source, Category = newtable.Category, InOutType = newtable.InOutType, TypeInt = newtable.TypeInt, State = newtable.State, CreateMan = newtable.CreateMan, CreateDate = newtable.CreateDate, OutputMasterId = newtable.OutputMasterId, WeldTaskId = newtable.WeldTaskId, ReqUnitId = newtable.ReqUnitId, AuditMan = newtable.AuditMan, AuditDate = newtable.AuditDate, Remark = newtable.Remark, AuditMan2 = newtable.AuditMan2, AuditDate2 = newtable.AuditDate2, WarehouseMan = newtable.WarehouseMan, WarehouseDate = newtable.WarehouseDate, }; Funs.DB.Tw_InOutPlanMaster.InsertOnSubmit(table); Funs.DB.SubmitChanges(); } public static void Update(Model.Tw_InOutPlanMaster newtable) { Model.Tw_InOutPlanMaster table = Funs.DB.Tw_InOutPlanMaster.FirstOrDefault(x => x.Id == newtable.Id); if (table != null) { table.Id = newtable.Id; table.ProjectId = newtable.ProjectId; table.CusBillCode = newtable.CusBillCode; table.WarehouseCode = newtable.WarehouseCode; table.Category = newtable.Category; table.Source = newtable.Source; table.InOutType = newtable.InOutType; table.TypeInt = newtable.TypeInt; table.State = newtable.State; table.CreateMan = newtable.CreateMan; table.CreateDate = newtable.CreateDate; table.OutputMasterId = newtable.OutputMasterId; table.WeldTaskId = newtable.WeldTaskId; table.ReqUnitId = newtable.ReqUnitId; table.AuditMan = newtable.AuditMan; table.AuditDate = newtable.AuditDate; table.Remark = newtable.Remark; table.AuditMan2 = newtable.AuditMan2; table.AuditDate2 = newtable.AuditDate2; table.WarehouseMan = newtable.WarehouseMan; table.WarehouseDate = newtable.WarehouseDate; Funs.DB.SubmitChanges(); } } public static void DeleteById(string Id) { Model.Tw_InOutPlanMaster table = Funs.DB.Tw_InOutPlanMaster.FirstOrDefault(x => x.Id == Id); if (table != null) { TwInOutplandetailService.DeleteByInOutPlanMasterId(table.Id); TwInoutplandetailRelationService.DeleteByInOutPlanMasterId(table.Id); Funs.DB.Tw_InOutPlanMaster.DeleteOnSubmit(table); Funs.DB.SubmitChanges(); } } /// /// 根据出库单生成计划单 /// /// /// /// public static string GenPlanMasterByOutputMasterId(string outputMasterId, TwConst.TypeInt typeInt) { string message = ""; #region 数据校验 //获取出库单 var outMasterQuery = new Tw_InOutMasterOutput(); outMasterQuery.Id = outputMasterId; var outMaster = TwOutputmasterService.GetListData(outMasterQuery).FirstOrDefault(); if (outMaster == null || outMaster.State != (int)TwConst.State.已完成) { message = "出库单状态错误,无法生成计划单!"; return message; } //判断是否已经生成过计划单 var queryIsExitInMaster = new Tw_InOutMasterOutput(); queryIsExitInMaster.OutputMasterId = outMaster.Id; queryIsExitInMaster.TypeInt = (int)typeInt; var IsExitInMaster = GetModle(queryIsExitInMaster).FirstOrDefault(); if (IsExitInMaster != null) { message = "该出库单已生成过计划单"; return message; } //明细校验 Tw_InOutDetailOutput query = new Tw_InOutDetailOutput(); query.OutputMasterId = outputMasterId; var details = TwOutputdetailService.GetByModle(query).ToList(); switch (typeInt) { case TwConst.TypeInt.补料出库: details = details.Where(x => x.PlanNum > x.ActNum).ToList(); foreach (var item in details) { item.PlanNum = (item.PlanNum - item.ActNum); } if (details.Count == 0) { message = "该出库单无需补料"; return message; } break; case TwConst.TypeInt.退料入库: details = details.Where(x => x.PlanNum < x.ActNum).ToList(); foreach (var item in details) { item.PlanNum = (item.ActNum - item.PlanNum); } if (details.Count == 0) { message = "该出库单无需退料"; return message; } break; } #endregion //生成计划单 var planMasterModel = new Model.Tw_InOutMasterOutput() { Id = Guid.NewGuid().ToString(), OutputMasterId = outMaster.Id, ProjectId = outMaster.ProjectId, WarehouseCode = outMaster.WarehouseCode, Source = 2, Category = outMaster.Category, TypeInt = outMaster.TypeInt, State = (int)TwConst.State.已审核, CreateMan = outMaster.CreateMan, CreateDate = DateTime.Now, ReqUnitId = outMaster.ReqUnitId, WeldTaskId = GetById(outMaster.InOutPlanMasterId).WeldTaskId }; switch (typeInt) { case TwConst.TypeInt.补料出库: planMasterModel.CusBillCode = GetCusBillCodeByTaskCode(outMaster.WeldTaskCode, TwConst.TypeInt.补料出库, (BLL.TwConst.Category)planMasterModel.Category); planMasterModel.TypeInt = (int)TwConst.TypeInt.补料出库; planMasterModel.InOutType = (int)TwConst.InOutType.出库; break; case TwConst.TypeInt.退料入库: string pre = ""; pre += DateTime.Now.ToString("yyyyMMdd"); pre += UnitService.GetUnitCodeByUnitId(outMaster.ReqUnitId); pre += "-" + UnitWorkService.getUnitWorkByUnitWorkId(outMaster.UnitWorkId)?.UnitWorkCode; planMasterModel.CusBillCode = GetCusBillCodeByTaskCode(pre, TwConst.TypeInt.退料入库, (BLL.TwConst.Category)planMasterModel.Category); planMasterModel.TypeInt = (int)TwConst.TypeInt.退料入库; planMasterModel.InOutType = (int)TwConst.InOutType.入库; ; break; } Add(planMasterModel); //生成明细 TwInOutplandetailService.AddList(details, planMasterModel.Id); return message; } /// /// 获取出库申请单编号 /// /// /// /// /// public static string GetCusBillCodeByTaskCode(string taskCode, TwConst.TypeInt typeInt, TwConst.Category category) { string cusBillCode = ""; switch (typeInt) { case TwConst.TypeInt.领料出库: if (category == TwConst.Category.管段) { cusBillCode = taskCode + "-AP-P01"; } else if (category == TwConst.Category.管件) { cusBillCode = taskCode + "-AP-PF01"; } break; case TwConst.TypeInt.补料出库: if (category == TwConst.Category.管段) { cusBillCode = taskCode + "-GI-P01-AP01"; } else if (category == TwConst.Category.管件) { cusBillCode = taskCode + "-GI-PF01-AP01"; } break; case TwConst.TypeInt.退料入库: if (category == TwConst.Category.管段) { cusBillCode = taskCode + "-GI-P01-RE01-GR01"; } else if (category == TwConst.Category.管件) { cusBillCode = taskCode + "-GI-PF01-RE01-GR01"; } break; } return cusBillCode; } public static Dictionary GetWarehouseCode(string projectId) { var q = Base_WarehouseService.GetWarehouseList(projectId).Distinct().ToDictionary(x => x.WarehouseName, x => x.WarehouseName); return q; } public static string GetDataInCusBillCode(string projectid, string unitcode, string typeString, string unitWorkCode = "", string Category = "") { if (typeString == TwConst.TypeInt.其他入库.ToString()) { //生成规则是20240919-unitcode-AP-GR01 string cusBillCode = string.Format("{0:yyyyMMdd}", DateTime.Now); var queryAll = new Tw_InOutMasterOutput() { ProjectId = projectid, CusBillCode = cusBillCode, }; var queryAllresult = GetModle(queryAll).Count(); cusBillCode = cusBillCode + "-AP-" + (queryAllresult + 1).ToString().PadLeft(2, '0'); return cusBillCode; } else if (typeString == TwConst.TypeInt.散件出库.ToString()) { //生成规则是20240919-unitcode-AP-GR01 string cusBillCode = string.Format("{0:yyyyMMdd}", DateTime.Now) + unitcode + "-" + unitWorkCode + "-"; var queryAll = new Tw_InOutMasterOutput() { ProjectId = projectid, CusBillCode = cusBillCode, Category = Category == TwConst.Category.管段.ToString() ? (int)TwConst.Category.管段 : (int)TwConst.Category.管件 }; var queryAllresult = GetModle(queryAll).Count(); if (Category == TwConst.Category.管段.ToString()) { cusBillCode = cusBillCode + (queryAllresult + 1).ToString().PadLeft(2, '0') + "-AP-P01"; } else { cusBillCode = cusBillCode + (queryAllresult + 1).ToString().PadLeft(2, '0') + "-AP-PF01"; } return cusBillCode; } else if (typeString == TwConst.TypeInt.其他出库.ToString()) { //生成规则是20240919-01 string cusBillCode = string.Format("{0:yyyyMMdd}", DateTime.Now); var queryAll = new Tw_InOutMasterOutput() { ProjectId = projectid, CusBillCode = cusBillCode, TypeInt = (int)TwConst.TypeInt.其他出库, }; var queryAllresult = GetModle(queryAll).Count(); cusBillCode = cusBillCode + "-AP-" + (queryAllresult + 1).ToString().PadLeft(2, '0'); return cusBillCode; } else { //生成规则是20240919-unitcode-AP-GR01 string cusBillCode = string.Format("{0:yyyyMMdd}", DateTime.Now) + "-" + unitcode + "-AP-GR"; var queryAll = new Tw_InOutMasterOutput() { ProjectId = projectid, CusBillCode = cusBillCode, }; var queryAllresult = GetModle(queryAll).Count(); cusBillCode = cusBillCode + (queryAllresult + 1).ToString().PadLeft(2, '0'); return cusBillCode; } } /// /// 根据任务单生成出库计划单 /// /// /// /// /// public static void GenOutPlanmasterByWeldTaskId(string unitworkid, string unitid, DateTime date, string serialNumber, string Personid) { Model.SGGLDB db = Funs.DB; Model.View_HJGL_WeldingTask weldTask = db.View_HJGL_WeldingTask.FirstOrDefault(e => e.UnitWorkId == unitworkid && e.UnitId == unitid && e.TaskDate.Value.Date == date.Date && e.SerialNumber == serialNumber); if (weldTask == null) { return; } string WeldTaskId = unitworkid + "|" + unitid + "|" + string.Format("{0:yyyyMMdd}", date) + "|" + serialNumber; //判断是否已经生成过出库计划单 var queryIsExitInMaster = new Tw_InOutMasterOutput(); queryIsExitInMaster.WeldTaskId = WeldTaskId; queryIsExitInMaster.InOutType = (int)TwConst.InOutType.出库; queryIsExitInMaster.TypeInt = (int)TwConst.TypeInt.领料出库; var IsExitInMaster = TwInOutplanmasterService.GetModle(queryIsExitInMaster).FirstOrDefault(); if (IsExitInMaster != null) { return; } var pipelineList = db.View_HJGL_WeldingTask.Where(e => e.UnitWorkId == unitworkid && e.UnitId == unitid && e.TaskDate.Value.Date == date.Date && e.SerialNumber == serialNumber).OrderBy(x => x.PipeLineSortIndex).Select(x => x.PipelineId).Distinct().ToList(); var taskWeldJointIds = db.View_HJGL_WeldingTask .Where(e => e.UnitWorkId == unitworkid && e.UnitId == unitid && e.TaskDate.Value.Date == date.Date && e.SerialNumber == serialNumber && e.WeldJointId != null) .Select(e => e.WeldJointId) .Distinct() .ToList(); //领料出库需要排除散件材料 var allMaterDatial = (from x in db.HJGL_PipeLineMat join y in db.HJGL_MaterialCodeLib on x.MaterialCode equals y.MaterialCode where pipelineList.Contains(x.PipelineId) && taskWeldJointIds.Contains(x.WeldJointId) && x.PrefabricatedComponents != null select new { x.PipelineId, x.PrefabricatedComponents, x.WeldJointId, x.MaterialCode, x.Number, y.MaterialUnit, }).ToList(); var MaterDatial = allMaterDatial; //var outMateriaList = from x in db.HJGL_PipeLineMat // join y in db.HJGL_MaterialCodeLib on x.MaterialCode equals y.MaterialCode // where pipelineList.Contains(x.PipelineId) // group x by new { x.MaterialCode, y.MaterialUnit } // into g // select new // { // g.Key.MaterialCode, // planNum = g.Sum(x => x.Number) ?? 0, // MaterialName = g.Key.MaterialUnit, // }; var outMateriaList = from x in MaterDatial group x by new { x.MaterialCode, x.MaterialUnit } into g select new { g.Key.MaterialCode, planNum = g.Sum(x => x.Number) ?? 0, MaterialName = g.Key.MaterialUnit, }; var outPlanDetailListPiece = outMateriaList.Where(x => x.MaterialName.Contains("个"));//管件 var outPlanDetailListOthere = outMateriaList.Where(x => x.MaterialName.Contains("米"));//管段 var weldTaskCode = WeldTaskService.GetWeldTaskById(weldTask.WeldTaskId)?.TaskCode; if (outPlanDetailListPiece.Any()) { Model.Tw_InOutPlanMaster table = new Model.Tw_InOutPlanMaster { Id = Guid.NewGuid().ToString(), ProjectId = weldTask.ProjectId, // CusBillCode = string.Format("{0:yyyyMMdd}", DateTime.Now) + UnitService.GetUnitCodeByUnitId(weldTask.UnitId) + "-" + UnitWorkService.getUnitWorkByUnitWorkId(weldTask.UnitWorkId)?.UnitWorkCode + "AP-PF01", CusBillCode = TwInOutplanmasterService.GetCusBillCodeByTaskCode(weldTaskCode, TwConst.TypeInt.领料出库, TwConst.Category.管件), WarehouseCode = BLL.Base_WarehouseService.GetWarehouseByWarehouseId(PipelineService.GetPipelineByPipelineId(weldTask.PipelineId).WarehouseId).WarehouseName, Source = 1, InOutType = (int)TwConst.InOutType.出库, TypeInt = (int)TwConst.TypeInt.领料出库, State = (int)TwConst.State.待审核, Category = (int)TwConst.Category.管件, CreateMan = Personid, CreateDate = DateTime.Now, WeldTaskId = WeldTaskId, ReqUnitId = weldTask.UnitId, }; TwInOutplanmasterService.Add(table); foreach (var item in outPlanDetailListPiece) { Model.Tw_InOutPlanDetail detail = new Model.Tw_InOutPlanDetail { Id = Guid.NewGuid().ToString(), InOutPlanMasterId = table.Id, MaterialCode = item.MaterialCode, PlanNum = item.planNum, }; TwInOutplandetailService.Add(detail); } var twinoutplandetailRelationList = MaterDatial.Where(x => x.MaterialUnit.Contains("个")).Select(x => new Tw_InOutPlanDetail_Relation { PipelineId = x.PipelineId, MaterialCode = x.MaterialCode, Number = x.Number, PrefabricatedComponents = x.PrefabricatedComponents, WeldJointId = x.WeldJointId, }).ToList(); TwInoutplandetailRelationService.AddList(twinoutplandetailRelationList, table.Id); } if (outPlanDetailListOthere.Any()) { Model.Tw_InOutPlanMaster table = new Model.Tw_InOutPlanMaster { Id = Guid.NewGuid().ToString(), ProjectId = weldTask.ProjectId, CusBillCode = TwInOutplanmasterService.GetCusBillCodeByTaskCode(weldTaskCode, TwConst.TypeInt.领料出库, TwConst.Category.管段), WarehouseCode = BLL.Base_WarehouseService.GetWarehouseByWarehouseId(PipelineService.GetPipelineByPipelineId(weldTask.PipelineId).WarehouseId).WarehouseName, Source = 1, InOutType = (int)TwConst.InOutType.出库, TypeInt = (int)TwConst.TypeInt.领料出库, State = (int)TwConst.State.待审核, Category = (int)TwConst.Category.管段, CreateMan = Personid, CreateDate = DateTime.Now, WeldTaskId = WeldTaskId, ReqUnitId = weldTask.UnitId, }; TwInOutplanmasterService.Add(table); foreach (var item in outPlanDetailListOthere) { Model.Tw_InOutPlanDetail detail = new Model.Tw_InOutPlanDetail { Id = Guid.NewGuid().ToString(), InOutPlanMasterId = table.Id, MaterialCode = item.MaterialCode, PlanNum = item.planNum, }; TwInOutplandetailService.Add(detail); } var twinoutplandetailRelationList = MaterDatial.Where(x => x.MaterialUnit.Contains("米")).Select(x => new Tw_InOutPlanDetail_Relation { PipelineId = x.PipelineId, MaterialCode = x.MaterialCode, Number = x.Number, PrefabricatedComponents = x.PrefabricatedComponents, WeldJointId = x.WeldJointId, }).ToList(); TwInoutplandetailRelationService.AddList(twinoutplandetailRelationList, table.Id); } } } }