namespace Tiobon.Core.Services;

/// <summary>
/// 简历模板 (服务)
/// </summary>
public class Ghrh_ResumeTemplateServices : BaseServices<Ghrh_ResumeTemplate, Ghrh_ResumeTemplateDto, InsertGhrh_ResumeTemplateInput, EditGhrh_ResumeTemplateInput>, IGhrh_ResumeTemplateServices
{
    private readonly IBaseRepository<Ghrh_ResumeTemplate> _dal;
    public Ghrh_ResumeTemplateServices(ICaching caching, IBaseRepository<Ghrh_ResumeTemplate> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
    }

    public override async Task<ServicePageResult<Ghrh_ResumeTemplateDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
    {
        var result = await base.QueryFilterPage(filter, condition, IsEnable);
        result.result.DT_TableDataT1.ForEach(t =>
        {
            t.IsMaster = t.TemplateType == "1" ? true : false;
        });
        return result;

    }
    public override async Task<bool> Update(long Id, EditGhrh_ResumeTemplateInput editModel)
    {
        return await base.Update(Id, editModel, ["TemplateName", "RemarkSz"]);
    }

    #region 是否开启
    public async Task<ServiceResult> SwitchPublish(long id, int? isPublish)
    {

        if (isPublish == 0)
            return ServiceResult.OprateFailed("当前已开启状态 不可以取消！");

        await Db.Updateable<Ghrh_ResumeTemplate>()
            .SetColumns(it => new Ghrh_ResumeTemplate()
            {
                IsPublish = 0,
                UpdateBy = App.User.ID,
                UpdateTime = DateTime.Now
            })
           .Where(it => it.IsPublish == 1)
           .ExecuteCommandAsync();
        await Db.Updateable<Ghrh_ResumeTemplate>()
            .SetColumns(it => new Ghrh_ResumeTemplate()
            {
                IsPublish = isPublish,
                UpdateBy = App.User.ID,
                UpdateTime = DateTime.Now
            })
           .Where(it => it.Id == id)
           .ExecuteCommandAsync();
        return ServiceResult.OprateSuccess();
    }
    #endregion

    #region 获取分组数据
    public async Task<ServiceResult<List<Ghrh_ResumeInfoGroupDto>>> QueryGroup(QueryForm filter)
    {

        var sql = @$"DECLARE
   @jsonParam   NVARCHAR (MAX),                                   -- 动态Json字符串
   @ID          BIGINT = {filter.id},                                                 -- ID
   @langId      INT= {LangId},                                                     -- 语言
   @userId      INT,                                                    -- 使用者
   @MenuName    NVARCHAR (100)                    -- Insert Update Copy  Query

BEGIN
   -- insert into GhrDemo.dbo.aaa(a1) select @jsonParam
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

   --
      BEGIN
      DECLARE
         @MasterTemplateID   INT = 0,
         @TemplateType       INT                                     -- 母版设定ID
      SELECT @TemplateType = TemplateType
      FROM Ghrh_ResumeTemplate
      WHERE ID = @ID
      SELECT @MasterTemplateID = ID
      FROM Ghrh_ResumeTemplate
      WHERE IsEnable = 1 AND TemplateType = 1

      SELECT *
      FROM (SELECT a.ID,
                   a.ID ResumeTemplateInfoGroupID,
                   b.ID ResumeInfoGroupID,
                   [dbo].[FLangKeyToValue](b.MKey,@langId,b.GroupName)  ResumeInfoGroupName,
                   b.GroupType,
                   b.TableName,
                   b.TablePKIDName,
                   b.CanModifyBySelf,
                   b.CanEssDisplay,
                   CASE WHEN @TemplateType = 1 THEN 1 ELSE 0 END editable, --  分组标题 是否可编辑
                   CASE
                      WHEN @TemplateType = 1 AND b.GroupType = 'Other' THEN 1
                      ELSE 0
                   END removable,                                  --  群组是否可删除
                   CASE
                      WHEN b.GroupType = 'Base' OR b.GroupType = 'Photo'
                      THEN
                         0
                      ELSE
                         1
                   END canDrag,                                    --  群组是否可拖拽
                   CASE WHEN b.GroupType = 'Base' THEN 0 ELSE 1 END canHide, --  群组是否可隐藏
                   CASE
                      WHEN        @TemplateType = 1
                              AND b.GroupType ! = 'Attachment'
                           OR @TemplateType ! = 1 AND b.GroupType = 'Base'
                      THEN
                         1
                      ELSE
                         0
                   END configurable,                               --  是否可挑选栏位
                   ISNULL (a.IsDisplay, 0) isDisplay,
                   isnull (a.SortNo, b.SortNo) SortNo,
                   -- case when ISNULL(a.IsDisplay,0)=0 then ''
                   CASE
                      WHEN 1 = 2
                      THEN
                         ''
                      ELSE
                         CASE
                            WHEN b.GroupType = 'Base'               -- 读取各模板设定
                            THEN
                               isnull
                               (
                                  (SELECT [dbo].[FLangKeyToValue](pp.MKey,@langId,pp.ResumeInfoColumnName) ResumeInfoColumnName,
                                          pp.Id ResumeInfoColumnId,
                                          pp.IsSingleColumn,
                                          pp.DataType,
                                          pp.ColumnType,
                                          kk.SortNo,
                                          pp.Placeholder,
                                          pp.CanModifyBySelf
                                   FROM Ghrh_ResumeTemplateInfoGroupColumn kk,
                                        Ghrh_ResumeInfoColumn pp
                                   WHERE     kk.ResumeTemplateInfoGroupID =
                                             a.ID
                                         -- a.ResumeTemplateInfoGroupID
                                         AND kk.ResumeInfoColumnID = pp.ID
                                         AND kk.IsDisplay = 1
                                         AND pp.IsEnable = 1
                                         AND kk.IsEnable = 1
                                    order by kk.SortNo
                                   FOR JSON PATH, INCLUDE_NULL_VALUES),
                                  '')
                            WHEN b.GroupType = 'Photo'              -- 读取各模板设定
                            THEN
                               (SELECT PhotoType
                                FROM Ghrh_ResumeTemplate
                                WHERE ResumeTemplateID = @MasterTemplateID
                                FOR JSON PATH, INCLUDE_NULL_VALUES)
                            WHEN b.GroupType NOT IN ('Base', 'Photo') -- 读取母版的设定
                            THEN
                               isnull
                               (
                                  (SELECT [dbo].[FLangKeyToValue](pp.MKey,@langId,pp.ResumeInfoColumnName) ResumeInfoColumnName,
                                          pp.Id ResumeInfoColumnId,
                                          pp.IsSingleColumn,
                                          pp.DataType,
                                          pp.ColumnType,
                                          kk.SortNo,
                                          pp.Placeholder,
                                          pp.CanModifyBySelf
                                   FROM Ghrh_ResumeTemplateInfoGroupColumn kk,
                                        Ghrh_ResumeInfoColumn pp
                                   WHERE     kk.ResumeTemplateID =
                                             @MasterTemplateID
                                         AND kk.ResumeInfoGroupId = b.Id
                                         -- b.ResumeInfoGroupId
                                         AND pp.Id = kk.ResumeInfoColumnId
                                         AND kk.IsDisplay = 1
                                         AND pp.IsEnable = 1
                                         AND kk.IsEnable = 1
                                     order by kk.SortNo
                                   FOR JSON PATH, INCLUDE_NULL_VALUES),
                                  '')
                         END
                   END GroupColumn1
            FROM Ghrh_ResumeInfoGroup b
                 LEFT JOIN Ghrh_ResumeTemplateInfoGroup a
                    ON     a.ResumeInfoGroupId = b.ID
                       AND a.IsEnable = 1                 -- and a.IsDisplay=1
                       AND a.ResumeTemplateID = @ID
            WHERE b.IsEnable = 1) a
      -- order by ISNULL(a.IsDisplay,0) desc,isnull(a.SortNo,b.SortNo)  -- 显示的放上面, 不显示的放下面, 再按照序号排序
      ORDER BY a.SortNo ASC                        -- 显示的放上面, 不显示的放下面, 再按照序号排序
   END
END";

        var entitys = await Db.Ado.SqlQueryAsync<Ghrh_ResumeInfoGroupDto>(sql);

        entitys.ForEach(x =>
        {
            if (x.GroupColumn1.IsNotEmptyOrNull())
            {
                x.GroupColumn = JsonHelper.JsonToObj<List<Dictionary<string, object>>>(x.GroupColumn1);
            }
            x.GroupColumn1 = null;
        });

        return ServiceResult<List<Ghrh_ResumeInfoGroupDto>>.OprateSuccess("查询成功！", entitys);

    }
    #endregion

    #region 切换照片类型
    public async Task<ServiceResult> PhotoSwitch(long id, int? photoType)
    {
        await Db.Updateable<Ghrh_ResumeTemplate>()
            .SetColumns(it => new Ghrh_ResumeTemplate()
            {
                PhotoType = photoType,
                UpdateBy = App.User.ID,
                UpdateTime = DateTime.Now
            })
           .Where(it => it.Id == id)
           .ExecuteCommandAsync();
        return ServiceResult.OprateSuccess();
    }
    #endregion

    #region 模板栏位异动
    public async Task<ServiceResult> ColumnMove(long id, List<ResumeTemplateColumnMove> columns)
    {
        QueryForm1 filter = new QueryForm1();
        filter.jsonParam = JsonHelper.ObjToJson(columns);
        filter.id = id;
        filter.langId = 1;
        filter.userId = App.User.ID.ObjToString();
        filter.doType = "ColumnMove";
        await PT_GHR30_StaffTemplate_IUD(filter);
        return ServiceResult.OprateSuccess();
    }
    #endregion

    #region 获取分组数据
    public async Task<ServiceResult<List<ResumeTemplateGroupColumn>>> QueryTemplateColumn(QueryForm filter)
    {
        var sql = @$"DECLARE @ResumeInfoGroupId   INT = 0
                    DECLARE @ID   INT = {filter.id}
                    SELECT @ResumeInfoGroupId = ResumeInfoGroupId
                    FROM Ghrh_ResumeTemplateInfoGroup
                    WHERE ID = @ID

                    SELECT b.ID ResumeInfoColumnID,
                           b.ResumeInfoColumnName,
                           a.SortNo,
                           b.IsBuildIn,
                           isnull (a.IsRequired, b.IsRequired) IsRequired,
                           1  Removable,                                           -- 是否可以从  已选中移除
                           CASE WHEN b.IsRelease = 0 THEN -1 ELSE isnull (a.IsDisplay, 0) END DisplayType -- 判断是 1;已选择  0:可选择  -1: 未发布的栏位
                    FROM Ghrh_ResumeInfoColumn b
                         LEFT JOIN Ghrh_ResumeTemplateInfoGroupColumn a
                            ON     a.ResumeTemplateInfoGroupID = @ID
                               AND a.IsEnable = 1
                               AND a.ResumeInfoColumnID = b.ID
                    WHERE b.IsEnable = 1 AND b.ResumeInfoGroupId = @ResumeInfoGroupId
                    ORDER BY a.SortNo";

        var entitys = await Db.Ado.SqlQueryAsync<ResumeTemplateGroupColumn>(sql);

        return ServiceResult<List<ResumeTemplateGroupColumn>>.OprateSuccess("查询成功！", entitys);

    }
    #endregion

    #region 执行模板栏位变更
    public async Task<ServiceResult> ExceteTemplateColumnChange(QueryForm filter)
    {
        var sql = string.Empty;
        var paramSql = @$"DECLARE
                           @jsonParam   NVARCHAR (MAX) = '{filter.jsonParam}', -- 查询条件通过Json格式回传 数据库
                           @doType      NVARCHAR (100) = '{filter.doType}', -- Insert:新增  Update:修改  Delete:删除 Unpublish:取消发布  BatchDelete:批次删除 BatchUpdaate:多行数据更新
                           -- SortData: 修改序号和是否默认
                           @id          bigint = {filter.id},                  -- 表的唯一值。自增长的值   Delete /Update 用到
                           @langId      INT = {filter.langId},                                                 -- 语言
                           @userId      INT = {filter.userId},                                                -- 使用者
                           @menuName    NVARCHAR (1000) = '{filter.menuName}' ";

        switch (filter.doType)
        {
            case "Delete":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE
      @ResumeTemplateInfoGroupID   NVARCHAR (1000),
      @ResumeInfoColumnNo          NVARCHAR (1000),
      @ResumeInfoColumnName        NVARCHAR (1000),
      @MKey                        NVARCHAR (1000),
      @TableName                   NVARCHAR (1000),
      @ColumnName                  NVARCHAR (1000),
      @MapTableName                NVARCHAR (1000),
      @MapColumnName               NVARCHAR (1000),
      @IsSingleColumn              NVARCHAR (1000),
      @ColumnType                  NVARCHAR (1000),
      @DataType                    NVARCHAR (1000),
      @DataSourceType              NVARCHAR (1000),
      @DataSource                  NVARCHAR (1000),
      @IsRequired                  NVARCHAR (1000),
      @IsChangeColumn              NVARCHAR (1000),
      @IsQuickJoinColumn           NVARCHAR (1000),
      @Editable                    NVARCHAR (1000),
      @IsBuildIn                   NVARCHAR (1000),
      @InitValue                   NVARCHAR (1000),
      @Placeholder                 NVARCHAR (2000),
      @RemarkSz                    NVARCHAR (2000),
      @ResumeInfoGroupId           INT = 0,
      @GroupType                   NVARCHAR (100),
      @TemplateType                INT,
      @IsRelease                   INT,
      @CanModifyBySelf             INT,
      @multipleSelect              NVARCHAR (100),
      @CanEssDisplay               INT

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeInfoColumn                                -- 开始事物处理

      -- 写入日志
      -- EXEC [dbo].[PS_OperateLog]@userId, @doType, @menuName, 'Ghrh_ResumeInfoColumn', @ID, @jsonParam, @OperateLogID OUTPUT

      IF @doType = 'Delete'                                            -- 资料删除
         BEGIN
            UPDATE Ghrh_ResumeInfoColumn
            SET OperateLogID = @OperateLogID,
                IsEnable = 0,
                UpdateBy = @userId,
                UpdateTime = GETDATE (),
                UpdateProg = @menuName
            WHERE Id = @ID

            -- 删除对应模板里面设定的字段 micro 2023-09-14 add
            UPDATE Ghrh_ResumeTemplateInfoGroupColumn
            SET OperateLogID = @OperateLogID,
                IsEnable = 0,
                UpdateBy = @userId,
                UpdateTime = GETDATE (),
                UpdateProg = @menuName
            WHERE ResumeInfoColumnId = @ID

            SET @SuccessMsg = '删除成功'

            SELECT 'true' Success,
                   @ErrorMsg ErrorMsg,
                   @WarningMsg WarnMsg,
                   @SuccessMsg 'Message'               -- IsOK 0:代表正确,  1:代表错误

            COMMIT TRAN tranResumeInfoColumn
            RETURN
         END 

      COMMIT TRAN tranResumeInfoColumn                               -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeInfoColumn                         -- 退回数据库的操作记录

      SELECT 'false' Success,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg 'Message'                     -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 'true' Success,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg 'Message'                        -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "Unpublish":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE
      @ResumeTemplateInfoGroupID   NVARCHAR (1000),
      @ResumeInfoColumnNo          NVARCHAR (1000),
      @ResumeInfoColumnName        NVARCHAR (1000),
      @MKey                        NVARCHAR (1000),
      @TableName                   NVARCHAR (1000),
      @ColumnName                  NVARCHAR (1000),
      @MapTableName                NVARCHAR (1000),
      @MapColumnName               NVARCHAR (1000),
      @IsSingleColumn              NVARCHAR (1000),
      @ColumnType                  NVARCHAR (1000),
      @DataType                    NVARCHAR (1000),
      @DataSourceType              NVARCHAR (1000),
      @DataSource                  NVARCHAR (1000),
      @IsRequired                  NVARCHAR (1000),
      @IsChangeColumn              NVARCHAR (1000),
      @IsQuickJoinColumn           NVARCHAR (1000),
      @Editable                    NVARCHAR (1000),
      @IsBuildIn                   NVARCHAR (1000),
      @InitValue                   NVARCHAR (1000),
      @Placeholder                 NVARCHAR (2000),
      @RemarkSz                    NVARCHAR (2000),
      @ResumeInfoGroupId           INT = 0,
      @GroupType                   NVARCHAR (100),
      @TemplateType                INT,
      @IsRelease                   INT,
      @CanModifyBySelf             INT,
      @multipleSelect              NVARCHAR (100),
      @CanEssDisplay               INT

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeInfoColumn                                -- 开始事物处理

      -- 写入日志
      -- EXEC [dbo].[PS_OperateLog]@userId, @doType, @menuName, 'Ghrh_ResumeInfoColumn', @ID, @jsonParam, @OperateLogID OUTPUT

      IF @doType = 'Unpublish'                                         -- 取消发布
         BEGIN
            -- 写入日志
            --exec [dbo].[PS_OperateLog] @userId,'Unpublish','/ResumeInfoColumn','Ghrh_ResumeInfoColumn',@ID, @OperateLogID output
            UPDATE Ghrh_ResumeInfoColumn
            SET OperateLogID = @OperateLogID,
                IsRelease = 0,
                UpdateBy = @userId,
                UpdateTime = GETDATE (),
                UpdateProg = @menuName
            WHERE Id = @ID

            SET @SuccessMsg = '取消成功'

            SELECT 'true' Success,
                   @ErrorMsg ErrorMsg,
                   @WarningMsg WarnMsg,
                   @SuccessMsg 'Message'               -- IsOK 0:代表正确,  1:代表错误


            COMMIT TRAN tranResumeInfoColumn
            RETURN
         END

      COMMIT TRAN tranResumeInfoColumn                               -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeInfoColumn                         -- 退回数据库的操作记录

      SELECT 'false' Success,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg 'Message'                     -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 'true' Success,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg 'Message'                        -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "Update":
            case "Insert":
            case "Copy":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE
      @ResumeTemplateInfoGroupID   NVARCHAR (1000),
      @ResumeInfoColumnNo          NVARCHAR (1000),
      @ResumeInfoColumnName        NVARCHAR (1000),
      @MKey                        NVARCHAR (1000),
      @TableName                   NVARCHAR (1000),
      @ColumnName                  NVARCHAR (1000),
      @MapTableName                NVARCHAR (1000),
      @MapColumnName               NVARCHAR (1000),
      @IsSingleColumn              NVARCHAR (1000),
      @ColumnType                  NVARCHAR (1000),
      @DataType                    NVARCHAR (1000),
      @DataSourceType              NVARCHAR (1000),
      @DataSource                  NVARCHAR (1000),
      @IsRequired                  BIT,
      @IsChangeColumn              NVARCHAR (1000),
      @IsQuickJoinColumn           NVARCHAR (1000),
      @Editable                    NVARCHAR (1000),
      @IsBuildIn                   NVARCHAR (1000),
      @InitValue                   NVARCHAR (1000),
      @Placeholder                 NVARCHAR (2000),
      @RemarkSz                    NVARCHAR (2000),
      @ResumeInfoGroupId           INT = 0,
      @GroupType                   NVARCHAR (100),
      @TemplateType                INT,
      @IsRelease                   INT,
      @CanModifyBySelf             INT,
      @multipleSelect              NVARCHAR (100),
      @CanEssDisplay               INT,
      @QueryCondition               INT

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeInfoColumn                                -- 开始事物处理

      -- 写入日志
      -- EXEC [dbo].[PS_OperateLog]@userId, @doType, @menuName, 'Ghrh_ResumeInfoColumn', @ID, @jsonParam, @OperateLogID OUTPUT

      IF @doType IN ('Update', 'Insert', 'Copy')
         BEGIN
            IF @jsonParam ! = ''                                    -- 传入的参数处理
               BEGIN
                  SELECT @ResumeTemplateInfoGroupID =
                            ResumeTemplateInfoGroupID,
                         @ResumeInfoColumnNo = ResumeInfoColumnNo,
                         @ResumeInfoColumnName = ResumeInfoColumnName,
                         @MKey = MKey,
                         @TableName = TableName,
                         @ColumnName = ColumnName,
                         @MapTableName = MapTableName,
                         @MapColumnName = MapColumnName,
                         @IsSingleColumn = IsSingleColumn,
                         @ColumnType = ColumnType,
                         @DataType = DataType,
                         @DataSourceType = DataSourceType,
                         @DataSource = DataSource,
                         @IsRequired = IsRequired,
                         @Editable = Editable,
                         @IsBuildIn = IsBuildIn,
                         @InitValue = InitValue,
                         @Placeholder = Placeholder,
                         @RemarkSz = RemarkSz,
                         @IsRelease = IsRelease,
                         @IsChangeColumn = IsChangeColumn,
                         @IsQuickJoinColumn = IsQuickJoinColumn,
                         @CanModifyBySelf = CanModifyBySelf,
                         @CanEssDisplay = CanEssDisplay,
                         @QueryCondition = QueryCondition,
                         @multipleSelect = multipleSelect
                  FROM OPENJSON (@jsonParam) WITH (
                                               ResumeTemplateInfoGroupID NVARCHAR (1000),
                                               ResumeInfoColumnNo NVARCHAR (1000),
                                               ResumeInfoColumnName NVARCHAR (1000),
                                               MKey NVARCHAR (1000),
                                               TableName NVARCHAR (1000),
                                               ColumnName NVARCHAR (1000),
                                               MapTableName NVARCHAR (1000),
                                               MapColumnName NVARCHAR (1000),
                                               IsSingleColumn NVARCHAR (1000),
                                               ColumnType NVARCHAR (1000),
                                               DataType NVARCHAR (1000),
                                               DataSourceType NVARCHAR (1000),
                                               DataSource NVARCHAR (1000),
                                               IsRequired NVARCHAR (1000),
                                               Editable NVARCHAR (1000),
                                               IsBuildIn NVARCHAR (1000),
                                               InitValue NVARCHAR (1000),
                                               Placeholder NVARCHAR (1000),
                                               RemarkSz NVARCHAR (1000),
                                               IsRelease INT,
                                               IsChangeColumn INT,
                                               IsQuickJoinColumn INT,
                                               CanModifyBySelf INT,
                                               CanEssDisplay INT,
                                               QueryCondition INT,
                                               multipleSelect NVARCHAR (100))
                  SET @CanModifyBySelf = isnull (@CanModifyBySelf, 0)
                  SET @CanEssDisplay = isnull (@CanEssDisplay, 0)
                  SET @QueryCondition = isnull (@QueryCondition, 0)

                  IF @ColumnType IN ('Input',
                                     'InputNumber',
                                     'InputTextArea',
                                     'GDatePicker',
                                     'DatePicker',
                                     'FileUpload',
                                     'Switch')
                     SET @DataSourceType = ''                      -- 修改数据源的处理
               END

            -- 设定单  设定单群组  设定单类别  设定群组类别
            SELECT @ResumeInfoGroupId = ResumeInfoGroupId,
                   @TemplateType = TemplateType,
                   @GroupType = GroupType
            FROM Ghrh_ResumeTemplateInfoGroup
            WHERE ID = @ResumeTemplateInfoGroupID                 -- 取得本次单据的设定

            -- 资料验证
            BEGIN
               IF @TableName = ''
                  SET @ErrorMsg = @ErrorMsg + '表名不能为空' --  [dbo].[FLangKeyToValue]('F_ResumeInfoColumn_0001',@langId,'集团数据归属名称不能为空')

               -- 栏位名称不能重复被使用
               IF (SELECT count (1)
                   FROM Ghrh_ResumeInfoColumn
                   WHERE     IsEnable = 1
                         AND Id ! = @id
                         AND TableName = @TableName
                         AND ColumnName = @ColumnName) >
                  0
                  SET @ErrorMsg =
                           @ErrorMsg
                         + '此栏位名称已经被使用,不能重复使用'

               IF @MapTableName = ''
                  SET @ErrorMsg = @ErrorMsg + '关联表名不能为空' --  [dbo].[FLangKeyToValue]('F_ResumeInfoColumn_0001',@langId,'集团数据归属名称不能为空')

               -- 栏位名称不能重复被使用
               IF (SELECT count (1)
                   FROM Ghrh_ResumeInfoColumn
                   WHERE     IsEnable = 1
                         AND Id ! = @id
                         AND MapTableName = @MapTableName
                         AND MapColumnName = @MapColumnName) >
                  0
                  SET @ErrorMsg =
                           @ErrorMsg
                         + '此栏位名称已经被使用,不能重复使用'

               -- 下拉必须选择 数据源
               IF @ColumnType = 'ApiSelect'
                  BEGIN
                     IF @DataSourceType = '' OR @DataSourceType IS NULL
                        SET @ErrorMsg =
                                 @ErrorMsg
                               + '下拉框必须有数据来源方式'

                     IF    @DataSource = ''
                        OR @DataSource IS NULL AND @ErrorMsg ! = ''
                        SET @ErrorMsg =
                               @ErrorMsg + '下拉框必须有数据来源'
                  END

               IF @ColumnType IN
                     ('OrgTreeSelect', 'ResumeSelect', 'ResumeSearch')
                  BEGIN
                     IF @DataSourceType = '' OR @DataSourceType IS NULL
                        SET @ErrorMsg =
                                 @ErrorMsg
                               + '组织或人员选择必须有数据来源方式'
                  END

               -- 数据库字段的存储方式,适用的栏位类型
               DECLARE @DBDateType   NVARCHAR (100) = '' -- bigint  date  datetime  decimal  int  nvarchar
               /* Input 输入框
               ApiSelect 下拉框
               ApiRadioGroup 单选按钮
               Switch 开关
               InputTextArea 多行输入
               GDatePicker 日期选择
               Checkbox 多选框
               OrgTreeSelect 组织树选择框
               FileUpload 附件
               ResumeSelect 选择人员
               Divider 分割线
               ResumeSearch 员工模糊搜索
               DateTypeSelect DateType选择
               YearPicker 年度选择
               YearMonthPicker 年月选择
               GTimePicker 时间选择
               FormSubTable 子表格
               GCascader 级联选择
               InputNumber 数字输入框  */
               /* ParaDetail 参数明细
      CommonList 通用下拉
      OrgTreeWithPriv 权限内组织图
      OrgTreeWithoutPriv 全部组织图
      ParaDetailNo 参数明细编号
      ResumeWithoutPriv 全部员工选择
      ResumeWithPriv 权限内员工选择  */
               SELECT @DBDateType =
                         isnull
                         (
                            (SELECT (SELECT kk.name
                                     FROM sys.types kk
                                     WHERE kk.user_type_id = pp.user_type_id)
                             FROM sys.columns pp
                             WHERE     object_id =
                                       (SELECT id
                                        FROM sysobjects
                                        WHERE     xtype = 'u'
                                              AND name = @TableName)
                                   AND name = @ColumnName),
                            '')

               IF @ColumnType = 'Switch' AND @DBDateType ! = 'int'
                  SET @ErrorMsg =
                         @ErrorMsg + '只有int,才可以用做开关按钮'

               IF @ColumnType = 'Checkbox' AND @DBDateType ! = 'nvarchar'
                  SET @ErrorMsg =
                           @ErrorMsg
                         + '只有nvarchar,才可以用做多选框'

               IF     @ColumnType = 'InputTextArea'
                  AND @DBDateType ! = 'nvarchar'
                  SET @ErrorMsg =
                           @ErrorMsg
                         + '只有nvarchar,才可以用做多行输入'

               IF     @ColumnType = 'InputNumber'
                  AND @DBDateType NOT IN ('decimal', 'int')
                  SET @ErrorMsg =
                           @ErrorMsg
                         + '只有int/decimal,才可以用做数字输入框'

               IF @ColumnType = 'ApiSelect'
                  BEGIN
                     IF     @DataSourceType = 'ParaDetail'
                        AND @DBDateType ! = 'int'
                        SET @ErrorMsg =
                                 @ErrorMsg
                               + '只有int,才可以用做下拉参数明细'

                     IF     @DataSourceType = 'ParaDetailNo'
                        AND @DBDateType ! = 'nvarchar'
                        SET @ErrorMsg =
                                 @ErrorMsg
                               + '只有nvarchar,才可以用做下拉参数明细编号'

                     IF     @DataSourceType = 'CommonList'
                        AND @DBDateType ! = 'int'
                        SET @ErrorMsg =
                                 @ErrorMsg
                               + '只有int,才可以用做通用下拉'
                  END

               IF     @DBDateType = 'int'
                  AND @DataType NOT IN ('int', 'decimal')
                  AND @ErrorMsg = ''
                  BEGIN
                     SET @ErrorMsg =
                            @ErrorMsg + 'int只能用作数字输入'
                  END

               IF     @DBDateType = 'decimal'
                  AND @DataType NOT IN ('int', 'decimal')
                  AND @ErrorMsg = ''
                  BEGIN
                     SET @ErrorMsg =
                            @ErrorMsg + 'decimal只能用作数字输入'
                  END

               IF     @DBDateType = 'date'
                  AND @DataType NOT IN ('date',
                                        'datetime',
                                        'year',
                                        'yearMonth')
                  AND @ErrorMsg = ''
                  BEGIN
                     SET @ErrorMsg =
                            @ErrorMsg + 'date只能用作日期输入'
                  END

               IF @ErrorMsg ! = ''
                  BEGIN
                     ROLLBACK TRAN tranResumeInfoColumn          -- 退回数据库的操作记录

                     SELECT 'fale' Success,
                            @ErrorMsg ErrorMsg,
                            @WarningMsg WarnMsg,
                            @SuccessMsg 'Message'      -- IsOK 0:代表正确,  1:代表错误

                     RETURN
                  END
            END

            IF @ID > 0                                  -- @doType =  'Update'
               BEGIN
                  --exec [dbo].[PS_OperateLog] 1,'Insert','/ResumeInfoColumn','Ghrh_ResumeInfoColumn',1, @OperateLogID output
                  SET @doType = 'Update'
                  -- 该项目是否已在模板  分组的栏位里面, 如果有,则修改,没有则新增
                  DECLARE @ResumeTemplateInfoGroupColumnId   INT = 0
                  SELECT @ResumeTemplateInfoGroupColumnId = Id
                  FROM Ghrh_ResumeTemplateInfoGroupColumn
                  WHERE     IsEnable = 1
                        AND ResumeTemplateInfoGroupId =
                            @ResumeTemplateInfoGroupID
                        AND ResumeInfoColumnID = @ID

                  IF @ResumeTemplateInfoGroupColumnId > 0              -- 修改数据
                     UPDATE Ghrh_ResumeTemplateInfoGroupColumn
                     SET IsRequired = @IsRequired,
                         Editable = @Editable,
                         InitValue = @InitValue
                     WHERE Id = @ResumeTemplateInfoGroupColumnId
                  ELSE                                                 -- 新增数据
                     BEGIN
                        INSERT INTO Ghrh_ResumeTemplateInfoGroupColumn (
                                       ResumeTemplateInfoGroupId,
                                       ResumeTemplateId,
                                       TemplateType,
                                       ResumeInfoGroupId,
                                       GroupType,
                                       ResumeInfoColumnID,
                                       IsRequired,
                                       Editable,
                                       InitValue,
                                       IsDisplay,
                                       SortNo,
                                       RowNum,
                                       ColNum)
                           SELECT a.Id
                                     ResumeTemplateInfoGroupId,
                                  ResumeTemplateId,
                                  a.TemplateType,
                                  a.ResumeInfoGroupId,
                                  a.GroupType,
                                  b.ID
                                     ResumeInfoColumnID,
                                  @IsRequired,
                                  @Editable,
                                  @InitValue,
                                  0,
                                  (SELECT isnull (max (SortNo), 0) + 1
                                   FROM Ghrh_ResumeTemplateInfoGroup
                                   WHERE     IsDisplay = 0
                                         AND IsEnable = 1
                                         AND Id =
                                             @ResumeTemplateInfoGroupColumnId),
                                  1,
                                  1
                           FROM Ghrh_ResumeTemplateInfoGroup a,
                                Ghrh_ResumeInfoColumn b
                           WHERE     a.Id = @ResumeTemplateInfoGroupColumnId
                                 AND b.ID = @id
                     END

                  IF @TemplateType = 1                                -- 母版的修改
                     UPDATE Ghrh_ResumeInfoColumn
                     SET ResumeInfoGroupId = @ResumeInfoGroupId,
                         ResumeInfoColumnNo = @ResumeInfoColumnNo,
                         ResumeInfoColumnName = @ResumeInfoColumnName,
                         MKey = @MKey,
                         TableName = @TableName,
                         ColumnName = @ColumnName,
                         MapTableName = @MapTableName,
                         MapColumnName = @MapColumnName,
                         IsSingleColumn = @IsSingleColumn,
                         ColumnType = @ColumnType,
                         DataType = @DataType,
                         DataSourceType = @DataSourceType,
                         CanModifyBySelf = @CanModifyBySelf,
                         CanEssDisplay = @CanEssDisplay,
                         QueryCondition = @QueryCondition,
                         multipleSelect = @multipleSelect,
                         DataSource = @DataSource,
                         IsRequired = @IsRequired,
                         Editable = @Editable,
                         IsBuildIn = @IsBuildIn,
                         InitValue = @InitValue,
                         Placeholder = @Placeholder,
                         RemarkSz = @RemarkSz,
                         IsRelease = @IsRelease,
                         IsChangeColumn = @IsChangeColumn,
                         IsQuickJoinColumn = @IsQuickJoinColumn,
                         OperateLogID = @OperateLogID,
                         UpdateBy = @userId,
                         UpdateTime = GETDATE (),
                         UpdateProg = @menuName
                     WHERE Id = @ID
               END

            IF @ID = 0                                  -- @doType =  'Insert'
               BEGIN
                  SET @doType = 'Insert'

                  SET @ID = '{SnowFlakeSingle.Instance.NextId()}'

                  --exec [dbo].[PS_OperateLog] 1,'Insert','/ResumeInfoColumn','Ghrh_ResumeInfoColumn',1, @OperateLogID output
                  INSERT INTO [dbo].[Ghrh_ResumeInfoColumn] (
                                 id,
                                 ResumeInfoGroupId,
                                 ResumeInfoColumnNo,
                                 ResumeInfoColumnName,
                                 MKey,
                                 TableName,
                                 ColumnName,
                                 MapTableName,
                                 MapColumnName,
                                 IsSingleColumn,
                                 ColumnType,
                                 DataType,
                                 DataSourceType,
                                 DataSource,
                                 IsRequired,
                                 Editable,
                                 IsBuildIn,
                                 InitValue,
                                 Placeholder,
                                 RemarkSz,
                                 IsRelease,
                                 IsChangeColumn,
                                 IsQuickJoinColumn,
                                 SortNo,
                                 CanModifyBySelf,
                                 CanEssDisplay,
                                 QueryCondition,
                                 multipleSelect,
                                 OperateLogID,
                                 [CreateBy],
                                 [CreateTime],
                                 [CreateProg],
                                 [CreateIP],
                                 [UpdateBy],
                                 [UpdateTime],
                                 [UpdateProg],
                                 [UpdateIP])
                     SELECT @ID,
                            @ResumeInfoGroupId,
                            @ResumeInfoColumnNo,
                            @ResumeInfoColumnName,
                            @MKey,
                            @TableName,
                            @ColumnName,
                            @MapTableName,
                            @MapColumnName,
                            @IsSingleColumn,
                            @ColumnType,
                            @DataType,
                            @DataSourceType,
                            @DataSource,
                            @IsRequired,
                            @Editable,
                            @IsBuildIn,
                            @InitValue,
                            @Placeholder,
                            @RemarkSz,
                            @IsRelease,
                            @IsChangeColumn,
                            @IsQuickJoinColumn,
                            1,
                            @CanModifyBySelf,
                            @CanEssDisplay,
                            @QueryCondition,
                            @multipleSelect,
                            @OperateLogID,
                            @userId,
                            GETDATE (),
                            @menuName,
                            '0',
                            @userId,
                            GETDATE (),
                            @menuName,
                            '0'
               END

            -- 非异动栏位,不显示在此
            BEGIN
               IF @ColumnName ! = 'ResumeID'
                  BEGIN
                     UPDATE kk
                     SET kk.IsEnable = 0,
                         kk.UpdateBy = @userId,
                         kk.UpdateTime = GETDATE (),
                         kk.UpdateProg = @menuName
                     FROM Ghrs_PageSettingQuery kk
                     WHERE     kk.pageno LIKE 'F_ResumeChange%'
                           AND field = @ColumnName
                           AND kk.IsEnable = 1
                           AND CreateProg = 'F_ResumeInfoColumn'

                     UPDATE kk
                     SET kk.IsEnable = 0,
                         kk.UpdateBy = @userId,
                         kk.UpdateTime = GETDATE (),
                         kk.UpdateProg = @menuName
                     FROM Ghrs_PageSettingEdit kk
                     WHERE     kk.pageno LIKE 'F_ResumeChange%'
                           AND field = @ColumnName
                           AND kk.IsEnable = 1
                           AND CreateProg = 'F_ResumeInfoColumn'
                  END
            END
         END

      COMMIT TRAN tranResumeInfoColumn                               -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeInfoColumn                         -- 退回数据库的操作记录

      SELECT 'false' Success,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg 'Message'                     -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 'true' Success,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg 'Message'                        -- IsOK 0:代表正确,  1:代表错误
END";
                break;
        }
        sql = paramSql + sql;
        var dt = await Db.Ado.GetDataTableAsync(sql);
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0]["Success"].ToString() == "true")
                return ServiceResult.OprateSuccess(dt.Rows[0]["Message"].ToString());
            else
                return ServiceResult.OprateFailed(dt.Rows[0]["ErrorMsg"].ToString());
        }
        return ServiceResult.OprateFailed("操作失败！");
    }
    #endregion

    #region 查询模板栏位
    public async Task<ServiceResult<Ghrh_ResumeInfoColumnDto>> QueryTemplateColumnSingle(QueryForm filter)
    {
        var sql = @$"DECLARE
                       @jsonParam   NVARCHAR (MAX) = '{filter.jsonParam}', -- 动态Json字符串
                       @ID          NVARCHAR (1000) = '{filter.id}',                                             -- ID
                       @langId      INT = '{filter.langId}',                                                 -- 语言
                       @userId      INT = '{filter.userId}',                                                -- 使用者
                       @MenuName    NVARCHAR (100) = '{filter.menuName}',
                       @DoType      NVARCHAR (100) = 'Update'         -- Insert Update Copy  Query

                    BEGIN
                       -- insert into GhrDemo.dbo.aaa(a1) select @jsonParam
                       SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
                       DECLARE
                          @SQL           NVARCHAR (MAX) = '',
                          @SQL2          NVARCHAR (MAX) = '',
                          @JsonString    NVARCHAR (MAX) = '',
                          @JsonString1   NVARCHAR (MAX) = '',
                          @JsonString2   NVARCHAR (MAX) = ''

                       -- SELECT 'DT_TableDataT1' Table4, 'DT_ProcedureResult' Table5

                       DECLARE @ResumeTemplateInfoGroupID   NVARCHAR (100) = '0'         -- 模板分组ID
                       SELECT @ResumeTemplateInfoGroupID = value
                       FROM OPENJSON (@jsonParam)
                       WHERE [Key] = 'ResumeTemplateInfoGroupID'

                       IF @DoType IN ('Update', 'Copy')                                    -- 读取修改
                          BEGIN
                             SELECT a.Id,a.ID
                                       ResumeInfoColumnID,
                                    CASE
                                       WHEN @DoType = 'Copy' THEN ''
                                       ELSE a.ResumeInfoColumnNo
                                    END
                                       ResumeInfoColumnNo,                            -- 复制时,此栏位为空
                                    CASE
                                       WHEN @DoType = 'Copy' THEN ''
                                       ELSE a.ResumeInfoColumnName
                                    END
                                       ResumeInfoColumnName,                          -- 复制时,此栏位为空
                                    a.ResumeInfoGroupId,
                                    CASE WHEN @DoType = 'Copy' THEN '' ELSE a.MKey END
                                       MKey,
                                    a.TableName,
                                    CASE WHEN @DoType = 'Copy' THEN '' ELSE a.ColumnName END
                                       ColumnName,
                                    isnull
                                    (
                                       (SELECT (SELECT kk.name
                                                FROM sys.types kk
                                                WHERE kk.user_type_id = pp.user_type_id)
                                        FROM sys.columns pp
                                        WHERE     object_id =
                                                  (SELECT id
                                                   FROM sysobjects
                                                   WHERE xtype = 'u' AND name = a.TableName)
                                              AND name = a.ColumnName),
                                       '')
                                       DBColumnType,
                                    a.IsSingleColumn,
                                    a.ColumnType,
                                    a.DataType,
                                    a.DataSourceType,
                                    a.DataSource,
                                    a.IsBuildIn,
                                    isnull (b.IsRequired, a.IsRequired)
                                       IsRequired,
                                    isnull (b.Editable, a.Editable)
                                       Editable,
                                    isnull (b.InitValue, a.InitValue)
                                       InitValue,
                                    a.IsRelease
                                       IsRelease,
                                    a.IsChangeColumn,
                                    a.IsQuickJoinColumn,
                                    a.Placeholder,
                                    a.RemarkSz,
                                    a.CanModifyBySelf,
                                    a.CanEssDisplay,
                                    a.QueryCondition,
                                    a.MapTableName,
                                    a.MapColumnName,
                                    a.multipleSelect,
                                      isnull ((SELECT UserName
                                               FROM Ghrs_User kk
                                               WHERE kk.UserId = a.CreateBy),
                                              '')
                                    + '于'
                                    + CONVERT (NVARCHAR (16), a.CreateTime, 121)
                                    + '创建'
                                       CreateDataInfo,
                                      isnull ((SELECT UserName
                                               FROM Ghrs_User kk
                                               WHERE kk.UserId = a.UpdateBy),
                                              '')
                                    + '于'
                                    + CONVERT (NVARCHAR (16), a.UpdateTime, 121)
                                    + '最后修改'
                                       UpdateDataInfo
                             FROM Ghrh_ResumeInfoColumn a
                                  LEFT JOIN Ghrh_ResumeTemplateInfoGroupColumn b
                                     ON     b.ResumeTemplateInfoGroupID =
                                            @ResumeTemplateInfoGroupID
                                        AND b.ResumeInfoColumnID = a.ID
                                        AND b.IsEnable = 1
                             WHERE a.ID = @ID
                          END

                       IF @DoType = 'Insert'                                               -- 空白新增
                          BEGIN
                             SELECT 0  ResumeInfoColumnID,0  Id,
                                    NULL ResumeInfoGroupId,
                                    '' ResumeInfoColumnNo,
                                    '' ResumeInfoColumnName,
                                    '' MKey,
                                    '' TableName,
                                    '' ColumnName,
                                    0  IsSingleColumn,
                                    'input' ColumnType,
                                    'string' DataType,
                                    '' DataSourceType,
                                    '' DataSource,
                                    1  IsRequired,
                                    1  Editable,
                                    1  IsBuildIn,
                                    1  IsRelease,
                                    0  IsChangeColumn,
                                    0  IsQuickJoinColumn,
                                    0  CanModifyBySelf,
                                    0  CanEssDisplay,
                                    0  QueryCondition,
                                    '' multipleSelect,
                                    '' InitValue,
                                    '' Placeholder,
                                    '' RemarkSz
                          END
                    END";

        var entitys = await Db.Ado.SqlQuerySingleAsync<Ghrh_ResumeInfoColumnDto>(sql);

        return ServiceResult<Ghrh_ResumeInfoColumnDto>.OprateSuccess("查询成功！", entitys);
    }
    #endregion

    #region 执行模板栏位变更
    public async Task<ServiceResult> PT_GHR30_StaffTemplate_IUD(QueryForm1 filter)
    {
        var paramSql = @$"DECLARE
                           @jsonParam   NVARCHAR (MAX)
                                           = '{filter.jsonParam}', -- 查询条件通过Json格式回传 数据库
                           @doType      NVARCHAR (100) = '{filter.doType}', -- Insert:新增  Update:修改  Delete:删除  BatchDelete:批次删除 BatchUpdaate:多行数据更新
                           -- ColumnMove:模板-分组-栏位移动   GroupMove:模板-分组 移动
                           -- GroupInsert:新增  GroupUpdate:修改  GroupDelete:删除
                           -- SortData: 修改序号和是否默认
                           @id          BIGINT = '{filter.id}',                  -- 表的唯一值。自增长的值   Delete /Update 用到
                           @langId      INT = '{filter.langId}',                                               -- 语言
                           @userId      INT = '{filter.userId}',                                              -- 使用者
                           @menuName    NVARCHAR (1000) = '{filter.menuName}' ";
        string sql = string.Empty;

        switch (filter.doType)
        {
            case "GroupInsert":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE @OperateLogID   BIGINT = 0                           -- 是否需要写入操作日志表

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE @NewResumeTemplateId   BIGINT = 0
   DECLARE
      @title            NVARCHAR (1000),
      @description      NVARCHAR (1000),
      @titleAllowEdit   NVARCHAR (1000),
      @isMaster         NVARCHAR (1000),
      @isPublish        NVARCHAR (1000),
      @SortNo           NVARCHAR (100)

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeTemplate                                  -- 开始事物处理

      -- GroupInsert:新增  GroupUpdate:修改  GroupDelete:删除
      IF @doType = 'GroupInsert'
         BEGIN
            -- if @id>0
            BEGIN
               DECLARE @ResumeInfoGroupID   BIGINT = '{SnowFlakeSingle.Instance.NextId()}'

               INSERT INTO Ghrh_ResumeInfoGroup (Id,
                                                 GroupNo,
                                                 GroupName,
                                                 MKey,
                                                 GroupType,
                                                 TableNames,
                                                 RemarkSz,
                                                 TableName,
                                                 TablePKIDName,
                                                 CanModifyBySelf,
                                                 CanEssDisplay,
                                                 CreateBy,
                                                 CreateTime)
                  SELECT @ResumeInfoGroupID,
                         GroupNo,
                         GroupNo,
                         MKey,
                         'Other',
                         TableNames,
                         RemarkSz,
                         TableName,
                         TablePKIDName,
                         CanModifyBySelf,
                         CanEssDisplay,
                         @userId,
                         getdate ()
                  FROM OPENJSON (@jsonParam) WITH (
                                               GroupNo NVARCHAR (1000),
                                               GroupName NVARCHAR (1000),
                                               MKey NVARCHAR (1000),
                                               TableNames NVARCHAR (1000),
                                               RemarkSz NVARCHAR (1000),
                                               TableName NVARCHAR (1000),
                                               TablePKIDName NVARCHAR (1000),
                                               CanModifyBySelf NVARCHAR (1000),
                                               CanEssDisplay NVARCHAR (1000))

               -- 如果之前没有则需要新增
               IF @ResumeInfoGroupID > 0
                  INSERT INTO Ghrh_ResumeTemplateInfoGroup (
                                 Id,
                                 ResumeTemplateId,
                                 TemplateType,
                                 ResumeInfoGroupId,
                                 IsDisplay,
                                 GroupType,
                                 SortNo,
                                 CreateBy,
                                 CreateTime)
                     SELECT '{SnowFlakeSingle.Instance.NextId()}',
                            a.Id ResumeTemplateId,
                            a.TemplateType,
                            c.Id ResumeInfoGroupId,
                            1,
                            c.GroupType,
                            (SELECT isnull (max (SortNo), 0) + 1
                             FROM Ghrh_ResumeTemplateInfoGroup
                             WHERE ResumeTemplateId = @id AND IsEnable = 1),
                            @userId,
                            getdate ()
                     FROM Ghrh_ResumeTemplate a, Ghrh_ResumeInfoGroup c
                     WHERE a.Id = @id AND c.Id = @ResumeInfoGroupID
            END
         END


      COMMIT TRAN tranResumeTemplate                                 -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeTemplate                           -- 退回数据库的操作记录

      SELECT 1  IsOK,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg SuccessMsg,
             @NewResumeTemplateId NewResumeTemplateId  -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 0  IsOK,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg SuccessMsg,
          @NewResumeTemplateId NewResumeTemplateId     -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "GroupUpdate":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE @NewResumeTemplateId   BIGINT = 0
   DECLARE
      @title            NVARCHAR (1000),
      @description      NVARCHAR (1000),
      @titleAllowEdit   NVARCHAR (1000),
      @isMaster         NVARCHAR (1000),
      @isPublish        NVARCHAR (1000),
      @SortNo           NVARCHAR (100)

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeTemplate                                  -- 开始事物处理

      IF @doType = 'GroupUpdate'
         BEGIN
            IF @id > 0
               BEGIN
                  DECLARE @ResumeInfoGroupId   BIGINT = 0

                  BEGIN
                     SELECT @ResumeInfoGroupId = ResumeInfoGroupId
                     FROM Ghrh_ResumeTemplateInfoGroup
                     WHERE ID = @id
                  END

                  BEGIN
                     UPDATE a
                     SET a.GroupNo = b.ResumeInfoGroupNo,
                         a.GroupName = b.ResumeInfoGroupName,
                         a.MKey = b.MKey,
                         a.TableNames = b.TableNames,
                         a.RemarkSz = b.RemarkSz,
                         a.TableName = b.TableName,
                         a.TablePKIDName = b.TablePKIDName,
                         a.CanModifyBySelf = b.CanModifyBySelf,
                         a.CanEssDisplay = b.CanEssDisplay,
                         a.UpdateBy = @userId,
                         a.UpdateTime = GETDATE (),
                         a.UpdateProg = @menuName
                     FROM Ghrh_ResumeInfoGroup a,
                          (SELECT TOP 1 GroupNo ResumeInfoGroupNo,
                                        GroupName ResumeInfoGroupName,
                                        MKey,
                                        TableNames,
                                        RemarkSz,
                                        TableName,
                                        TablePKIDName,
                                        CanModifyBySelf,
                                        CanEssDisplay
                           FROM OPENJSON (@jsonParam) WITH (
                                                        GroupNo NVARCHAR (1000),
                                                        GroupName NVARCHAR (1000),
                                                        MKey NVARCHAR (1000),
                                                        TableNames NVARCHAR (1000),
                                                        RemarkSz NVARCHAR (1000),
                                                        TableName NVARCHAR (1000),
                                                        TablePKIDName NVARCHAR (1000),
                                                        CanModifyBySelf NVARCHAR (1000),
                                                        CanEssDisplay NVARCHAR (1000)))
                          b
                     WHERE a.ID = @ResumeInfoGroupId
                  END
               END
         END
      

      COMMIT TRAN tranResumeTemplate                                 -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeTemplate                           -- 退回数据库的操作记录

      SELECT 1  IsOK,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg SuccessMsg,
             @NewResumeTemplateId NewResumeTemplateId  -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 0  IsOK,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg SuccessMsg,
          @NewResumeTemplateId NewResumeTemplateId     -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "GroupDelete":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog         INT = 1,
      @OperateLogID      BIGINT = 0,
      @ResumeInfoGroupId   BIGINT = 0                             -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE @NewResumeTemplateId   BIGINT = 0
   DECLARE
      @title            NVARCHAR (1000),
      @description      NVARCHAR (1000),
      @titleAllowEdit   NVARCHAR (1000),
      @isMaster         NVARCHAR (1000),
      @isPublish        NVARCHAR (1000),
      @SortNo           NVARCHAR (100)

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeTemplate                                  -- 开始事物处理
 
      IF @doType = 'GroupDelete'
         BEGIN
            BEGIN
               SELECT @ResumeInfoGroupId = ResumeInfoGroupId 
               FROM Ghrh_ResumeTemplateInfoGroup
               WHERE ID = @id
            END

            BEGIN
               UPDATE a
               SET a.IsEnable = 0,
                   UpdateBy = @userId,
                   UpdateTime = GETDATE (),
                   UpdateProg = @menuName
               FROM Ghrh_ResumeInfoGroup a
               WHERE a.ID = @ResumeInfoGroupId
            END
         END

      COMMIT TRAN tranResumeTemplate                                 -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeTemplate                           -- 退回数据库的操作记录

      SELECT 1  IsOK,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg SuccessMsg,
             @NewResumeTemplateId NewResumeTemplateId  -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 0  IsOK,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg SuccessMsg,
          @NewResumeTemplateId NewResumeTemplateId     -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "GroupSwitch":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE @NewResumeTemplateId   BIGINT = 0
   DECLARE
      @title            NVARCHAR (1000),
      @description      NVARCHAR (1000),
      @titleAllowEdit   NVARCHAR (1000),
      @isMaster         NVARCHAR (1000),
      @isPublish        NVARCHAR (1000),
      @SortNo           NVARCHAR (100)

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeTemplate                                  -- 开始事物处理

      IF @doType = 'GroupSwitch'                                  -- 分组隐藏和分组显示
         BEGIN
            UPDATE a
            SET a.IsDisplay = b.DisplayType,
                UpdateBy = @userId,
                UpdateTime = GETDATE (),
                UpdateProg = @menuName
            FROM Ghrh_ResumeTemplateInfoGroup a,
                 (SELECT ResumeInfoGroupId, DisplayType
                  FROM OPENJSON (@jsonParam) WITH (ResumeInfoGroupId INT,
                                                   DisplayType INT)) b
            WHERE     a.ResumeInfoGroupID = b.ResumeInfoGroupId
                  AND a.ResumeTemplateId = @id
                  AND a.IsEnable = 1

            -- 如果之前没有则需要新增
            INSERT INTO Ghrh_ResumeTemplateInfoGroup (Id,ResumeTemplateId,
                                                      TemplateType,
                                                      ResumeInfoGroupId,
                                                      IsDisplay,
                                                      GroupType,
                                                      SortNo)
               SELECT ABS(CHECKSUM(NewId())),a.Id ResumeTemplateId,
                      a.TemplateType,
                      b.ResumeInfoGroupId,
                      b.DisplayType,
                      c.GroupType,
                      b.SortNo
               FROM Ghrh_ResumeTemplate a,
                    (SELECT ResumeInfoGroupId, DisplayType, SortNo
                     FROM OPENJSON (@jsonParam) WITH (ResumeInfoGroupId INT,
                                                      DisplayType INT,
                                                      SortNo INT)) b,
                    Ghrh_ResumeInfoGroup c
               WHERE     a.Id = @id
                     AND b.ResumeInfoGroupID = c.Id
                     AND b.ResumeInfoGroupID NOT IN
                            (SELECT ResumeInfoGroupID
                             FROM Ghrh_ResumeTemplateInfoGroup
                             WHERE ResumeTemplateId = @id AND IsEnable = 1)
         END
 

      COMMIT TRAN tranResumeTemplate                                 -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeTemplate                           -- 退回数据库的操作记录

      SELECT 1  IsOK,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg SuccessMsg,
             @NewResumeTemplateId NewResumeTemplateId  -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 0  IsOK,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg SuccessMsg,
          @NewResumeTemplateId NewResumeTemplateId     -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "GroupMove":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE @NewResumeTemplateId   BIGINT = 0
   DECLARE
      @title            NVARCHAR (1000),
      @description      NVARCHAR (1000),
      @titleAllowEdit   NVARCHAR (1000),
      @isMaster         NVARCHAR (1000),
      @isPublish        NVARCHAR (1000),
      @SortNo           NVARCHAR (100)

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeTemplate                                  -- 开始事物处理

      IF @doType = 'GroupMove'                                    -- :模板-分组 移动
         BEGIN
            UPDATE a                                                   -- 移动栏位
            SET a.SortNo = b.SortNo, a.IsDisplay = b.DisplayType
            FROM Ghrh_ResumeTemplateInfoGroup a,
                 (SELECT ResumeInfoGroupId, DisplayType, SortNo
                  FROM OPENJSON (@jsonParam) WITH (ResumeInfoGroupId INT,
                                                   DisplayType INT,
                                                   SortNo INT)) b
            WHERE     a.ResumeTemplateID = @id
                  AND a.IsEnable = 1
                  AND a.ResumeInfoGroupId = b.ResumeInfoGroupId

            -- 如果没有的需要新增
            INSERT INTO Ghrh_ResumeTemplateInfoGroup (Id,
                                                      ResumeTemplateId,
                                                      TemplateType,
                                                      ResumeInfoGroupId,
                                                      IsDisplay,
                                                      GroupType,
                                                      SortNo)
               SELECT ABS (CHECKSUM (NewId ())),
                      a.Id ResumeTemplateId,
                      a.TemplateType,
                      b.ResumeInfoGroupId,
                      b.DisplayType,
                      c.GroupType,
                      b.SortNo
               FROM Ghrh_ResumeTemplate a,
                    (SELECT ResumeInfoGroupId, DisplayType, SortNo
                     FROM OPENJSON (@jsonParam) WITH (ResumeInfoGroupId BIGINT,
                                                      DisplayType INT,
                                                      SortNo INT)) b,
                    Ghrh_ResumeInfoGroup c
               WHERE     a.Id = @id
                     AND b.ResumeInfoGroupID = c.Id
                     AND b.ResumeInfoGroupID NOT IN
                            (SELECT ResumeInfoGroupID
                             FROM Ghrh_ResumeTemplateInfoGroup
                             WHERE ResumeTemplateId = @id AND IsEnable = 1)
         END

      COMMIT TRAN tranResumeTemplate                                 -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeTemplate                           -- 退回数据库的操作记录

      SELECT 1  IsOK,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg SuccessMsg,
             @NewResumeTemplateId NewResumeTemplateId  -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 0  IsOK,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg SuccessMsg,
          @NewResumeTemplateId NewResumeTemplateId     -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "ColumnMove":
                sql = @$"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE @NewResumeTemplateId   BIGINT = 0
   DECLARE
      @title            NVARCHAR (1000),
      @description      NVARCHAR (1000),
      @titleAllowEdit   NVARCHAR (1000),
      @isMaster         NVARCHAR (1000),
      @isPublish        NVARCHAR (1000),
      @SortNo           NVARCHAR (100)

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeTemplate                                  -- 开始事物处理

      IF @doType = 'ColumnMove'               -- 模板栏位异动   模板里面的栏位增加减少 移动  批次操作
         BEGIN
            UPDATE a                                                   -- 移动栏位
            SET a.SortNo = b.SortNo, a.IsDisplay = b.DisplayType
            FROM Ghrh_ResumeTemplateInfoGroupColumn a,
                 (SELECT ResumeInfoColumnID, DisplayType, SortNo
                  FROM OPENJSON (@jsonParam) WITH (ResumeInfoColumnID BIGINT,
                                                   DisplayType INT,
                                                   SortNo INT)) b
            WHERE     a.ResumeTemplateInfoGroupID = @id
                  AND a.IsEnable = 1
                  AND a.ResumeInfoColumnID = b.ResumeInfoColumnID
                  AND b.DisplayType IN (0, 1)

            -- 如果之前的栏位是 从 作废里面(未发布)--> 可选或者已选, 则需要修改 ResumeInfoColumn
            UPDATE Ghrh_ResumeInfoColumn
            SET IsRelease = 1
            WHERE     ID IN
                         (SELECT ResumeInfoColumnID
                          FROM OPENJSON (@jsonParam) WITH (
                                                       ResumeInfoColumnID BIGINT,
                                                       DisplayType INT,
                                                       SortNo INT)
                          WHERE DisplayType IN (0, 1))
                  AND IsRelease = 0

            DELETE FROM
               Ghrh_ResumeTemplateInfoGroupColumn                      -- 作废项目
            WHERE     ResumeTemplateInfoGroupID = @id
                  AND IsEnable = 1
                  AND ResumeInfoColumnID IN
                         (SELECT ResumeInfoColumnID
                          FROM OPENJSON (@jsonParam) WITH (
                                                       ResumeInfoColumnID BIGINT,
                                                       DisplayType INT,
                                                       SortNo INT)
                          WHERE DisplayType = -1)

            -- 新增栏位
            INSERT INTO Ghrh_ResumeTemplateInfoGroupColumn (
                           Id,
                           ResumeTemplateInfoGroupId,
                           ResumeTemplateId,
                           TemplateType,
                           ResumeInfoGroupId,
                           GroupType,
                           ResumeInfoColumnID,
                           IsRequired,
                           Editable,
                           InitValue,
                           IsDisplay,
                           SortNo,
                           RowNum,
                           ColNum)
               SELECT ABS (CHECKSUM (NewId ())),
                      a.Id ResumeTemplateInfoGroupId,
                      ResumeTemplateId,
                      a.TemplateType,
                      a.ResumeInfoGroupId,
                      a.GroupType,
                      c.ID ResumeInfoColumnID,
                      c.IsRequired,
                      c.Editable,
                      c.InitValue,
                      b.DisplayType,
                      b.SortNo,
                      1,
                      1
               FROM Ghrh_ResumeTemplateInfoGroup a,
                    (SELECT ResumeInfoColumnID, DisplayType, SortNo
                     FROM OPENJSON (@jsonParam) WITH (
                                                  ResumeInfoColumnID BIGINT,
                                                  DisplayType INT,
                                                  SortNo INT)) b,
                    Ghrh_ResumeInfoColumn c
               WHERE     a.Id = @id
                     AND b.ResumeInfoColumnID = c.ID
                     AND b.DisplayType IN (0, 1)
                     AND c.IsEnable = 1
                     AND b.ResumeInfoColumnID NOT IN
                            (SELECT ResumeInfoColumnID
                             FROM Ghrh_ResumeTemplateInfoGroupColumn
                             WHERE     ResumeTemplateInfoGroupId = @id
                                   AND IsEnable = 1)

            -- micro 2022-08-05 add 模板自动更新 栏位设定 Ghrs_PageSettingQuery
            DECLARE
               @FResumeMenuName    NVARCHAR (1000) = 'F_Resume~',
               @FResumeGroupType   NVARCHAR (100)
            SELECT @FResumeMenuName =
                        @FResumeMenuName
                      + CONVERT (NVARCHAR (100), ResumeTemplateId),
                   @FResumeGroupType = GroupType
            FROM Ghrh_ResumeTemplateInfoGroup
            WHERE Id = @id

            IF @FResumeGroupType IN ('Base',
                                     'Education',
                                     'Family',
                                     'Contract',
                                     'WorkExp',
                                     'Training',
                                     'Licence')
               BEGIN
                  IF @FResumeGroupType = 'Education'
                     SET @FResumeMenuName = 'F_ResumeEduBG'

                  IF @FResumeGroupType = 'Contract'
                     SET @FResumeMenuName = 'F_ResumeContract'

                  IF @FResumeGroupType = 'Family'
                     SET @FResumeMenuName = 'F_ResumeSocialRelation'

                  IF @FResumeGroupType = 'WorkExp'
                     SET @FResumeMenuName = 'F_ResumeWorkExp'

                  IF @FResumeGroupType = 'Training'
                     SET @FResumeMenuName = 'F_ResumeTraining'

                  IF @FResumeGroupType = 'Licence'
                     SET @FResumeMenuName = 'F_ResumeLicence'

                  -- 更新后的栏位设定写入临时表
                  IF OBJECT_ID ('tempdb.[dbo].[#TempPageSettingQuery]')
                        IS NOT NULL
                     DROP TABLE [dbo].[#TempPageSettingQuery]

                  --insert into Ghrs_PageSettingQuery(PageNo,ColumnGroup,elementType,field,label,Meky,multipleSelect,dataType,required,palceholder,
                  --       dataSourceType,dataSource,sortable,sortOrder,defaultHidden,filterable,width,align,fixed,editable,
                  --       searchable,searchRequired,searchColumnSeq,isPrimaryKey,SortNo,searchCanOperator)
                  SELECT @FResumeMenuName PageNo,
                         '' ColumnGroup,
                         b.ColumnType elementType,
                         b.ResumeInfoColumnNo field,
                         b.ResumeInfoColumnName label,
                         b.MKey Meky,
                         NULL multipleSelect,
                         b.DataType dataType,
                         CASE
                            WHEN a.IsRequired = 1 THEN 'true'
                            ELSE 'false'
                         END required,
                         NULL palceholder,
                         CASE
                            WHEN b.ColumnType IN
                                    ('OrgTreeSelect', 'ApiSelect')
                            THEN
                               'dict'
                            ELSE
                               ''
                         END dataSourceType,
                         dbo.FS_GetdataSourceBySet
                         ('', b.DataSourceType, b.DataSource) dataSource,
                         'true' sortable,
                         NULL sortOrder,
                         CASE
                            WHEN b.isPrimaryKey = 1 THEN 'true'
                            ELSE 'false'
                         END defaultHidden,
                         NULL filterable,
                         '100' width,
                         'left' align,
                         NULL fixed,
                         'false' editable,
                         'false' searchable,
                         'false' searchRequired,
                         '' searchColumnSeq,
                         CASE
                            WHEN b.isPrimaryKey = 1 THEN 'true'
                            ELSE 'false'
                         END isPrimaryKey,
                         a.SortNo,
                         CONVERT (NVARCHAR (1000), '') searchCanOperator,
                         0  PageSettingQueryID
                  INTO dbo.[#TempPageSettingQuery]
                  FROM Ghrh_ResumeTemplateInfoGroupColumn a,
                       Ghrh_ResumeInfoColumn b         --Ghrs_PageSettingQuery
                  WHERE     a.ResumeInfoColumnID = b.ID
                        AND a.ResumeTemplateInfoGroupId = @id
                        AND b.IsEnable = 1
                        AND grouptype = @FResumeGroupType
                        AND (   b.isPrimaryKey = 1
                             OR a.IsEnable = 1 AND IsDisPlay = 1)
                  ORDER BY a.SortNo

                  -- Group Type 的特别处理
                  IF @FResumeGroupType = 'Base'
                     BEGIN
                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 1,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'ResumeNo'

                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 2,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'ResumeName'

                        -- 判断生成 照片是否显示
                        INSERT INTO [#TempPageSettingQuery] (
                                       PageNo,
                                       ColumnGroup,
                                       elementType,
                                       field,
                                       label,
                                       Meky,
                                       multipleSelect,
                                       dataType,
                                       required,
                                       palceholder,
                                       dataSourceType,
                                       dataSource,
                                       sortable,
                                       sortOrder,
                                       defaultHidden,
                                       filterable,
                                       width,
                                       align,
                                       fixed,
                                       editable,
                                       searchable,
                                       searchRequired,
                                       searchColumnSeq,
                                       isPrimaryKey,
                                       SortNo,
                                       searchCanOperator,
                                       PageSettingQueryID)
                           SELECT @FResumeMenuName,
                                  '',
                                  'Avatar',
                                  'PhotoUrl',
                                  '照片',
                                  'F_User_UserPhotoUrl',
                                  '',
                                  'string',
                                  '',
                                  '',
                                  '',
                                  '',
                                  '',
                                  '',
                                  'false',
                                  NULL,
                                  '120',
                                  '',
                                  '',
                                  'true',
                                  '',
                                  '',
                                  '',
                                  '',
                                  1,
                                  '',
                                  0
                     END

                  IF @FResumeGroupType = 'Education'
                     BEGIN
                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 1,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'SchoolName'

                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 2,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'DeptName'
                     END

                  IF @FResumeGroupType = 'Family'
                     BEGIN
                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 1,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'RelationName'

                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 2,
                            searchCanOperator =
                               'Equal,NotEqual,EqualAny,NotEqualAny'
                        WHERE field = 'RelationType'
                     END

                  IF @FResumeGroupType = 'Contract'
                     BEGIN
                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 1,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'ContractNo'

                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 2,
                            searchCanOperator =
                               'Equal,NotEqual,EqualAny,NotEqualAny'
                        WHERE field = 'ContractType'
                     END

                  IF @FResumeGroupType = 'WorkExp'
                     BEGIN
                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 1,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'CompanyName'

                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 2,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'DeptName'
                     END

                  IF @FResumeGroupType = 'Training'
                     BEGIN
                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 1,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'CourseName'

                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 2,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'TrainingOrgName'
                     END

                  IF @FResumeGroupType = 'Licence'
                     BEGIN
                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 1,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'LicenceName'

                        UPDATE dbo.[#TempPageSettingQuery]
                        SET searchable = 'true',
                            searchColumnSeq = 2,
                            searchCanOperator =
                               'Include,Equal,NotEqual,NotInclude'
                        WHERE field = 'LicenceNo'
                     END


                  -- 修改临时表的 PageSettingQueryID
                  UPDATE a
                  SET a.PageSettingQueryID = b.PageSettingQueryID
                  FROM dbo.[#TempPageSettingQuery] a, Ghrs_PageSettingQuery b
                  WHERE     a.PageNo = b.PageNo
                        AND a.field = b.field
                        AND b.IsEnable = 1

                  -- 删除 Ghrs_PageSettingQuery  中本次取消的栏位
                  UPDATE Ghrs_PageSettingQuery
                  SET IsEnable = 0, UpdateProg = @menuName
                  WHERE     NOT EXISTS
                               (SELECT 1
                                FROM dbo.[#TempPageSettingQuery] a
                                WHERE a.PageSettingQueryID =
                                      Ghrs_PageSettingQuery.PageSettingQueryID)
                        AND PageNo = @FResumeMenuName
                        AND elementType ! = 'FnKey'
                        AND field NOT IN ('CreateTime',
                                          'UpdateTime',
                                          'CreateBy',
                                          'UpdateBy',
                                          'ResumeJobStatus')
                        AND CreateProg = @menuName

                  -- 删除自定义栏位 Ghrs_UserPageSettingQueryColumn  中本次取消的栏位
                  UPDATE Ghrs_UserPageSettingQueryColumn
                  SET IsEnable = 0
                  WHERE     NOT EXISTS
                               (SELECT 1
                                FROM Ghrs_PageSettingQuery a
                                WHERE a.PageSettingQueryID =
                                      Ghrs_UserPageSettingQueryColumn.PageSettingQueryID)
                        AND IsEnable = 1
                        AND CreateProg = @FResumeMenuName
                        AND EXISTS
                               (SELECT 1
                                FROM Ghrs_PageSettingQuery a
                                WHERE     a.PageSettingQueryID =
                                          Ghrs_UserPageSettingQueryColumn.PageSettingQueryID
                                      AND a.elementType ! = 'FnKey')

                  -- 修改栏位
                  UPDATE a
                  SET a.label = b.label,
                      a.Meky = b.Meky,
                      a.required = b.required,
                      a.elementType = b.elementType,
                      a.dataType = b.dataType,
                      a.dataSourceType = b.dataSourceType,
                      a.dataSource = b.dataSource,
                      --a.sortable=b.sortable,
                      a.defaultHidden =
                         CASE
                            WHEN a.field = 'ResumeID' THEN 'true'
                            ELSE b.defaultHidden
                         END,
                      -- a.filterable=b.filterable,a.width=b.width,a.align=b.align,a.fixed=b.fixed,
                      a.editable = b.editable,
                      --a.searchable=b.searchable,a.searchRequired=b.searchRequired,a.searchColumnSeq=b.searchColumnSeq,
                      a.isPrimaryKey = b.isPrimaryKey,
                      a.SortNo = b.SortNo,
                      -- a.searchCanOperator=b.searchCanOperator,
                      a.UpdateProg = @menuName,
                      a.CreateProg = @menuName
                  FROM Ghrs_PageSettingQuery a, dbo.[#TempPageSettingQuery] b
                  WHERE a.PageSettingQueryID = b.PageSettingQueryID

                  -- 新增本次设定的栏位
                  INSERT INTO Ghrs_PageSettingQuery (PageNo,
                                                     ColumnGroup,
                                                     elementType,
                                                     field,
                                                     label,
                                                     Meky,
                                                     multipleSelect,
                                                     dataType,
                                                     required,
                                                     palceholder,
                                                     dataSourceType,
                                                     dataSource,
                                                     sortable,
                                                     sortOrder,
                                                     defaultHidden,
                                                     filterable,
                                                     width,
                                                     align,
                                                     fixed,
                                                     editable,
                                                     searchable,
                                                     searchRequired,
                                                     searchColumnSeq,
                                                     isPrimaryKey,
                                                     SortNo,
                                                     searchCanOperator,
                                                     UpdateProg,
                                                     CreateProg)
                     SELECT PageNo,
                            ColumnGroup,
                            elementType,
                            field,
                            label,
                            Meky,
                            multipleSelect,
                            dataType,
                            required,
                            palceholder,
                            dataSourceType,
                            dataSource,
                            sortable,
                            sortOrder,
                            defaultHidden,
                            filterable,
                            width,
                            align,
                            fixed,
                            editable,
                            searchable,
                            searchRequired,
                            searchColumnSeq,
                            isPrimaryKey,
                            SortNo,
                            searchCanOperator,
                            @menuName,
                            @menuName
                     FROM dbo.[#TempPageSettingQuery]
                     WHERE isnull (PageSettingQueryID, 0) = 0
               END
         END

      COMMIT TRAN tranResumeTemplate                                 -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeTemplate                           -- 退回数据库的操作记录

      SELECT 1  IsOK,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg SuccessMsg,
             @NewResumeTemplateId NewResumeTemplateId  -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 0  IsOK,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg SuccessMsg,
          @NewResumeTemplateId NewResumeTemplateId     -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "Delete":
                sql = $@"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE @NewResumeTemplateId   BIGINT = 0
   DECLARE
      @title            NVARCHAR (1000),
      @description      NVARCHAR (1000),
      @titleAllowEdit   NVARCHAR (1000),
      @isMaster         NVARCHAR (1000),
      @isPublish        NVARCHAR (1000),
      @SortNo           NVARCHAR (100)

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeTemplate                                  -- 开始事物处理

      IF @doType = 'Delete'                                            -- 资料删除
         BEGIN
            UPDATE Ghrh_ResumeTemplate
            SET OperateLogID = @OperateLogID,
                IsEnable = 0,
                UpdateBy = @userId,
                UpdateTime = GETDATE (),
                UpdateProg = @menuName
            WHERE Id = @ID

            SET @SuccessMsg = '删除成功'

            SELECT 0  IsOK,
                   @ErrorMsg ErrorMsg,
                   @WarningMsg WarnMsg,
                   @SuccessMsg SuccessMsg              -- IsOK 0:代表正确,  1:代表错误

            COMMIT TRAN tranResumeTemplate
            RETURN
         END

      COMMIT TRAN tranResumeTemplate                                 -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeTemplate                           -- 退回数据库的操作记录

      SELECT 1  IsOK,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg SuccessMsg,
             @NewResumeTemplateId NewResumeTemplateId  -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 0  IsOK,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg SuccessMsg,
          @NewResumeTemplateId NewResumeTemplateId     -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            case "Update":
            case "Insert":
            case "Copy":
                sql = $@"BEGIN
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   DECLARE
      @NeedOPLog      INT = 1,
      @OperateLogID   BIGINT = 0                                -- 是否需要写入操作日志表

   -- SELECT 'DT_ProcedureResult' Table3

   DECLARE @ErrorMsg   NVARCHAR (MAX) = ''                           -- 错误信息置空
   DECLARE @WarningMsg   NVARCHAR (MAX) = ''                         -- 提醒信息置空
   DECLARE @SuccessMsg   NVARCHAR (MAX) = ''
   DECLARE @NewResumeTemplateId   BIGINT = 0
   DECLARE
      @title            NVARCHAR (1000),
      @description      NVARCHAR (1000),
      @titleAllowEdit   NVARCHAR (1000),
      @isMaster         NVARCHAR (1000),
      @isPublish        NVARCHAR (1000),
      @SortNo           NVARCHAR (100)

   BEGIN TRY                                                         -- 开始错误处理
      BEGIN TRAN tranResumeTemplate                                  -- 开始事物处理

      IF @doType IN ('Update', 'Insert', 'Copy')
         BEGIN
            IF @jsonParam ! = ''                                    -- 传入的参数处理
               BEGIN
                  SELECT @title = title,
                         @description = description,
                         @titleAllowEdit = titleAllowEdit,
                         @isMaster = isMaster,
                         @isPublish = isPublish,
                         @SortNo = sortNo
                  FROM OPENJSON (@jsonParam) WITH (
                                               title NVARCHAR (1000),
                                               description NVARCHAR (1000),
                                               titleAllowEdit NVARCHAR (1000),
                                               isMaster NVARCHAR (1000),
                                               isPublish NVARCHAR (1000),
                                               sortNo NVARCHAR (100))
               END

            -- 资料验证
            BEGIN
               IF @title = ''
                  SET @ErrorMsg = @ErrorMsg + '模板名称不能为空' --  [dbo].[FLangKeyToValue]('F_ResumeTemplate_0001',@langId,'集团数据归属名称不能为空')

               IF @ErrorMsg ! = ''
                  BEGIN
                     ROLLBACK TRAN tranResumeTemplate            -- 退回数据库的操作记录

                     SELECT 1  IsOK,
                            @ErrorMsg ErrorMsg,
                            @WarningMsg WarnMsg,
                            @SuccessMsg SuccessMsg     -- IsOK 0:代表正确,  1:代表错误

                     RETURN
                  END
            END

            IF @ID > 0                                  -- @doType =  'Update'
               BEGIN
                  -- exec [dbo].[PS_OperateLog] 1,'Insert','/ResumeTemplate','Ghrh_ResumeTemplate',1, @OperateLogID output
                  IF @doType = 'Update'
                     BEGIN
                        UPDATE Ghrh_ResumeTemplate
                        SET TemplateNo = @title,
                            TemplateName = @title,
                            RemarkSz = @description,
                            SortNo =
                               CASE
                                  WHEN isnull (@SortNo, '') = '' THEN '1'
                                  ELSE @SortNo
                               END,
                            IsPublish = @isPublish,
                            -- TemplateType=case when @isMaster=1 then  1 else 2 end,
                            OperateLogID = @OperateLogID,
                            UpdateBy = @userId,
                            UpdateTime = GETDATE (),
                            UpdateProg = @menuName
                        WHERE Id = @ID
                     END

                  IF @doType = 'Copy'
                     BEGIN
                        SET @NewResumeTemplateId = '{SnowFlakeSingle.Instance.NextId()}'

                        INSERT INTO [dbo].[Ghrh_ResumeTemplate] (
                                       Id,
                                       TemplateNo,
                                       TemplateName,
                                       SortNo,
                                       IsPublish,
                                       TemplateType,
                                       OperateLogID,
                                       RemarkSz,
                                       [CreateBy],
                                       [CreateTime],
                                       [CreateProg],
                                       [CreateIP],
                                       [UpdateBy],
                                       [UpdateTime],
                                       [UpdateProg],
                                       [UpdateIP])
                           SELECT @NewResumeTemplateId,
                                  @title,
                                  @title,
                                  CASE
                                     WHEN isnull (@SortNo, '') = '' THEN '1'
                                     ELSE @SortNo
                                  END,
                                  @isPublish,
                                  2,
                                  @OperateLogID,
                                  @description,
                                  @userId,
                                  GETDATE (),
                                  @menuName,
                                  '',
                                  @userId,
                                  GETDATE (),
                                  @menuName,
                                  ''
                           FROM Ghrh_ResumeTemplate
                           WHERE Id = @ID


                        -- 复制 模板的Detail
                        --1. 复制分组
                        INSERT INTO Ghrh_ResumeTemplateInfoGroup (
                                       Id,
                                       ResumeTemplateId,
                                       TemplateType,
                                       ResumeInfoGroupId,
                                       IsDisplay,
                                       GroupType,
                                       SortNo)
                           SELECT ABS (CHECKSUM (NewId ())),
                                  @NewResumeTemplateId,
                                  2,
                                  a.ResumeInfoGroupId,
                                  a.IsDisplay,
                                  a.GroupType,
                                  a.SortNo
                           FROM Ghrh_ResumeTemplateInfoGroup a
                           WHERE a.ResumeTemplateId = @id

                        -- 复制分组栏位
                        INSERT INTO Ghrh_ResumeTemplateInfoGroupColumn (
                                       Id,
                                       ResumeTemplateInfoGroupId,
                                       ResumeTemplateId,
                                       TemplateType,
                                       ResumeInfoGroupId,
                                       GroupType,
                                       ResumeInfoColumnID,
                                       IsRequired,
                                       Editable,
                                       InitValue,
                                       IsDisplay,
                                       SortNo,
                                       RowNum,
                                       ColNum)
                           SELECT ABS (CHECKSUM (NewId ())),
                                  c.Id ResumeTemplateInfoGroupId,
                                  c.ResumeTemplateId,
                                  2,
                                  a.ResumeInfoGroupId,
                                  a.GroupType,
                                  a.ResumeInfoColumnID,
                                  a.IsRequired,
                                  a.Editable,
                                  a.InitValue,
                                  a.IsDisplay,
                                  a.SortNo,
                                  a.RowNum,
                                  a.ColNum
                           FROM Ghrh_ResumeTemplateInfoGroupColumn a,
                                Ghrh_ResumeTemplateInfoGroup c
                           WHERE     a.IsEnable = 1
                                 AND c.IsEnable = 1
                                 AND a.ResumeInfoGroupId =
                                     c.ResumeInfoGroupId
                                 AND a.ResumeTemplateId = @id
                                 AND c.ResumeTemplateId =
                                     @NewResumeTemplateId

                        -- 复制人员基本资料的 修改/删除/复制的功能按钮到 Ghrs_PageSettingQuery
                        DECLARE @CopyMenuName   NVARCHAR (100)
                                   =   'F_Resume~'
                                     + CONVERT
                                       (NVARCHAR (MAX), @NewResumeTemplateId)

                        INSERT INTO Ghrs_PageSettingQuery (PageNo,
                                                           ColumnGroup,
                                                           elementType,
                                                           field,
                                                           label,
                                                           Meky,
                                                           multipleSelect,
                                                           dataType,
                                                           required,
                                                           palceholder,
                                                           dataSourceType,
                                                           dataSource,
                                                           sortable,
                                                           sortOrder,
                                                           defaultHidden,
                                                           filterable,
                                                           width,
                                                           align,
                                                           fixed,
                                                           editable,
                                                           searchable,
                                                           searchRequired,
                                                           searchColumnSeq,
                                                           isPrimaryKey,
                                                           SortNo,
                                                           searchCanOperator)
                           SELECT @CopyMenuName,
                                  ColumnGroup,
                                  elementType,
                                  field,
                                  label,
                                  Meky,
                                  multipleSelect,
                                  dataType,
                                  required,
                                  palceholder,
                                  dataSourceType,
                                  dataSource,
                                  sortable,
                                  sortOrder,
                                  defaultHidden,
                                  filterable,
                                  width,
                                  align,
                                  fixed,
                                  editable,
                                  searchable,
                                  searchRequired,
                                  searchColumnSeq,
                                  isPrimaryKey,
                                  SortNo,
                                  searchCanOperator
                           FROM Ghrs_PageSettingQuery a
                           WHERE     IsEnable = 1
                                 AND PageNo = 'F_Resume~1'
                                 AND elementType = 'FnKey'
                                 AND NOT EXISTS
                                        (SELECT 1
                                         FROM Ghrs_PageSettingQuery b
                                         WHERE     b.IsEnable = 1
                                               AND b.PageNo = @CopyMenuName
                                               AND a.elementType =
                                                   b.elementType
                                               AND a.field = b.field)
                     END
               END

            IF @ID = 0                                  -- @doType =  'Insert'
               BEGIN
                  SET @doType = 'Insert'

                  -- exec [dbo].[PS_OperateLog] 1,'Insert','/ResumeTemplate','Ghrh_ResumeTemplate',1, @OperateLogID output

                  SET @NewResumeTemplateId = {SnowFlakeSingle.Instance.NextId()}
                  INSERT INTO [dbo].[Ghrh_ResumeTemplate] (Id,TemplateNo,
                                                           TemplateName,
                                                           SortNo,
                                                           IsPublish,
                                                           TemplateType,
                                                           OperateLogID,
                                                           RemarkSz,
                                                           [CreateBy],
                                                           [CreateTime],
                                                           [CreateProg],
                                                           [CreateIP],
                                                           [UpdateBy],
                                                           [UpdateTime],
                                                           [UpdateProg],
                                                           [UpdateIP])
                     SELECT @NewResumeTemplateId,@title,
                            @title,
                            isnull (@SortNo, '1'),
                            @IsPublish,
                            CASE WHEN @isMaster = 1 THEN 1 ELSE 2 END,
                            @OperateLogID,
                            @description,
                            @userId,
                            GETDATE (),
                            @menuName,
                            '0',
                            @userId,
                            GETDATE (),
                            @menuName,
                            '0'
               END
         END

      COMMIT TRAN tranResumeTemplate                                 -- 提交事物处理
   END TRY
   BEGIN CATCH                                                         -- 错误捕捉
      SET @ErrorMsg = error_message ()                                 -- 错误信息
      ROLLBACK TRAN tranResumeTemplate                           -- 退回数据库的操作记录

      SELECT 1  IsOK,
             @ErrorMsg ErrorMsg,
             @WarningMsg WarnMsg,
             @SuccessMsg SuccessMsg,
             @NewResumeTemplateId NewResumeTemplateId  -- IsOK 0:代表正确,  1:代表错误

      RETURN
   END CATCH

   SET @SuccessMsg =
          [dbo].[FLangKeyToValue]
          ('GHR_Common_000014',
           @langId,
           '保存成功.您可以继续新增或关闭窗口.')

   SELECT 0  IsOK,
          @ErrorMsg ErrorMsg,
          @WarningMsg WarnMsg,
          @SuccessMsg SuccessMsg,
          @NewResumeTemplateId NewResumeTemplateId     -- IsOK 0:代表正确,  1:代表错误
END";
                break;
            default:

                break;
        }

        sql = paramSql + sql;
        var dt = await Db.Ado.GetDataTableAsync(sql);

        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0]["IsOK"].ToString() == "0")
                return ServiceResult.OprateSuccess(dt.Rows[0]["SuccessMsg"].ToString());
            else
                return ServiceResult.OprateFailed(dt.Rows[0]["ErrorMsg"].ToString());
        }
        return ServiceResult.OprateFailed("操作失败！");
    }
    #endregion
}