public async Task <int> AddOrUpdate(BuyerGroup buyerGroup) { using (DatanetCMSWebEntities context = new DatanetCMSWebEntities()) { context.BuyerGroups.AddOrUpdate(buyerGroup); return(await context.SaveChangesAsync()); } }
public async Task <int> DeleteBuyerGroup(BuyerGroup buyerGroup) { using (DatanetCMSWebEntities context = new DatanetCMSWebEntities()) { buyerGroup.ExpireTime = DateTime.UtcNow; context.BuyerGroups.AddOrUpdate(buyerGroup); return(await context.SaveChangesAsync()); } }
public static BuyerGroupModel ToBuyerGroupModel(this BuyerGroup buyerGroup) { var model = new BuyerGroupModel() { Id = buyerGroup.Id, Code = buyerGroup.Code }; return(model); }
public async Task <Operate> AddOrUpdate(GroupProductModel model, string userName) { var result = new Operate(); try { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { //check duplicated group code var dupGroup = await _buyerGroupRepository.GetGroupByName(model.Code, model.Id); if (dupGroup != null) { result.Status = -2; result.Message = "Name is already taken"; return(result); } BuyerGroup buyerGroup; if (model.Id == 0) { buyerGroup = new BuyerGroup() { Code = model.Code, CreateBy = userName, CreateTime = DateTime.UtcNow }; } else { buyerGroup = await _buyerGroupRepository.GetById(model.Id); if (buyerGroup == null) { result.Status = -3; result.Message = "Buyer Group does not exist"; Logger.WriteErrorLog("BuyerGroupService", "AddOrUpdate", new Exception("Buyer Group does not exist")); return(result); } buyerGroup.Code = model.Code; buyerGroup.EditTime = DateTime.UtcNow; buyerGroup.EditBy = userName; } await _buyerGroupRepository.AddOrUpdate(buyerGroup); //update customer table's buyergroupId var customerIdList = new List <int>(); foreach (var customerItem in model.CustomerTempModelList) { customerIdList.Add(customerItem.Id); var customer = await _customerRepository.GetById(customerItem.Id); if (customer == null) { continue; } customer.BuyerGroupId = buyerGroup.Id; await _customerRepository.AddOrUpdate(customer); } var customerListByGroupPRoduct = await _customerRepository.GetByBuyerGroupId(buyerGroup.Id); foreach (var customer in customerListByGroupPRoduct) { if (customerIdList.IndexOf(customer.Id) < 0) { customer.BuyerGroupId = 0; await _customerRepository.AddOrUpdate(customer); } } //if group has been altered, set expire time now and insert new mapping relation var groupProduct = await _buyerGroupRepository.GetGroupProductByBuyerGroupId(buyerGroup.Id); if (groupProduct.Count != 0) { foreach (var item in groupProduct) { await _buyerGroupRepository.DeleteGroupProduct(item); } } foreach (var productItem in model.ProductTempModelList) { var groupProductNew = new GroupProduct() { Id = 0, BuyerGroupId = buyerGroup.Id, ProductId = productItem.ProductId, Price = productItem.Price }; await _buyerGroupRepository.AddOrUpdateGroupProduct(groupProductNew); } scope.Complete(); } } catch (Exception ex) { result.Status = -1; result.Message = ex.Message; Logger.WriteErrorLog("BuyerGroupService", "AddOrUpdate", ex); } return(result); }