public int SetAllotProjectInfo(int allotId, string userName, string userTrueName, int fxtCompanyId, int cityId, ProjectDto projectDto, int isValid, out long returnAllotId, out int returnProjectId) { Stopwatch sw = new Stopwatch(); sw.Start(); log.Info("Stopwatch开始计时," + allotId); int housecount = 0; int housedetailcount = 0; string message = string.Empty; returnAllotId = allotId; returnProjectId = projectDto.ProjectId; DateTime nowTime = DateTime.Now; Project project = Mapper.Map <ProjectDto, Project>(projectDto); AllotFlow allotFlow = _unitOfWork.AllotFlowRepository.GetById(allotId); if (allotFlow == null) { return(-5); } //不是当前用户的任务 if (allotFlow.SurveyUserName != userName.ToLower()) { return(-2); } else if (allotFlow.StateCode != SYSCodeManager.STATECODE_4) { //不是查勘中任务 return(-3); } //行政区 + 楼盘名称||已有的projectid||没有入库 //var exists = _unitOfWork.ProjectRepository.Get(m => // ( // (m.ProjectName == project.ProjectName && m.AreaID == project.AreaID) // && m.ProjectId != project.ProjectId // ) // && m.Status != SYSCodeManager.STATECODE_10 && m.Valid == 1 // ).FirstOrDefault(); //if (exists != null) //{ // message = "楼盘已存在"; // return -4; //} #region (Project更新值) if (isValid == 0 && !CheckProjectObj(project, out message)) { return(0); } allotFlow.Project.EndDate = projectDto.Enddate; allotFlow.Project.ManagerQuality = projectDto.Managerquality; allotFlow.Project.ParkingStatus = projectDto.ParkingStatus; allotFlow.Project.PhotoCount = projectDto.PhotoCount; allotFlow.Project.FxtProjectId = projectDto.FxtProjectId; allotFlow.Project.ProjectName = projectDto.ProjectName; allotFlow.Project.OtherName = projectDto.OtherName; allotFlow.Project.CityID = projectDto.CityID; allotFlow.Project.AreaID = projectDto.AreaID; allotFlow.Project.SubAreaId = projectDto.SubAreaId; allotFlow.Project.Address = projectDto.Address; allotFlow.Project.PurposeCode = projectDto.PurposeCode; allotFlow.Project.RightCode = projectDto.RightCode; allotFlow.Project.Detail = projectDto.Detail; allotFlow.Project.X = projectDto.X; allotFlow.Project.Y = projectDto.Y; allotFlow.Project.BuildingNum = projectDto.BuildingNum; allotFlow.Project.TotalNum = projectDto.Totalnum; allotFlow.Project.ParkingNumber = projectDto.Parkingnumber; allotFlow.Project.East = projectDto.East; allotFlow.Project.West = projectDto.West; allotFlow.Project.South = projectDto.South; allotFlow.Project.North = projectDto.North; allotFlow.Project.Status = SYSCodeManager.STATECODE_4; allotFlow.Project.FxtCompanyId = fxtCompanyId; allotFlow.Project.CityID = cityId; allotFlow.Project.Valid = 1; allotFlow.Project.SaveDateTime = nowTime; //最后修改时间 allotFlow.Project.SaveUser = userName; //修改人 returnProjectId = allotFlow.Project.ProjectId; #endregion #region (停车状况) int classcode = project.ParkingStatus.HasValue ? project.ParkingStatus.Value : 0; PAppendage pAppendage = new PAppendage() { CityId = cityId, ProjectId = project.ProjectId, ClassCode = classcode, AppendageCode = SYSCodeManager.APPENDAGECODE_14 }; PAppendage pAppend = _unitOfWork.P_AppendageRepository.GetBy(m => m.CityId == cityId && m.ProjectId == project.ProjectId && m.AppendageCode == SYSCodeManager.APPENDAGECODE_14); if (pAppend == null) { _unitOfWork.P_AppendageRepository.Insert(pAppendage); } else { pAppend.ClassCode = classcode; _unitOfWork.P_AppendageRepository.Update(pAppend); } #endregion #region (Project更新关联公司) //更新开发商 if (!string.IsNullOrEmpty(projectDto.Developers)) { var developers = _unitOfWork.P_CompanyRepository.GetBy(m => m.CityId == cityId && m.ProjectId == project.ProjectId && m.CompanyType == SYSCodeApi.COMPANYTYPECODE_1); if (developers == null) { PCompany pcompany = new PCompany() { CityId = cityId, CompanyName = projectDto.Developers, CompanyType = SYSCodeApi.COMPANYTYPECODE_1, ProjectId = project.ProjectId }; _unitOfWork.P_CompanyRepository.Insert(pcompany); } else { developers.CompanyName = projectDto.Developers; _unitOfWork.P_CompanyRepository.Update(developers); } } else { _unitOfWork.P_CompanyRepository.Delete(m => m.CityId == cityId && m.ProjectId == project.ProjectId && m.CompanyType == SYSCodeApi.COMPANYTYPECODE_1); } //更新物业管理公司 if (!string.IsNullOrEmpty(projectDto.ManagerCompany)) { var managerCompany = _unitOfWork.P_CompanyRepository.GetBy(m => m.CityId == cityId && m.ProjectId == project.ProjectId && m.CompanyType == SYSCodeApi.COMPANYTYPECODE_4); if (managerCompany == null) { PCompany pcompany = new PCompany() { CityId = cityId, CompanyName = projectDto.ManagerCompany, CompanyType = SYSCodeApi.COMPANYTYPECODE_4, ProjectId = project.ProjectId }; _unitOfWork.P_CompanyRepository.Insert(pcompany); } else { managerCompany.CompanyName = projectDto.ManagerCompany; _unitOfWork.P_CompanyRepository.Update(managerCompany); } } else { _unitOfWork.P_CompanyRepository.Delete(m => m.CityId == cityId && m.ProjectId == project.ProjectId && m.CompanyType == SYSCodeApi.COMPANYTYPECODE_4); } #endregion #region (Building更新值) if (projectDto.BuildingDtolist != null && projectDto.BuildingDtolist.Count > 0) { int bid = 0; foreach (var buildingDto in projectDto.BuildingDtolist) { var t = false; Stopwatch swbs = new Stopwatch(); swbs.Start(); //验证楼栋字段数据 if (!CheckBuildingObj(buildingDto, out message)) { return(0); } //给building各属性赋值 Building building = Mapper.Map <BuildingDto, Building>(buildingDto); building.Valid = 1; building.ProjectId = project.ProjectId; building.FxtCompanyId = allotFlow.FxtCompanyId; building.CityID = cityId; //删除楼栋 if (buildingDto.Valid == 0) { var build = _unitOfWork.BuildingRepository.GetBy(m => m.AppId == building.AppId); if (build != null) { _unitOfWork.HouseRepository.Delete(m => m.BuildingId == build.BuildingId); _unitOfWork.HouseDetailsRepository.Delete(m => m.BuildingId == build.BuildingId); _unitOfWork.BuildingRepository.Delete(m => m.AppId == building.AppId); } } else { if (allotFlow.Project.Buildings == null) { allotFlow.Project.Buildings = new List <Building>(); } Building eBuilding = allotFlow.Project.Buildings.Where(m => m.Valid == 1 && m.AppId == building.AppId).FirstOrDefault(); if (eBuilding == null) { building.Creator = userName; building.CreateTime = nowTime; eBuilding = building; eBuilding.BuildingId = bid++; allotFlow.Project.Buildings.Add(eBuilding); } else { eBuilding.AppId = buildingDto.AppId; eBuilding.BuildDate = buildingDto.BuildDate; eBuilding.BuildingName = buildingDto.BuildingName; eBuilding.ElevatorRate = buildingDto.ElevatorRate; eBuilding.IsElevator = buildingDto.IsElevator; eBuilding.LocationCode = buildingDto.LocationCode; eBuilding.MaintenanceCode = buildingDto.MaintenanceCode; eBuilding.OtherName = buildingDto.OtherName; eBuilding.PurposeCode = buildingDto.PurposeCode; eBuilding.Remark = buildingDto.Remark; eBuilding.StructureCode = buildingDto.StructureCode; eBuilding.TotalFloor = buildingDto.TotalFloor; eBuilding.UnitsNumber = buildingDto.UnitsNumber; eBuilding.X = buildingDto.X; eBuilding.Y = buildingDto.Y; eBuilding.SaveUser = userName; eBuilding.SaveDateTime = nowTime; } #region (设置house) if (buildingDto.HouseDtolist != null && buildingDto.HouseDtolist.Count > 0) { foreach (var houseDto in buildingDto.HouseDtolist) { //验证房号字段数据 if (!CheckHouseObj(houseDto, out message)) { return(0); } houseDto.HouseId = 0; housecount++; House house = Mapper.Map <HouseDto, House>(houseDto); //给house各属性赋值 house.BuildingId = eBuilding.BuildingId; house.Valid = 1; house.FxtCompanyId = fxtCompanyId; house.CityID = cityId; if (string.IsNullOrEmpty(houseDto.UnitNo) && string.IsNullOrEmpty(houseDto.HouseNo)) { house.UnitNo = houseDto.HouseName.Replace(houseDto.FloorNo.ToString(), "$"); } else { house.UnitNo = _houseService.SetHouseUnitNoAndHouseNo(house.UnitNo, houseDto.HouseNo); } //设置新增实体 if (houseDto.Valid == 0) { var hou = _unitOfWork.HouseRepository.GetBy(m => m.AppId == houseDto.AppId && m.BuildingId == eBuilding.BuildingId); if (hou != null) { _unitOfWork.HouseDetailsRepository.Delete(m => m.HouseId == hou.HouseId); _unitOfWork.HouseRepository.Delete(m => m.AppId == houseDto.AppId && m.BuildingId == eBuilding.BuildingId); } } else { House eHouse = eBuilding.Houses == null ? null : eBuilding.Houses.Where(m => m.AppId == houseDto.AppId && m.BuildingId == eBuilding.BuildingId && m.Valid == 1).FirstOrDefault(); if (eHouse == null) { house.CreateTime = nowTime; house.Creator = userName; eHouse = house; if (eBuilding.Houses == null) { eBuilding.Houses = new List <House>(); } eBuilding.Houses.Add(eHouse); } else { eHouse.BuildArea = house.BuildArea; eHouse.EndFloorNo = house.EndFloorNo; eHouse.FloorNo = house.FloorNo; eHouse.FrontCode = house.FrontCode; eHouse.HouseName = house.HouseName; eHouse.HouseTypeCode = house.HouseTypeCode; eHouse.NoiseCode = house.NoiseCode; eHouse.NominalFloor = house.NominalFloor; eHouse.PhotoName = house.PhotoName; eHouse.PurposeCode = house.PurposeCode; eHouse.Remark = house.Remark; eHouse.SightCode = house.SightCode; eHouse.Status = house.Status; eHouse.StructureCode = house.StructureCode; eHouse.UnitNo = house.UnitNo; eHouse.VDCode = house.VDCode; eHouse.SaveUser = userName; eHouse.SaveDateTime = nowTime; } //生成房号 if (house.EndFloorNo >= house.FloorNo) { string unitNo = _houseService.GetUnitNoByUnitNoStr(house.UnitNo); string houseNo = _houseService.GetHouseNoByUnitNoStr(house.UnitNo); for (int i = house.FloorNo; i <= house.EndFloorNo; i++) { housedetailcount++; List <int> cs = new List <int>() { SYSCodeManager.HOUSEPURPOSECODE_5, SYSCodeManager.HOUSEPURPOSECODE_6, SYSCodeManager.HOUSEPURPOSECODE_8, SYSCodeManager.HOUSEPURPOSECODE_27 }; HouseDetails detail = new HouseDetails(); detail = Mapper.Map <House, HouseDetails>(eHouse); detail.FloorNo = i; detail.NominalFloor = i.ToString(); detail.UnitNo = unitNo; detail.RoomNo = houseNo; detail.CreateTime = nowTime; detail.Creator = userName; if (detail.PurposeCode.HasValue && cs.Contains(detail.PurposeCode.Value)) { detail.HouseName = unitNo + houseNo; } else { if (i < 0) { detail.HouseName = "-" + unitNo + -i + houseNo; } else { detail.HouseName = unitNo + i + houseNo; } } if (i != 0) { if (eHouse.HouseDetails == null) { eHouse.HouseDetails = new List <HouseDetails>(); } var hd = eHouse.HouseDetails.Where(m => m.HouseId == detail.HouseId && m.RoomNo == houseNo && m.UnitNo == detail.UnitNo && m.FloorNo == detail.FloorNo).FirstOrDefault(); if (hd == null) { eHouse.HouseDetails.Add(detail); } else { hd.FloorNo = detail.FloorNo; hd.FrontCode = detail.FrontCode; hd.HouseName = detail.HouseName; hd.HouseTypeCode = detail.HouseTypeCode; hd.NoiseCode = detail.NoiseCode; hd.NominalFloor = detail.NominalFloor; hd.PurposeCode = detail.PurposeCode; hd.Remark = detail.Remark; hd.SightCode = detail.SightCode; hd.StructureCode = detail.StructureCode; hd.UnitNo = detail.UnitNo; hd.VDCode = detail.VDCode; hd.SaveUser = userName; hd.SaveDateTime = nowTime; } } } } } } } #endregion } swbs.Stop(); log.Info("循环一个楼栋需要" + swbs.Elapsed.TotalMilliseconds + "ms."); } } #endregion //更新任务 allotFlow.StateCode = SYSCodeManager.STATECODE_4; allotFlow.StateDate = nowTime; allotFlow.X = projectDto.X; allotFlow.Y = projectDto.Y; returnAllotId = allotFlow.Id; returnProjectId = allotFlow.Project.ProjectId; sw.Stop(); TimeSpan ts2 = sw.Elapsed; if (projectDto.BuildingDtolist != null) { log.Info("楼栋:" + projectDto.BuildingDtolist.Count + "条."); log.Info("单元室号:" + housecount + "条."); log.Info("房号:" + housedetailcount + "条."); } log.Info("循环操作总共花费" + ts2.TotalMilliseconds + "ms."); Stopwatch sw2 = new Stopwatch(); sw2.Start(); int r = _unitOfWork.Commit(); TimeSpan ts3 = sw2.Elapsed; log.Info("执行数据库操作总共花费" + ts3.TotalMilliseconds + "ms."); return(r); }