USE [AMES_PDC]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[人事档案管理_部门关系_增加节点]
@父节点 int = null,
@节点名称 nvarchar(100) = null,
@子节点 int out
AS
BEGIN
  SET NOCOUNT ON

  IF ISNULL(@节点名称, '') = ''
  BEGIN
    SELECT 0 AS result, N'部门名称不能为空' AS msg
    RETURN
  END

  IF ISNULL(@父节点, 0) <> 0
     AND NOT EXISTS (SELECT 1 FROM 人事档案管理_基础表_部门 WHERE 考核部门编号 = @父节点)
  BEGIN
    SELECT 0 AS result, N'上级部门不存在' AS msg
    RETURN
  END

  BEGIN TRY
    BEGIN TRAN

    INSERT INTO [人事档案管理_部门关系]([父节点], 节点排序)
    SELECT ISNULL(@父节点, 0), ISNULL(MAX(节点排序), 0) + 1
    FROM 人事档案管理_部门关系
    WHERE 父节点 = ISNULL(@父节点, 0)

    SET @子节点 = SCOPE_IDENTITY()

    INSERT INTO 人事档案管理_基础表_部门 (考核部门编号, 部门名称)
    VALUES(@子节点, @节点名称)

    COMMIT TRAN
    SELECT 1 AS result, N'添加成功' AS msg
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK TRAN
    SELECT 0 AS result, ERROR_MESSAGE() AS msg
  END CATCH
END
GO

ALTER PROCEDURE [dbo].[人事档案管理_部门关系_修改节点]
@子节点 int,
@节点名称 nvarchar(100)
AS
BEGIN
  SET NOCOUNT ON

  IF ISNULL(@节点名称, '') = ''
  BEGIN
    SELECT 0 AS result, N'部门名称不能为空' AS msg
    RETURN
  END

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_基础表_部门 WHERE 考核部门编号 = @子节点)
  BEGIN
    SELECT 0 AS result, N'部门不存在' AS msg
    RETURN
  END

  UPDATE 人事档案管理_基础表_部门
  SET [部门名称] = @节点名称
  WHERE [考核部门编号] = @子节点

  SELECT 1 AS result, N'编辑成功' AS msg
END
GO

ALTER PROCEDURE [dbo].[人事档案管理_部门关系_删除节点]
@子节点 int = null
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_部门关系 WHERE 子节点 = @子节点)
  BEGIN
    SELECT 0 AS result, N'部门不存在' AS msg
    RETURN
  END

  IF EXISTS (SELECT 1 FROM 人事档案管理_部门关系 WHERE 父节点 = @子节点)
  BEGIN
    SELECT 0 AS result, N'该部门存在下级部门，请先删除或调整下级部门' AS msg
    RETURN
  END

  IF EXISTS (
    SELECT 1
    FROM 人事档案管理_人员名单
    WHERE 考核部门编号 = @子节点
      AND ISNULL(是否离职, 0) = 0
      AND ISNULL(岗位状态编号, 1) = 1
  )
  BEGIN
    SELECT 0 AS result, N'该部门下存在在职人员，请先移动、离职或删除人员' AS msg
    RETURN
  END

  BEGIN TRY
    BEGIN TRAN
    DELETE FROM 人事档案管理_基础表_部门 WHERE [考核部门编号] = @子节点
    DELETE FROM [人事档案管理_部门关系] WHERE [子节点] = @子节点
    COMMIT TRAN
    SELECT 1 AS result, N'删除成功' AS msg
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK TRAN
    SELECT 0 AS result, ERROR_MESSAGE() AS msg
  END CATCH
END
GO

ALTER PROCEDURE [dbo].[人事档案管理_人员名单_增加数据_MESWORK]
@考核部门编号 int = null,
@性别编号 int = null,
@学历编号 int = null,
@岗位编号 int = null,
@考勤编号 nvarchar(50) = null,
@密码 nvarchar(50) = null,
@姓名 nvarchar(50) = null,
@身份证号 nvarchar(50) = null,
@入公司时间 datetime = null,
@出生日期 datetime = null,
@联系电话 nvarchar(50) = null,
@家庭住址 nvarchar(50) = null,
@民族 nvarchar(10) = null,
@班组 int = null
AS
BEGIN
  SET NOCOUNT ON

  DECLARE @人员编号 int

  IF ISNULL(@姓名, '') = ''
  BEGIN
    SELECT 0 AS result, N'姓名不能为空' AS msg
    RETURN
  END

  IF ISNULL(@考勤编号, '') = ''
  BEGIN
    SELECT 0 AS result, N'工号不能为空' AS msg
    RETURN
  END

  IF TRY_CONVERT(int, @考勤编号) IS NULL
  BEGIN
    SELECT 0 AS result, N'工号格式不正确' AS msg
    RETURN
  END

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_基础表_部门 WHERE 考核部门编号 = @考核部门编号)
  BEGIN
    SELECT 0 AS result, N'所属部门不存在，请重新选择部门' AS msg
    RETURN
  END

  IF EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 序号 = TRY_CONVERT(int, @考勤编号))
  BEGIN
    SELECT 0 AS result, N'工号已存在，不能重复新增' AS msg
    RETURN
  END

  BEGIN TRY
    BEGIN TRAN

    INSERT INTO [dbo].[人事档案管理_人员名单](
      [序号], [考勤编号], 姓名, 身份证号, 性别编号, 学历编号, 入公司时间,
      出生日期, 联系电话, 考核部门编号, 密码, 家庭住址, 民族, 班组流水号)
    VALUES(
      TRY_CONVERT(int, @考勤编号), @考勤编号, @姓名, @身份证号, @性别编号, @学历编号, @入公司时间,
      @出生日期, @联系电话, @考核部门编号, @密码, @家庭住址, @民族, @班组)

    SET @人员编号 = SCOPE_IDENTITY()

    INSERT INTO [dbo].[人事档案管理_部门与人员](部门编号, 人员编号, 岗位编号)
    VALUES(@考核部门编号, @人员编号, @岗位编号)

    COMMIT TRAN
    SELECT 1 AS result, N'添加成功' AS msg
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK TRAN
    SELECT 0 AS result, ERROR_MESSAGE() AS msg
  END CATCH
END
GO

ALTER PROCEDURE [dbo].[人事档案管理_人员名单_修改数据_MESWORK]
@人员编号 int = null,
@性别编号 nvarchar(50) = null,
@学历编号 nvarchar(50) = null,
@毕业院校编号 int = null,
@专业编号 int = null,
@外语及熟练程度编号 int = null,
@职称编号 nvarchar(50) = null,
@岗位状态编号 int = null,
@岗位编号 int = null,
@考勤编号 nvarchar(50) = null,
@密码 nvarchar(50) = null,
@姓名 nvarchar(50) = null,
@身份证号 nvarchar(50) = null,
@入公司时间 datetime = null,
@出生日期 datetime = null,
@联系电话 nvarchar(50) = null,
@家庭住址 nvarchar(50) = null,
@民族 nvarchar(10) = null,
@班组 nvarchar(50) = null
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号)
  BEGIN
    SELECT 0 AS result, N'人员不存在' AS msg
    RETURN
  END

  IF ISNULL(@姓名, '') = ''
  BEGIN
    SELECT 0 AS result, N'姓名不能为空' AS msg
    RETURN
  END

  IF ISNULL(@考勤编号, '') = ''
  BEGIN
    SELECT 0 AS result, N'工号不能为空' AS msg
    RETURN
  END

  IF TRY_CONVERT(int, @考勤编号) IS NULL
  BEGIN
    SELECT 0 AS result, N'工号格式不正确' AS msg
    RETURN
  END

  IF EXISTS (
    SELECT 1
    FROM 人事档案管理_人员名单
    WHERE 序号 = TRY_CONVERT(int, @考勤编号)
      AND 人员编号 <> @人员编号
  )
  BEGIN
    SELECT 0 AS result, N'工号已存在，不能重复保存' AS msg
    RETURN
  END

  IF(@学历编号 = 'NAN') SET @学历编号 = null
  IF(@性别编号 = 'NAN') SET @性别编号 = null
  IF(@职称编号 = 'NAN') SET @职称编号 = null
  IF(@班组 = 'NAN') SET @班组 = null

  BEGIN TRY
    BEGIN TRAN

    UPDATE [dbo].[人事档案管理_人员名单]
    SET 序号 = TRY_CONVERT(int, @考勤编号),
        考勤编号 = @考勤编号,
        姓名 = @姓名,
        身份证号 = @身份证号,
        性别编号 = TRY_CONVERT(int, @性别编号),
        学历编号 = TRY_CONVERT(int, @学历编号),
        入公司时间 = @入公司时间,
        出生日期 = @出生日期,
        联系电话 = @联系电话,
        密码 = @密码,
        家庭住址 = @家庭住址,
        民族 = @民族,
        班组流水号 = TRY_CONVERT(int, @班组)
    WHERE 人员编号 = @人员编号

    UPDATE [dbo].[人事档案管理_部门与人员]
    SET 岗位编号 = @岗位编号
    WHERE 人员编号 = @人员编号

    COMMIT TRAN
    SELECT 1 AS result, N'编辑成功' AS msg
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK TRAN
    SELECT 0 AS result, ERROR_MESSAGE() AS msg
  END CATCH
END
GO

ALTER PROCEDURE [dbo].[人事档案管理_人员名单_移动数据_MESWORK]
@考核部门编号 int = null,
@人员编号 int = null
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号)
  BEGIN
    SELECT 0 AS result, N'人员不存在' AS msg
    RETURN
  END

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_基础表_部门 WHERE 考核部门编号 = @考核部门编号)
  BEGIN
    SELECT 0 AS result, N'目标部门不存在' AS msg
    RETURN
  END

  BEGIN TRY
    BEGIN TRAN
    UPDATE [dbo].[人事档案管理_人员名单]
    SET 考核部门编号 = @考核部门编号
    WHERE 人员编号 = @人员编号

    UPDATE [dbo].[人事档案管理_部门与人员]
    SET 部门编号 = @考核部门编号
    WHERE 人员编号 = @人员编号

    COMMIT TRAN
    SELECT 1 AS result, N'移组成功' AS msg
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK TRAN
    SELECT 0 AS result, ERROR_MESSAGE() AS msg
  END CATCH
END
GO

ALTER PROCEDURE [dbo].[人事档案管理_人员名单_删除数据_MESWORK]
@人员编号 int = null
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号)
  BEGIN
    SELECT 0 AS result, N'人员不存在' AS msg
    RETURN
  END

  UPDATE 人事档案管理_人员名单
  SET 是否离职 = 1,
      岗位状态编号 = 2
  WHERE 人员编号 = @人员编号

  DELETE FROM 菜单系统模块_人员与角色
  WHERE 人员编号 = @人员编号

  SELECT 1 AS result, N'删除成功' AS msg
END
GO

ALTER PROCEDURE [dbo].[人事档案管理_人员_离职操作]
@人员编号 int
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号)
  BEGIN
    SELECT 0 AS result, N'人员不存在' AS msg
    RETURN
  END

  IF EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号 AND ISNULL(岗位状态编号, 1) = 2)
  BEGIN
    SELECT 0 AS result, N'该员工已离职' AS msg
    RETURN
  END

  UPDATE 人事档案管理_人员名单
  SET 岗位状态编号 = 2
  WHERE 人员编号 = @人员编号

  SELECT 1 AS result, N'离职成功' AS msg
END
GO

CREATE OR ALTER PROCEDURE [dbo].[人事档案管理_人员_复职操作]
@人员编号 int
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号)
  BEGIN
    SELECT 0 AS result, N'人员不存在' AS msg
    RETURN
  END

  IF EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号 AND ISNULL(是否离职, 0) <> 0)
  BEGIN
    SELECT 0 AS result, N'该员工已被删除，不能复职' AS msg
    RETURN
  END

  IF EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号 AND ISNULL(岗位状态编号, 1) = 1)
  BEGIN
    SELECT 0 AS result, N'该员工已是在职状态' AS msg
    RETURN
  END

  UPDATE 人事档案管理_人员名单
  SET 岗位状态编号 = 1,
      是否离职 = 0
  WHERE 人员编号 = @人员编号

  SELECT 1 AS result, N'复职成功' AS msg
END
GO

ALTER PROCEDURE [dbo].[人事档案管理_离职人员_删除数据]
@考勤编号 nvarchar(50) = null
AS
BEGIN
  SET NOCOUNT ON

  DECLARE @人员编号 int

  SELECT @人员编号 = 人员编号
  FROM 人事档案管理_人员名单
  WHERE 考勤编号 = @考勤编号

  IF @人员编号 IS NULL
  BEGIN
    SELECT 0 AS result, N'人员不存在' AS msg
    RETURN
  END

  IF EXISTS (
    SELECT 1
    FROM 人事档案管理_人员名单
    WHERE 人员编号 = @人员编号
      AND ISNULL(岗位状态编号, 1) <> 2
  )
  BEGIN
    SELECT 0 AS result, N'只能删除离职人员' AS msg
    RETURN
  END

  BEGIN TRY
    BEGIN TRAN
    DELETE FROM 菜单系统模块_人员与角色 WHERE 人员编号 = @人员编号
    DELETE FROM 人事档案管理_部门与人员 WHERE 人员编号 = @人员编号
    DELETE FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号
    COMMIT TRAN
    SELECT 1 AS result, N'删除成功' AS msg
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK TRAN
    SELECT 0 AS result, ERROR_MESSAGE() AS msg
  END CATCH
END
GO

ALTER PROCEDURE [dbo].[菜单系统模块_项目角色员工综合_增加数据]
@人员编号 int = null,
@角色编号 int = null,
@项目编号 int = null
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 人事档案管理_人员名单 WHERE 人员编号 = @人员编号)
  BEGIN
    SELECT 0 AS result, N'人员不存在' AS msg
    RETURN
  END

  IF EXISTS (
    SELECT 1
    FROM 人事档案管理_人员名单
    WHERE 人员编号 = @人员编号
      AND (ISNULL(是否离职, 0) <> 0 OR ISNULL(岗位状态编号, 1) <> 1)
  )
  BEGIN
    SELECT 0 AS result, N'该员工不是在职状态，不能分配角色' AS msg
    RETURN
  END

  IF NOT EXISTS (SELECT 1 FROM 菜单系统模块_项目角色 WHERE 角色编号 = @角色编号)
  BEGIN
    SELECT 0 AS result, N'角色不存在' AS msg
    RETURN
  END

  IF EXISTS (SELECT 1 FROM 菜单系统模块_人员与角色 WHERE 人员编号 = @人员编号 AND 角色编号 = @角色编号)
  BEGIN
    SELECT 0 AS result, N'该员工已分配该角色，不能重复新增' AS msg
    RETURN
  END

  INSERT INTO [菜单系统模块_人员与角色](人员编号, 角色编号, 操作时间)
  VALUES(@人员编号, @角色编号, GETDATE())

  SELECT 1 AS result, N'增加成功' AS msg
END
GO

ALTER PROCEDURE [dbo].[菜单系统模块_项目角色员工综合_修改数据]
@人员角色流水号 int = null,
@人员编号 int = null,
@角色编号 int = null
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 菜单系统模块_人员与角色 WHERE 人员角色流水号 = @人员角色流水号)
  BEGIN
    SELECT 0 AS result, N'人员角色关系不存在' AS msg
    RETURN
  END

  IF NOT EXISTS (SELECT 1 FROM 菜单系统模块_项目角色 WHERE 角色编号 = @角色编号)
  BEGIN
    SELECT 0 AS result, N'角色不存在' AS msg
    RETURN
  END

  IF EXISTS (
    SELECT 1
    FROM 菜单系统模块_人员与角色
    WHERE 人员编号 = @人员编号
      AND 角色编号 = @角色编号
      AND 人员角色流水号 <> @人员角色流水号
  )
  BEGIN
    SELECT 0 AS result, N'该员工已分配该角色，不能重复保存' AS msg
    RETURN
  END

  UPDATE [菜单系统模块_人员与角色]
  SET [角色编号] = @角色编号,
      操作时间 = GETDATE()
  WHERE [人员角色流水号] = @人员角色流水号

  SELECT 1 AS result, N'修改成功' AS msg
END
GO

ALTER PROCEDURE [dbo].[菜单系统模块_项目角色员工综合_删除数据]
@人员角色流水号 int = null
AS
BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT 1 FROM 菜单系统模块_人员与角色 WHERE 人员角色流水号 = @人员角色流水号)
  BEGIN
    SELECT 0 AS result, N'人员角色关系不存在或已删除' AS msg
    RETURN
  END

  DELETE FROM 菜单系统模块_人员与角色
  WHERE 人员角色流水号 = @人员角色流水号

  SELECT 1 AS result, N'删除成功' AS msg
END
GO
