CREATE PROCEDURE [dbo].[ProcAccountManagerAssignCompany]
--用户账号基本信息表中所需字段
@UserName NVARCHAR(50), --用户登录的账户名称
@UserPwd NVARCHAR(50), --用户登录的密码
@AssignUserID INT, --分配此用户的管理员ID
@ParentId INT, --父ID
--企业基本信息表中所需字段
@AddUser INT, --添加者ID
@Name NVARCHAR(80), --公司名称
@Address NVARCHAR(80),--公司地址
@Tel VARCHAR(15),--联系电话
@Email VARCHAR(50),--邮箱
@PostCode VARCHAR(10)--邮政编码
AS
BEGIN
DECLARE @isExist INT,@isExistTwo INT,@CompanyExist INT,@FrameworkExist INT
SET @isExist = 0 SET @isExistTwo = 0 SET @CompanyExist=0
SELECT @isExist=COUNT(*) FROM UserAccountBasicInfo WHERE UserName=@UserName
SELECT @isExistTwo=COUNT(*) FROM CompanyBasicInfo WHERE AdminUserName=@UserName
SELECT @CompanyExist =COUNT(*) FROM CompanyBasicInfo WHERE [Name]=@Name
IF @isExist > 0
SELECT -100 --返回 -100 说明用户账号基本信息表中已存在用户名
ELSE IF @isExistTwo>0
SELECT -200 --返回 -200 说明企业信息表中用户名已存在
ELSE IF @CompanyExist >0
SELECT -300 --返回 -300 说明企业名称已经存在
ELSE -- 执行插入
BEGIN
BEGIN TRAN
BEGIN TRY
--1.往UserAccountBasicInfo表中插入账号信息
INSERT INTO UserAccountBasicInfo
(
[UserName],
[UserPwd],
[AssignUserID],
[AssignTime],
[LastUpdateTime],
[IsDeleted],
[ParentId]
)
VALUES
(
@UserName,
@UserPwd,
@AssignUserID,
GETDATE(),
GETDATE(),
0,
@ParentId
)
--2.往CompanyBasicInfo表中插入企业信息
INSERT INTO CompanyBasicInfo
(
[AdminUserName],
[Name],
[Address],
[Tel],
[Email],
[PostCode],
[IsDeleted],
[AddTime],
[AddUser],
[UpdateTime],
[UpdateUser]
)
VALUES
(
@UserName,
@Name,
@Address,
@Tel,
@Email,
@PostCode,
0,
GETDATE(),
@AddUser,
GETDATE(),
@AddUser
)
--3.往组织架构表中插入数据
DECLARE @CompanyId INT
SELECT @CompanyId = IDENT_CURRENT( 'CompanyBasicInfo' )
INSERT INTO CompanyFramework
(
[CompanyID],
[Name],
[ParentID],
[IsDeleted]
)
VALUES
(
@CompanyID,
@Name,
0,
0
)
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
END CATCH
END
END