-- 闲管家开放平台：应用配置中心 + ERP 订单落库（执行前请备份）
-- 说明：菜单需在「系统管理-菜单管理」中自行新增，组件路径示例：
--   配置中心 system/goofish/erpOpenConfig/index
--   订单跟踪 system/goofish/erpGoofishOrder/index
--   变更日志（跨单排查，对接 GET /jarvis/erpGoofishOrder/eventLog/list）system/goofish/erpGoofishEventLog/index

CREATE TABLE IF NOT EXISTS erp_open_config (
  id              bigint(20)   NOT NULL AUTO_INCREMENT COMMENT '主键',
  app_key         varchar(64)  NOT NULL COMMENT '开放平台 AppKey(appid)',
  app_secret      varchar(128) NOT NULL COMMENT '开放平台 AppSecret',
  xy_user_name    varchar(128)          DEFAULT NULL COMMENT '默认闲鱼会员名（展示）',
  express_code    varchar(64)           DEFAULT NULL COMMENT '发货用快递公司编码（日日顺物流在官方列表中为 rrs，以「查询快递公司」接口为准）',
  express_name    varchar(64)           DEFAULT NULL COMMENT '快递公司名称（展示）',
  status          char(1)      NOT NULL DEFAULT '0' COMMENT '0正常 1停用',
  order_num       int(11)      NOT NULL DEFAULT 0 COMMENT '排序（小优先）',
  create_by       varchar(64)           DEFAULT '',
  create_time     datetime              DEFAULT NULL,
  update_by       varchar(64)           DEFAULT '',
  update_time     datetime              DEFAULT NULL,
  remark          varchar(500)          DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (id),
  UNIQUE KEY uk_app_key (app_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='闲管家开放平台应用配置';

CREATE TABLE IF NOT EXISTS erp_goofish_order (
  id               bigint(20)   NOT NULL AUTO_INCREMENT COMMENT '主键',
  app_key          varchar(64)  NOT NULL COMMENT 'AppKey',
  seller_id        bigint(20)            DEFAULT NULL COMMENT '商家ID',
  user_name        varchar(128)          DEFAULT NULL COMMENT '闲鱼会员名',
  order_no         varchar(64)  NOT NULL COMMENT '闲鱼订单号',
  order_type       int(11)               DEFAULT NULL COMMENT '订单类型',
  order_status     int(11)               DEFAULT NULL COMMENT '订单状态(推送/列表)',
  refund_status    int(11)               DEFAULT NULL COMMENT '退款状态',
  modify_time      bigint(20)            DEFAULT NULL COMMENT '订单更新时间(秒)',
  product_id       bigint(20)            DEFAULT NULL COMMENT '管家商品ID',
  item_id          bigint(20)            DEFAULT NULL COMMENT '闲鱼商品ID',
  goods_title      varchar(512)          DEFAULT NULL COMMENT '商品标题(详情 goods.title)',
  goods_image_url  varchar(1024)         DEFAULT NULL COMMENT '商品主图 URL(goods.images 首图)',
  buyer_nick       varchar(256)          DEFAULT NULL COMMENT '买家昵称(详情 buyer_nick)',
  pay_amount       bigint(20)            DEFAULT NULL COMMENT '实付金额(分) pay_amount',
  detail_waybill_no varchar(128)         DEFAULT NULL COMMENT '闲管家详情回传运单号 waybill_no',
  detail_express_code varchar(64)       DEFAULT NULL COMMENT '详情快递编码 express_code',
  detail_express_name varchar(128)      DEFAULT NULL COMMENT '详情快递名称 express_name',
  receiver_name    varchar(128)          DEFAULT NULL COMMENT '收货人(详情有则落库)',
  receiver_mobile  varchar(64)           DEFAULT NULL COMMENT '收货手机',
  receiver_address varchar(1000)         DEFAULT NULL COMMENT '收货详细地址(address)',
  receiver_region  varchar(256)          DEFAULT NULL COMMENT '省市区街道等拼接展示',
  recv_prov_name   varchar(64)           DEFAULT NULL COMMENT 'prov_name',
  recv_city_name   varchar(64)           DEFAULT NULL COMMENT 'city_name',
  recv_area_name   varchar(64)           DEFAULT NULL COMMENT 'area_name',
  recv_town_name   varchar(128)          DEFAULT NULL COMMENT 'town_name',
  detail_json      longtext COMMENT '订单详情接口全量 JSON',
  last_notify_json longtext COMMENT '最近一次推送原文 JSON',
  jd_order_id      bigint(20)            DEFAULT NULL COMMENT '关联 jd_order.id（第三方单号=闲鱼单号）',
  local_waybill_no varchar(128)          DEFAULT NULL COMMENT '本地物流扫描得到的运单号',
  ship_status      tinyint(4)   NOT NULL DEFAULT '0' COMMENT '0未发货 1已调用发货成功 2发货失败',
  ship_error       varchar(500)          DEFAULT NULL COMMENT '发货失败原因',
  ship_time        datetime              DEFAULT NULL COMMENT '发货调用成功时间',
  ship_express_code varchar(64)          DEFAULT NULL COMMENT '实际发货使用的快递编码',
  create_time      datetime              DEFAULT NULL,
  update_time      datetime              DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_app_order (app_key, order_no),
  KEY idx_jd_order (jd_order_id),
  KEY idx_order_status (order_status),
  KEY idx_modify_time (modify_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='闲管家 ERP 订单（全量跟踪）';

CREATE TABLE IF NOT EXISTS erp_goofish_order_event_log (
  id           bigint(20)   NOT NULL AUTO_INCREMENT COMMENT '主键',
  order_id     bigint(20)   NOT NULL COMMENT 'erp_goofish_order.id',
  app_key      varchar(64)           DEFAULT NULL,
  order_no     varchar(64)  NOT NULL,
  event_type   varchar(32)  NOT NULL COMMENT 'ORDER_SYNC/LOGISTICS_SYNC/SHIP',
  source       varchar(64)           NULL COMMENT 'NOTIFY/LIST/DETAIL_REFRESH 等',
  message      varchar(1024) NOT NULL,
  create_time  datetime              DEFAULT NULL,
  PRIMARY KEY (id),
  KEY idx_goofish_evt_order (order_id),
  KEY idx_goofish_evt_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='闲管家订单状态/物流/发货变更日志';

-- 可选：从旧枚举迁入两条示例（密钥请上线后立即修改）
-- INSERT INTO erp_open_config (app_key,app_secret,xy_user_name,remark,express_code,express_name,status,order_num)
-- VALUES ('1016208368633221','***','余生请多关照66','海尔胡歌',NULL,'日日顺','0',1);

-- 若依菜单（在「菜单管理」手工添加时参考）
-- 父菜单：系统管理下新增目录「闲管家ERP」
-- 子菜单1：组件 system/goofish/erpOpenConfig/index  权限前缀 jarvis:erpOpenConfig
-- 子菜单2：组件 system/goofish/erpGoofishOrder/index  权限前缀 jarvis:erpGoofishOrder
-- 子菜单3：组件 system/goofish/erpGoofishEventLog/index  权限沿用 jarvis:erpGoofishOrder:list 即可（仅列表查询）
--           路由地址建议与订单页同级，如订单为 …/erpGoofishOrder 则本页 …/erpGoofishEventLog（订单页「变更日志排查」按钮依赖此规则）
-- 按钮权限示例：
--   jarvis:erpOpenConfig:list,query,add,edit,remove
--   jarvis:erpGoofishOrder:list,query,edit

-- —— 已建表升级：详情摘要字段（若列已存在会报错，可逐条执行并忽略）——
-- ALTER TABLE erp_goofish_order ADD COLUMN goods_title varchar(512) NULL COMMENT '商品标题' AFTER item_id;
-- ALTER TABLE erp_goofish_order ADD COLUMN goods_image_url varchar(1024) NULL COMMENT '商品主图URL' AFTER goods_title;
-- ALTER TABLE erp_goofish_order ADD COLUMN buyer_nick varchar(256) NULL COMMENT '买家昵称' AFTER goods_image_url;
-- ALTER TABLE erp_goofish_order ADD COLUMN pay_amount bigint(20) NULL COMMENT '实付金额(分)' AFTER buyer_nick;
-- ALTER TABLE erp_goofish_order ADD COLUMN detail_waybill_no varchar(128) NULL COMMENT '详情运单号' AFTER pay_amount;
-- ALTER TABLE erp_goofish_order ADD COLUMN detail_express_code varchar(64) NULL COMMENT '详情快递编码' AFTER detail_waybill_no;
-- ALTER TABLE erp_goofish_order ADD COLUMN detail_express_name varchar(128) NULL COMMENT '详情快递名称' AFTER detail_express_code;
-- ALTER TABLE erp_goofish_order ADD COLUMN receiver_name varchar(128) NULL COMMENT '收货人' AFTER detail_express_name;
-- ALTER TABLE erp_goofish_order ADD COLUMN receiver_mobile varchar(64) NULL COMMENT '收货手机' AFTER receiver_name;
-- ALTER TABLE erp_goofish_order ADD COLUMN receiver_address varchar(1000) NULL COMMENT '收货地址' AFTER receiver_mobile;
-- ALTER TABLE erp_goofish_order ADD COLUMN receiver_region varchar(256) NULL COMMENT '省市区' AFTER receiver_address;
-- ALTER TABLE erp_goofish_order ADD COLUMN recv_prov_name varchar(64) NULL COMMENT 'prov_name' AFTER receiver_region;
-- ALTER TABLE erp_goofish_order ADD COLUMN recv_city_name varchar(64) NULL COMMENT 'city_name' AFTER recv_prov_name;
-- ALTER TABLE erp_goofish_order ADD COLUMN recv_area_name varchar(64) NULL COMMENT 'area_name' AFTER recv_city_name;
-- ALTER TABLE erp_goofish_order ADD COLUMN recv_town_name varchar(128) NULL COMMENT 'town_name' AFTER recv_area_name;

-- 已建库一键升级（可重复执行、自动判存）：请使用同目录 erp_goofish_upgrade.sql
