/// <summary>
 /// 车辆帖子的数量( 求购+转让)
 /// </summary>
 /// <returns></returns>
 public int InfoCount()
 {
     using (var context=new FxCarContext())
     {
         return context.CarBuyInfos.Count() + context.CarTransferInfos.Count();
     }
 }
 public void CreateCar()
 {
     FxCarContext context = new FxCarContext();
     var entity = new Entity.FxCar.CarTransferInfo()
     {
         Action = "Action",
         AreaId = 1,
         CarMileage = 232,
         CarYear = 1998,
         CatagroyId = 1,
         CityId = 1,
         Controller = "Controller",
         IsDelete = false,
         IsPublish = false,
         Mark = "Mark",
         Pictures = new System.Collections.Generic.List<Entity.TransferPicture>() { 
             new Entity.TransferPicture() { 
                  CdnUrl="CdnUrl", ImageUrl="CdnUrl", IsCdn=false, PhysicalPath="CdnUrl", TransferPictureCatagroy=2
             } 
         },
         Price = 1232,
         PublishTitle = "PublishTitle",
         PublishUserEmail = "PublishUserEmail",
         UserAccount = "UserAccount"
     };
     context.SaveChanges();
     context.CarTransferInfos.Remove(entity);
 }
 public Entity.FxCar.CarTransferInfo Get(int Id)
 {
     using (FxCarContext context = new FxCarContext())
     {
         return context.CarTransferInfos.Include(r => r.Pictures)
             .Where(r => r.CarTransferInfoId == Id).FirstOrDefault();
     }
 }
 public Entity.FxCar.CarBuyInfo Get(int Id)
 {
     using (FxCarContext context = new FxCarContext())
     {
         return context.CarBuyInfos
             .Where(r => r.CarBuyInfoId == Id).FirstOrDefault();
     }
 }
 public bool SaveTransferCar(Entity.FxCar.CarTransferInfo car)
 {
     using (FxCarContext context = new FxCarContext())
     {
         context.CarTransferInfos.Add(car);
         context.SaveChanges();
     }
     return car.CarTransferInfoId > 0;
 }
 public bool SaveBuyCar(CarBuyInfo car)
 {
     using (FxCarContext context = new FxCarContext())
     {
         context.CarBuyInfos.Add(car);
         context.SaveChanges();
     }
     return car.CarBuyInfoId > 0;
 }
 public void Authorize()
 {
     int carId = JobQueue.CarTransferJobLoadQueue.GetItem(carTransferJobService.Authorizing);
     if (carId == 0)
     {
         return;
     }
     using (var context = new FxCarContext())
     {
         while (carId != 0)
         {
             var car = context.CarTransferInfos.Where(r => r.CarTransferInfoId == carId).
                     Select(r => new { r.Mark, r.PublishTitle }).FirstOrDefault();
             if (car != null)
             {
                 if (filter.FilterContent(car.Mark).Success && filter.FilterContent(car.PublishTitle).Success)
                 {
                     try
                     {
                         carTransferJobService.AuthorizeSuccess(carId);
                         JobQueue.CarTransferJobPictureProcessQueue.Add(carId);
                     }
                     catch (Exception ex)
                     {
                         ex.LogEx(string.Format("{0} {1} {2}", JobKey, "carTransferJobService.AuthorizeSuccess", carId));
                     }
                 }
                 else
                 {
                     try
                     {
                         string msg = "";
                         var filter1 = filter.FilterContent(car.Mark);
                         var filter2 = filter.FilterContent(car.PublishTitle);
                         if (!filter1.Success)
                         {
                             msg += string.Format("你的帖子中包含了[[{0}]] 这个关键字", filter1.Key);
                         }
                         if (!filter2.Success)
                         {
                             msg += string.Format("你的帖子中包含了[[{0}]] 这个关键字", filter2.Key);
                         }
                         carTransferJobService.AuthorizeFaild(carId, msg);
                     }
                     catch (Exception ex)
                     {
                         ex.LogEx(string.Format("{0} {1} {2}", JobKey, "carTransferJobService.AuthorizeFaild", carId));
                     }
                 }
                 carId = JobQueue.CarTransferJobLoadQueue.GetItem(carTransferJobService.Authorizing);
             }
         }
     }
 }
 public bool PictureProcessdSuccessd(int carId)
 {
     using (var context = new FxCarContext())
     {
         var car = context.CarBuyInfos.Where(r => r.CarBuyInfoId == carId).FirstOrDefault();
         if (car != null)
         {
             car.InfoProcessState = (int)ProcessState.AuthorizeFaild;
             car.Logs.Add(new Entity.FxCar.CarBuyLog()
             {
                 OperteName = Enum.GetName(typeof(ProcessState), ProcessState.AuthorizeFaild)
             });
             return context.SaveChanges() > 0;
         }
     }
     return false;
 }
 public bool PictrueCdning(int carId)
 {
     using (var context = new FxCarContext())
     {
         var car = context.CarTransferInfos.Where(r => r.CarTransferInfoId == carId).FirstOrDefault();
         if (car != null)
         {
             car.InfoProcessState = (int)ProcessState.PictrueCdning;
             car.Logs.Add(new Entity.FxCar.CarTransferLog()
             {
                 OperteName = Enum.GetName(typeof(ProcessState), ProcessState.PictrueCdning)
             });
             return context.SaveChanges() > 0;
         }
     }
     return false;
 }
 public void LoadJob()
 {
     try
     {
         List<int> list = new List<int>();
         using (var context = new FxCarContext())
         {
             list = context.CarBuyInfos
                  .Where(r => r.InfoProcessState == (int)ProcessState.Commit).
                      Select(r => r.CarBuyInfoId).Take(20).ToList();
         }
         JobQueue.CarBuyJobLoadQueue.AddRange(list);
     }
     catch (Exception ex)
     {
         ex.LogEx(string.Format("{0} {1}", JobKey, "CarBuyJobLoad"));
     }
 }
 public List<CarTransferInfo> GetSecondHandCar()
 {
     List<CarTransferInfo> list;
     using (FxCarContext context = new FxCarContext())
     {
         list = context.CarTransferInfos.Include(r => r.Pictures)
             .Where(r => r.IsPublish==true)
             .OrderByDescending(r => r.CreatedTime)
             .Take(20).ToList();
         //&&
         //        (r.CarTransferInfoId >= (int)ChannelListDetailCatagroy.Audi &&
         //        r.CarTransferInfoId >= (int)ChannelListDetailCatagroy.SecondHandCarOther)
         //只有一个栏目 优化语句
     }
     if ((list.Count % 4) != 0)
     {
         return list.Take(list.Count - (list.Count % 4)).ToList();
     }
     return list;
 }
 public bool Delete(int carId)
 {
     using (var context = new FxCarContext())
     {
         var car = context.CarBuyInfos.Where(r => r.CarBuyInfoId == carId).FirstOrDefault();
         if (car != null)
         {
             car.InfoProcessState = (int)ProcessState.Delete;
             car.IsDelete = true;
             car.IsPublish = false;
             car.Logs.Add(new Entity.FxCar.CarBuyLog()
             {
                 OperteName = Enum.GetName(typeof(ProcessState), ProcessState.Delete)
             });
             return context.SaveChanges() > 0;
         }
     }
     return false;
 }
 public void PictureProcess()
 {
     int carId = JobQueue.CarTransferJobPictureProcessQueue.GetItem(carTransferJobService.PictrueCdning);
     if (carId == 0)
     {
         return;
     }
     using (var context = new FxCarContext())
     {
         while (carId != 0)
         {
             var car = context.CarTransferInfos.Where(r => r.CarTransferInfoId == carId).FirstOrDefault();
             if (car != null)
             {
                 string source;
                 string destnation;
                 string destnationmin;
                 int error = 0;
                 string errmsg = "";
                 foreach (var picture in car.Pictures)
                 {
                     try
                     {
                         source = string.Format(picture.PhysicalPath);
                         destnation = string.Format("{0}{1}", appSettings.CdnPath(), picture.ImageUrl.Replace(@"/", @"\"));
                         destnationmin = string.Format("{0}{1}", appSettings.CdnPath(), picture.MinImageUrl.Replace(@"/", @"\"));
                         var job = new ImageJob(source, destnation, new ResizeSettings()
                         {
                             MaxHeight = 500,
                             MaxWidth = 500,
                         }) { CreateParentDirectory = true };
                         var jobmin = new ImageJob(source, destnationmin, new ResizeSettings()
                         {
                             MaxHeight = 64,
                             MaxWidth = 64,
                         }) { CreateParentDirectory = true };
                         ImageBuilder.Current.Build(job);
                         ImageBuilder.Current.Build(jobmin);
                     }
                     catch (Exception ex)
                     {
                         if (string.IsNullOrEmpty(errmsg))
                         {
                             if (ex.InnerException != null)
                             {
                                 errmsg = ex.InnerException.Message;
                             }
                             else
                             {
                                 errmsg = ex.Message;
                             }
                         }
                         error++;
                         ex.LogEx(string.Format("{0} {1} {2}", JobKey, "PictureProcess", picture.TransferPictureId));
                     }
                 }
                 if (error == 0)
                 {
                     carTransferJobService.PictrueCdnSuccessd(carId);
                     carTransferJobService.Publish(carId);
                 }
                 else
                 {
                     carTransferJobService.PictrueCdnFailed(carId, errmsg);
                 }
                 carId = JobQueue.CarTransferJobPictureProcessQueue.GetItem(carTransferJobService.PictrueCdning);
             }
         }
     }
 }
 /// <summary>
 /// 图片CDN失败
 /// </summary>
 /// <param name="carId">车辆帖子id</param>
 /// <param name="errorMsg">错误信息</param>
 /// <returns>是否成功</returns>
 public bool PictrueCdnFailed(int carId, string errorMsg)
 {
     using (var context = new FxCarContext())
     {
         var car = context.CarTransferInfos.Where(r => r.CarTransferInfoId == carId).FirstOrDefault();
         if (car != null)
         {
             car.InfoProcessState = (int)ProcessState.PictrueCdnFailed;
             car.Logs.Add(new Entity.FxCar.CarTransferLog()
             {
                 OperteName = Enum.GetName(typeof(ProcessState), ProcessState.PictrueCdnFailed)
             });
             context.PictureCdnErrors.Add(new PictureCdnError()
             {
                 ErorMsg = errorMsg,
                 ObjectId = carId,
                 SourceType = (int)ChannelCatagroy.FxCarTransfer
             });
             return context.SaveChanges() > 0;
         }
     }
     return false;
 }