예제 #1
0
        public ActionResult Save()
        {
            string json = Request["data"];

            if (string.IsNullOrWhiteSpace(json))
            {
                throw new BusinessException("传入数据不能为空");
            }
            Company entity = SerializationUtility.JsonDeserialize2 <Company>(json);

            SetEntityBaseUserInfo(entity);
            entity.CompanyStatus = CompanyStatus.Init;
            if (string.IsNullOrEmpty(entity.Name))
            {
                throw new BusinessException("请输入客户名称");
            }
            SystemUser customer = new SystemUser();

            //if (!string.IsNullOrEmpty(entity.ContactCellPhone))
            //{
            if (string.IsNullOrEmpty(entity.ContactName))
            {
                throw new BusinessException("请输入联系人姓名");
            }
            if (string.IsNullOrEmpty(entity.ContactCellPhone))
            {
                throw new BusinessException("请输入联系人手机号");
            }
            if (!WebPortalHelper.IsPhonenum(entity.ContactCellPhone))
            {
                throw new BusinessException("请输入正确的手机号!");
            }

            customer = SystemUserService.LoadSystemUserByLoginNameAndCellPhone(entity.ContactCellPhone, ConstValue.ApplicationID);
            // }

            Company company = CompanyService.LoadCompanyByName(entity.Name);

            if (entity.SysNo > 0)
            {
                Company curentcompany = CompanyService.LoadCompany(entity.SysNo, false);
                if (curentcompany == null)
                {
                    throw new BusinessException("未找到此公司的相关信息");
                }
                if (company != null && company.SysNo != curentcompany.SysNo)
                {
                    throw new BusinessException("系统中已存在当前客户,请重新输入客户名称!");
                }


                if (customer != null && customer.SysNo > 0 && ((!string.IsNullOrEmpty(entity.ContactCellPhone) && !string.Equals(customer.CellPhone, entity.ContactCellPhone)) || (entity.SysNo != customer.MasterSysNo)))
                {
                    throw new BusinessException("系统中已存在此手机号,请更换手机号重试!");
                }
                using (ITransaction it = TransactionManager.Create())
                {
                    //if (!string.IsNullOrEmpty(entity.ContactCellPhone))
                    //{
                    //    if (customer == null || !string.Equals(entity.ContactCellPhone, customer.CellPhone))
                    //    {
                    //        customer = new SystemUser();
                    //        customer.MasterSysNo = entity.SysNo;
                    //        customer.LoginName = customer.CellPhone = entity.ContactCellPhone;
                    //        customer.LoginPassword = AuthMgr.EncryptPassword(entity.ContactCellPhone);
                    //        customer.UserFullName = entity.ContactName;
                    //        customer.CommonStatus = CommonStatus.Actived;
                    //        SetEntityBaseUserInfo(customer);

                    //        var usersysno = user_service.InsertSystemUser(customer);
                    //        if (usersysno > 0)
                    //        {
                    //            if (rolesysno != 0)
                    //            {
                    //                List<Role> roles = new List<Role> { new Role { SysNo = rolesysno } };
                    //                usersrole.SaveUsersRole(usersysno, roles, ConstValue.ApplicationID);
                    //            }
                    //        }

                    //    }
                    //}
                    entity.CompanyStatus = curentcompany.CompanyStatus;
                    entity.AccountSysNo  = curentcompany.AccountSysNo;
                    if (curentcompany.CompanyStatus == CompanyStatus.Invalid)
                    {
                        entity.CompanyStatus = CompanyStatus.Init;
                    }
                    CompanyService.UpdateCompany(entity);
                    it.Complete();
                }
            }
            else
            {
                if (company != null)
                {
                    throw new BusinessException("系统中已存在当前客户,请重新输入客户名称!");
                }
                if (customer != null && customer.SysNo > 0)
                {
                    throw new BusinessException("系统中已存在此手机号,请更换手机号重试!");
                }
                if (!string.IsNullOrEmpty(entity.ContactCellPhone))
                {
                    customer               = new SystemUser();
                    customer.LoginName     = entity.LoginName;
                    customer.CellPhone     = "";
                    customer.LoginPassword = AuthMgr.EncryptPassword(entity.LoginName);
                    customer.UserFullName  = entity.ContactName;
                    customer.CommonStatus  = CommonStatus.Actived;
                    SetEntityBaseUserInfo(customer);
                }
                using (ITransaction it = TransactionManager.Create())
                {
                    entity.SysNo         = CompanyService.InsertCompany(entity);
                    customer.MasterSysNo = entity.SysNo;
                    if (!string.IsNullOrEmpty(entity.ContactCellPhone))
                    {
                        var usersysno = user_service.InsertSystemUser(customer);
                        if (usersysno > 0)
                        {
                            entity.AccountSysNo = usersysno;
                            CompanyService.UpdateCompany(entity);

                            if (rolesysno != 0)
                            {
                                List <Role> roles = new List <Role> {
                                    new Role {
                                        SysNo = rolesysno
                                    }
                                };
                                usersrole.SaveUsersRole(usersysno, roles, ConstValue.ApplicationID);
                            }
                        }
                    }
                    it.Complete();
                }
            }

            return(Json(new AjaxResult {
                Success = true, Message = "保存成功", Data = entity.SysNo
            }, JsonRequestBehavior.AllowGet));
        }