DROP TABLE IF EXISTS `mall_trade_after_sale_log`;
CREATE TABLE `mall_trade_after_sale_log` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `after_sale_id` int NOT NULL COMMENT '售后单ID，关联mall_trade_after_sale表的id字段',
  `operation_type` varchar(50) NOT NULL COMMENT '操作类型，如：退款成功通知、退款失败通知等',
  `description` varchar(500) DEFAULT NULL COMMENT '操作描述',
  `operator_id` int DEFAULT NULL COMMENT '操作人ID',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `create_by` varchar(20) DEFAULT NULL COMMENT '创建人',
  `update_by` varchar(20) DEFAULT NULL COMMENT '更新人',
  `tenant_id` varchar(20) DEFAULT 'Default' COMMENT '租户ID',
  PRIMARY KEY (`id`),
  KEY `idx_after_sale_id` (`after_sale_id`),
  KEY `idx_operator_id` (`operator_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='售后日志表';


DROP TABLE IF EXISTS `mall_order_goods`;
CREATE TABLE `mall_order_goods`  (
  `id` mediumint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
  `order_id` mediumint UNSIGNED NOT NULL DEFAULT 0 COMMENT '订单Id',
  `child_order_id` bigint NULL DEFAULT NULL COMMENT '关联子订单ID',
  `goods_id` mediumint UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品id',
  `goods_name` varchar(120) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL DEFAULT '' COMMENT '商品名称',
  `number` smallint UNSIGNED NOT NULL DEFAULT 1 COMMENT '商品数量',
  `price` decimal(18, 2) NOT NULL DEFAULT 0.00 COMMENT '商品价格',
  `coin` decimal(18, 2) NULL DEFAULT 0.00 COMMENT '订单支付的总学豆',
  `sku_id` mediumint UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品sku id',
  `cart_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '购物车项编号',
  `comment_status` bit(1) NOT NULL DEFAULT b'0' COMMENT '评价状态，1：已评价，0：为评价',
  `attributes` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品sku 属性',
  `is_real` tinyint UNSIGNED NULL DEFAULT 0 COMMENT '虚拟商品',
  `pic_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '' COMMENT '图片链接',
  `deleted` bit(1) NOT NULL COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `order_id`(`order_id` ASC) USING BTREE,
  INDEX `goods_id`(`goods_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 35 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '商城订单sku表' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `mall_order`;
CREATE TABLE `mall_order`  (
  `id` mediumint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '订单唯一标识',
  `order_sn` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '订单编号（唯一）',
  `user_id` mediumint UNSIGNED NULL DEFAULT 0 COMMENT '下单用户ID',
  `address_id` int NULL DEFAULT NULL COMMENT '下单用户的收获地址Id',
  `order_status` int UNSIGNED NULL DEFAULT 0 COMMENT '订单状态（0-待付款，1-已付款，2-已发货，3-已完成，4-已取消）',
  `pay_status` tinyint UNSIGNED NULL DEFAULT 0 COMMENT '支付状态（0-未支付，1-已支付，2-支付失败）',
  `shipping_status` tinyint UNSIGNED NULL DEFAULT 0 COMMENT '物流状态（0-未发货，1-已发货，2-已签收）',
  `comment_status` bit(1) NOT NULL DEFAULT b'0' COMMENT '评价状态，1：已评价，0：为评价',
  `consignee` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '收货人姓名',
  `country` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '国家',
  `province` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '省份',
  `city` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '城市',
  `district` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '区/县',
  `address` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '详细地址',
  `mobile` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '联系电话',
  `pay_order_id` int NULL DEFAULT NULL COMMENT '支付订单ID',
  `pay_channel_code` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '支付渠道名称',
  `payment_mode` int NULL DEFAULT 0 COMMENT '支付模式：0-线上支付 1-线下支付',
  `integral` int UNSIGNED NULL DEFAULT 0 COMMENT '使用积分数量',
  `integral_money` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '积分抵扣金额',
  `actual_price` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '实际支付金额（已扣除优惠）',
  `order_price` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '订单总价（含运费）',
  `goods_price` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '商品总价',
  `order_total_coin` decimal(18, 2) NULL DEFAULT 0.00 COMMENT '订单支付的总学豆',
  `exchange_ratio` decimal(18, 2) NULL DEFAULT 0.00 COMMENT '兑换比例',
  `coin_config_id` int NULL DEFAULT 0 COMMENT '学豆配置ID',
  `add_time` datetime NULL DEFAULT NULL COMMENT '订单创建时间',
  `confirm_time` datetime NULL DEFAULT NULL COMMENT '用户确认收货时间',
  `pay_time` datetime NULL DEFAULT NULL COMMENT '支付时间',
  `delivery_time` datetime NULL DEFAULT NULL COMMENT '发货时间',
  `delivery_user_id` int NULL DEFAULT 0 COMMENT '发货人ID',
  `logistics_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '物流公司编码',
  `shipping_no` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '物流单号',
  `postscript` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '订单备注',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '(后端使用)商家备注',
  `deleted` bit(1) NOT NULL COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `uq_order_sn`(`order_sn` ASC) USING BTREE COMMENT '订单编号唯一索引',
  INDEX `idx_user_id`(`user_id` ASC) USING BTREE COMMENT '用户ID索引',
  INDEX `idx_order_status`(`order_status` ASC) USING BTREE,
  INDEX `idx_pay_status`(`pay_status` ASC) USING BTREE,
  INDEX `idx_delivery_user_id`(`delivery_user_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 32 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城订单主表' ROW_FORMAT = DYNAMIC;


DROP TABLE IF EXISTS `mall_order_child`;
CREATE TABLE `mall_order_child`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '子订单ID',
  `child_order_sn` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '子订单编号',
  `parent_order_id` bigint NOT NULL COMMENT '关联父订单ID',
  `user_id` mediumint UNSIGNED NULL DEFAULT 0 COMMENT '下单用户ID',
  `address_id` int NULL DEFAULT NULL COMMENT '下单用户的收获地址Id',
  `add_time` datetime NULL DEFAULT NULL COMMENT '订单创建时间',
  `merchant_user_id` bigint NULL DEFAULT NULL COMMENT '商家ID',
  `actual_price` decimal(18, 2) NULL DEFAULT 0.00 COMMENT '子实际支付金额（已扣除优惠）',
  `order_price` decimal(18, 2) NULL DEFAULT 0.00 COMMENT '子订单总价',
  `goods_price` decimal(18, 2) NULL DEFAULT 0.00 COMMENT '子订单的商品总价',
  `order_total_coin` decimal(18, 2) NULL DEFAULT 0.00 COMMENT '订单支付的总学豆',
  `shipping_status` tinyint UNSIGNED NULL DEFAULT 0 COMMENT '物流状态（0-未发货，1-已发货，2-已签收）',
  `order_status` tinyint NOT NULL DEFAULT 0 COMMENT '子订单状态',
  `comment_status` tinyint NULL DEFAULT 0 COMMENT '评价状态，1：已评价，0：未评价',
  `delivery_time` datetime NULL DEFAULT NULL COMMENT '发货时间',
  `delivery_user_id` int NULL DEFAULT 0 COMMENT '发货人ID',
  `weight` double NULL DEFAULT NULL COMMENT '重量，单位kg，范围0.01-30 默认0.02）',
  `volume` double NOT NULL DEFAULT 0 COMMENT '商品体积，单位：m^3 平米',
  `logistics_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '物流公司编码',
  `shipping_no` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '物流单号（子订单独立发货，对应一个物流单号）',
  `postscript` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '下单用户备注',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '(后端使用)商家备注',
  `confirm_time` datetime NULL DEFAULT NULL COMMENT '确认收货时间',
  `pay_status` tinyint NOT NULL DEFAULT 0 COMMENT '子订单支付状态：0-待支付，1-已支付，2-退款中，3-退款完成',
  `pay_time` datetime NULL DEFAULT NULL COMMENT '支付时间',
  `pay_channel_code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '支付渠道编码',
  `consignee` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '收货人姓名',
  `country` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '国家',
  `province` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '省份',
  `city` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '城市',
  `district` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '区/县',
  `address` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '详细地址',
  `mobile` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '联系电话',
  `deleted` bit(1) NOT NULL COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `uk_child_order_sn`(`child_order_sn` ASC) USING BTREE,
  INDEX `idx_parent_order_id`(`parent_order_id` ASC) USING BTREE,
  INDEX `idx_merchant_user_id`(`merchant_user_id` ASC) USING BTREE,
  INDEX `idx_user_id`(`user_id` ASC) USING BTREE,
  INDEX `idx_order_status`(`order_status` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 34 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '商城订单子表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `mall_order_log`;
CREATE TABLE `mall_order_log`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '日志主键',
  `user_id` bigint NOT NULL COMMENT '用户编号',
  `operate_type` tinyint NULL DEFAULT NULL COMMENT '操作类型',
  `merchant_id` bigint NULL DEFAULT NULL COMMENT '操作者的userId',
  `before_status` tinyint NULL DEFAULT NULL COMMENT '订单变更前状态',
  `after_status` tinyint NULL DEFAULT NULL COMMENT '订单变更后状态',
  `user_type` tinyint NOT NULL DEFAULT 0 COMMENT '用户类型',
  `order_id` bigint NOT NULL COMMENT '订单号',
  `child_order_id` bigint NULL DEFAULT NULL COMMENT '子订单Id',
  `shipping_status` tinyint NULL DEFAULT NULL COMMENT '物流状态',
  `comment_status` int NULL DEFAULT 0 COMMENT '评价状态',
  `content` varchar(2000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '操作内容',
  `deleted` bit(1) NOT NULL COMMENT '逻辑删除标记(0-正常,1-已删除)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `tenant_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_order_id`(`order_id` ASC) USING BTREE,
  INDEX `idx_user_id`(`user_id` ASC) USING BTREE COMMENT '用户ID'
) ENGINE = InnoDB AUTO_INCREMENT = 9378 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '订单日志' ROW_FORMAT = Dynamic;