public async Task <ApiRequestResult> AddAsync(CouponDto dto) { var command = dto.EntityMap <CouponDto, Coupon>(); command.Id = Guid.NewGuid(); if (dto.ProductRelationList.Count > 0) { var newEntitys = dto.ProductRelationList.Select(c => new CouponProductRelation { CouponId = command.Id, ProductId = c.ProductId, ProductName = c.ProductName, ProductSn = c.ProductSn, }).ToList(); await _couponProductRelationRepository.MultiAddAsync(newEntitys); } if (dto.ProductCategoryRelationList.Count > 0) { var newEntitys = dto.ProductCategoryRelationList.Select(c => new CouponProductCategoryRelation { CouponId = command.Id, ProductCategoryId = c.ProductCategoryId, ProductCategoryName = c.ProductCategoryName, ParentCategoryName = c.ParentCategoryName, }).ToList(); await _couponProductCategoryRelationRepository.MultiAddAsync(newEntitys); } await _couponRepository.AddAsync(command); return(ApiRequestResult.Success("添加成功")); }
public async Task <ApiRequestResult> UpdateAsync(CouponDto dto) { var entity = await _couponRepository.GetAsync(dto.Id.Value); if (dto.ProductRelationList.Count > 0) { var newEntitys = dto.ProductRelationList.Select(c => new CouponProductRelation { CouponId = dto.Id.Value, ProductId = c.ProductId, ProductName = c.ProductName, ProductSn = c.ProductSn, }).ToList(); await _couponProductCategoryRelationRepository.DeleteAsync(c => c.CouponId == dto.Id.Value); await _couponProductRelationRepository.MultiAddAsync(newEntitys); } if (dto.ProductCategoryRelationList.Count > 0) { var newEntitys = dto.ProductCategoryRelationList.Select(c => new CouponProductCategoryRelation { CouponId = dto.Id.Value, ProductCategoryId = c.ProductCategoryId, ProductCategoryName = c.ProductCategoryName, ParentCategoryName = c.ParentCategoryName, }).ToList(); await _couponProductRelationRepository.DeleteAsync(c => c.CouponId == dto.Id.Value); await _couponProductCategoryRelationRepository.MultiAddAsync(newEntitys); } var newEntity = dto.EntityMap(entity); await _couponRepository.UpdateAsync(newEntity); return(ApiRequestResult.Success("修改成功")); }