Ejemplo n.º 1
0
        public bool SaveCustomer(BCustomer customer)
        {
            bool ret = false;
            if (customer == null)
            {
                throw new KMBitException("Customer is NULL");
            }

            if (CurrentLoginUser.IsAdmin)
            {
                //TBD
            }
            else
            {
                if (customer.AgentId > 0 && customer.AgentId != CurrentLoginUser.User.Id)
                {
                    throw new KMBitException("不能为其他代理商创建客户");
                }
            }

            using (chargebitEntities db = new chargebitEntities())
            {
                Customer dbCus = null;
                if (customer.Id > 0)
                {
                    dbCus = (from c in db.Customer where c.Id == customer.Id select c).FirstOrDefault<Customer>();
                    if (dbCus == null)
                    {
                        throw new KMBitException(string.Format("编号为{0}的客户不存在", customer.Id));
                    }

                    dbCus.Description = customer.Description != null ? dbCus.Description : dbCus.Description;
                    dbCus.ContactEmail = customer.ContactEmail;
                    dbCus.ContactAddress = customer.ContactAddress;
                    dbCus.ContactPeople = customer.ContactPeople;
                    dbCus.ContactPhone = customer.ContactPhone;
                    if (string.IsNullOrEmpty(dbCus.Token))
                    {
                        dbCus.Token = Guid.NewGuid().ToString();
                    }

                }
                else
                {
                    if (string.IsNullOrEmpty(customer.Name))
                    {
                        throw new KMBitException("客户名称不能为空");
                    }

                    Customer existed = (from c in db.Customer where c.Name == customer.Name select c).FirstOrDefault<Customer>();
                    if (existed != null)
                    {
                        throw new KMBitException(string.Format("名称为:{0}的客户已经存在", customer.Name));
                    }
                    dbCus = new Customer()
                    {
                        AgentId = customer.AgentId,
                        ContactEmail = customer.ContactEmail,
                        ContactAddress = customer.ContactAddress,
                        ContactPeople = customer.ContactPeople,
                        ContactPhone = customer.ContactPhone,
                        CreatedTime = customer.CreatedTime > 0 ? customer.CreatedTime : DateTimeUtil.ConvertDateTimeToInt(DateTime.Now),
                        CreditAmount = customer.CreditAmount,
                        Description = customer.Description,
                        Name = customer.Name,
                        OpenId = customer.OpenId,
                        OpenType = customer.OpenType,
                        RemainingAmount = customer.RemainingAmount,
                        Token = Guid.NewGuid().ToString()
                    };
                    db.Customer.Add(dbCus);
                }
                db.SaveChanges();
                ret = true;
            }

            return ret;
        }
Ejemplo n.º 2
0
        public ActionResult SaveCustomer(CreateCustomerModel model)
        {
            if(ModelState.IsValid)
            {
                CustomerManagement customerMgr = new CustomerManagement(User.Identity.GetUserId<int>());
                BCustomer customer = new BCustomer()
                {
                    AgentId = User.Identity.GetUserId<int>(),
                    ContactAddress = model.ContactAddress,
                    ContactEmail = model.ContactEmail,
                    ContactPeople = model.ContactPeople,
                    ContactPhone = model.ContactPhone,
                    CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now),
                    CreditAmount = model.CreditAmount,
                    Description = model.Description,
                    Id = model.Id,
                    Name = model.Name,
                    OpenId = model.OpenAccount,
                    OpenType = model.OpenType,
                    RemainingAmount = model.Amount
                };

                try
                {
                    if (customerMgr.SaveCustomer(customer))
                    {
                        return RedirectToAction("Customers");
                    }
                }catch(KMBitException ex)
                {
                    ViewBag.Message = ex.Message;
                }               
            }
            List<DictionaryTemplate> types = StaticDictionary.GetOpenTypeList();
            ViewBag.OpenTypes = new SelectList(types, "Id", "Value");
            return View("CreateCustomer",model);
        }