/// <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="freight"></param> /// <param name="freightDetailList"></param> /// <returns></returns> public Jinher.AMP.BTP.Deploy.CustomDTO.ResultDTO SaveFreightTemplateFull(Jinher.AMP.BTP.Deploy.CustomDTO.FreightTempFullDTO freightDTO) { base.Do(); return(this.Command.SaveFreightTemplateFull(freightDTO)); }