private FreightPartialFreeExtDTO ConvertFreightPartialFree2ExtDTO(FreightPartialFree fpf) { FreightPartialFreeExtDTO fpfDto = new FreightPartialFreeExtDTO(); fpfDto.FillWith(fpf); return(fpfDto); }
/// <summary> /// 删除运费模板 /// </summary> /// <param name="Id"></param> /// <returns></returns> public ResultDTO DeleteFreightExt(Guid Id) { try { ContextSession contextSession = ContextFactory.CurrentThreadContext; var query = FreightTemplateDetail.ObjectSet().Where(s => s.FreightTemplateId == Id).ToList(); if (query != null && query.Count > 0) { foreach (FreightTemplateDetail detail in query) { detail.EntityState = System.Data.EntityState.Deleted; contextSession.Delete(detail); } } var fpfQuery = FreightPartialFree.ObjectSet().Where(fpf => fpf.FreightTemplateId == Id); if (fpfQuery.Any()) { foreach (FreightPartialFree partialFree in fpfQuery) { partialFree.EntityState = System.Data.EntityState.Deleted; contextSession.Delete(partialFree); } } FreightTemplate tem = FreightTemplate.ObjectSet().Where(s => s.Id == Id).FirstOrDefault(); if (tem != null) { tem.EntityState = System.Data.EntityState.Deleted; contextSession.Delete(tem); } contextSession.SaveChanges(); return(new ResultDTO { ResultCode = 0, Message = "Success" }); } catch (Exception ex) { LogHelper.Error(string.Format("删除运费模板服务异常。Id:{0}", Id), ex); return(new ResultDTO { ResultCode = 1, Message = ex.Message }); } }
/// <summary> /// 保存运费模板及其明细 /// </summary> /// <param name="freightDTO">一个运费模板相关的所有信息</param> /// <returns></returns> public Jinher.AMP.BTP.Deploy.CustomDTO.ResultDTO SaveFreightTemplateFullExt(Jinher.AMP.BTP.Deploy.CustomDTO.FreightTempFullDTO freightDTO) { ResultDTO result = new ResultDTO(); try { #region 参数验证 if (freightDTO == null) { result.ResultCode = 2; result.Message = "参数错误,参数不能为空!"; return(result); } List <FreightTemplateDetailDTO> fdetailList = freightDTO.DetailList; List <FreightPartialFreeDTO> pfList = freightDTO.PartialFreeList; if (fdetailList != null && fdetailList.Any()) { foreach (FreightTemplateDetailDTO ftd in fdetailList) { if (string.IsNullOrWhiteSpace(ftd.DestinationCodes)) { result.ResultCode = 4; result.Message = "参数错误,请指定区域!"; return(result); } } } if (pfList != null && pfList.Any()) { foreach (FreightPartialFreeDTO fpf in pfList) { if (string.IsNullOrWhiteSpace(fpf.DestinationCodes)) { result.ResultCode = 5; result.Message = "参数错误,请指定包邮区域!"; return(result); } } } #endregion ContextSession contextSession = ContextFactory.CurrentThreadContext; FreightTemplate tem = null; bool isNewTemp = false; Guid tempId = freightDTO.Freight.Id; if (tempId != Guid.Empty) { tem = FreightTemplate.ObjectSet().Where(s => s.Id == freightDTO.Freight.Id).FirstOrDefault(); } else { tempId = Guid.NewGuid(); isNewTemp = true; int existNameCount = (from ft in FreightTemplate.ObjectSet() where ft.Name == freightDTO.Freight.Name && ft.AppId == freightDTO.Freight.AppId select ft.Id).Count(); if (existNameCount > 0) { result.ResultCode = 3; result.Message = "已存在相同名称的运费模板!"; return(result); } } if (tem == null) { tem = new FreightTemplate(); isNewTemp = true; } //运费模板 tem.FillWith(freightDTO.Freight); tem.Id = tempId; tem.FreightTo = string.IsNullOrWhiteSpace(freightDTO.Freight.FreightTo) ? "" : freightDTO.Freight.FreightTo; tem.ModifiedOn = DateTime.Now; if (isNewTemp) { tem.SubTime = DateTime.Now; tem.EntityState = System.Data.EntityState.Added; contextSession.SaveObject(tem); } IEnumerable <FreightTemplateDetail> detailListOri = null; IEnumerable <FreightPartialFree> partialFreeListOri = null; if (!isNewTemp) { detailListOri = from ftd in FreightTemplateDetail.ObjectSet() where ftd.FreightTemplateId == tempId select ftd; partialFreeListOri = from pf in FreightPartialFree.ObjectSet() where pf.FreightTemplateId == tempId select pf; } if (detailListOri == null) { detailListOri = new List <FreightTemplateDetail>(); } if (partialFreeListOri == null) { partialFreeListOri = new List <FreightPartialFree>(); } //模板详情。 IEnumerable <Guid> detailIdsNew = new List <Guid>(); if (fdetailList != null && fdetailList.Count > 0) { detailIdsNew = from ftd in fdetailList select ftd.Id; foreach (FreightTemplateDetailDTO detail in fdetailList) { bool isdetailNew = false; FreightTemplateDetail ftDetail = detailListOri.Where(detOri => detOri.Id == detail.Id).FirstOrDefault(); if (ftDetail == null) { ftDetail = new FreightTemplateDetail(); isdetailNew = true; } ftDetail.FillWith(detail); ftDetail.FreightTemplateId = tempId; if (isdetailNew) { ftDetail.Id = Guid.NewGuid(); ftDetail.EntityState = System.Data.EntityState.Added; contextSession.SaveObject(ftDetail); } } } //删除db中有,更新列表中没有的FreightTemplateDetail if (detailListOri.Any()) { var delDetails = from ftd in detailListOri where (!detailIdsNew.Contains(ftd.Id)) select ftd; if (delDetails.Any()) { foreach (var det in delDetails) { det.EntityState = System.Data.EntityState.Deleted; } } } //部分包邮信息。 IEnumerable <Guid> pfIdsNew = new List <Guid>(); if (pfList != null && pfList.Count > 0) { pfIdsNew = from pf in pfList select pf.Id; foreach (FreightPartialFreeDTO fpfDto in pfList) { bool isFpfNew = false; FreightPartialFree fpf = partialFreeListOri.Where(detOri => detOri.Id == fpfDto.Id).FirstOrDefault(); if (fpf == null) { fpf = new FreightPartialFree(); isFpfNew = true; } fpf.FillWith(fpfDto); fpf.FreightTemplateId = tempId; if (isFpfNew) { fpf.Id = Guid.NewGuid(); fpf.EntityState = System.Data.EntityState.Added; contextSession.SaveObject(fpf); } } } //删除db中有,更新列表中没有的FreightTemplateDetail if (partialFreeListOri.Any()) { var delpfs = from pf in partialFreeListOri where (!pfIdsNew.Contains(pf.Id)) select pf; if (delpfs.Any()) { foreach (var det in delpfs) { det.EntityState = System.Data.EntityState.Deleted; } } } contextSession.SaveChanges(); return(new ResultDTO { ResultCode = 0, Message = tempId.ToString() }); } catch (Exception ex) { LogHelper.Error(string.Format("SaveFreightTemplateFullExt服务异常。freightDTO:{0}", JsonHelper.JsonSerializer(freightDTO)), ex); return(new ResultDTO { ResultCode = 1, Message = "保存运费模板及其明细服务异常!" }); } }
/// <summary> /// 获取一条运费模板记录 /// </summary> /// <param name="freightTemplateId"></param> /// <returns></returns> public FreightDTO GetOneFreightExt(Guid freightTemplateId) { try { FreightDTO freight = null; FreightTemplate ftDTO = FreightTemplate.ObjectSet().Where(s => s.Id == freightTemplateId).FirstOrDefault(); if (ftDTO == null) { return(freight); } freight = new FreightDTO(); freight.Id = ftDTO.Id; freight.AppId = ftDTO.AppId; freight.Name = ftDTO.Name; freight.IsFreeExp = ftDTO.IsFreeExp; freight.FreightMethod = ftDTO.FreightMethod; freight.FreightTo = ftDTO.FreightTo; freight.FirstCount = ftDTO.FirstCount; freight.FirstCountPrice = ftDTO.FirstCountPrice; freight.NextCount = ftDTO.NextCount; freight.NextCountPrice = ftDTO.NextCountPrice; freight.PricingMethod = ftDTO.PricingMethod; freight.ExpressType = ftDTO.ExpressType; //运费详情。 freight.FreightDetailList = GetFreightTemplateDetailListByTemId(freightTemplateId); //部分包邮 var pfQuery = from fpf in FreightPartialFree.ObjectSet() where fpf.FreightTemplateId == ftDTO.Id select fpf; if (pfQuery.Any()) { var pfList = pfQuery.ToList().ConvertAll(ConvertFreightPartialFree2ExtDTO); try { var provList = CBCBP.Instance.GeProvinceByCountryCode(); if (provList != null && provList.Any()) { foreach (var pf in pfList) { var provNamesQ = from p in provList where ("," + pf.DestinationCodes + ",").Contains("," + p.Code + ",") select p.Name; pf.FreightTo = string.Join(",", provNamesQ); } } } catch (Exception ex) { LogHelper.Error("GetOneFreightExt中调用Jinher.AMP.CBC.IBP.Facade.GeProvinceByCountryCode异常", ex); } freight.PartialFreeList = pfList; } #region 价格区间运费 var details = FreightRangeDetails.ObjectSet().Where(predicate => predicate.TemplateId == freightTemplateId); TPS.CBCBPFacade facade = new CBCBPFacade(); if (details.Any()) { foreach (var group in details.GroupBy(selector => new { selector.ProvinceCodes, selector.IsSpecific })) { var sort = group.OrderBy(selector => selector.Min); if (!group.Key.IsSpecific) { foreach (var detail in sort) { freight.DefaultRangeFreightDetails.Add(new Deploy.CustomDTO.FreightRangeDefaultDetailsDTO { Min = detail.Min, Max = detail.Max, Cost = detail.Cost }); } continue; } freight.SpecificRangeFreightDetails.Add(new FreightRangeSpecificDetailsDTO { ProvinceNames = facade.GetProvincesNameByCode(group.Key.ProvinceCodes), ProvinceCodes = group.Key.ProvinceCodes, Details = sort.Select(selector => new Jinher.AMP.BTP.Deploy.CustomDTO.FreightRangeDefaultDetailsDTO { Min = selector.Min, Max = selector.Max, Cost = selector.Cost }).ToList() }); } } #endregion return(freight); } catch (Exception ex) { LogHelper.Error(string.Format("获取一条运费记录服务异常。freightTemplateId:{0}", freightTemplateId), ex); return(null); } }