﻿IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
    CREATE TABLE [__EFMigrationsHistory] (
        [MigrationId] nvarchar(150) NOT NULL,
        [ProductVersion] nvarchar(32) NOT NULL,
        CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
    );
END;
GO

BEGIN TRANSACTION;
GO

CREATE TABLE [APIGrou] (
    [FID] int NOT NULL IDENTITY,
    [FName] nvarchar(250) NOT NULL,
    [FVersion] nvarchar(250) NOT NULL,
    [FTokenUrl] nvarchar(2000) NOT NULL,
    CONSTRAINT [PK_APIGrou] PRIMARY KEY ([FID])
);
GO

CREATE TABLE [r_ClumnConvert] (
    [FID] int NOT NULL IDENTITY,
    [FFunctionID] int NOT NULL,
    [FDesTableName] nvarchar(200) NOT NULL,
    [FSourceName] nvarchar(80) NOT NULL,
    [FDesName] nvarchar(80) NOT NULL,
    [FKDCaption] nvarchar(80) NOT NULL,
    CONSTRAINT [PK_r_ClumnConvert] PRIMARY KEY ([FID])
);
GO

CREATE TABLE [r_MesRequestStatus] (
    [FID] nvarchar(50) NOT NULL,
    [FERPBillType] int NOT NULL,
    [FERPBillID] int NOT NULL,
    [FERPBillEntryID] int NOT NULL,
    [FIMSDocType] nvarchar(80) NOT NULL,
    [FRequestKey] nvarchar(80) NOT NULL,
    [FStatus] int NOT NULL,
    CONSTRAINT [PK_r_MesRequestStatus] PRIMARY KEY ([FID], [FERPBillType], [FERPBillID], [FERPBillEntryID])
);
GO

CREATE TABLE [r_SystemProfiles] (
    [FCategoryID] int NOT NULL,
    [FKey] nvarchar(450) NOT NULL,
    [FID] int NOT NULL,
    [FValue] nvarchar(200) NOT NULL,
    [FReadonly] bit NOT NULL,
    [FDescription] nvarchar(max) NOT NULL,
    [FName] nvarchar(200) NOT NULL,
    [FLevel] int NOT NULL,
    [FParentID] int NOT NULL,
    [FFormatID] int NOT NULL,
    [FDetail] bit NOT NULL,
    CONSTRAINT [PK_r_SystemProfiles] PRIMARY KEY ([FCategoryID], [FKey])
);
GO

CREATE TABLE [RBLog] (
    [FID] int NOT NULL IDENTITY,
    [FFunctionID] int NOT NULL,
    [FDocType] nvarchar(80) NOT NULL,
    [FDateTime] datetime2 NOT NULL,
    [FStatus] bit NOT NULL,
    [FMess] nvarchar(255) NOT NULL,
    [FSource] xml NOT NULL,
    CONSTRAINT [PK_RBLog] PRIMARY KEY ([FID])
);
GO

CREATE TABLE [r_FunctionList] (
    [FID] int NOT NULL IDENTITY,
    [FGoupID] int NOT NULL,
    [FFunctionName] nvarchar(80) NOT NULL,
    [FInterfaceType] int NOT NULL,
    [FUrl] nvarchar(255) NOT NULL,
    [FDescription] nvarchar(255) NOT NULL,
    [FUsing] bit NOT NULL,
    CONSTRAINT [PK_r_FunctionList] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_FunctionList_APIGrou_FGoupID] FOREIGN KEY ([FGoupID]) REFERENCES [APIGrou] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_FormIDType] (
    [FID] int NOT NULL IDENTITY,
    [FFunctionID] int NOT NULL,
    [FActionName] nvarchar(80) NOT NULL,
    [FDocType] nvarchar(80) NOT NULL,
    [FDBName] nvarchar(80) NOT NULL,
    [FUsing] bit NOT NULL,
    [FIsHostService] bit NOT NULL,
    [FDescription] nvarchar(255) NOT NULL,
    CONSTRAINT [PK_r_FormIDType] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_FormIDType_r_FunctionList_FFunctionID] FOREIGN KEY ([FFunctionID]) REFERENCES [r_FunctionList] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_ReceiveStockBillTypes] (
    [FID] int NOT NULL IDENTITY,
    [FFunctionID] int NOT NULL,
    [ErpBillTypeID] nvarchar(450) NOT NULL,
    [ErpBillName] nvarchar(100) NOT NULL,
    [FROB] bit NOT NULL,
    [FHeadTbName] nvarchar(100) NOT NULL,
    [FEntryTbName] nvarchar(100) NOT NULL,
    [FStatus] int NOT NULL,
    [FDefaultUserid] int NOT NULL,
    [FDefaultDepid] int NOT NULL,
    CONSTRAINT [PK_r_ReceiveStockBillTypes] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_ReceiveStockBillTypes_r_FunctionList_FFunctionID] FOREIGN KEY ([FFunctionID]) REFERENCES [r_FunctionList] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_SelectJoinID] (
    [FID] int NOT NULL IDENTITY,
    [FFunctionID] int NOT NULL,
    [FDocType] nvarchar(80) NOT NULL,
    [FOptcode] nvarchar(80) NOT NULL,
    CONSTRAINT [PK_r_SelectJoinID] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_SelectJoinID_r_FunctionList_FFunctionID] FOREIGN KEY ([FFunctionID]) REFERENCES [r_FunctionList] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_CloudBillQuery] (
    [FID] int NOT NULL IDENTITY,
    [FFormIDTypeID] int NOT NULL,
    [FFieldKeys] nvarchar(max) NOT NULL,
    [FKDBillName] nvarchar(80) NOT NULL,
    [FRBBillName] nvarchar(80) NOT NULL,
    [FFiledString] nvarchar(max) NOT NULL,
    [FOrderString] nvarchar(max) NOT NULL,
    [FTopRowCount] int NOT NULL,
    [FStartRow] int NOT NULL,
    [FLimit] int NOT NULL,
    [FSubSystemID] nvarchar(80) NOT NULL,
    CONSTRAINT [PK_r_CloudBillQuery] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_CloudBillQuery_r_FormIDType_FFormIDTypeID] FOREIGN KEY ([FFormIDTypeID]) REFERENCES [r_FormIDType] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_BillConvertes] (
    [FID] int NOT NULL IDENTITY,
    [FReceiveID] int NOT NULL,
    [FPageID] int NOT NULL,
    [FSourceName] nvarchar(100) NOT NULL,
    [FK3FiledName] nvarchar(100) NOT NULL,
    [FNeedInput] bit NOT NULL,
    CONSTRAINT [PK_r_BillConvertes] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_BillConvertes_r_ReceiveStockBillTypes_FReceiveID] FOREIGN KEY ([FReceiveID]) REFERENCES [r_ReceiveStockBillTypes] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_K3DefaultColVals] (
    [FID] int NOT NULL IDENTITY,
    [FReceiveID] int NOT NULL,
    [FPageID] int NOT NULL,
    [K3FiledName] nvarchar(100) NOT NULL,
    [FSqlType] nvarchar(100) NOT NULL,
    [FVal] nvarchar(100) NOT NULL,
    [FDescribe] nvarchar(max) NULL,
    CONSTRAINT [PK_r_K3DefaultColVals] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_K3DefaultColVals_r_ReceiveStockBillTypes_FReceiveID] FOREIGN KEY ([FReceiveID]) REFERENCES [r_ReceiveStockBillTypes] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_JoinTb] (
    [FID] int NOT NULL IDENTITY,
    [FSID] int NOT NULL,
    [FCtlID] int NOT NULL,
    [FTbName] nvarchar(200) NOT NULL,
    [FAsName] nvarchar(50) NOT NULL,
    [FJoinType] nvarchar(20) NOT NULL,
    [FJoinSymbol] nvarchar(20) NOT NULL,
    [FCondition] nvarchar(2000) NOT NULL,
    CONSTRAINT [PK_r_JoinTb] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_JoinTb_r_SelectJoinID_FSID] FOREIGN KEY ([FSID]) REFERENCES [r_SelectJoinID] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_SCol] (
    [FID] int NOT NULL IDENTITY,
    [FSID] int NOT NULL,
    [FCtlID] int NOT NULL,
    [FAsTable] nvarchar(50) NOT NULL,
    [FColName] nvarchar(50) NOT NULL,
    [FDeaful] nvarchar(2000) NOT NULL,
    [FAsName] nvarchar(50) NOT NULL,
    CONSTRAINT [PK_r_SCol] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_SCol_r_SelectJoinID_FSID] FOREIGN KEY ([FSID]) REFERENCES [r_SelectJoinID] ([FID]) ON DELETE CASCADE
);
GO

CREATE UNIQUE INDEX [IX_r_BillConvertes_FReceiveID_FPageID_FK3FiledName] ON [r_BillConvertes] ([FReceiveID], [FPageID], [FK3FiledName]);
GO

CREATE UNIQUE INDEX [IX_r_CloudBillQuery_FFormIDTypeID] ON [r_CloudBillQuery] ([FFormIDTypeID]);
GO

CREATE INDEX [IX_r_ClumnConvert_FFunctionID] ON [r_ClumnConvert] ([FFunctionID]);
GO

CREATE INDEX [IX_r_FormIDType_FFunctionID_FDocType] ON [r_FormIDType] ([FFunctionID], [FDocType]);
GO

CREATE INDEX [IX_r_FunctionList_FGoupID] ON [r_FunctionList] ([FGoupID]);
GO

CREATE UNIQUE INDEX [IX_r_JoinTb_FSID_FCtlID] ON [r_JoinTb] ([FSID], [FCtlID]);
GO

CREATE UNIQUE INDEX [IX_r_K3DefaultColVals_FReceiveID_FPageID_K3FiledName] ON [r_K3DefaultColVals] ([FReceiveID], [FPageID], [K3FiledName]);
GO

CREATE UNIQUE INDEX [IX_r_ReceiveStockBillTypes_FFunctionID_ErpBillTypeID] ON [r_ReceiveStockBillTypes] ([FFunctionID], [ErpBillTypeID]);
GO

CREATE UNIQUE INDEX [IX_r_SCol_FSID_FAsName] ON [r_SCol] ([FSID], [FAsName]);
GO

CREATE UNIQUE INDEX [IX_r_SCol_FSID_FCtlID] ON [r_SCol] ([FSID], [FCtlID]);
GO

CREATE INDEX [IX_r_SelectJoinID_FDocType] ON [r_SelectJoinID] ([FDocType]);
GO

CREATE INDEX [IX_r_SelectJoinID_FFunctionID] ON [r_SelectJoinID] ([FFunctionID]);
GO

CREATE INDEX [IX_RBLog_FDateTime_FDocType] ON [RBLog] ([FDateTime], [FDocType]);
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20220906123111_appinfo', N'7.0.4');
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

DROP TABLE [r_JoinTb];
GO

DROP TABLE [r_K3DefaultColVals];
GO

DROP TABLE [r_MesRequestStatus];
GO

DROP TABLE [r_SCol];
GO

DECLARE @var0 sysname;
SELECT @var0 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[r_FunctionList]') AND [c].[name] = N'FIsDetail');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [r_FunctionList] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [r_FunctionList] DROP COLUMN [FIsDetail];
GO

DECLARE @var1 sysname;
SELECT @var1 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[RBLog]') AND [c].[name] = N'FSource');
IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [RBLog] DROP CONSTRAINT [' + @var1 + '];');
ALTER TABLE [RBLog] ALTER COLUMN [FSource] nvarchar(max) NOT NULL;
GO

DECLARE @var2 sysname;
SELECT @var2 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[RBLog]') AND [c].[name] = N'FMess');
IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [RBLog] DROP CONSTRAINT [' + @var2 + '];');
ALTER TABLE [RBLog] ALTER COLUMN [FMess] nvarchar(max) NOT NULL;
GO

DROP INDEX [IX_RBLog_FDateTime_FDocType] ON [RBLog];
DECLARE @var3 sysname;
SELECT @var3 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[RBLog]') AND [c].[name] = N'FDocType');
IF @var3 IS NOT NULL EXEC(N'ALTER TABLE [RBLog] DROP CONSTRAINT [' + @var3 + '];');
ALTER TABLE [RBLog] ALTER COLUMN [FDocType] nvarchar(200) NOT NULL;
CREATE INDEX [IX_RBLog_FDateTime_FDocType] ON [RBLog] ([FDateTime], [FDocType]);
GO

ALTER TABLE [RBLog] ADD [FGUID] nvarchar(max) NOT NULL DEFAULT N'';
GO

ALTER TABLE [r_FormIDType] ADD [FGuidColName] nvarchar(100) NOT NULL DEFAULT N'';
GO

ALTER TABLE [r_FormIDType] ADD [FNeedBreakSQL] nvarchar(500) NOT NULL DEFAULT N'';
GO

ALTER TABLE [r_ClumnConvert] ADD [FIndexID] int NOT NULL DEFAULT 0;
GO

CREATE TABLE [r_ApiHostServices] (
    [FID] int NOT NULL IDENTITY,
    [FActionName] nvarchar(80) NOT NULL,
    [FDocType] nvarchar(80) NOT NULL,
    [FTimer] int NOT NULL,
    CONSTRAINT [PK_r_ApiHostServices] PRIMARY KEY ([FID])
);
GO

CREATE TABLE [r_BillTypeRules] (
    [FID] int NOT NULL IDENTITY,
    [FPostTypeID] nvarchar(80) NOT NULL,
    [FSBillTypeNumber] nvarchar(80) NOT NULL,
    [FDBillTypeNumber] nvarchar(80) NOT NULL,
    CONSTRAINT [PK_r_BillTypeRules] PRIMARY KEY ([FID])
);
GO

CREATE TABLE [r_CustomServices] (
    [FID] int NOT NULL IDENTITY,
    [FApiGroupID] int NOT NULL,
    [FNamespace] nvarchar(255) NOT NULL,
    [FClassName] nvarchar(255) NOT NULL,
    [FActionName] nvarchar(255) NOT NULL,
    [FAppComponents] nvarchar(255) NOT NULL,
    CONSTRAINT [PK_r_CustomServices] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_CustomServices_APIGrou_FApiGroupID] FOREIGN KEY ([FApiGroupID]) REFERENCES [APIGrou] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_DataSaveBatch] (
    [FID] int NOT NULL IDENTITY,
    [FDocTypeID] int NOT NULL,
    [FNumberSearch] bit NOT NULL,
    [FValidateFlag] bit NOT NULL,
    [FIsDeleteEntry] bit NOT NULL,
    [FIsEntryBatchFill] bit NOT NULL,
    [FNeedUpDateFields] nvarchar(max) NOT NULL,
    [FNeedReturnFields] nvarchar(max) NOT NULL,
    [FSubSystemId] nvarchar(max) NOT NULL,
    [FInterationFlags] nvarchar(max) NOT NULL,
    [FIsAutoSubmitAndAudit] bit NOT NULL,
    [FBatchCount] int NOT NULL,
    [FIsVerifyBaseDataField] bit NOT NULL,
    [FIsAutoAdjustField] bit NOT NULL,
    [FIgnoreInterationFlag] bit NOT NULL,
    [FIsControlPrecision] bit NOT NULL,
    [FValidateRepeatJson] bit NOT NULL,
    [FSubmitUrl] nvarchar(255) NOT NULL,
    [FAuditUrl] nvarchar(255) NOT NULL,
    [FErpBillType] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_r_DataSaveBatch] PRIMARY KEY ([FID]),
    CONSTRAINT [FK_r_DataSaveBatch_r_FormIDType_FDocTypeID] FOREIGN KEY ([FDocTypeID]) REFERENCES [r_FormIDType] ([FID]) ON DELETE CASCADE
);
GO

CREATE TABLE [r_LockPRD_MO] (
    [FID] int NOT NULL IDENTITY,
    [ERP_MO] nvarchar(36) NOT NULL,
    [MO_SEQ] nvarchar(450) NOT NULL,
    [MOID] nvarchar(max) NOT NULL,
    [MOEntryID] nvarchar(max) NOT NULL,
    [TRX_DATE] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_r_LockPRD_MO] PRIMARY KEY ([FID])
);
GO

CREATE TABLE [r_ReciveDataBack] (
    [FID] int NOT NULL IDENTITY,
    [FDocType] nvarchar(450) NOT NULL,
    [FBillNo] nvarchar(max) NOT NULL,
    [FErpID] int NOT NULL,
    [FSeq] int NOT NULL,
    [QUEUE_ID] nvarchar(max) NOT NULL,
    [FJson] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_r_ReciveDataBack] PRIMARY KEY ([FID])
);
GO

CREATE TABLE [t_AuthorizationInfo] (
    [FID] int NOT NULL IDENTITY,
    [FAppKey] nvarchar(max) NOT NULL,
    [FAppSecret] nvarchar(max) NOT NULL,
    [FAuthCode] nvarchar(max) NOT NULL,
    [FAppTicket] nvarchar(max) NULL,
    [FUserCode] nvarchar(max) NULL,
    [FUserAuthPermanentCode] nvarchar(max) NULL,
    [FAppAccessToken] nvarchar(max) NULL,
    [FOrgAuthTempCode] nvarchar(max) NULL,
    [FOrgAuthPermanentCode] nvarchar(max) NULL,
    [FOrgAccessToken] nvarchar(max) NULL,
    [FToken] nvarchar(max) NULL,
    [FStartDate] datetime2 NOT NULL,
    [FExpireDate] datetime2 NOT NULL,
    CONSTRAINT [PK_t_AuthorizationInfo] PRIMARY KEY ([FID])
);
GO

CREATE INDEX [IX_r_BillTypeRules_FPostTypeID_FSBillTypeNumber] ON [r_BillTypeRules] ([FPostTypeID], [FSBillTypeNumber]);
GO

CREATE INDEX [IX_r_CustomServices_FApiGroupID] ON [r_CustomServices] ([FApiGroupID]);
GO

CREATE INDEX [IX_r_DataSaveBatch_FDocTypeID] ON [r_DataSaveBatch] ([FDocTypeID]);
GO

CREATE INDEX [IX_r_LockPRD_MO_ERP_MO_MO_SEQ] ON [r_LockPRD_MO] ([ERP_MO], [MO_SEQ]);
GO

CREATE UNIQUE INDEX [IX_r_ReciveDataBack_FDocType_FErpID_FSeq] ON [r_ReciveDataBack] ([FDocType], [FErpID], [FSeq]);
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20240827172740_updateInfoV1', N'7.0.4');
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

CREATE TABLE [r_ERPPutOperationAnchor] (
    [FID] int NOT NULL IDENTITY,
    [FActionName] nvarchar(80) NOT NULL,
    [FDocType] nvarchar(80) NOT NULL,
    [FErpBillNo] nvarchar(255) NOT NULL,
    [FOperation] int NOT NULL,
    [FOperationResult] int NOT NULL,
    [QUEUE_ID] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_r_ERPPutOperationAnchor] PRIMARY KEY ([FID])
);
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20240827173122_updateInfoV2', N'7.0.4');
GO

COMMIT;
GO

