/* ============================================================================ FMS 新系统业务表建表脚本 — 联系人 依据文档:FMS业务表设计.md 使用说明: 1. 执行前先切换到目标数据库(USE [数据库名])。 2. 脚本只建表、索引和查询视图,不含初始化数据和迁移数据。 3. 不使用外键、CHECK、触发器;关联完整性和业务校验由业务层处理。 4. 表已存在时先用「重建」段落删除旧对象,再执行建表段落。 ============================================================================ */ SET NOCOUNT ON; GO /* ============================================================================ 重建(仅旧结构需要重建时执行,会删除这些表及其数据) ============================================================================ */ drop view if exists dbo.v_b_contact; GO drop table if exists dbo.b_contact_type; GO drop table if exists dbo.b_contact; GO /* ============================================================================ 主表 ============================================================================ */ create table dbo.b_contact ( b_id bigint not null primary key, -- 主键(雪花) b_ywid varchar(50) not null, -- 业务ID(旧库 b_contact.b_id) b_name_ch nvarchar(50) null, -- 中文名 b_name_en nvarchar(50) null, -- 英文名 b_sex nvarchar(50) null, -- 性别 b_birthday date null, -- 生日 b_duty nvarchar(50) null, -- 职务 b_departmentname nvarchar(50) null, -- 部门 b_phone nvarchar(50) null, -- 电话 b_mobilephone nvarchar(50) null, -- 手机 b_fax nvarchar(50) null, -- 传真 b_email varchar(50) null, -- 邮件 b_homephone nvarchar(50) null, -- 家庭电话 b_homeaddr nvarchar(255) null, -- 家庭住址 b_taste nvarchar(50) null, -- 口味偏好 b_habit nvarchar(50) null, -- 习惯 b_homesituation nvarchar(50) null, -- 家庭情况 b_picture varchar(500) null, -- 照片(文件路径) b_cate_id varchar(50) null, -- 联系人类型(b_contact_type.b_id) b_type1_id varchar(50) null, -- 备用分类 b_customer_id bigint null, -- 所属往来单位(b_othercompany.b_id) b_owner_id varchar(50) null, -- 业务员(b_user.b_id) b_isdefault tinyint not null default 0, -- 是否默认联系人(0/1) b_showcolor int null, -- 显示颜色 b_htbegindate date null, -- 合同开始日期 b_htenddate date null, -- 合同结束日期 b_state varchar(50) null, -- 状态 b_bz nvarchar(1000) null, -- 备注 b_created_by varchar(50) null, -- 创建人 b_created_at datetime2 null, -- 创建时间 b_updated_by varchar(50) null, -- 最后修改人 b_updated_at datetime2 null -- 最后修改时间 ); GO create unique index ux_b_contact_ywid on dbo.b_contact (b_ywid); GO create index ix_b_contact_customer on dbo.b_contact (b_customer_id, b_id); GO create index ix_b_contact_name on dbo.b_contact (b_name_ch, b_id); GO /* ============================================================================ 联系人类型字典 ============================================================================ */ create table dbo.b_contact_type ( b_id varchar(50) not null primary key, -- 类型编码 b_name nvarchar(50) null, -- 类型名称 b_i18n varchar(150) null, -- 多语言资源键 b_canuse tinyint not null default 1, -- 是否启用(0/1) b_xh int not null default 0, -- 显示顺序 b_bz nvarchar(200) null, -- 备注 b_created_by varchar(50) null, b_created_at datetime2 null, b_updated_by varchar(50) null, b_updated_at datetime2 null ); GO /* ============================================================================ 查询视图 用途:作为 s_module.b_view_table 的数据源。列表查询直接 select ... from 该视图, s_field.b_field 必须是视图里真实存在的列。 说明:视图只供查询,写库仍走 s_module.b_save_table = b_contact。 ============================================================================ */ create or alter view dbo.v_b_contact as select c.b_id, c.b_ywid, c.b_name_ch, c.b_name_en, c.b_sex, c.b_phone, c.b_mobilephone, c.b_fax, c.b_email, c.b_duty, c.b_homephone, c.b_cate_id, c.b_homeaddr, c.b_bz, c.b_departmentname, c.b_picture, c.b_customer_id, c.b_owner_id, c.b_isdefault, c.b_taste, c.b_habit, c.b_homesituation, u.b_name as v_ownername, -- 业务员姓名 d.b_name as v_ownerdepartment, -- 业务员所属部门 c.b_showcolor, o.b_name as v_customer_name, -- 客户名称 c.b_birthday, case when isnull(c.b_name_ch, '') = '' then c.b_name_en when isnull(c.b_name_en, '') = '' then c.b_name_ch else isnull(c.b_name_ch, '') + isnull(c.b_name_en, '') end as b_name, c.b_type1_id, c.b_htbegindate, c.b_htenddate, c.b_created_by, c.b_created_at, c.b_updated_by, c.b_updated_at, c.b_state from dbo.b_contact c left join dbo.b_othercompany o on o.b_id = c.b_customer_id left join dbo.b_user u on u.b_id = c.b_owner_id left join dbo.b_dept d on d.b_id = u.b_dept_id where isnull(o.b_ywid, '') <> 'b_employee'; GO