Files
workspace/code/fms/fms-api/config/migrations/014_migrate_contact.sql
T
2026-07-29 23:18:54 +08:00

126 lines
4.1 KiB
Transact-SQL

SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN TRANSACTION;
-- 删除废弃的 b_contact_type 表
IF OBJECT_ID(N'dbo.b_contact_type', N'U') IS NOT NULL
DROP TABLE dbo.b_contact_type;
-- b_contact
DECLARE @ContactObjectId int = OBJECT_ID(N'dbo.b_contact', N'U');
DECLARE @RebuildContact bit = 0;
IF @ContactObjectId IS NULL
SET @RebuildContact = 1;
ELSE IF EXISTS (
SELECT 1 FROM sys.columns
WHERE object_id = @ContactObjectId
AND name = N'b_id'
AND system_type_id = 167 -- varchar → old structure
)
SET @RebuildContact = 1;
IF @RebuildContact = 1
BEGIN
-- 先删依赖视图
IF OBJECT_ID(N'dbo.v_b_contact', N'V') IS NOT NULL
DROP VIEW dbo.v_b_contact;
IF @ContactObjectId IS NOT NULL
DROP TABLE dbo.b_contact;
CREATE TABLE dbo.b_contact (
b_id bigint NOT NULL,
b_name_ch nvarchar(50) NULL,
b_name_en nvarchar(50) NULL,
b_sex nvarchar(50) NULL,
b_phone nvarchar(50) NULL,
b_mobilephone nvarchar(50) NULL,
b_fax nvarchar(50) NULL,
b_email nvarchar(50) NULL,
b_cate_id bigint NULL,
b_duty nvarchar(50) NULL,
b_homephone nvarchar(50) NULL,
b_homeaddr nvarchar(255) NULL,
b_bz nvarchar(max) NULL,
b_departmentname nvarchar(50) NULL,
b_picture varbinary(max) NULL,
b_customer_id bigint NULL,
b_owner_id bigint NULL,
b_isdefault int NULL,
b_showcolor int NULL,
b_birthday datetime NULL,
b_type1_id bigint NULL,
b_taste nvarchar(50) NULL,
b_habit nvarchar(50) NULL,
b_homesituation nvarchar(50) NULL,
b_htbegindate datetime NULL,
b_htenddate datetime NULL,
b_state nvarchar(50) NULL,
b_xh int NOT NULL CONSTRAINT DF_b_contact_xh DEFAULT (0),
b_canuse int NOT NULL CONSTRAINT DF_b_contact_canuse DEFAULT (1),
b_inputuser_id bigint NULL,
b_inputdatetime datetime NULL,
b_updateuser_id bigint NULL,
b_updatedatetime datetime NULL,
CONSTRAINT PK_b_contact PRIMARY KEY (b_id)
);
CREATE INDEX IX_b_contact_customer
ON dbo.b_contact (b_customer_id, b_id);
CREATE INDEX IX_b_contact_owner
ON dbo.b_contact (b_owner_id, b_id);
CREATE INDEX IX_b_contact_cate
ON dbo.b_contact (b_cate_id, b_id);
END
-- v_b_contact 视图
IF OBJECT_ID(N'dbo.v_b_contact', N'V') IS NOT NULL
DROP VIEW dbo.v_b_contact;
EXEC sp_executesql N'
CREATE VIEW dbo.v_b_contact AS
SELECT
b_contact.b_id,
b_contact.b_name_ch,
b_contact.b_name_en,
b_contact.b_sex,
b_contact.b_phone,
b_contact.b_mobilephone,
b_contact.b_fax,
b_contact.b_email,
b_contact.b_cate_id,
b_contact.b_duty,
b_contact.b_homephone,
b_contact.b_homeaddr,
b_contact.b_bz,
b_contact.b_departmentname,
b_contact.b_picture,
b_contact.b_customer_id,
b_contact.b_owner_id,
b_contact.b_isdefault,
b_contact.b_showcolor,
b_contact.b_birthday,
b_contact.b_type1_id,
b_contact.b_taste,
b_contact.b_habit,
b_contact.b_homesituation,
b_contact.b_htbegindate,
b_contact.b_htenddate,
b_contact.b_state,
b_contact.b_xh,
b_contact.b_canuse,
b_contact.b_inputuser_id,
b_contact.b_inputdatetime,
b_contact.b_updateuser_id,
b_contact.b_updatedatetime,
ISNULL(b_user.b_name, b_user.b_account) AS v_ownername,
b_user.b_department AS v_ownerdepartment,
b_othercompany.b_name AS v_customer_name
FROM dbo.b_contact
LEFT JOIN dbo.b_user ON b_user.b_id = b_contact.b_owner_id
LEFT JOIN dbo.b_othercompany ON b_othercompany.b_id = b_contact.b_customer_id;
';
COMMIT TRANSACTION;