/// <summary> /// Creates the specified photo. /// </summary> /// <param name="photo">The photo.</param> /// <returns>Task<Photo>.</returns> public async Task<int> Create(Photo photo) { using (ChePingContext db = new ChePingContext()) { photo.UploadTime = DateTime.UtcNow.AddHours(8); await db.SaveAsync(photo); } return photo.Id; }
/// <summary> /// Creates the specified user. /// </summary> /// <param name="user">The user.</param> /// <returns>Task<User>.</returns> public async Task<User> Create(User user) { user.Available = true; using (ChePingContext db = new ChePingContext()) { await db.SaveAsync(user); } return user; }
/// <summary> /// Creates the specified city. /// </summary> /// <param name="city">The city.</param> /// <returns>Task<City>.</returns> /// <exception cref="ApplicationException">城市信息已经存在</exception> public async Task<City> Create(City city) { if (await this.Exist(city)) { throw new ApplicationException("城市信息已经存在"); } using (ChePingContext db = new ChePingContext()) { await db.SaveAsync(city); } return city; }
/// <summary> /// Creates the specified outlet. /// </summary> /// <param name="outlet">The outlet.</param> /// <returns>Task<Outlet>.</returns> /// <exception cref="System.ApplicationException">网点信息已经存在</exception> /// <exception cref="ApplicationException">网点信息已经存在</exception> public async Task<Outlet> Create(Outlet outlet) { if (await this.Exist(outlet)) { throw new ApplicationException("网点信息已经存在"); } outlet.Available = true; using (ChePingContext db = new ChePingContext()) { await db.SaveAsync(outlet); } return outlet; }
/// <summary> /// Creates the specified model. /// </summary> /// <param name="model">The model.</param> /// <returns>Task<Model>.</returns> /// <exception cref="System.ApplicationException">车型信息已经存在</exception> public async Task<Model> Create(Model model) { if (await this.Exist(model)) { throw new ApplicationException("车型信息已经存在"); } model.Available = true; using (ChePingContext db = new ChePingContext()) { await db.SaveAsync(model); } return model; }
/// <summary> /// add special case as an asynchronous operation. /// </summary> /// <param name="case">The case.</param> /// <param name="info">The information.</param> /// <param name="photos">The photos.</param> /// <returns>Task<CaseDto>.</returns> /// <exception cref="ApplicationException">无法加载用户信息</exception> /// <exception cref="System.ApplicationException">无法加载用户信息</exception> private async Task<Case> AddSpecialCaseAsync(Case @case, VehicleInfo info, IEnumerable<int> photos) { Case newCase = new Case { Abandon = false, AbandonReason = "", CaseType = @case.CaseType, DirectorId = null, // need update ManagerId = null, OutletId = 0, // need update PurchasePrice = 0, PurchaserId = @case.PurchaserId, QueryingId = null, SerialId = @case.SerialId, State = State.Shenhe, ValuerId = null, VehicleInfoId = 0, // need update VehicleInspecId = 0, // need update CreateTime = DateTime.UtcNow.AddHours(8) }; VehicleInfo newInfo = new VehicleInfo { BrandName = info.BrandName, CooperationMethod = info.CooperationMethod, DisplayMileage = info.DisplayMileage, ExpectedPrice = info.ExpectedPrice, FactoryTime = info.FactoryTime, InnerColor = info.InnerColor, InnerColorName = info.InnerColorName, LicenseLocation = info.LicenseLocation, LicenseTime = info.LicenseTime, ModelId = info.ModelId, ModelName = info.ModelName, ModifiedContent = info.ModifiedContent, OuterColor = info.OuterColor, OuterColorName = info.OuterColorName, SeriesName = info.SeriesName, VehicleLocation = info.VehicleLocation }; VehicleInspection newVehicleInspection = new VehicleInspection(); int purchaserId = @case.PurchaserId; using (ChePingContext db = new ChePingContext()) { await db.SaveAsync(newInfo); await db.SaveAsync(newVehicleInspection); User user = await db.Users.FirstOrDefaultAsync(u => u.Id == purchaserId && u.JobTitle == JobTitle.Purchaser && u.Available); if (user == null) { throw new ApplicationException("无法加载用户信息"); } List<User> directors = await db.Users.Where(u => u.Available && u.OutletId == user.OutletId && u.JobTitle == JobTitle.Director).ToListAsync(); int caseCount = await db.Cases.CountAsync(c => c.OutletId == user.OutletId && DirectorTodoStates.Contains(c.State) && c.DirectorId != null); int index = caseCount % directors.Count; int directorId = directors[index].Id; newCase.DirectorId = directorId; newCase.OutletId = user.OutletId; newCase.VehicleInfoId = newInfo.Id; newCase.VehicleInspecId = newVehicleInspection.Id; await db.SaveAsync(newCase); foreach (int photo in photos) { Photo photoModel = await db.Photos.FirstOrDefaultAsync(p => p.Id == photo); if (photoModel != null) { photoModel.CaseId = newCase.Id; } } await db.ExecuteSaveChangesAsync(); } return newCase; }
/// <summary> /// add general case as an asynchronous operation. /// </summary> /// <param name="case">The case.</param> /// <param name="info">The information.</param> /// <param name="photos">The photos.</param> /// <returns>System.Threading.Tasks.Task<ChepingServer.Models.Case>.</returns> /// <exception cref="ApplicationException"> /// 无法加载用户信息 /// or /// 无在岗评估师,事项添加失败 /// or /// 评估师分配错误,事项添加失败 /// </exception> /// <exception cref="System.ApplicationException"> /// 无法加载车型信息 /// or /// 无法加载用户信息 /// or /// 无在岗评估师,事项添加失败 /// or /// 评估师分配错误,事项添加失败 /// </exception> private async Task<Case> AddGeneralCaseAsync(Case @case, VehicleInfo info, IEnumerable<int> photos) { Case newCase = new Case { Abandon = false, AbandonReason = "", CaseType = @case.CaseType, DirectorId = null, ManagerId = null, OutletId = 0, // need update PurchasePrice = 0, PurchaserId = @case.PurchaserId, QueryingId = null, SerialId = @case.SerialId, State = State.Pinggu, ValuerId = null, // need update VehicleInfoId = 0, // need update VehicleInspecId = 0, // need update CreateTime = DateTime.UtcNow.AddHours(8), Times = null }; VehicleInfo newInfo = new VehicleInfo { BrandName = info.BrandName, CooperationMethod = info.CooperationMethod, DisplayMileage = info.DisplayMileage, ExpectedPrice = info.ExpectedPrice, FactoryTime = info.FactoryTime, InnerColor = info.InnerColor, InnerColorName = info.InnerColorName, LicenseLocation = info.LicenseLocation, LicenseTime = info.LicenseTime, ModelId = info.ModelId, ModelName = info.ModelName, ModifiedContent = info.ModifiedContent, OuterColor = info.OuterColor, OuterColorName = info.OuterColorName, SeriesName = info.SeriesName, VehicleLocation = info.VehicleLocation }; VehicleInspection newVehicleInspection = new VehicleInspection(); int purchaserId = @case.PurchaserId; using (ChePingContext db = new ChePingContext()) { await db.SaveAsync(newInfo); await db.SaveAsync(newVehicleInspection); User user = await db.Users.FirstOrDefaultAsync(u => u.Id == purchaserId && u.JobTitle == JobTitle.Purchaser && u.Available); if (user == null) { throw new ApplicationException("无法加载用户信息"); } int valuerId; List<User> valuers = await db.Users.Where(u => u.Available && !u.HangOn && u.JobTitle == JobTitle.Valuer && u.ValuerGroup.Contains(((int)newCase.CaseType).ToString())).ToListAsync(); var workingValuers = await db.Cases.Where(c => ValuerTodoStates.Contains(c.State) && c.ValuerId != null) .GroupBy(c => c.ValuerId).Select(g => new { g.Key, Count = g.Count() }).ToListAsync(); valuers.RemoveAll(v => workingValuers.Select(i => i.Key).Contains(v.Id)); if (valuers.Count > 0) { valuerId = valuers[0].Id; } else { if (workingValuers.Count == 0) { throw new ApplicationException("无在岗评估师,事项添加失败"); } valuerId = workingValuers.OrderBy(v => v.Count).Select(v => v.Key).First().GetValueOrDefault(); if (valuerId == 0) { throw new ApplicationException("评估师分配错误,事项添加失败"); } } newCase.ValuerId = valuerId; newCase.OutletId = user.OutletId; newCase.VehicleInfoId = newInfo.Id; newCase.VehicleInspecId = newVehicleInspection.Id; await db.SaveAsync(newCase); foreach (int photo in photos) { Photo photoModel = await db.Photos.FirstOrDefaultAsync(p => p.Id == photo); if (photoModel != null) { photoModel.CaseId = newCase.Id; } } await db.ExecuteSaveChangesAsync(); await this.smsService.SendNoticeMessage(newCase.ValuerId.GetValueOrDefault()); } return newCase; }