Exemplo n.º 1
0
        public async Task <IHttpActionResult> RegisterIotUser([FromBody] UserRegisterIotModel user)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var result = await SysUserService.AddAsync(new SysUserDto()
                    {
                        LoginName              = user.LoginName,
                        UserName               = user.Name,
                        Password               = user.Password,
                        Email                  = user.Email,
                        SysRoleSerialnum       = user.RoleCode,
                        SysDepartmentSerialnum = user.DepartmentCode,
                        Status                 = 0,
                        CreateTime             = DateTime.Now
                    });

                    if (result > 0)
                    {
                        //添加企业
                        var company = new CompanyDto
                        {
                            Serialnum              = user.DepartmentCode + "", //todo 生成企业编码
                            Name                   = user.CompanyName,
                            Addr                   = user.Location,
                            Status                 = 0,
                            CreateTime             = DateTime.Now,
                            SysDepartmentSerialnum = user.DepartmentCode,
                            ContactMan             = user.Name,
                            ContactPhone           = user.Mobile
                        };

                        var companyInsertResult = await CompanyService.AddAsync(company);

                        if (companyInsertResult > 0)
                        {
                            //添加基地
                            var farm = new FarmDto();
                            farm.Serialnum              = company.Serialnum;
                            farm.APIKey                 = Guid.NewGuid().ToString();
                            farm.UploadPassword         = "******"; //上传密码应随机生成
                            farm.Name                   = company.Name + "-默认基地";
                            farm.CompanySerialnum       = company.Serialnum;
                            farm.Address                = company.Addr;
                            farm.ContactMan             = user.Name;
                            farm.ContactPhone           = user.Mobile;
                            farm.Status                 = false;
                            farm.SysDepartmentSerialnum = user.DepartmentCode;

                            await FarmService.AddAsync(farm);
                        }
                        else
                        {
                            ModelState.AddModelError("farm", "默认基地生成失败");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("company", "企业添加失败");
                    }
                }
                catch (AggregateException ex)
                {
                    if (ex.InnerExceptions.Any())
                    {
                        ex.InnerExceptions.ForEach(e => { ModelState.AddModelError(e.HResult.ToString(), e); });
                    }
                }
            }
            else
            {
                ModelState.AddModelError("user", "用户添加失败");
            }
            return(new ApiV1Result(ModelState.IsValid ? ResultMessageType.Success : ResultMessageType.InternalError,
                                   ModelState.ToString(), null));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> RegisterIotUser([FromBody] UserRegisterIotModel user)
        {
            return(await ResultFactory.Create(ModelState, async arg =>
            {
                var result = await SysUserService.AddAsync(new SysUserDto()
                {
                    LoginName = user.LoginName,
                    UserName = user.Name,
                    Password = user.Password,
                    Email = user.Email,
                    SysRoleSerialnum = user.RoleCode,
                    SysDepartmentSerialnum = user.DepartmentCode,
                    Status = 0,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                });
                if (result > 0)
                {
                    //添加企业
                    var company = new CompanyDto
                    {
                        Serialnum = user.DepartmentCode + "",     //todo 生成企业编码
                        Name = user.CompanyName,
                        Addr = user.Location,
                        Status = 0,
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        SysDepartmentSerialnum = user.DepartmentCode,
                        ContactMan = user.Name,
                        ContactPhone = user.Mobile
                    };

                    var companyInsertResult = await CompanyService.AddAsync(company);
                    if (companyInsertResult > 0)
                    {
                        //添加基地
                        var farm = new FarmDto();
                        farm.Serialnum = company.Serialnum;
                        farm.APIKey = Guid.NewGuid().ToString();
                        farm.UploadPassword = "******";     //上传密码应随机生成
                        farm.Name = company.Name + "-默认基地";
                        farm.CompanySerialnum = company.Serialnum;
                        farm.Address = company.Addr;
                        farm.ContactMan = user.Name;
                        farm.CreateTime = DateTime.Now;
                        farm.UpdateTime = DateTime.Now;
                        farm.ContactPhone = user.Mobile;
                        farm.Status = false;
                        farm.SysDepartmentSerialnum = user.DepartmentCode;

                        await FarmService.AddAsync(farm);
                    }
                    else
                    {
                        ModelState.AddModelError("farm", "默认基地生成失败");
                    }
                }
                else
                {
                    ModelState.AddModelError("company", "企业添加失败");
                }
                return true;
            }, user, "success", "请检查请求参数"));
        }