public object Post(CustomerModel model) { if (model == null) { return Failed("客户不得为空"); } if (string.IsNullOrEmpty(model.Name)) { return Failed("名称不能为空"); } if (_customerService.GetCustomers().Any() && _customerService.GetCustomers().Any(n => n.Name == model.Name.Trim())) { return Failed("客户名称重复"); } var currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); var currentUser = HttpContext.Current.User.Identity.GetUser(); var positionId = _employeesService.GetEmployee(currentUser.EmployeeId).EmployeePostions.Where(n => n.StartDate <= currentDate && (n.EndDate == null || n.EndDate >= currentDate) && n.IsDeleted == false).Select(n => n.Position.Id).FirstOrDefault(); var item = new Customer { Id = Guid.NewGuid(), PositionId = positionId, Name = model.Name.Trim(), Address = string.IsNullOrEmpty(model.Address) ? null : model.Address.Trim(), Email = string.IsNullOrEmpty(model.Email) ? null : model.Email.Trim(), ContactPerson = string.IsNullOrEmpty(model.ContactPerson) ? null : model.ContactPerson.Trim(), TelNumber = string.IsNullOrEmpty(model.TelNumber) ? null : model.TelNumber.Trim(), Origin = string.IsNullOrEmpty(model.Origin) ? null : model.Origin.Trim(), CustomerTypeId = model.CustomerTypeModel?.Id }; try { _customerService.Insert(item); return Success(); } catch (Exception ex) { return Failed(ex.Message); } }
public object Put(CustomerModel model) { var errormessage = string.Empty; if (model == null) { errormessage = "客户不得为空"; } else if (string.IsNullOrEmpty(model.Name)) { errormessage = "名称不能为空"; } else if (_customerService.GetCustomers().Any() && _customerService.GetCustomers().Any(n => n.Name == model.Name.Trim() && n.Id != model.Id)) { errormessage = "客户名称重复"; } else { var item = _customerService.GetCustomer(model.Id); if (item.IsDeleted) { errormessage = "该客户已删除"; } if (string.IsNullOrEmpty(errormessage)) { var currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); var currentUser = HttpContext.Current.User.Identity.GetUser(); var positionId = _employeesService.GetEmployee(currentUser.EmployeeId).EmployeePostions.Where(n => n.StartDate <= currentDate && (n.EndDate == null || n.EndDate >= currentDate) && n.IsDeleted == false).Select(n => n.Position.Id).FirstOrDefault(); item.PositionId = positionId; item.Name = model.Name.Trim(); item.Address = string.IsNullOrEmpty(model.Address) ? null : model.Address.Trim(); item.Email = string.IsNullOrEmpty(model.Email) ? null : model.Email.Trim(); item.ContactPerson = string.IsNullOrEmpty(model.ContactPerson) ? null : model.ContactPerson.Trim(); item.TelNumber = string.IsNullOrEmpty(model.TelNumber) ? null : model.TelNumber.Trim(); item.Origin = string.IsNullOrEmpty(model.Origin) ? null : model.Origin.Trim(); item.CustomerTypeId = model.CustomerTypeModel != null ? model.CustomerTypeModel.Id : (Guid?)null; try { _customerService.Update(); } catch (Exception ex) { errormessage = ex.Message; } } } return string.IsNullOrEmpty(errormessage) ? Success() : Failed(errormessage); }