Example #1
0
        /// <summary>
        ///     add 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&lt;CaseDto&gt;.</returns>
        public async Task<Case> AddCaseAsync(Case @case, VehicleInfo info, IEnumerable<int> photos)
        {
            using (ChePingContext db = new ChePingContext())
            {
                Model model = await db.Models.FirstOrDefaultAsync(m => m.Brand == info.BrandName &&
                                                                       m.Series == info.SeriesName && m.Modeling == info.ModelName);

                Color outerColor = await db.Colors.FirstOrDefaultAsync(c => c.ColorName == info.OuterColorName);

                if (outerColor == null)
                {
                    info.OuterColor = -1;
                }
                else
                {
                    info.OuterColor = outerColor.Id;
                }

                Color innerColor = await db.Colors.FirstOrDefaultAsync(c => c.ColorName == info.InnerColorName);

                if (innerColor == null)
                {
                    info.InnerColor = -1;
                }
                else
                {
                    info.InnerColor = innerColor.Id;
                }

                if (model == null)
                {
                    info.ModelId = -1;
                }
                else
                {
                    info.ModelId = model.Id;
                    info.BrandName = model.Brand;
                    info.SeriesName = model.Series;
                    info.ModelName = model.Modeling;
                }
            }

            if (info.ModelId == -1)
            {
                return await this.AddSpecialCaseAsync(@case, info, photos);
            }

            return await this.AddGeneralCaseAsync(@case, info, photos);
        }
Example #2
0
        /// <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&lt;ChepingServer.Models.Case&gt;.</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;
        }
Example #3
0
        /// <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&lt;CaseDto&gt;.</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;
        }
Example #4
0
        public async Task<IHttpActionResult> AddCase(AddCaseRequest request)
        {
            User user = await this.userService.Get(this.CurrentUser.Id);

            if (user == null)
            {
                return this.BadRequest("无法加载用户信息");
            }

            Case @case = new Case
            {
                Abandon = null,
                AbandonReason = "",
                CaseType = request.CaseType,
                DirectorId = null,
                ManagerId = null,
                OutletId = user.OutletId,
                PurchasePrice = 0,
                State = State.Pinggu,
                PurchaserId = user.Id,
                QueryingId = null,
                SerialId = user.UserCode + DateTime.UtcNow.AddHours(8).ToString("yyyyMMddHHmmss"),
                ValuerId = null,
                VehicleInfoId = 0,
                VehicleInspecId = 0
            };

            VehicleInfo info = new VehicleInfo
            {
                BrandName = request.BrandName,
                CooperationMethod = request.CooperationMethod,
                DisplayMileage = request.DisplayMileage,
                ExpectedPrice = request.ExpectedPrice,
                FactoryTime = request.FactoryTime,
                InnerColor = request.InnerColor,
                InnerColorName = request.InnerColorName,
                LicenseLocation = request.LicenseLocation,
                LicenseTime = request.LicenseTime,
                ModelId = -1,
                ModelName = request.ModelName,
                ModifiedContent = request.ModifiedContent,
                OuterColor = request.OuterColor,
                OuterColorName = request.OuterColorName,
                SeriesName = request.SeriesName,
                VehicleLocation = request.VehicleLocation
            };

            @case = await this.caseService.AddCaseAsync(@case, info, request.PhotoIds);

            return this.Ok(@case.ToDto());
        }