using System; using System.Collections.Generic; using System.Data; using System.Linq; namespace BLL { public class WorkPackageService { /// /// 添加分部分项工程 /// /// public static void AddWorkPackage(Model.WBS_WorkPackage WorkPackage) { Model.SGGLDB db = Funs.DB; Model.WBS_WorkPackage newWorkPackage = new Model.WBS_WorkPackage(); newWorkPackage.WorkPackageId = WorkPackage.WorkPackageId; newWorkPackage.WorkPackageCode = WorkPackage.WorkPackageCode; newWorkPackage.UnitWorkId = WorkPackage.UnitWorkId; newWorkPackage.SuperWorkPack = WorkPackage.SuperWorkPack; newWorkPackage.PackageCode = WorkPackage.PackageCode; newWorkPackage.SuperWorkPackageId = WorkPackage.SuperWorkPackageId; newWorkPackage.PackageContent = WorkPackage.PackageContent; newWorkPackage.ProjectId = WorkPackage.ProjectId; newWorkPackage.IsChild = WorkPackage.IsChild; newWorkPackage.SortIndex = WorkPackage.SortIndex; newWorkPackage.InitWorkPackageCode = WorkPackage.InitWorkPackageCode; newWorkPackage.Weights = WorkPackage.Weights; newWorkPackage.ProjectType = WorkPackage.ProjectType; newWorkPackage.IsApprove = WorkPackage.IsApprove; newWorkPackage.Costs = WorkPackage.Costs; newWorkPackage.SubItemType = WorkPackage.SubItemType; newWorkPackage.PreJobId = WorkPackage.PreJobId; newWorkPackage.PlanProjectQuantity = WorkPackage.PlanProjectQuantity; newWorkPackage.RealProjectQuantity = WorkPackage.RealProjectQuantity; newWorkPackage.PlanStartDate = WorkPackage.PlanStartDate; newWorkPackage.PlanEndDate = WorkPackage.PlanEndDate; newWorkPackage.RealStartDate = WorkPackage.RealStartDate; newWorkPackage.RealEndDate = WorkPackage.RealEndDate; newWorkPackage.Unit = WorkPackage.Unit; db.WBS_WorkPackage.InsertOnSubmit(newWorkPackage); db.SubmitChanges(); } /// /// 修改分部分项工程 /// /// public static void UpdateWorkPackage(Model.WBS_WorkPackage WorkPackage) { Model.SGGLDB db = Funs.DB; Model.WBS_WorkPackage newWorkPackage = db.WBS_WorkPackage.First(e => e.WorkPackageId == WorkPackage.WorkPackageId); newWorkPackage.WorkPackageCode = WorkPackage.WorkPackageCode; newWorkPackage.UnitWorkId = WorkPackage.UnitWorkId; newWorkPackage.PackageCode = WorkPackage.PackageCode; newWorkPackage.PackageContent = WorkPackage.PackageContent; newWorkPackage.ProjectId = WorkPackage.ProjectId; newWorkPackage.IsChild = WorkPackage.IsChild; newWorkPackage.SortIndex = WorkPackage.SortIndex; newWorkPackage.Weights = WorkPackage.Weights; newWorkPackage.IsApprove = WorkPackage.IsApprove; newWorkPackage.Costs = WorkPackage.Costs; newWorkPackage.SubItemType = WorkPackage.SubItemType; newWorkPackage.PreJobId = WorkPackage.PreJobId; newWorkPackage.PlanProjectQuantity = WorkPackage.PlanProjectQuantity; newWorkPackage.RealProjectQuantity = WorkPackage.RealProjectQuantity; newWorkPackage.PlanStartDate = WorkPackage.PlanStartDate; newWorkPackage.PlanEndDate = WorkPackage.PlanEndDate; newWorkPackage.RealStartDate = WorkPackage.RealStartDate; newWorkPackage.RealEndDate = WorkPackage.RealEndDate; newWorkPackage.Unit = WorkPackage.Unit; newWorkPackage.IsMileStone = WorkPackage.IsMileStone; newWorkPackage.PlanCost = WorkPackage.PlanCost; newWorkPackage.JDWeights = WorkPackage.JDWeights; newWorkPackage.PreWorkCode = WorkPackage.PreWorkCode; db.SubmitChanges(); } /// /// 根据单位工程id获取一个第一级分部信息 /// /// /// public static Model.WBS_WorkPackage GetWorkPackages1ByUnitWorkId(string unitWorkId) { return Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.SuperWorkPackageId == null && x.IsApprove == true && x.UnitWorkId == unitWorkId); } /// /// 根据单位工程Id获取第一级所有分部信息 /// /// /// public static List GetWorkPackages1sByUnitWorkId(string unitWorkId) { return (from x in Funs.DB.WBS_WorkPackage where x.SuperWorkPackageId == null && x.UnitWorkId.ToString() == unitWorkId orderby x.WorkPackageCode select x).ToList(); } /// /// 根据单位工程Id获取第一级审批所有分部信息 /// /// /// public static List GetApproveWorkPackages1sByUnitWorkId(string unitWorkId) { return (from x in Funs.DB.WBS_WorkPackage where x.SuperWorkPackageId == null && x.UnitWorkId.ToString() == unitWorkId && x.IsApprove == true orderby x.WorkPackageCode select x).ToList(); } /// /// 根据分部分项名称和单位工程id获取项目分部分项内容 /// /// /// /// public static Model.WBS_WorkPackage GetWorkPackageByPackageContent(string packageContent, string unitWorkId) { return Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.PackageContent == packageContent && x.UnitWorkId == unitWorkId); } /// /// 根据单位工程Id获取所有分部信息 /// /// /// public static List GetAllWorkPackagesByUnitWorkId(string unitWorkId) { return (from x in Funs.DB.WBS_WorkPackage where x.UnitWorkId.ToString() == unitWorkId orderby x.WorkPackageCode select x).ToList(); } /// /// 根据项目Id获取所有分部信息 /// /// /// public static List GetAllWorkPackagesByProjectId(string projectId) { return (from x in Funs.DB.WBS_WorkPackage where x.ProjectId == projectId orderby x.WorkPackageCode select x).ToList(); } /// /// 根据分部分项Id获取分部分项信息 /// /// /// public static Model.WBS_WorkPackage GetWorkPackageByWorkPackageId(string workPackageId) { return Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == workPackageId); } /// /// 根据单位工程Id和初始化编号获取分部分项信息 /// /// /// /// public static Model.WBS_WorkPackage GetWorkPackageByUnitWorkIdAndInitWorkPackageCode(string unitWorkId, string initWorkPackageCode) { return Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.UnitWorkId == unitWorkId && x.InitWorkPackageCode == initWorkPackageCode); } /// /// 根据父级Id获取所有分部分项信息 /// /// /// public static List GetAllWorkPackagesBySuperWorkPackageId(string workPackageId) { return (from x in Funs.DB.WBS_WorkPackage where x.SuperWorkPackageId == workPackageId orderby x.WorkPackageCode select x).ToList(); } /// /// 根据父级Id获取所有审批分部分项信息 /// /// /// public static List GetAllApproveWorkPackagesBySuperWorkPackageId(string workPackageId) { return (from x in Funs.DB.WBS_WorkPackage where x.SuperWorkPackageId == workPackageId && x.IsApprove == true orderby x.WorkPackageCode select x).ToList(); } /// /// 根据子分部工程Id删除一个子分部工程信息 /// /// public static void DeleteWorkPackage(string WorkPackageId) { Model.SGGLDB db = Funs.DB; Model.WBS_WorkPackage WorkPackage = db.WBS_WorkPackage.First(e => e.WorkPackageId == WorkPackageId); db.WBS_WorkPackage.DeleteOnSubmit(WorkPackage); db.SubmitChanges(); } /// /// 根据分部分项Id删除所有明细信息 /// /// public static void DeleteAllWorkPackageByUnitWorkId(string unitWorkId) { Model.SGGLDB db = Funs.DB; List q = (from x in db.WBS_WorkPackage where x.UnitWorkId == unitWorkId orderby x.WorkPackageCode select x).ToList(); db.WBS_WorkPackage.DeleteAllOnSubmit(q); db.SubmitChanges(); } /// /// 根据初始工作包编码和单位工程获取工作包集合 /// /// /// /// public static List GetWorkPackagesByInitWorkPackageCodeAndUnitWorkId(string initWorkPackageCode, string unitWorkId) { return (from x in Funs.DB.WBS_WorkPackage where x.InitWorkPackageCode == initWorkPackageCode && x.UnitWorkId.ToString() == unitWorkId orderby x.WorkPackageCode descending select x).ToList(); } /// /// 根据单位工程Id获取所有分部信息 /// /// /// public static List GetAllWorkPackagesByUnitWorkIds(string[] unitWorkIds) { return (from x in Funs.DB.WBS_WorkPackage where unitWorkIds.Contains(x.UnitWorkId) orderby x.WorkPackageCode select x).ToList(); } /// /// 获取单位工程分部分项下拉列表 /// /// /// /// public static void InitWorkPackagesDropDownListByUnitWorkId(FineUIPro.DropDownList dropName, string unitWorkId, bool isShowPlease) { dropName.DataValueField = "WorkPackageId"; dropName.DataTextField = "PackageContent"; dropName.DataSource = GetAllWorkPackagesByUnitWorkId(unitWorkId); dropName.DataBind(); if (isShowPlease) { Funs.FineUIPleaseSelect(dropName); } } /// /// 获取模拟树表格 /// /// public static DataTable GetAllTreeDataTable0(string projectId, string IsOut, string startTime, string endTime, string isOK) { using (var db = new Model.SGGLDB(Funs.ConnString)) { IQueryable workPackages = from x in db.WBS_WorkPackage where x.ProjectId == projectId select x; IQueryable unitWorks = from x in db.WBS_UnitWork where x.ProjectId == projectId select x; IQueryable getDetails = from x in db.View_WBS_WorkPackageDetail where x.ProjectId == projectId select x; List getWBSs = (from x in db.View_WBS where x.ProjectId == projectId select x).ToList(); List WBSList = new List(); DateTime startDate, endDate, startMonth, endMonth; List months = new List(); var project = Funs.DB.Base_Project.FirstOrDefault(x => x.ProjectId == projectId); if (project.StartDate != null) { startDate = Convert.ToDateTime(project.StartDate); endDate = DateTime.Now; if (project.EndDate != null) { endDate = Convert.ToDateTime(project.EndDate); } startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01"); endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01"); do { months.Add(startMonth); startMonth = startMonth.AddMonths(1); } while (startMonth <= endMonth); } DataTable table = new DataTable(); table.Columns.Add(new DataColumn("Id", typeof(String))); table.Columns.Add(new DataColumn("SupId", typeof(String))); table.Columns.Add(new DataColumn("Name", typeof(String))); table.Columns.Add(new DataColumn("WBSType", typeof(String))); table.Columns.Add(new DataColumn("ProjectId", typeof(String))); table.Columns.Add(new DataColumn("ShowId", typeof(String))); table.Columns.Add(new DataColumn("JDWeights", typeof(String))); table.Columns.Add(new DataColumn("Unit", typeof(String))); table.Columns.Add(new DataColumn("PlanProjectQuantity", typeof(String))); table.Columns.Add(new DataColumn("RealProjectQuantity", typeof(String))); table.Columns.Add(new DataColumn("PlanStartDate", typeof(DateTime))); table.Columns.Add(new DataColumn("PlanEndDate", typeof(DateTime))); table.Columns.Add(new DataColumn("RealStartDate", typeof(DateTime))); table.Columns.Add(new DataColumn("RealEndDate", typeof(DateTime))); table.Columns.Add(new DataColumn("IsMileStone", typeof(bool))); table.Columns.Add(new DataColumn("PlanCost", typeof(String))); table.Columns.Add(new DataColumn("PreWorkCode", typeof(String))); table.Columns.Add(new DataColumn("Code", typeof(String))); if (getWBSs.Count() > 0) { List newList = new List(); var projectTypes = getWBSs.Where(x => x.WBSType == "ProjectType" && x.SupId == "0"); int a = 1; foreach (var item in projectTypes) { item.Code = a.ToString(); newList.Add(item); if (string.IsNullOrEmpty(IsOut)) { AddDetail(newList, getWBSs.ToList(), item.Id, a.ToString(), startTime, endTime, isOK); } else { AddDetail(newList, getWBSs.ToList(), item.Id, a.ToString(), string.Empty, startTime, endTime, isOK); } a++; } DataRow row; foreach (Model.View_WBS item in newList) { row = table.NewRow(); row[0] = item.Id; row[1] = item.SupId; row[2] = item.Name; row[3] = item.WBSType; row[4] = item.ProjectId; if (item.WBSType == "WorkPackage") { var childWorkPackages = from x in workPackages where x.SuperWorkPackageId == item.Id && x.IsApprove == true select x; var workPackage = workPackages.FirstOrDefault(x => x.WorkPackageId == item.Id); if (workPackage != null) { if (childWorkPackages.Count() == 0) { row[5] = item.Id; if (workPackage.JDWeights != null) { row[6] = decimal.Round(Convert.ToDecimal(workPackage.JDWeights), 2); } row[7] = workPackage.Unit; if (workPackage.PlanProjectQuantity != null) { row[8] = decimal.Round(Convert.ToDecimal(workPackage.PlanProjectQuantity), 2); } if (workPackage.RealProjectQuantity != null) { row[9] = decimal.Round(Convert.ToDecimal(workPackage.RealProjectQuantity), 2); } if (workPackage.PlanStartDate != null) { row[10] = workPackage.PlanStartDate; } if (workPackage.PlanEndDate != null) { row[11] = workPackage.PlanEndDate; } if (workPackage.RealStartDate != null) { row[12] = workPackage.RealStartDate; } if (workPackage.RealEndDate != null) { row[13] = workPackage.RealEndDate; } if (workPackage.IsMileStone != null) { row[14] = workPackage.IsMileStone; } if (workPackage.PlanCost != null) { row[15] = decimal.Round(Convert.ToDecimal(workPackage.PlanCost), 2); } row[16] = workPackage.PreWorkCode; } else { if (workPackage.JDWeights != null) { row[6] = decimal.Round(Convert.ToDecimal(workPackage.JDWeights), 2); } if (workPackage.PlanStartDate != null) { row[10] = workPackage.PlanStartDate; } if (workPackage.PlanEndDate != null) { row[11] = workPackage.PlanEndDate; } if (workPackage.RealStartDate != null) { row[12] = workPackage.RealStartDate; } if (workPackage.RealEndDate != null) { row[13] = workPackage.RealEndDate; } if (workPackage.PlanCost != null) { row[15] = decimal.Round(Convert.ToDecimal(workPackage.PlanCost), 2); } } } } else if (item.WBSType == "UnitWork") { var unitWork = unitWorks.FirstOrDefault(x => x.UnitWorkId == item.Id); if (unitWork != null) { if (unitWork.JDWeights != null) { row[6] = decimal.Round(Convert.ToDecimal(unitWork.JDWeights), 2); } if (unitWork.PlanStartDate != null) { row[10] = unitWork.PlanStartDate; } if (unitWork.PlanEndDate != null) { row[11] = unitWork.PlanEndDate; } if (unitWork.RealStartDate != null) { row[12] = unitWork.RealStartDate; } if (unitWork.RealEndDate != null) { row[13] = unitWork.RealEndDate; } if (unitWork.PlanCost != null) { row[15] = decimal.Round(Convert.ToDecimal(unitWork.PlanCost), 2); } } } row[17] = item.Code; table.Rows.Add(row); } } return table; } } public static List GetAllTreeDataTable2(string projectId, string IsOut, string startTime, string endTime, string isOK) { using (var db = new Model.SGGLDB(Funs.ConnString)) { List getWBSs = (from x in db.View_WBS where x.ProjectId == projectId select x).ToList(); List newList = new List(); if (getWBSs.Count() > 0) { var projectTypes = getWBSs.Where(x => x.WBSType == "ProjectType" && x.SupId == "0"); int a = 1; foreach (var item in projectTypes) { item.Code = a.ToString(); newList.Add(item); if (string.IsNullOrEmpty(IsOut)) { AddDetail(newList, getWBSs.ToList(), item.Id, a.ToString(), startTime, endTime, isOK); } else { AddDetail(newList, getWBSs.ToList(), item.Id, a.ToString(), string.Empty, startTime, endTime, isOK); } a++; } } return newList; } } /// /// 获取模拟树表格 /// /// public static DataTable GetAllTreeDataTable(string projectId, string IsOut, string startTime, string endTime, string isOK) { using (var db = new Model.SGGLDB(Funs.ConnString)) { IQueryable getDetails = from x in db.View_WBS_WorkPackageDetail where x.ProjectId == projectId select x; List getWBSs = (from x in db.View_WBS where x.ProjectId == projectId select x).ToList(); DateTime startDate, endDate, startMonth, endMonth; List months = new List(); var project = Funs.DB.Base_Project.FirstOrDefault(x => x.ProjectId == projectId); if (project.StartDate != null) { startDate = Convert.ToDateTime(project.StartDate); endDate = DateTime.Now; if (project.EndDate != null) { endDate = Convert.ToDateTime(project.EndDate); } startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01"); endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01"); do { months.Add(startMonth); startMonth = startMonth.AddMonths(1); } while (startMonth <= endMonth); } DataTable table = new DataTable(); table.Columns.Add(new DataColumn("Id", typeof(String))); table.Columns.Add(new DataColumn("SupId", typeof(String))); table.Columns.Add(new DataColumn("Name", typeof(String))); table.Columns.Add(new DataColumn("WBSType", typeof(String))); table.Columns.Add(new DataColumn("ProjectId", typeof(String))); table.Columns.Add(new DataColumn("ShowId", typeof(String))); table.Columns.Add(new DataColumn("JDWeights", typeof(String))); table.Columns.Add(new DataColumn("Unit", typeof(String))); table.Columns.Add(new DataColumn("PlanProjectQuantity", typeof(String))); table.Columns.Add(new DataColumn("RealProjectQuantity", typeof(String))); table.Columns.Add(new DataColumn("PlanStartDate", typeof(DateTime))); table.Columns.Add(new DataColumn("PlanEndDate", typeof(DateTime))); table.Columns.Add(new DataColumn("RealStartDate", typeof(DateTime))); table.Columns.Add(new DataColumn("RealEndDate", typeof(DateTime))); table.Columns.Add(new DataColumn("IsMileStone", typeof(bool))); table.Columns.Add(new DataColumn("PlanCost", typeof(String))); table.Columns.Add(new DataColumn("PreWorkCode", typeof(String))); table.Columns.Add(new DataColumn("Code", typeof(String))); for (int i = 0; i < months.Count; i++) { table.Columns.Add(new DataColumn("PlanNum" + (i + 1).ToString(), typeof(String))); table.Columns.Add(new DataColumn("ThisNum" + (i + 1).ToString(), typeof(String))); } if (getWBSs.Count() > 0) { List newList = new List(); var projectTypes = getWBSs.Where(x => x.WBSType == "ProjectType" && x.SupId == "0"); int a = 1; foreach (var item in projectTypes) { item.Code = a.ToString(); newList.Add(item); if (string.IsNullOrEmpty(IsOut)) { AddDetail(newList, getWBSs.ToList(), item.Id, a.ToString(), startTime, endTime, isOK); } else { AddDetail(newList, getWBSs.ToList(), item.Id, a.ToString(), string.Empty, startTime, endTime, isOK); } a++; } DataRow row; foreach (Model.View_WBS item in newList) { row = table.NewRow(); row[0] = item.Id; row[1] = item.SupId; row[2] = item.Name; row[3] = item.WBSType; row[4] = item.ProjectId; row[5] = item.ShowId; row[6] = item.JDWeights; row[7] = item.Unit; row[8] = item.PlanProjectQuantity; row[9] = item.RealProjectQuantity; if (item.PlanStartDate != null) { row[10] = item.PlanStartDate; } if (item.PlanEndDate != null) { row[11] = item.PlanEndDate; } if (item.RealStartDate != null) { row[12] = item.RealStartDate; } if (item.RealEndDate != null) { row[13] = item.RealEndDate; } if (item.IsMileStone != null) { row[14] = item.IsMileStone; } row[15] = item.PlanCost; row[16] = item.PreWorkCode; row[17] = item.Code; if (!string.IsNullOrEmpty(item.ShowId)) { var details = getDetails.Where(x => x.WorkPackageId == item.Id).ToList(); foreach (var item1 in details) { var index = months.FindIndex(x => x.Equals(item1.Months)); if (item1.PlanNum != 0) { row[18 + index * 2] = decimal.Round(Convert.ToDecimal(item1.PlanNum), 2); } if (item1.ThisNum != 0) { row[19 + index * 2] = decimal.Round(Convert.ToDecimal(item1.ThisNum), 2); } } } table.Rows.Add(row); } } return table; } } private static void AddDetail(List newList, List oldList, string id, string preCode, string startTime, string endTime, string isOK) { if (isOK == "0") { var items = oldList.Where(x => x.SupId == id && (x.PlanStartDate <= Funs.GetNewDateTime(startTime) || string.IsNullOrEmpty(startTime)) && (x.PlanEndDate >= Funs.GetNewDateTime(endTime) || string.IsNullOrEmpty(endTime))).OrderBy(x => x.Code); int b = 1; foreach (var item in items) { item.Code = preCode + "." + b.ToString(); newList.Add(item); AddDetail(newList, oldList, item.Id, item.Code, startTime, endTime, isOK); b++; } } else if (isOK == "1") { var items = oldList.Where(x => x.SupId == id && x.RealEndDate != null && (x.PlanStartDate <= Funs.GetNewDateTime(startTime) || string.IsNullOrEmpty(startTime)) && (x.PlanEndDate >= Funs.GetNewDateTime(endTime) || string.IsNullOrEmpty(endTime))).OrderBy(x => x.Code); int b = 1; foreach (var item in items) { item.Code = preCode + "." + b.ToString(); newList.Add(item); AddDetail(newList, oldList, item.Id, item.Code, startTime, endTime, isOK); b++; } } else if (isOK == "2") { var items = oldList.Where(x => x.SupId == id && x.RealEndDate == null && (x.PlanStartDate <= Funs.GetNewDateTime(startTime) || string.IsNullOrEmpty(startTime)) && (x.PlanEndDate >= Funs.GetNewDateTime(endTime) || string.IsNullOrEmpty(endTime))).OrderBy(x => x.Code); int b = 1; foreach (var item in items) { item.Code = preCode + "." + b.ToString(); newList.Add(item); AddDetail(newList, oldList, item.Id, item.Code, startTime, endTime, isOK); b++; } } } private static void AddDetail(List newList, List oldList, string id, string preCode, string prefix, string startTime, string endTime, string isOK) { if (isOK == "0") { var items = oldList.Where(x => x.SupId == id && (x.PlanStartDate <= Funs.GetNewDateTime(startTime) || string.IsNullOrEmpty(startTime)) && (x.PlanEndDate >= Funs.GetNewDateTime(endTime) || string.IsNullOrEmpty(endTime))).OrderBy(x => x.Code); int b = 1; foreach (var item in items) { item.Code = preCode + "." + b.ToString(); item.Name = prefix + "...." + item.Name; newList.Add(item); AddDetail(newList, oldList, item.Id, item.Code, prefix + "....", startTime, endTime, isOK); b++; } } else if (isOK == "1") { var items = oldList.Where(x => x.SupId == id && x.RealEndDate != null && (x.PlanStartDate <= Funs.GetNewDateTime(startTime) || string.IsNullOrEmpty(startTime)) && (x.PlanEndDate >= Funs.GetNewDateTime(endTime) || string.IsNullOrEmpty(endTime))).OrderBy(x => x.Code); int b = 1; foreach (var item in items) { item.Code = preCode + "." + b.ToString(); item.Name = prefix + "...." + item.Name; newList.Add(item); AddDetail(newList, oldList, item.Id, item.Code, prefix + "....", startTime, endTime, isOK); b++; } } else if (isOK == "2") { var items = oldList.Where(x => x.SupId == id && x.RealEndDate == null && (x.PlanStartDate <= Funs.GetNewDateTime(startTime) || string.IsNullOrEmpty(startTime)) && (x.PlanEndDate >= Funs.GetNewDateTime(endTime) || string.IsNullOrEmpty(endTime))).OrderBy(x => x.Code); int b = 1; foreach (var item in items) { item.Code = preCode + "." + b.ToString(); item.Name = prefix + "...." + item.Name; newList.Add(item); AddDetail(newList, oldList, item.Id, item.Code, prefix + "....", startTime, endTime, isOK); b++; } } } #region 更新工作包、工作项 /// /// 更新月工作包、工作项 /// /// /// /// /// public static void UpdateWorkPackages(Model.SGGLDB db, Model.WBS_WorkPackage workPackage, DateTime? planStartDate, DateTime? planEndDate, DateTime? realStartDate, DateTime? realEndDate) { if (workPackage != null) { Model.WBS_WorkPackage parentWorkPackage = BLL.WorkPackageService.GetWorkPackageByWorkPackageId(workPackage.SuperWorkPackageId); if (parentWorkPackage != null) { if (planStartDate != null) { if (parentWorkPackage.PlanStartDate == null) { parentWorkPackage.PlanStartDate = planStartDate; } else { if (parentWorkPackage.PlanStartDate > planStartDate) { parentWorkPackage.PlanStartDate = planStartDate; } } } if (planEndDate != null) { if (parentWorkPackage.PlanEndDate == null) { parentWorkPackage.PlanEndDate = planEndDate; } else { if (parentWorkPackage.PlanEndDate < planEndDate) { parentWorkPackage.PlanEndDate = planEndDate; } } } if (realStartDate != null) { if (parentWorkPackage.RealStartDate == null) { parentWorkPackage.RealStartDate = realStartDate; } else { if (parentWorkPackage.RealStartDate > realStartDate) { parentWorkPackage.RealStartDate = realStartDate; } } } if (realEndDate != null) { if (parentWorkPackage.RealEndDate == null) { parentWorkPackage.RealEndDate = realEndDate; } else { if (parentWorkPackage.RealEndDate < realEndDate) { parentWorkPackage.RealEndDate = realEndDate; } } } UpdateWorkPackages(db, parentWorkPackage, planStartDate, planEndDate, realStartDate, realEndDate); } } } #endregion /// /// 更新分部分项工程实际完成时间 /// /// public static void UpdateWorkPackageRealEndDate(string workPackageId) { using (var db = new Model.SGGLDB(Funs.ConnString)) { var controlItemAndCycles = from x in db.WBS_ControlItemAndCycle where x.WorkPackageId == workPackageId && x.IsApprove == true select x; var spotCheckDetails = from x in db.Check_SpotCheckDetail where x.IsOK == true select x; bool b = true; if (controlItemAndCycles.Count() > 0) { foreach (var c in controlItemAndCycles) { if (c.CheckNum != null && c.CheckNum != 0) //检查次数为0表示一直检查 { var okSpotCheckDetails = spotCheckDetails.Where(x => x.ControlItemAndCycleId == c.ControlItemAndCycleId); if (okSpotCheckDetails.Count() != c.CheckNum) { b = false; break; } } else { b = false; break; } } if (b) { var LastEndControlItemAndCycle = controlItemAndCycles.OrderByDescending(x => x.RealEndDate).FirstOrDefault(); if (LastEndControlItemAndCycle != null) { var workPackage = db.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == LastEndControlItemAndCycle.WorkPackageId); if (workPackage != null) { workPackage.RealEndDate = LastEndControlItemAndCycle.RealEndDate; UpdateWorkPackages(db, workPackage, null, null, null, workPackage.RealEndDate); db.SubmitChanges(); var unitWork = db.WBS_UnitWork.FirstOrDefault(x => x.UnitWorkId == workPackage.UnitWorkId); //获取是否存在其他未完成的分部分项 var notEndOthenWorkPackage = db.WBS_WorkPackage.FirstOrDefault(x => x.UnitWorkId == unitWork.UnitWorkId && x.IsApprove == true && x.RealEndDate == null); if (notEndOthenWorkPackage == null) { if (workPackage.RealEndDate != null) { if (unitWork.RealEndDate == null) { unitWork.RealEndDate = workPackage.RealEndDate; } else { if (unitWork.RealEndDate < workPackage.RealEndDate) { unitWork.RealEndDate = workPackage.RealEndDate; } } } } } } db.SubmitChanges(); } } } } /// /// 更新分部分项工程实际完成时间 /// /// public static void UpdateWorkPackageRealEndDate2(string workPackageId) { using (var db = new Model.SGGLDB(Funs.ConnString)) { var workPackage = db.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == workPackageId); if (workPackage != null) { workPackage.RealEndDate = DateTime.Now; UpdateWorkPackages(db, workPackage, null, null, null, workPackage.RealEndDate); db.SubmitChanges(); var unitWork = db.WBS_UnitWork.FirstOrDefault(x => x.UnitWorkId == workPackage.UnitWorkId); //获取是否存在其他未完成的分部分项 var notEndOthenWorkPackage = db.WBS_WorkPackage.FirstOrDefault(x => x.UnitWorkId == unitWork.UnitWorkId && x.IsApprove == true && x.RealEndDate == null); if (notEndOthenWorkPackage == null) { if (workPackage.RealEndDate != null) { if (unitWork.RealEndDate == null) { unitWork.RealEndDate = workPackage.RealEndDate; } else { if (unitWork.RealEndDate < workPackage.RealEndDate) { unitWork.RealEndDate = workPackage.RealEndDate; } } } } } db.SubmitChanges(); } } #region 根据工作包ID获取wbs节点及父节点字符串 /// /// 根据工作包ID获取wbs节点及父节点字符串 /// /// /// public static string getWorkPageIdsByControlItemAndCycle(string workPackageId, string controlItemAndCycleId) { string returnValues = string.Empty; if (!string.IsNullOrEmpty(controlItemAndCycleId)) { var getControlItemAndCycle = Funs.DB.WBS_ControlItemAndCycle.FirstOrDefault(x => x.ControlItemAndCycleId == controlItemAndCycleId); if (getControlItemAndCycle != null) { workPackageId = getControlItemAndCycle.WorkPackageId; } } var getWorkPackage = Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == workPackageId); if (getWorkPackage != null) { if (!string.IsNullOrEmpty(controlItemAndCycleId)) { returnValues = getSelectIds(getWorkPackage.WorkPackageId, getWorkPackage.WorkPackageId + "[" + getWorkPackage.InitWorkPackageCode + "]$" + controlItemAndCycleId); } else { returnValues = getSelectIds(getWorkPackage.WorkPackageId, getWorkPackage.WorkPackageId + "[" + getWorkPackage.InitWorkPackageCode + "]"); } } return returnValues; } /// /// id /// /// /// /// public static string getSelectIds(string workPageId, string returnValues) { var getWorkPackage = Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == workPageId); if (getWorkPackage != null) { if (!string.IsNullOrEmpty(getWorkPackage.SuperWorkPackageId)) { var getSupWorkPackage = Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == getWorkPackage.SuperWorkPackageId); if (getSupWorkPackage != null) { returnValues = getWorkPackage.SuperWorkPackageId + "[" + getWorkPackage.InitWorkPackageCode + "]" + "|" + returnValues; returnValues = getSelectIds(getWorkPackage.SuperWorkPackageId, returnValues); } } else { var getUnitWork = Funs.DB.WBS_UnitWork.FirstOrDefault(x => x.UnitWorkId == getWorkPackage.UnitWorkId); if (getUnitWork != null) { returnValues = ("Type" + getUnitWork.ProjectType ?? "1") + "|" + getUnitWork.UnitWorkId + "|" + returnValues; } } } return returnValues; } #endregion #region 根据工作包ID获取wbs节点及父节点名称字符串 /// /// 根据工作包ID获取wbs节点及父节点字符串 /// /// /// public static string getWorkPageNamesByControlItemAndCycle(string workPackageId, string controlItemAndCycleId) { string returnValues = string.Empty; string ControlItemName = string.Empty; if (!string.IsNullOrEmpty(controlItemAndCycleId)) { var getControlItemAndCycle = Funs.DB.WBS_ControlItemAndCycle.FirstOrDefault(x => x.ControlItemAndCycleId == controlItemAndCycleId); if (getControlItemAndCycle != null) { workPackageId = getControlItemAndCycle.WorkPackageId; ControlItemName = getControlItemAndCycle.ControlItemContent; } } var getWorkPackage = Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == workPackageId); if (getWorkPackage != null) { if (!string.IsNullOrEmpty(ControlItemName)) { returnValues = getSelectNames(getWorkPackage.WorkPackageId, getWorkPackage.PackageContent + "|" + ControlItemName); } else { returnValues = getSelectNames(getWorkPackage.WorkPackageId, getWorkPackage.PackageContent); } } return returnValues; } /// /// id /// /// /// /// public static string getSelectNames(string workPageId, string returnValues) { var getWorkPackage = Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == workPageId); if (getWorkPackage != null) { if (!string.IsNullOrEmpty(getWorkPackage.SuperWorkPackageId)) { var getSupWorkPackage = Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.WorkPackageId == getWorkPackage.SuperWorkPackageId); if (getSupWorkPackage != null) { returnValues = getWorkPackage.PackageContent + "|" + returnValues; returnValues = getSelectIds(getWorkPackage.SuperWorkPackageId, returnValues); } } else { var getUnitWork = Funs.DB.WBS_UnitWork.FirstOrDefault(x => x.UnitWorkId == getWorkPackage.UnitWorkId); if (getUnitWork != null) { if (getUnitWork.ProjectType == "1") { returnValues = "建筑工程|" + getUnitWork.UnitWorkName + "|" + returnValues; } else { returnValues = "安装工程|" + getUnitWork.UnitWorkName + "|" + returnValues; } } } } return returnValues; } #endregion #region 更新分部分项概算费用 /// /// 更新分部分项概算费用 /// /// /// /// /// public static void UpdateWorkPackagesPlanCost(List workPackages, string workPackageId, decimal changePlanCost) { var workPackage = workPackages.FirstOrDefault(x => x.WorkPackageId == workPackageId); if (workPackage != null) { if (workPackage.PlanCost == null) { workPackage.PlanCost = changePlanCost; } else { workPackage.PlanCost += changePlanCost; } UpdateWorkPackagesPlanCost(workPackages, workPackage.SuperWorkPackageId, changePlanCost); } } #endregion /// /// 获取模拟树表格 /// /// public static DataTable GetAllTreeDataTableConstructionMonthReport(string projectId, string IsOut, DateTime startTime, DateTime endTime) { using (var db = new Model.SGGLDB(Funs.ConnString)) { IQueryable workPackages = from x in db.WBS_WorkPackage where x.ProjectId == projectId select x; IQueryable unitWorks = from x in db.WBS_UnitWork where x.ProjectId == projectId select x; IQueryable getDetails = from x in db.View_WBS_WorkPackageDetail where x.ProjectId == projectId select x; //获取符合时间范围的所有项 List getWBSs = (from x in db.View_WBS where x.ProjectId == projectId select x).ToList(); List list = getWBSs.Where(x => x.PlanEndDate > startTime && x.PlanEndDate <= endTime).ToList(); List newLists = new List(); foreach (var item in list) { var supItem = getWBSs.FirstOrDefault(x => x.Id == item.SupId); if (supItem != null) { newLists.Add(item); newLists.Add(supItem); AddSupItem(supItem, getWBSs, newLists); } } List ids = newLists.Select(x => x.Id).ToList(); List WBSList = new List(); DateTime startDate, endDate, startMonth, endMonth; List months = new List(); var project = Funs.DB.Base_Project.FirstOrDefault(x => x.ProjectId == projectId); if (project.StartDate != null) { startDate = Convert.ToDateTime(project.StartDate); endDate = DateTime.Now; if (project.EndDate != null) { endDate = Convert.ToDateTime(project.EndDate); } startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01"); endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01"); do { months.Add(startMonth); startMonth = startMonth.AddMonths(1); } while (startMonth <= endMonth); } DataTable table = new DataTable(); table.Columns.Add(new DataColumn("Id", typeof(String))); table.Columns.Add(new DataColumn("SupId", typeof(String))); table.Columns.Add(new DataColumn("Name", typeof(String))); table.Columns.Add(new DataColumn("WBSType", typeof(String))); table.Columns.Add(new DataColumn("ProjectId", typeof(String))); table.Columns.Add(new DataColumn("ShowId", typeof(String))); table.Columns.Add(new DataColumn("JDWeights", typeof(String))); table.Columns.Add(new DataColumn("Unit", typeof(String))); table.Columns.Add(new DataColumn("PlanProjectQuantity", typeof(String))); table.Columns.Add(new DataColumn("RealProjectQuantity", typeof(String))); table.Columns.Add(new DataColumn("PlanStartDate", typeof(DateTime))); table.Columns.Add(new DataColumn("PlanEndDate", typeof(DateTime))); table.Columns.Add(new DataColumn("RealStartDate", typeof(DateTime))); table.Columns.Add(new DataColumn("RealEndDate", typeof(DateTime))); table.Columns.Add(new DataColumn("IsMileStone", typeof(bool))); table.Columns.Add(new DataColumn("PlanCost", typeof(String))); table.Columns.Add(new DataColumn("PreWorkCode", typeof(String))); table.Columns.Add(new DataColumn("Code", typeof(String))); for (int i = 0; i < months.Count; i++) { table.Columns.Add(new DataColumn("PlanNum" + (i + 1).ToString(), typeof(String))); table.Columns.Add(new DataColumn("ThisNum" + (i + 1).ToString(), typeof(String))); } if (getWBSs.Count() > 0) { List newList = new List(); var projectTypes = getWBSs.Where(x => x.WBSType == "ProjectType" && x.SupId == "0"); int a = 1; foreach (var item in projectTypes) { item.Code = a.ToString(); newList.Add(item); if (string.IsNullOrEmpty(IsOut)) { AddDetail2(newList, getWBSs.ToList(), item.Id, a.ToString(), startTime, endTime, ids); } else { AddDetail2(newList, getWBSs.ToList(), item.Id, a.ToString(), string.Empty, startTime, endTime, ids); } a++; } DataRow row; foreach (Model.View_WBS item in newList) { row = table.NewRow(); row[0] = item.Id; row[1] = item.SupId; row[2] = item.Name; row[3] = item.WBSType; row[4] = item.ProjectId; if (item.WBSType == "WorkPackage") { var childWorkPackages = from x in workPackages where x.SuperWorkPackageId == item.Id && x.IsApprove == true select x; var workPackage = workPackages.FirstOrDefault(x => x.WorkPackageId == item.Id); if (workPackage != null) { if (childWorkPackages.Count() == 0) { row[5] = item.Id; if (workPackage.JDWeights != null) { row[6] = decimal.Round(Convert.ToDecimal(workPackage.JDWeights), 2); } row[7] = workPackage.Unit; if (workPackage.PlanProjectQuantity != null) { row[8] = decimal.Round(Convert.ToDecimal(workPackage.PlanProjectQuantity), 2); } if (workPackage.RealProjectQuantity != null) { row[9] = decimal.Round(Convert.ToDecimal(workPackage.RealProjectQuantity), 2); } if (workPackage.PlanStartDate != null) { row[10] = workPackage.PlanStartDate; } if (workPackage.PlanEndDate != null) { row[11] = workPackage.PlanEndDate; } if (workPackage.RealStartDate != null) { row[12] = workPackage.RealStartDate; } if (workPackage.RealEndDate != null) { row[13] = workPackage.RealEndDate; } if (workPackage.IsMileStone != null) { row[14] = workPackage.IsMileStone; } if (workPackage.PlanCost != null) { row[15] = decimal.Round(Convert.ToDecimal(workPackage.PlanCost), 2); } row[16] = workPackage.PreWorkCode; var details = getDetails.Where(x => x.WorkPackageId == item.Id).ToList(); foreach (var item1 in details) { var index = months.FindIndex(x => x.Equals(item1.Months)); if (item1.PlanNum != 0) { row[18 + index * 2] = decimal.Round(Convert.ToDecimal(item1.PlanNum), 2); } if (item1.ThisNum != 0) { row[19 + index * 2] = decimal.Round(Convert.ToDecimal(item1.ThisNum), 2); } } } else { if (workPackage.JDWeights != null) { row[6] = decimal.Round(Convert.ToDecimal(workPackage.JDWeights), 2); } if (workPackage.PlanStartDate != null) { row[10] = workPackage.PlanStartDate; } if (workPackage.PlanEndDate != null) { row[11] = workPackage.PlanEndDate; } if (workPackage.RealStartDate != null) { row[12] = workPackage.RealStartDate; } if (workPackage.RealEndDate != null) { row[13] = workPackage.RealEndDate; } if (workPackage.PlanCost != null) { row[15] = decimal.Round(Convert.ToDecimal(workPackage.PlanCost), 2); } } } } else if (item.WBSType == "UnitWork") { var unitWork = unitWorks.FirstOrDefault(x => x.UnitWorkId == item.Id); if (unitWork != null) { if (unitWork.JDWeights != null) { row[6] = decimal.Round(Convert.ToDecimal(unitWork.JDWeights), 2); } if (unitWork.PlanStartDate != null) { row[10] = unitWork.PlanStartDate; } if (unitWork.PlanEndDate != null) { row[11] = unitWork.PlanEndDate; } if (unitWork.RealStartDate != null) { row[12] = unitWork.RealStartDate; } if (unitWork.RealEndDate != null) { row[13] = unitWork.RealEndDate; } if (unitWork.PlanCost != null) { row[15] = decimal.Round(Convert.ToDecimal(unitWork.PlanCost), 2); } } } row[17] = item.Code; table.Rows.Add(row); } } return table; } } private static void AddSupItem(Model.View_WBS item, List getWBSs, List newLists) { var supItem = getWBSs.FirstOrDefault(x => x.Id == item.SupId); if (supItem != null) { newLists.Add(item); newLists.Add(supItem); AddSupItem(supItem, getWBSs, newLists); } } private static void AddDetail2(List newList, List oldList, string id, string preCode, DateTime startTime, DateTime endTime, List ids) { var items = oldList.Where(x => x.SupId == id && ((x.PlanEndDate > startTime && x.PlanEndDate <= endTime) || ids.Contains(x.Id))).OrderBy(x => x.Code); int b = 1; foreach (var item in items) { item.Code = preCode + "." + b.ToString(); newList.Add(item); AddDetail2(newList, oldList, item.Id, item.Code, startTime, endTime, ids); b++; } } private static void AddDetail2(List newList, List oldList, string id, string preCode, string prefix, DateTime startTime, DateTime endTime, List ids) { var items = oldList.Where(x => x.SupId == id && ((x.PlanEndDate > startTime && x.PlanEndDate <= endTime) || ids.Contains(x.Id))).OrderBy(x => x.Code); int b = 1; foreach (var item in items) { item.Code = preCode + "." + b.ToString(); item.Name = prefix + "...." + item.Name; newList.Add(item); AddDetail2(newList, oldList, item.Id, item.Code, prefix + "....", startTime, endTime, ids); b++; } } } }