begin;

set search_path to public;

create or replace function comment_on_column_if_exists(p_table_name text, p_column_name text, p_comment_text text)
returns void
language plpgsql
as $$
begin
  if exists (
    select 1
    from information_schema.columns c
    where c.table_schema = current_schema()
      and c.table_name = p_table_name
      and c.column_name = p_column_name
  ) then
    execute format(
      'comment on column %I.%I is %L',
      p_table_name,
      p_column_name,
      p_comment_text
    );
  end if;
end;
$$;

with column_comments(table_name, column_name, comment_text) as (
  values
    ('sys_user', 'id', '用户主键'),
    ('sys_user', 'user_id', '用户ID'),
    ('sys_user', 'user_code', '工号/员工编号'),
    ('sys_user', 'username', '登录账号'),
    ('sys_user', 'real_name', '姓名'),
    ('sys_user', 'display_name', '显示名称'),
    ('sys_user', 'mobile', '手机号'),
    ('sys_user', 'phone', '手机号'),
    ('sys_user', 'email', '邮箱'),
    ('sys_user', 'org_id', '所属组织ID'),
    ('sys_user', 'job_title', '职位'),
    ('sys_user', 'status', '用户状态'),
    ('sys_user', 'hire_date', '入职日期'),
    ('sys_user', 'avatar_url', '头像地址'),
    ('sys_user', 'password_hash', '密码哈希'),
    ('sys_user', 'created_at', '创建时间'),
    ('sys_user', 'updated_at', '更新时间'),
    ('sys_user', 'is_deleted', '逻辑删除标记'),
    ('sys_user', 'pwd_reset_required', '首次登录是否需要重置密码'),
    ('sys_user', 'is_platform_admin', '是否平台管理员'),

    ('crm_customer', 'id', '客户主键'),
    ('crm_customer', 'customer_code', '客户编码'),
    ('crm_customer', 'customer_name', '客户名称'),
    ('crm_customer', 'customer_type', '客户类型'),
    ('crm_customer', 'industry', '行业'),
    ('crm_customer', 'province', '省份'),
    ('crm_customer', 'city', '城市'),
    ('crm_customer', 'address', '详细地址'),
    ('crm_customer', 'owner_user_id', '当前负责人ID'),
    ('crm_customer', 'source', '客户来源'),
    ('crm_customer', 'status', '客户状态'),
    ('crm_customer', 'remark', '备注说明'),
    ('crm_customer', 'created_at', '创建时间'),
    ('crm_customer', 'updated_at', '更新时间'),

    ('crm_opportunity', 'id', '商机主键'),
    ('crm_opportunity', 'opportunity_code', '商机编号'),
    ('crm_opportunity', 'opportunity_name', '商机名称'),
    ('crm_opportunity', 'customer_id', '客户ID'),
    ('crm_opportunity', 'owner_user_id', '商机负责人ID'),
    ('crm_opportunity', 'sales_expansion_id', '关联销售拓展ID'),
    ('crm_opportunity', 'channel_expansion_id', '关联渠道拓展ID'),
    ('crm_opportunity', 'pre_sales_id', '售前ID'),
    ('crm_opportunity', 'pre_sales_name', '售前姓名'),
    ('crm_opportunity', 'project_location', '项目所在地'),
    ('crm_opportunity', 'operator_name', '运作方'),
    ('crm_opportunity', 'amount', '商机金额'),
    ('crm_opportunity', 'expected_close_date', '预计结单日期'),
    ('crm_opportunity', 'confidence_pct', '把握度(0-100)'),
    ('crm_opportunity', 'stage', '商机阶段'),
    ('crm_opportunity', 'opportunity_type', '商机类型'),
    ('crm_opportunity', 'product_type', '产品类型'),
    ('crm_opportunity', 'source', '商机来源'),
    ('crm_opportunity', 'competitor_name', '竞品名称'),
    ('crm_opportunity', 'archived', '是否归档'),
    ('crm_opportunity', 'pushed_to_oms', '是否已推送OMS'),
    ('crm_opportunity', 'oms_project_code', 'OMS系统项目编号'),
    ('crm_opportunity', 'oms_push_time', '推送OMS时间'),
    ('crm_opportunity', 'description', '商机说明/备注'),
    ('crm_opportunity', 'status', '商机状态'),
    ('crm_opportunity', 'created_at', '创建时间'),
    ('crm_opportunity', 'updated_at', '更新时间'),

    ('crm_opportunity_followup', 'id', '跟进记录主键'),
    ('crm_opportunity_followup', 'opportunity_id', '商机ID'),
    ('crm_opportunity_followup', 'followup_time', '跟进时间'),
    ('crm_opportunity_followup', 'followup_type', '跟进方式'),
    ('crm_opportunity_followup', 'content', '跟进内容'),
    ('crm_opportunity_followup', 'next_action', '下一步动作'),
    ('crm_opportunity_followup', 'followup_user_id', '跟进人ID'),
    ('crm_opportunity_followup', 'source_type', '来源类型'),
    ('crm_opportunity_followup', 'source_id', '来源记录ID'),
    ('crm_opportunity_followup', 'created_at', '创建时间'),
    ('crm_opportunity_followup', 'updated_at', '更新时间'),

    ('crm_sales_expansion', 'id', '销售拓展主键'),
    ('crm_sales_expansion', 'employee_no', '工号/员工编号'),
    ('crm_sales_expansion', 'candidate_name', '候选人姓名'),
    ('crm_sales_expansion', 'office_name', '办事处/代表处'),
    ('crm_sales_expansion', 'mobile', '手机号'),
    ('crm_sales_expansion', 'email', '邮箱'),
    ('crm_sales_expansion', 'target_dept', '所属部门'),
    ('crm_sales_expansion', 'industry', '所属行业'),
    ('crm_sales_expansion', 'title', '职务'),
    ('crm_sales_expansion', 'intent_level', '合作意向'),
    ('crm_sales_expansion', 'stage', '跟进阶段'),
    ('crm_sales_expansion', 'has_desktop_exp', '是否有云桌面经验'),
    ('crm_sales_expansion', 'in_progress', '是否持续跟进中'),
    ('crm_sales_expansion', 'employment_status', '候选人状态'),
    ('crm_sales_expansion', 'expected_join_date', '预计入职日期'),
    ('crm_sales_expansion', 'owner_user_id', '负责人ID'),
    ('crm_sales_expansion', 'remark', '备注说明'),
    ('crm_sales_expansion', 'created_at', '创建时间'),
    ('crm_sales_expansion', 'updated_at', '更新时间'),

    ('crm_channel_expansion', 'id', '渠道拓展主键'),
    ('crm_channel_expansion', 'channel_code', '渠道编码'),
    ('crm_channel_expansion', 'province', '省份'),
    ('crm_channel_expansion', 'city', '市'),
    ('crm_channel_expansion', 'channel_name', '渠道名称'),
    ('crm_channel_expansion', 'office_address', '办公地址'),
    ('crm_channel_expansion', 'channel_industry', '聚焦行业'),
    ('crm_channel_expansion', 'certification_level', '汇智内部认证级别'),
    ('crm_channel_expansion', 'industry', '行业(兼容旧字段)'),
    ('crm_channel_expansion', 'annual_revenue', '年度营业额(万元)'),
    ('crm_channel_expansion', 'staff_size', '人员规模'),
    ('crm_channel_expansion', 'contact_established_date', '建立联系日期'),
    ('crm_channel_expansion', 'intent_level', '合作意向'),
    ('crm_channel_expansion', 'has_desktop_exp', '是否有云桌面经验'),
    ('crm_channel_expansion', 'contact_name', '主联系人姓名(兼容旧结构)'),
    ('crm_channel_expansion', 'contact_title', '主联系人职务(兼容旧结构)'),
    ('crm_channel_expansion', 'contact_mobile', '主联系人电话(兼容旧结构)'),
    ('crm_channel_expansion', 'channel_attribute', '渠道属性编码，多个值逗号分隔'),
    ('crm_channel_expansion', 'internal_attribute', '新华三内部属性编码，多个值逗号分隔'),
    ('crm_channel_expansion', 'stage', '渠道合作阶段'),
    ('crm_channel_expansion', 'landed_flag', '是否已落地'),
    ('crm_channel_expansion', 'expected_sign_date', '预计签约日期'),
    ('crm_channel_expansion', 'owner_user_id', '负责人ID'),
    ('crm_channel_expansion', 'remark', '备注说明'),
    ('crm_channel_expansion', 'created_at', '创建时间'),
    ('crm_channel_expansion', 'updated_at', '更新时间'),

    ('crm_channel_expansion_contact', 'id', '联系人主键'),
    ('crm_channel_expansion_contact', 'channel_expansion_id', '渠道拓展ID'),
    ('crm_channel_expansion_contact', 'contact_name', '联系人姓名'),
    ('crm_channel_expansion_contact', 'contact_mobile', '联系人电话'),
    ('crm_channel_expansion_contact', 'contact_title', '联系人职务'),
    ('crm_channel_expansion_contact', 'sort_order', '排序号'),
    ('crm_channel_expansion_contact', 'created_at', '创建时间'),
    ('crm_channel_expansion_contact', 'updated_at', '更新时间'),

    ('crm_expansion_followup', 'id', '跟进记录主键'),
    ('crm_expansion_followup', 'biz_type', '业务类型'),
    ('crm_expansion_followup', 'biz_id', '业务对象ID'),
    ('crm_expansion_followup', 'followup_time', '跟进时间'),
    ('crm_expansion_followup', 'followup_type', '跟进方式'),
    ('crm_expansion_followup', 'content', '跟进内容'),
    ('crm_expansion_followup', 'next_action', '下一步动作'),
    ('crm_expansion_followup', 'followup_user_id', '跟进人ID'),
    ('crm_expansion_followup', 'visit_start_time', '拜访开始时间'),
    ('crm_expansion_followup', 'evaluation_content', '评估内容'),
    ('crm_expansion_followup', 'next_plan', '后续规划'),
    ('crm_expansion_followup', 'source_type', '来源类型'),
    ('crm_expansion_followup', 'source_id', '来源记录ID'),
    ('crm_expansion_followup', 'created_at', '创建时间'),
    ('crm_expansion_followup', 'updated_at', '更新时间'),

    ('work_checkin', 'id', '打卡记录主键'),
    ('work_checkin', 'user_id', '打卡人ID'),
    ('work_checkin', 'checkin_date', '打卡日期'),
    ('work_checkin', 'checkin_time', '打卡时间'),
    ('work_checkin', 'biz_type', '关联对象类型'),
    ('work_checkin', 'biz_id', '关联对象ID'),
    ('work_checkin', 'biz_name', '关联对象名称'),
    ('work_checkin', 'longitude', '经度'),
    ('work_checkin', 'latitude', '纬度'),
    ('work_checkin', 'location_text', '打卡地点'),
    ('work_checkin', 'remark', '备注说明(含现场照片元数据)'),
    ('work_checkin', 'user_name', '打卡人姓名快照'),
    ('work_checkin', 'dept_name', '所属部门快照'),
    ('work_checkin', 'status', '打卡状态'),
    ('work_checkin', 'created_at', '创建时间'),
    ('work_checkin', 'updated_at', '更新时间'),

    ('work_daily_report', 'id', '日报主键'),
    ('work_daily_report', 'user_id', '提交人ID'),
    ('work_daily_report', 'report_date', '日报日期'),
    ('work_daily_report', 'work_content', '今日工作内容(含结构化明细元数据)'),
    ('work_daily_report', 'tomorrow_plan', '明日工作计划(含结构化计划项元数据)'),
    ('work_daily_report', 'source_type', '提交来源'),
    ('work_daily_report', 'submit_time', '提交时间'),
    ('work_daily_report', 'status', '日报状态'),
    ('work_daily_report', 'score', '日报评分'),
    ('work_daily_report', 'created_at', '创建时间'),
    ('work_daily_report', 'updated_at', '更新时间'),

    ('work_daily_report_comment', 'id', '点评记录主键'),
    ('work_daily_report_comment', 'report_id', '日报ID'),
    ('work_daily_report_comment', 'reviewer_user_id', '点评人ID'),
    ('work_daily_report_comment', 'score', '点评评分'),
    ('work_daily_report_comment', 'comment_content', '点评内容'),
    ('work_daily_report_comment', 'reviewed_at', '点评时间'),
    ('work_daily_report_comment', 'created_at', '创建时间'),

    ('work_todo', 'id', '待办主键'),
    ('work_todo', 'user_id', '所属用户ID'),
    ('work_todo', 'title', '待办标题'),
    ('work_todo', 'biz_type', '业务类型'),
    ('work_todo', 'biz_id', '业务对象ID'),
    ('work_todo', 'due_date', '截止时间'),
    ('work_todo', 'status', '待办状态'),
    ('work_todo', 'priority', '优先级'),
    ('work_todo', 'created_at', '创建时间'),
    ('work_todo', 'updated_at', '更新时间'),

    ('sys_activity_log', 'id', '动态主键'),
    ('sys_activity_log', 'biz_type', '业务类型'),
    ('sys_activity_log', 'biz_id', '业务对象ID'),
    ('sys_activity_log', 'action_type', '动作类型'),
    ('sys_activity_log', 'title', '动态标题'),
    ('sys_activity_log', 'content', '动态内容'),
    ('sys_activity_log', 'operator_user_id', '操作人ID'),
    ('sys_activity_log', 'created_at', '创建时间')
)
select comment_on_column_if_exists(table_name, column_name, comment_text)
from column_comments;

commit;
