using FastReport.DevComponents.DotNetBar; using FineUIPro; using Model; using System; using System.Collections.Generic; using System.Data; using System.Linq; using WIA; namespace BLL { /// /// 包装管理服务类 /// public static class HJGLPackagingmanageService { #region Fields /// /// 包装分类映射字典 /// public static Dictionary CategoryIntMap = new Dictionary { { "打捆" ,(int)CategoryInt.打捆}, { "装箱" ,(int)CategoryInt.装箱}, { "散装" ,(int)CategoryInt.散装}, }; /// /// 未到场 /// public static int state_0 = 0; /// /// 已发货 /// public static int state_1 = 1; /// /// 已到场 /// public static int state_2 = 2; public static Dictionary TypeIntMap = new Dictionary { { "预制组件" ,(int)TypeInt.预制组件}, { "预制散件" ,(int)TypeInt.预制散件}, { "其他材料" ,(int)TypeInt.其他材料}, }; #endregion Fields #region Enums public enum CategoryInt : int { 打捆 = 10, 装箱 = 20, 散装 = 30, } public enum TypeInt : int { 预制组件 = 10, 预制散件 = 20, 其他材料 = 30, } #endregion Enums #region Methods /// /// 新增实体 /// /// public static void AddHJGL_PackagingManage(Model.HJGL_PackagingManage newtable) { Model.HJGL_PackagingManage table = new Model.HJGL_PackagingManage { PackagingManageId = newtable.PackagingManageId, PackagingCode = newtable.PackagingCode, ProjectId = newtable.ProjectId, PipelineComponentId = newtable.PipelineComponentId, StackingPosition = newtable.StackingPosition, State = newtable.State, ContactName = newtable.ContactName, ContactPhone = newtable.ContactPhone, Remark = newtable.Remark, ReceiveDate = newtable.ReceiveDate, ReceiveMan = newtable.ReceiveMan, TrainNumber = newtable.TrainNumber, TrainNumberId = newtable.TrainNumberId, TypeInt = newtable.TypeInt, CategoryInt = newtable.CategoryInt, CompileMan = newtable.CompileMan, CompileDate = newtable.CompileDate }; var db1 = Funs.DB; db1.HJGL_PackagingManage.InsertOnSubmit(table); db1.SubmitChanges(); } /// /// 新增或更新包装信息(Id为空则新增,否则更新),返回创建或更新后的Id。 /// /// 包装信息实体,包含包装编号、项目ID、状态等信息 /// 创建或更新后的包装管理ID /// 当model为null时抛出异常 public static string AddOrUpdatePackaging(Model.HJGL_PackagingManage model) { if (model == null) throw new ArgumentNullException(nameof(model)); if (string.IsNullOrEmpty(model.PackagingManageId)) { // 新增 model.PackagingManageId = SQLHelper.GetNewID(typeof(Model.HJGL_PackagingManage)); AddHJGL_PackagingManage(model); return model.PackagingManageId; } else { // 修改 var exist = GetHJGL_PackagingManageById(model.PackagingManageId); if (exist == null) { AddHJGL_PackagingManage(model); return model.PackagingManageId; } else { UpdateHJGL_PackagingManage(model); return model.PackagingManageId; } } } /// /// 添加包装与预制组件关联关系 /// /// 包装管理ID /// 管道组件ID /// 当该预制组件已被包装时抛出异常 public static void AddPipelineComponentToPackaging(string packagingManageId, string pipelineComponentId) { var ComponentModel = BLL.HJGL_PipelineComponentService.GetPipelineComponentById(pipelineComponentId); if (ComponentModel != null) { var model = new Model.HJGL_PackagingManageDetail() { Id = SQLHelper.GetNewID(), PackagingManageId = packagingManageId, PipelineId = ComponentModel.PipelineId, PipelineComponentId = pipelineComponentId, CreateTime = DateTime.Now, CreateUser = null, }; HJGLPackagingmanagedetailService.Add(model); } } /// /// 根据ID删除包装管理记录 /// /// 包装管理ID public static void DeleteHJGL_PackagingManageById(string PackagingManageId) { var db1 = Funs.DB; Model.HJGL_PackagingManage table = db1.HJGL_PackagingManage.FirstOrDefault(x => x.PackagingManageId == PackagingManageId); if (table != null) { db1.HJGL_PackagingManage.DeleteOnSubmit(table); db1.SubmitChanges(); } } /// /// 删除包装与预制组件的关联关系 /// /// 管道组件ID public static void DeletePipelineComponentFromPackaging(string pipelineComponentId) { HJGLPackagingmanagedetailService.DeleteByPipelineComponentId(pipelineComponentId); } /// /// 根据分类整数值获取分类字符串描述 /// /// 分类整数值(10:打捆, 20:装箱, 30:散装) /// 分类字符串描述,如果未找到则返回空字符串 public static string GetCategoryString(int? CategoryInt) { return CategoryIntMap.FirstOrDefault(c => c.Value == CategoryInt).Key; } /// /// 根据包装管理ID获取包装信息实体 /// /// 包装管理ID /// 包装信息实体,如果未找到则返回null public static Model.HJGL_PackagingManage GetHJGL_PackagingManageById(string PackagingManageId) { using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { return db.HJGL_PackagingManage.FirstOrDefault(x => x.PackagingManageId == PackagingManageId); } } /// /// 获取包装中所有组件的最早计划开始日期 /// /// 包装管理ID /// 最早计划开始日期字符串,如果没有组件则返回空字符串 public static string GetMinPlanStartDate(string PackagingManageId) { string PlanStartDate = ""; var tb = GetPackagingDetailById(PackagingManageId); if (tb == null || tb.Count == 0) { return PlanStartDate; } PlanStartDate = tb.OrderBy(x => x.PlanStartDate).FirstOrDefault()?.PlanStartDate.ToString(); return PlanStartDate; } /// /// 根据项目ID生成新的包装编号 /// /// 项目ID /// 新包装编号,格式为:项目编号-日期-序号(如:PROJ20241029-001) public static string GetNewPackagingCode(string projectid) { string code = ProjectService.GetProjectCodeByProjectId(projectid) + "-" + string.Format("{0:yyyyMMdd}", DateTime.Now) + "-"; var q = GetPackagingManageList(projectid, code); if (q.Total > 0) { code = code + (q.Total + 1).ToString().PadLeft(3, '0'); } else { code = code + "001"; } return code; } /// /// 根据项目号获取包装编号历史记录 /// /// 项目ID /// 该项目的所有包装编号列表(去重) public static List GetPackagingCode(string projectid) { Model.SGGLDB db = Funs.DB; var q = (from x in db.HJGL_PackagingManage where x.ProjectId.Contains(projectid) select x.PackagingCode).Distinct().ToList(); return q; } /// /// 根据主键获取装箱明细,用于打印输出 /// /// 包装管理ID /// 包装打印输出列表,包含组件信息、单位工程、计划开始日期等 /// /// 根据包装类型返回不同的数据: /// - 预制组件:从HJGL_PackagingManageDetail表获取关联的组件ID,查询详细的组件信息 /// - 预制散件/其他材料:从HJGL_PackagingManageDetail表获取散装材料信息 /// public static List GetPackagingDetailById(string PackagingManageId) { var model = GetHJGL_PackagingManageById(PackagingManageId); var result = new List(); if (model.TypeInt == (int)HJGLPackagingmanageService.TypeInt.预制组件) { // 从子表HJGL_PackagingManageDetail中获取关联的组件ID var PipelineComponentIdList = (from x in Funs.DB.HJGL_PackagingManageDetail where x.PackagingManageId == PackagingManageId select x.PipelineComponentId).ToList(); if (PipelineComponentIdList == null || PipelineComponentIdList.Count == 0) return new List(); var query = from com in Funs.DB.HJGL_Pipeline_Component join pipe in Funs.DB.HJGL_Pipeline on com.PipelineId equals pipe.PipelineId into pipeGroup from pipe in pipeGroup.DefaultIfEmpty() join unitwork in Funs.DB.WBS_UnitWork on pipe.UnitWorkId equals unitwork.UnitWorkId into unitworkGroup from unitwork in unitworkGroup.DefaultIfEmpty() where PipelineComponentIdList.Contains(com.PipelineComponentId) orderby com.PipelineComponentCode select new Model.PackagingManagePrintOutput { PipelineComponentId = com.PipelineComponentId, PipelineComponentCode = com.PipelineComponentCode, PlanStartDate = pipe != null && pipe.PlanStartDate != null ? pipe.PlanStartDate : DateTime.Now, UnitWorkName = unitwork != null ? unitwork.UnitWorkName : "", num = "1", CU = "个", //FlowingSection = pipe != null ? pipe.FlowingSection : "" FlowingSection = model.StackingPosition }; result = query.ToList(); } else { var query = HJGLPackagingmanagedetailService.GetPackagingData(PackagingManageId).ToList(); var detailList = from x in query select new Model.PackagingManagePrintOutput { PipelineComponentId = x.Id, PipelineComponentCode = x.MaterialCode, num = x.Number.ToString(), CU = x.MaterialUnit, UnitWorkName = UnitWorkService.GetNameById( TwOutputdetailService.GetInOutMasterById(x.TwOutputDetailId)?.UnitWorkId ), FlowingSection = model.StackingPosition, }; result = detailList.ToList(); } return result; } /// /// 根据ID获取包装信息(包含权限验证) /// /// 项目ID,用于权限验证 /// 人员ID,用于权限验证 /// 包装管理ID /// 包含包装详情、组件明细和权限信息的包装管理项 /// /// 该方法会: /// 1. 查询包装基本信息及关联的项目、接收人信息 /// 2. 从子表HJGL_PackagingManageDetail中获取关联的组件ID列表 /// 3. 根据组件ID查询详细的组件信息包括管道、单位工程等 /// 4. 验证用户是否有操作权限(总包单位或质检工程师) /// public static Model.PackagingManageItem GetPackagingInformationById(string projectId, string personId, string packagingManageId) { using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { PackagingManageItem packagingManageItem = new PackagingManageItem(); List packagingPrepipeItem = new List(); var q = (from x in db.HJGL_PackagingManage join n in db.Base_Project on x.ProjectId equals n.ProjectId join m in db.Person_Persons on x.ReceiveMan equals m.PersonId into tt from t in tt.DefaultIfEmpty() where x.PackagingManageId == packagingManageId select new PackagingManageDetailItem { PackagingManageId = x.PackagingManageId, PackagingCode = x.PackagingCode, ProjectName = n.ProjectName, ProjectId = x.ProjectId, ContactName = x.ContactName, ContactPhone = x.ContactPhone, StackingPosition = x.StackingPosition, State = x.State, ReceiveMan = t.PersonName, ReceiveDate = string.Format("{0:g}", x.ReceiveDate), TrainNumber = x.TrainNumber, }).FirstOrDefault(); // 从子表HJGL_PackagingManageDetail中根据PackagingManageId获取关联的组件ID var PipelineComponentIdList = (from x in db.HJGL_PackagingManageDetail where x.PackagingManageId == packagingManageId select x.PipelineComponentId).ToList(); if (PipelineComponentIdList != null && PipelineComponentIdList.Count() > 0) { packagingPrepipeItem = (from x in db.HJGL_Pipeline_Component join y in db.HJGL_Pipeline on x.PipelineId equals y.PipelineId join z in db.WBS_UnitWork on y.UnitWorkId equals z.UnitWorkId where PipelineComponentIdList.Contains(x.PipelineComponentId) select new PackagingPrepipeItem { PipelineComponentId = x.PipelineComponentId, PipelineComponentCode = x.PipelineComponentCode, PreUnit = "1/个", UnitWorkName = z.UnitWorkName, PlanStartDate = string.Format("{0:g}", y.PlanStartDate), }).ToList(); } bool isPower = Person_PersonsService.IsGeneralUnitByPersonId(personId, projectId); if (!isPower) { var roleList = Person_PersonsService.GetRoleListByProjectIdPersonId(projectId, personId); if (roleList.Contains(Const.CQEngineer)) { isPower = true; } } packagingManageItem.packagingManageDetailItem = q; packagingManageItem.packagingPrepipeItems = packagingPrepipeItem; packagingManageItem.isPower = isPower; return packagingManageItem; } } /// /// 根据车次ID获取包装管理列表 /// /// 车次ID /// 该车次关联的所有包装管理记录 public static List GetPackagingManage(string trainNumberId) { Model.SGGLDB db = Funs.DB; var q = (from x in db.HJGL_PackagingManage where x.TrainNumberId == trainNumberId select x).Distinct().ToList(); return q; } public static (List Data, int Total) GetPackagingManageList(string projectId, string PackagingCode, int pageIndex = 0, int pageSize = 20) { using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { var baseQuery = (from x in db.HJGL_PackagingManage join n in db.Base_Project on x.ProjectId equals n.ProjectId join m in db.Person_Persons on x.ReceiveMan equals m.PersonId into tt from t in tt.DefaultIfEmpty() join train in db.HJGL_TrainNumberManage on x.TrainNumberId equals train.Id into trains from train in trains.DefaultIfEmpty() where x.ProjectId == projectId && (string.IsNullOrEmpty(PackagingCode) || x.PackagingCode.Contains(PackagingCode)) select new PackagingManageOutput { PackagingManageId = x.PackagingManageId, PackagingCode = x.PackagingCode, ProjectName = n.ProjectName, ContactName = train.ContactName, ContactPhone = train.ContactPhone, DriverName = train.DriverName, DriverPhone = train.DriverPhone, LicensePlateNumber = train.LicensePlateNumber, StackingPosition = x.StackingPosition, State = x.State, TypeInt = x.TypeInt, CategoryInt = x.CategoryInt, TypeString = GetTypeString(x.TypeInt), CategoryString = GetCategoryString(x.CategoryInt), ReceiveMan = train.ContactName,//t.PersonName, ReceiveDate = x.ReceiveDate.HasValue ? string.Format("{0:g}", x.ReceiveDate) : "", PlanStartDate = GetMinPlanStartDate(x.PackagingManageId), TrainNumberOld = x.TrainNumber, TrainNumber = train.TrainNumber, Code = x.PackagingCode.Substring(0, x.PackagingCode.LastIndexOf("-")).Substring(x.PackagingCode.Substring(0, x.PackagingCode.LastIndexOf("-")).LastIndexOf("-") + 1), }).Distinct(); var pagedData = baseQuery .OrderByDescending(x => x.Code) .Skip((pageIndex) * pageSize) .Take(pageSize) .ToList(); // 获取总记录数(延迟计数优化) var totalCount = baseQuery.Count(); return (pagedData, totalCount); } } /// /// 获取包装管理列表(带过滤条件) /// /// 过滤条件 /// 页码 /// 页大小 /// 总记录数 /// 包装管理列表 public static List GetPackagingManageList(PackagingManageInput filter, int pageIndex, int pageSize, out int totalCount) { using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { // base join query to include project and receive person var baseQuery = from x in db.HJGL_PackagingManage join n in db.Base_Project on x.ProjectId equals n.ProjectId join m in db.Person_Persons on x.ReceiveMan equals m.PersonId into tt from t in tt.DefaultIfEmpty() join train in db.HJGL_TrainNumberManage on x.TrainNumberId equals train.Id into trains from train in trains.DefaultIfEmpty() select new { x, n, t , train }; if (filter != null) { if (!string.IsNullOrEmpty(filter.PackagingManageId)) { baseQuery = baseQuery.Where(z => z.x.PackagingManageId == filter.PackagingManageId); } if (!string.IsNullOrEmpty(filter.PackagingCode)) { baseQuery = baseQuery.Where(z => z.x.PackagingCode.Contains(filter.PackagingCode)); } if (!string.IsNullOrEmpty(filter.ProjectId)) { baseQuery = baseQuery.Where(z => z.x.ProjectId == filter.ProjectId); } if (!string.IsNullOrEmpty(filter.ProjectName)) { baseQuery = baseQuery.Where(z => z.n.ProjectName.Contains(filter.ProjectName)); } if (!string.IsNullOrEmpty(filter.ContactName)) { baseQuery = baseQuery.Where(z => z.x.ContactName.Contains(filter.ContactName)); } if (!string.IsNullOrEmpty(filter.ContactPhone)) { baseQuery = baseQuery.Where(z => z.x.ContactPhone.Contains(filter.ContactPhone)); } if (!string.IsNullOrEmpty(filter.StackingPosition)) { baseQuery = baseQuery.Where(z => z.x.StackingPosition.Contains(filter.StackingPosition)); } if (filter.State != null) { baseQuery = baseQuery.Where(z => z.x.State == filter.State); } if (!string.IsNullOrEmpty(filter.ReceiveMan)) { baseQuery = baseQuery.Where(z => z.train.ContactName == filter.ReceiveMan || (z.train != null && z.train.ContactName.Contains(filter.ReceiveMan))); } if (!string.IsNullOrEmpty(filter.ReceiveDate)) { DateTime dt; if (DateTime.TryParse(filter.ReceiveDate, out dt)) { var start = dt.Date; var end = start.AddDays(1); baseQuery = baseQuery.Where(z => z.x.ReceiveDate != null && z.x.ReceiveDate >= start && z.x.ReceiveDate < end); } } if (!string.IsNullOrEmpty(filter.TrainNumberId)) { baseQuery = baseQuery.Where(z => z.train.Id != null && z.train.Id.Contains(filter.TrainNumberId)); } // 是否关联车次筛选 if (filter.HasTrainNumber.HasValue) { if (filter.HasTrainNumber.Value) { // 已关联车次:TrainNumberId 不为空 baseQuery = baseQuery.Where(z => z.x.TrainNumberId != null && z.x.TrainNumberId != ""); } else { // 未关联车次:TrainNumberId 为空 baseQuery = baseQuery.Where(z => z.x.TrainNumberId == null || z.x.TrainNumberId == ""); } } } baseQuery = baseQuery.OrderByDescending(z => (z.x.ReceiveDate ?? DateTime.MinValue)).ThenBy(z => z.x.PackagingCode); var q = (from z in baseQuery select new PackagingManageDetailItem { PackagingManageId = z.x.PackagingManageId, PackagingCode = z.x.PackagingCode, ProjectName = z.n.ProjectName, ContactName = z.x.ContactName, ContactPhone = z.x.ContactPhone, StackingPosition = z.x.StackingPosition, State = z.x.State, ReceiveMan = z.train.ContactName, ReceiveDate = string.Format("{0:g}", z.x.ReceiveDate), TrainNumber = z.train.TrainNumber, ComponentCount = db.HJGL_PackagingManageDetail.Count(d => d.PackagingManageId == z.x.PackagingManageId), CategoryInt = z.x.CategoryInt, CategoryString = z.x.CategoryInt == 10 ? "打捆" : (z.x.CategoryInt == 20 ? "装箱" : (z.x.CategoryInt == 30 ? "散装" : "")) }).Distinct(); totalCount = q.Count(); if (pageIndex <= 0) pageIndex = 1; if (pageSize <= 0) pageSize = 20; return q.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } } /// /// 包装到场验收,将包装状态更新为已到场并记录验收人和验收时间 /// /// 包装管理ID /// 验收人员ID /// /// 该方法会将包装状态更新为已到场(state_2),并设置接收人和接收时间 /// public static void GetPackingInfoConfirmArrival(string packagingManageId, string PersonId) { var q = GetHJGL_PackagingManageById(packagingManageId); if (q != null) { q.State = state_2; q.ReceiveMan = PersonId; q.ReceiveDate = DateTime.Now; UpdateHJGL_PackagingManage(q); } } /// /// 保存包装信息(组件明细),将组件关联到包装并更新明细表 /// /// 包装管理ID /// 组件ID集合,多个ID用逗号分隔 /// /// 该方法会: /// 1. 获取现有的包装明细记录中的组件ID /// 2. 合并新旧组件ID(去重) /// 3. 根据组件ID查询对应的管道信息 /// 4. 删除原有的包装明细记录 /// 5. 批量插入新的包装明细记录到HJGL_PackagingManageDetail表 /// public static void getSavePackagingInformationById(string packagingManageId, string PipelineComponentIds) { using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { // 获取现有的组件ID列表 var existingComponentIds = (from x in db.HJGL_PackagingManageDetail where x.PackagingManageId == packagingManageId select x.PipelineComponentId).ToList(); // 合并新旧组件ID(去重) HashSet allComponentIds = new HashSet(existingComponentIds); if (!string.IsNullOrEmpty(PipelineComponentIds)) { string[] newIds = PipelineComponentIds.Split(','); foreach (var id in newIds) { if (!string.IsNullOrEmpty(id)) { allComponentIds.Add(id); } } } // 创建新的包装明细记录 var newDetailList = new List(); foreach (var item in allComponentIds) { var ComponentModel = BLL.HJGL_PipelineComponentService.GetPipelineComponentById(item); if (ComponentModel != null) { var model = new Model.HJGL_PackagingManageDetail() { Id = SQLHelper.GetNewID(), PackagingManageId = packagingManageId, PipelineId = ComponentModel.PipelineId, PipelineComponentId = item, CreateTime = DateTime.Now, }; newDetailList.Add(model); } } // 更新数据库:先删除旧记录,再插入新记录 HJGLPackagingmanagedetailService.DeleteByPackagingManageId(packagingManageId); if (newDetailList.Count > 0) { HJGLPackagingmanagedetailService.AddBulk(newDetailList); } // 可选:同时更新主表的PipelineComponentId字段以保持兼容性 var table = db.HJGL_PackagingManage.FirstOrDefault(x => x.PackagingManageId == packagingManageId); if (table != null) { table.PipelineComponentId = string.Join(",", allComponentIds); UpdateHJGL_PackagingManage(table); } } } /// /// 获取包装状态下拉框选项 /// /// 包装状态列表,包含状态文本和值的映射关系 public static ListItem[] GetState() { ListItem[] list = new ListItem[3]; list[0] = new ListItem("预出库", state_0.ToString()); list[1] = new ListItem("已出库", state_1.ToString()); list[2] = new ListItem("已到场", state_2.ToString()); return list; } /// /// 根据类型整数值获取类型字符串描述 /// /// 类型整数值(10:预制组件, 20:预制散件, 30:其他材料) /// 类型字符串描述,如果未找到则返回空字符串 public static string GetTypeString(int? TypeInt) { return TypeIntMap.FirstOrDefault(c => c.Value == TypeInt).Key; } /// /// 初始化包装编号下拉框 /// /// 下拉框控件名称 /// 项目ID /// 是否显示"请选择"选项 public static void InitPipelineDownList(FineUIPro.DropDownList dropName, string projectid, bool isShowPlease) { dropName.DataValueField = "string"; dropName.DataTextField = "string"; dropName.DataSource = GetPackagingCode(projectid); dropName.DataBind(); if (isShowPlease) { Funs.FineUIPleaseSelect(dropName); } } /// /// 形成出库单,将包装状态从未出库更新为已出库 /// /// 包装管理ID /// 仅当包装状态为预出库(state_0)时才允许更新为已出库(state_1) public static void PutOutOrder(string PackagingManageId) { var model = GetHJGL_PackagingManageById(PackagingManageId); if (model.State == state_0) { model.State = state_1; UpdateHJGL_PackagingManage(model); } } /// /// 更新包装管理信息 /// /// 包含更新后信息的包装管理实体 public static void UpdateHJGL_PackagingManage(Model.HJGL_PackagingManage newtable) { var db1 = Funs.DB; Model.HJGL_PackagingManage table = db1.HJGL_PackagingManage.FirstOrDefault(x => x.PackagingManageId == newtable.PackagingManageId); if (table != null) { table.PackagingManageId = newtable.PackagingManageId; table.PackagingCode = newtable.PackagingCode; table.ProjectId = newtable.ProjectId; table.PipelineComponentId = newtable.PipelineComponentId; table.StackingPosition = newtable.StackingPosition; table.State = newtable.State; table.ContactName = newtable.ContactName; table.ContactPhone = newtable.ContactPhone; table.Remark = newtable.Remark; table.ReceiveMan = newtable.ReceiveMan; table.ReceiveDate = newtable.ReceiveDate; table.TrainNumber = newtable.TrainNumber; table.TrainNumberId = newtable.TrainNumberId; table.TypeInt = newtable.TypeInt; table.CategoryInt = newtable.CategoryInt; db1.SubmitChanges(); } } #endregion Methods } }