Appearance
DSPlatform 数据库设计详解
概述
DSPlatform 采用 MySQL 8.0+ 作为主数据库,使用 ThinkPHP 8.0 的 ORM 进行数据访问。数据库设计遵循规范化原则,支持多平台、多商户的复杂业务场景,包含用户管理、商品管理、订单处理、支付结算等核心业务模块。
数据库文件结构
安装文件目录
php-server/public/install/db/
├── deshang.sql # 核心业务表结构 (273KB, 3144行)
├── points-goods.sql # 积分商品相关表 (7.7KB, 140行)
├── video.sql # 短视频相关表 (9.9KB, 215行)
├── kms.sql # 知识管理系统表 (1.1KB, 23行)
└── initarea.sql # 地区数据初始化 (189KB, 3803行)
表前缀说明
- 所有表使用
#__
作为占位符前缀 - 实际部署时会被替换为配置的表前缀(如
ds_
) - 支持多租户环境下的表前缀隔离
核心表分类
- 用户系统:
ds_user
,ds_user_address
,ds_user_balance_log
等 - 商品系统:
ds_tbl_goods
,ds_tbl_goods_category
,ds_tbl_goods_sku
等 - 订单系统:
ds_tbl_order
,ds_tbl_order_goods
,ds_tbl_order_delivery
等 - 店铺系统:
ds_tbl_store
,ds_tbl_store_category
,ds_tbl_store_coupon
等 - 支付系统:
ds_trade_pay_log
,ds_trade_refund_log
,ds_trade_transfer_log
等 - 管理系统:
ds_admin
,ds_admin_menu
,ds_admin_logs
等 - 积分系统:
ds_points_goods
,ds_points_goods_category
等 - 短视频系统:
ds_blogger
,ds_blogger_follow
,ds_video
等 - 知识管理:
ds_kms_lesson
等
数据库配置
基础配置
php
// config/database.php
return [
'default' => env('DB_DRIVER', 'mysql'),
'connections' => [
'mysql' => [
'type' => env('DB_TYPE', 'mysql'),
'hostname' => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_NAME', 'ds_admin'),
'username' => env('DB_USER', 'root'),
'password' => env('DB_PASS', ''),
'charset' => 'utf8mb4',
'prefix' => env('DB_PREFIX', 'ds_'),
'port' => env('DB_PORT', '3306'),
'debug' => env('DB_DEBUG', false),
'deploy' => 0,
'rw_separate' => false,
'master_num' => 1,
'slave_no' => '',
'fields_strict' => true,
'break_reconnect' => false,
'trigger_sql' => env('DB_TRIGGER_SQL', true),
'auto_timestamp' => true,
'datetime_format' => 'Y-m-d H:i:s',
],
],
];
环境变量配置
env
# 数据库配置
DB_TYPE=mysql
DB_HOST=127.0.0.1
DB_NAME=ds_admin
DB_USER=root
DB_PASS=
DB_PORT=3306
DB_PREFIX=ds_
DB_DEBUG=false
数据库架构
整体架构图
┌─────────────────────────────────────────────────────────────┐
│ 应用层 │
├─────────────┬─────────────┬─────────────┬─────────────────────┤
│ 控制器层 │ 服务层 │ 数据访问层 │ 模型层 │
│ Controller │ Service │ DAO │ Model │
└─────────────┴─────────────┴─────────────┴─────────────────────┘
│
┌─────────────────────────────────────────────────────────────┐
│ ORM 层 │
├─────────────┬─────────────┬─────────────┬─────────────────────┤
│ 关联关系 │ 获取器 │ 修改器 │ 验证器 │
│ Relations │ Accessors │ Mutators │ Validators │
└─────────────┴─────────────┴─────────────┴─────────────────────┘
│
┌─────────────────────────────────────────────────────────────┐
│ 数据库层 │
├─────────────┬─────────────┬─────────────┬─────────────────────┤
│ MySQL │ 索引优化 │ 查询缓存 │ 事务管理 │
│ 8.0+ │ Indexes │ Cache │ Transactions │
└─────────────┴─────────────┴─────────────┴─────────────────────┘
基础模型设计
BaseModel 基类
php
// app/deshang/base/BaseModel.php
class BaseModel extends Model
{
/**
* 数据表主键
* @var string
*/
protected $pk = 'id';
/**
* 自动写入时间戳
* @var bool
*/
protected $autoWriteTimestamp = true;
/**
* 创建时间字段
* @var string
*/
protected $createTime = 'create_at';
/**
* 更新时间字段
* @var string
*/
protected $updateTime = 'update_at';
/**
* 金额格式化
* @param mixed $price
* @return float
*/
protected function formatPrice($price)
{
$formatted = number_format($price, 2, '.', '');
return (float)$formatted;
}
/**
* 时间格式化
* @param mixed $time
* @return string
*/
protected function formatTime($time)
{
$time = (int)$time;
return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
}
}
核心数据表设计
1. 用户相关表
ds_user (用户表)
sql
CREATE TABLE `ds_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '会员自增ID',
`username` varchar(32) NOT NULL COMMENT '会员用户名',
`password` varchar(128) NOT NULL COMMENT '会员密码',
`pay_password` varchar(128) DEFAULT NULL COMMENT '支付密码',
`nickname` varchar(32) DEFAULT NULL COMMENT '会员昵称',
`avatar` varchar(255) DEFAULT NULL COMMENT '头像',
`mobile` varchar(20) DEFAULT NULL COMMENT '手机号',
`email` varchar(100) DEFAULT NULL COMMENT '邮箱',
`gender` tinyint(1) DEFAULT '0' COMMENT '性别 0未知 1男 2女',
`birthday` int(11) DEFAULT '0' COMMENT '生日',
`balance` decimal(10,2) DEFAULT '0.00' COMMENT '余额',
`balance_in` decimal(10,2) DEFAULT '0.00' COMMENT '余额收入总额',
`balance_out` decimal(10,2) DEFAULT '0.00' COMMENT '余额支出总额',
`points` int(11) DEFAULT '0' COMMENT '积分',
`growth` int(11) DEFAULT '0' COMMENT '成长值',
`distributor_balance` decimal(10,2) DEFAULT '0.00' COMMENT '分销余额',
`distributor_balance_in` decimal(10,2) DEFAULT '0.00' COMMENT '分销收入总额',
`distributor_balance_out` decimal(10,2) DEFAULT '0.00' COMMENT '分销支出总额',
`status` tinyint(1) DEFAULT '1' COMMENT '状态 0禁用 1启用',
`is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否删除 0否 1是',
`create_at` int(11) DEFAULT '0' COMMENT '创建时间',
`update_at` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `mobile` (`mobile`),
KEY `status` (`status`),
KEY `create_at` (`create_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
ds_user_address (用户地址表)
sql
CREATE TABLE `ds_user_address` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '地址ID',
`user_id` int(11) NOT NULL DEFAULT '0' COMMENT '会员ID',
`real_name` varchar(32) NOT NULL COMMENT '会员姓名',
`mob_phone` varchar(11) DEFAULT NULL COMMENT '手机',
`province_id` int(11) DEFAULT NULL COMMENT '省级ID',
`city_id` int(11) DEFAULT NULL COMMENT '市级ID',
`district_id` int(11) DEFAULT NULL COMMENT '区级ID',
`address` varchar(255) NOT NULL COMMENT '详细地址',
`latitude` decimal(10,6) DEFAULT NULL COMMENT '纬度',
`longitude` decimal(10,6) DEFAULT NULL COMMENT '经度',
`is_default` tinyint(1) DEFAULT '0' COMMENT '是否默认地址',
`create_at` int(11) DEFAULT '0' COMMENT '创建时间',
`update_at` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `is_default` (`is_default`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户地址表';
2. 商品相关表
ds_tbl_goods (商品表)
sql
CREATE TABLE `ds_tbl_goods` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品自增ID',
`platform` varchar(20) NOT NULL COMMENT '应用类型 system shop mall',
`store_id` int(11) NOT NULL COMMENT '店铺ID',
`goods_name` varchar(128) NOT NULL COMMENT '商品名称',
`goods_advword` varchar(150) DEFAULT NULL COMMENT '商品广告词',
`goods_body` text COMMENT '商品详情描述',
`goods_image` varchar(255) DEFAULT NULL COMMENT '商品主图',
`goods_images` text COMMENT '商品图片',
`goods_minprice` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '商品最低价格',
`goods_maxprice` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '商品最高价格',
`market_price` decimal(10,2) DEFAULT '0.00' COMMENT '市场价格',
`cost_price` decimal(10,2) DEFAULT '0.00' COMMENT '成本价格',
`stock` int(11) DEFAULT '0' COMMENT '库存数量',
`sales` int(11) DEFAULT '0' COMMENT '销量',
`weight` decimal(8,2) DEFAULT '0.00' COMMENT '重量(kg)',
`volume` decimal(8,2) DEFAULT '0.00' COMMENT '体积(m³)',
`status` tinyint(1) DEFAULT '1' COMMENT '状态 0下架 1上架',
`is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否删除',
`create_at` int(11) DEFAULT '0' COMMENT '创建时间',
`update_at` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `store_id` (`store_id`),
KEY `platform` (`platform`),
KEY `status` (`status`),
KEY `create_at` (`create_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品表';
ds_tbl_goods_category (商品分类表)
sql
CREATE TABLE `ds_tbl_goods_category` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '分类ID',
`parent_id` int(11) DEFAULT '0' COMMENT '父分类ID',
`platform` varchar(20) NOT NULL COMMENT '平台标识',
`category_name` varchar(100) NOT NULL COMMENT '分类名称',
`category_image` varchar(255) DEFAULT NULL COMMENT '分类图片',
`sort` int(11) DEFAULT '0' COMMENT '排序',
`status` tinyint(1) DEFAULT '1' COMMENT '状态 0禁用 1启用',
`is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否删除',
`create_at` int(11) DEFAULT '0' COMMENT '创建时间',
`update_at` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`),
KEY `platform` (`platform`),
KEY `sort` (`sort`),
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品分类表';
3. 订单相关表
ds_tbl_order (订单表)
sql
CREATE TABLE `ds_tbl_order` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单自增id',
`platform` varchar(20) NOT NULL COMMENT '应用类型 system mall',
`order_from` varchar(10) DEFAULT NULL COMMENT '订单来源 h5 pc',
`pay_merchant_id` int(11) NOT NULL COMMENT '收款商户ID 0为后台收款',
`pay_channel` varchar(20) DEFAULT NULL COMMENT '支付渠道 alipay wechat',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`store_id` int(11) NOT NULL COMMENT '店铺ID',
`order_no` varchar(50) NOT NULL COMMENT '订单号',
`order_status` tinyint(2) DEFAULT '0' COMMENT '订单状态',
`pay_status` tinyint(1) DEFAULT '0' COMMENT '支付状态 0未支付 1已支付',
`delivery_status` tinyint(1) DEFAULT '0' COMMENT '配送状态',
`order_amount` decimal(10,2) DEFAULT '0.00' COMMENT '订单金额',
`pay_amount` decimal(10,2) DEFAULT '0.00' COMMENT '支付金额',
`delivery_fee` decimal(10,2) DEFAULT '0.00' COMMENT '配送费',
`discount_amount` decimal(10,2) DEFAULT '0.00' COMMENT '优惠金额',
`pay_method` varchar(20) DEFAULT NULL COMMENT '支付方式',
`delivery_method` tinyint(1) DEFAULT '1' COMMENT '配送方式 1自提 2配送',
`remark` varchar(500) DEFAULT NULL COMMENT '订单备注',
`pay_time` int(11) DEFAULT '0' COMMENT '支付时间',
`delivery_time` int(11) DEFAULT '0' COMMENT '配送时间',
`finish_time` int(11) DEFAULT '0' COMMENT '完成时间',
`is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否删除',
`create_at` int(11) DEFAULT '0' COMMENT '创建时间',
`update_at` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `order_no` (`order_no`),
KEY `user_id` (`user_id`),
KEY `store_id` (`store_id`),
KEY `platform` (`platform`),
KEY `order_status` (`order_status`),
KEY `pay_status` (`pay_status`),
KEY `create_at` (`create_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';
ds_tbl_order_goods (订单商品表)
sql
CREATE TABLE `ds_tbl_order_goods` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`order_id` int(11) NOT NULL COMMENT '订单ID',
`goods_id` int(11) NOT NULL COMMENT '商品ID',
`sku_id` int(11) DEFAULT '0' COMMENT 'SKU ID',
`goods_name` varchar(255) NOT NULL COMMENT '商品名称',
`sku_name` varchar(255) DEFAULT NULL COMMENT 'SKU名称',
`goods_image` varchar(255) DEFAULT NULL COMMENT '商品图片',
`goods_price` decimal(10,2) DEFAULT '0.00' COMMENT '商品价格',
`pay_price` decimal(10,2) DEFAULT '0.00' COMMENT '支付价格',
`goods_num` int(11) DEFAULT '1' COMMENT '商品数量',
`promotion_type` tinyint(1) DEFAULT '0' COMMENT '促销类型',
`create_at` int(11) DEFAULT '0' COMMENT '创建时间',
`update_at` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `order_id` (`order_id`),
KEY `goods_id` (`goods_id`),
KEY `sku_id` (`sku_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单商品表';
4. 店铺相关表
ds_tbl_store (店铺表)
sql
CREATE TABLE `ds_tbl_store` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '店铺自增ID',
`platform` varchar(20) NOT NULL COMMENT '应用类型 system shop mall',
`merchant_id` int(11) NOT NULL COMMENT '商户ID',
`store_name` varchar(50) NOT NULL COMMENT '店铺名称',
`store_logo` varchar(255) DEFAULT NULL COMMENT '店铺LOGO',
`store_images` text COMMENT '店铺图片',
`store_desc` text COMMENT '店铺描述',
`category_id` int(11) DEFAULT '0' COMMENT '店铺分类ID',
`contact_name` varchar(50) DEFAULT NULL COMMENT '联系人姓名',
`contact_phone` varchar(20) DEFAULT NULL COMMENT '联系人电话',
`store_address` varchar(255) DEFAULT NULL COMMENT '店铺地址',
`store_latitude` decimal(10,6) DEFAULT NULL COMMENT '店铺纬度',
`store_longitude` decimal(10,6) DEFAULT NULL COMMENT '店铺经度',
`business_hours` varchar(100) DEFAULT NULL COMMENT '营业时间',
`delivery_range` int(11) DEFAULT '0' COMMENT '配送范围(km)',
`delivery_fee` decimal(10,2) DEFAULT '0.00' COMMENT '配送费',
`min_order_amount` decimal(10,2) DEFAULT '0.00' COMMENT '起送金额',
`status` tinyint(1) DEFAULT '1' COMMENT '状态 0禁用 1启用',
`apply_status` tinyint(1) DEFAULT '0' COMMENT '申请状态',
`audit_time` int(11) DEFAULT '0' COMMENT '审核时间',
`is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否删除',
`create_at` int(11) DEFAULT '0' COMMENT '创建时间',
`update_at` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `merchant_id` (`merchant_id`),
KEY `platform` (`platform`),
KEY `category_id` (`category_id`),
KEY `status` (`status`),
KEY `apply_status` (`apply_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='店铺表';
5. 支付相关表
ds_trade_pay_log (支付日志表)
sql
CREATE TABLE `ds_trade_pay_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT '支付ID',
`source_type` varchar(20) NOT NULL COMMENT '来源 比如订单,充值',
`source_id` int(11) NOT NULL COMMENT '来源ID',
`out_trade_no` varchar(32) DEFAULT NULL COMMENT '商户订单号',
`trade_no` varchar(32) DEFAULT NULL COMMENT '第三方平台交易号',
`pay_channel` varchar(20) DEFAULT NULL COMMENT '支付渠道',
`pay_amount` decimal(20,4) NOT NULL COMMENT '支付金额',
`pay_status` tinyint(1) DEFAULT '0' COMMENT '支付状态',
`pay_time` int(11) DEFAULT '0' COMMENT '支付时间',
`notify_time` int(11) DEFAULT '0' COMMENT '通知时间',
`create_at` int(11) DEFAULT '0' COMMENT '创建时间',
`update_at` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `source_type` (`source_type`),
KEY `source_id` (`source_id`),
KEY `out_trade_no` (`out_trade_no`),
KEY `pay_status` (`pay_status`),
KEY `pay_time` (`pay_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支付日志表';
6. 系统相关表
ds_admin (管理员表)
sql
CREATE TABLE `ds_admin` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '管理员自增ID',
`username` varchar(32) NOT NULL COMMENT '管理员名称',
`password` varchar(128) NOT NULL DEFAULT '' COMMENT '管理员密码',
`login_time` int(11) NOT NULL DEFAULT '0' COMMENT '登录时间',
`login_num` int(11) NOT NULL DEFAULT '0' COMMENT '登录次数',
`login_ip` varchar(15) NOT NULL DEFAULT '' COMMENT '登录IP',
`is_super` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否超级管理员',
`role_id` smallint(6) NOT NULL DEFAULT '0' COMMENT '权限组ID',
`create_at` int(11) NOT NULL,
`create_by` varchar(20) DEFAULT NULL,
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 0未删除 1已删除',
`deleted_at` int(11) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`),
UNIQUE KEY `udx_username` (`username`),
KEY `idx_is_deleted` (`is_deleted`),
KEY `idx_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='管理员表';
ds_admin_menu (系统菜单表)
sql
CREATE TABLE `ds_admin_menu` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '管理员权限',
`pid` int(11) NOT NULL DEFAULT '0' COMMENT '上级ID',
`path` varchar(128) NOT NULL COMMENT '路由地址',
`name` varchar(128) NOT NULL COMMENT '路由的唯一标识符',
`component` varchar(128) NOT NULL COMMENT '组件',
`title` varchar(32) DEFAULT NULL COMMENT '显示名称',
`icon` varchar(32) NOT NULL COMMENT '图标',
`api_url` varchar(128) NOT NULL COMMENT '权限API地址',
`type` varchar(10) NOT NULL COMMENT '类型 directory 目录 menu 菜单 button 按钮',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序',
`is_show` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否显示 1显示',
`is_enabled` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否可用 1可用',
`create_at` int(11) NOT NULL COMMENT '创建时间',
`update_at` int(11) DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_pid` (`pid`),
KEY `idx_is_show` (`is_show`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统菜单';
ds_sys_area (地区表)
sql
CREATE TABLE `ds_sys_area` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '地区ID',
`name` varchar(50) NOT NULL COMMENT '地区名称',
`pid` int(11) NOT NULL DEFAULT '0' COMMENT '父级ID',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序',
`deep` tinyint(1) NOT NULL DEFAULT '1' COMMENT '层级深度',
`latitude` decimal(10,6) DEFAULT NULL COMMENT '纬度',
`longitude` decimal(10,6) DEFAULT NULL COMMENT '经度',
`is_show` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否显示',
PRIMARY KEY (`id`),
KEY `idx_pid` (`pid`),
KEY `idx_sort` (`sort`),
KEY `idx_deep` (`deep`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='地区表';
7. 积分商品相关表
ds_points_goods (积分商品表)
sql
CREATE TABLE `ds_points_goods` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '积分商品自增ID',
`goods_name` varchar(128) NOT NULL COMMENT '积分商品名称',
`goods_advword` varchar(150) DEFAULT NULL COMMENT '商品广告词',
`goods_body` text COMMENT '商品详情描述',
`goods_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '商品状态 0:下架 1:上架',
`goods_image` varchar(255) NOT NULL DEFAULT '' COMMENT '商品主图',
`category_id` int(11) NOT NULL DEFAULT '0' COMMENT '积分商品分类ID',
`slide_image` varchar(2000) NOT NULL DEFAULT '' COMMENT '商品轮播图',
`points_price` int(11) NOT NULL DEFAULT '0' COMMENT '积分价格',
`market_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '市场参考价格',
`stock_num` int(11) NOT NULL DEFAULT '0' COMMENT '库存数量',
`limit_per_user` int(11) NOT NULL DEFAULT '0' COMMENT '每人限购数量 0为不限制',
`limit_per_day` int(11) NOT NULL DEFAULT '0' COMMENT '每日限购数量 0为不限制',
`click_num` int(11) NOT NULL DEFAULT '0' COMMENT '点击数量',
`exchange_num` int(11) NOT NULL DEFAULT '0' COMMENT '兑换数量',
`evaluate_num` int(11) NOT NULL DEFAULT '0' COMMENT '评价数量',
`avg_evaluate_score` decimal(3,2) NOT NULL DEFAULT '5.00' COMMENT '平均评价分数',
`goods_sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序权重',
`is_hot` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否热门 0:否 1:是',
`is_recommend` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否推荐 0:否 1:是',
`is_new` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否新品 0:否 1:是',
`create_at` int(11) NOT NULL COMMENT '创建时间',
`update_at` int(11) NOT NULL DEFAULT '0' COMMENT '更新时间',
`is_deleted` tinyint(4) NOT NULL DEFAULT '0' COMMENT '删除状态 0:未删除 1:已删除',
`deleted_at` int(11) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`),
KEY `idx_goods_name` (`goods_name`),
KEY `idx_category_id` (`category_id`),
KEY `idx_goods_status` (`goods_status`),
KEY `idx_points_price` (`points_price`),
KEY `idx_stock_num` (`stock_num`),
KEY `idx_is_deleted` (`is_deleted`),
KEY `idx_create_at` (`create_at`),
KEY `idx_goods_sort` (`goods_sort`),
KEY `idx_exchange_num` (`exchange_num`),
KEY `idx_evaluate_num` (`evaluate_num`),
KEY `idx_avg_evaluate_score` (`avg_evaluate_score`),
KEY `idx_is_hot` (`is_hot`),
KEY `idx_is_recommend` (`is_recommend`),
KEY `idx_is_new` (`is_new`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='积分商品表';
8. 短视频相关表
ds_blogger (短视频博主表)
sql
CREATE TABLE `ds_blogger` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`blogger_name` varchar(32) NOT NULL COMMENT '博主昵称',
`avatar` varchar(255) DEFAULT NULL COMMENT '头像',
`description` text COMMENT '描述',
`follower_count` int(11) DEFAULT '0' COMMENT '粉丝数 关注数在user表 following_blogger_count',
`video_count` int(11) DEFAULT '0' COMMENT '视频数量',
`drama_count` int(11) DEFAULT '0' COMMENT '短剧数量',
`live_count` int(11) DEFAULT '0' COMMENT '直播次数',
`total_likes` int(11) DEFAULT '0' COMMENT '总获赞数',
`total_views` int(11) DEFAULT '0' COMMENT '总播放量',
`total_collect` int(11) DEFAULT '0' COMMENT '总收藏',
`verification_status` tinyint(4) DEFAULT '0' COMMENT '认证状态 0待认证 1认证通过 2认证失败',
`verification_type` tinyint(4) DEFAULT '0' COMMENT '认证类型 1个人认证 2企业认证',
`verification_desc` varchar(255) DEFAULT NULL COMMENT '认证说明',
`is_live_enabled` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否开通直播权限',
`is_drama_enabled` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否开通短剧权限',
`is_enabled` tinyint(4) DEFAULT '1' COMMENT '是否可用',
`create_at` int(11) NOT NULL,
`update_at` int(11) DEFAULT NULL,
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 0未删除 1已删除',
`deleted_at` int(11) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`),
UNIQUE KEY `udx_user_id` (`user_id`),
KEY `idx_blogger_name` (`blogger_name`),
KEY `idx_verification_status` (`verification_status`),
KEY `idx_is_enabled` (`is_enabled`),
KEY `idx_follower_count` (`follower_count`),
KEY `idx_create_at` (`create_at`),
KEY `idx_is_deleted` (`is_deleted`),
KEY `idx_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='短视频博主';
9. 知识管理相关表
ds_kms_lesson (KMS课时表)
sql
CREATE TABLE `ds_kms_lesson` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '课时ID',
`goods_id` int(11) NOT NULL COMMENT '商品ID(课程ID)',
`store_id` int(11) NOT NULL COMMENT '店铺ID',
`lesson_name` varchar(255) NOT NULL COMMENT '课时名称',
`lesson_desc` text COMMENT '课时描述',
`lesson_type` tinyint(1) DEFAULT 1 COMMENT '课时类型:1-视频 2-图文 3-音频',
`video_url` varchar(500) DEFAULT NULL COMMENT '视频URL',
`audio_url` varchar(500) DEFAULT NULL COMMENT '音频URL',
`content` text COMMENT '图文内容',
`sort` int(11) DEFAULT 0 COMMENT '排序',
`is_free` tinyint(1) DEFAULT 0 COMMENT '是否免费:0-收费 1-免费',
`create_at` int(11) DEFAULT NULL COMMENT '创建时间',
`update_at` int(11) DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_goods_id` (`goods_id`),
KEY `idx_store_id` (`store_id`),
KEY `idx_sort` (`sort`),
KEY `idx_is_free` (`is_free`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='KMS课时表';
模型关联关系
用户模型关联
php
// app/common/model/user/UserModel.php
class UserModel extends BaseModel
{
protected $name = 'user';
// 用户地址关联
public function addresses()
{
return $this->hasMany(UserAddressModel::class, 'user_id', 'id');
}
// 用户订单关联
public function orders()
{
return $this->hasMany(TblOrderModel::class, 'user_id', 'id');
}
// 用户收藏商品关联
public function favoriteGoods()
{
return $this->hasMany(TblGoodsFavoritesModel::class, 'user_id', 'id');
}
}
商品模型关联
php
// app/common/model/goods/TblGoodsModel.php
class TblGoodsModel extends BaseModel
{
protected $name = 'tbl_goods';
// 关联店铺
public function store()
{
return $this->hasOne(TblStoreModel::class, 'id', 'store_id');
}
// 关联分类
public function category()
{
return $this->hasOne(TblGoodsCategoryModel::class, 'id', 'category_id');
}
// 关联SKU
public function skuList()
{
return $this->hasMany(TblGoodsSkuModel::class, 'goods_id', 'id');
}
// 关联平台
public function platform()
{
return $this->hasOne(SysPlatformModel::class, 'platform', 'platform');
}
}
订单模型关联
php
// app/common/model/order/TblOrderModel.php
class TblOrderModel extends BaseModel
{
protected $name = 'tbl_order';
// 关联用户
public function user()
{
return $this->hasOne(UserModel::class, 'id', 'user_id');
}
// 关联店铺
public function store()
{
return $this->hasOne(TblStoreModel::class, 'id', 'store_id');
}
// 关联订单商品
public function orderGoodsList()
{
return $this->hasMany(TblOrderGoodsModel::class, 'order_id', 'id');
}
// 关联订单地址
public function orderAddress()
{
return $this->hasOne(TblOrderAddressModel::class, 'order_id', 'id');
}
// 关联订单配送
public function orderDelivery()
{
return $this->hasOne(TblOrderDeliveryModel::class, 'order_id', 'id');
}
// 关联支付日志
public function payLogs()
{
return $this->hasMany(TradePayLogModel::class, 'order_id', 'id');
}
}
数据访问层设计
BaseDao 基类
php
// app/common/dao/BaseDao.php
class BaseDao
{
protected $model;
public function __construct() { }
/**
* 获取分页参数
* @return array
*/
public function getPageParams()
{
$page_current = (int) input('param.page_current');
$page_size = (int) input('param.page_size');
return [
'page_current' => $page_current > 0 ? $page_current : 1,
'page_size' => $page_size > 0 ? $page_size : 10,
];
}
/**
* 获取分页数据
* @param mixed $model
* @return array
*/
public function getPaginate($model)
{
$page_params = $this->getPageParams();
return $model->paginate([
'list_rows' => $page_params['page_size'],
'page' => $page_params['page_current'],
])->toArray();
}
/**
* 获取模型实例
* @return mixed
*/
public function getModel()
{
return $this->model;
}
/**
* 设置模型实例
* @param mixed $model
*/
public function setModel($model)
{
$this->model = $model;
}
}
订单DAO示例
php
// app/common/dao/order/TblOrderDao.php
class TblOrderDao extends BaseDao
{
public function __construct()
{
parent::__construct();
$this->model = new TblOrderModel();
}
/**
* 创建订单
* @param array $data
* @return int
*/
public function createOrder(array $data): int
{
$result = $this->model->create($data);
return $result->id;
}
/**
* 获取订单列表
* @param array $condition
* @param string $field
* @param string $order
* @return array
*/
public function getOrderPages(array $condition, string $field = '*', string $order = 'id desc'): array
{
$result = $this->model->where($condition)
->field($field)
->order($order);
return $this->getPaginate($result);
}
/**
* 获取带关联的订单信息
* @param array $condition
* @param string $field
* @return array
*/
public function getWithRelOrderInfo(array $condition, string $field = '*'): array
{
return $this->model->where($condition)
->with([
'store' => function ($query) {
$query->field('id,store_name,platform,store_latitude,store_longitude');
},
'user' => function ($query) {
$query->field('id,username,nickname,avatar');
},
'orderGoodsList' => function ($query) {
$query->append(['promotion_type_desc']);
},
'orderAddress' => function ($query) {
$query->field('*');
},
])
->append(['order_status_desc', 'delivery_method_desc'])
->field($field)
->findOrEmpty()
->toArray();
}
}
数据迁移
1. 迁移文件结构
database/
├── migrations/
│ ├── 2024_01_01_000001_create_user_table.php
│ ├── 2024_01_01_000002_create_goods_table.php
│ └── 2024_01_01_000003_create_order_table.php
├── seeds/
│ ├── UserSeeder.php
│ ├── GoodsSeeder.php
│ └── OrderSeeder.php
└── factories/
├── UserFactory.php
├── GoodsFactory.php
└── OrderFactory.php
2. 迁移示例
php
// database/migrations/2024_01_01_000001_create_user_table.php
class CreateUserTable extends Migration
{
public function up()
{
Schema::create('ds_user', function (Blueprint $table) {
$table->id();
$table->string('username', 50)->unique();
$table->string('nickname', 50)->nullable();
$table->string('mobile', 20)->nullable();
$table->string('email', 100)->nullable();
$table->string('avatar')->nullable();
$table->tinyInteger('gender')->default(0);
$table->integer('birthday')->default(0);
$table->decimal('balance', 10, 2)->default(0.00);
$table->decimal('balance_in', 10, 2)->default(0.00);
$table->decimal('balance_out', 10, 2)->default(0.00);
$table->integer('points')->default(0);
$table->tinyInteger('status')->default(1);
$table->tinyInteger('is_deleted')->default(0);
$table->integer('create_at')->default(0);
$table->integer('update_at')->default(0);
$table->index('mobile');
$table->index('status');
$table->index('create_at');
});
}
public function down()
{
Schema::dropIfExists('ds_user');
}
}
数据备份与恢复
1. 备份策略
bash
# 全量备份
mysqldump -u root -p ds_admin > backup_$(date +%Y%m%d_%H%M%S).sql
# 增量备份(基于binlog)
mysqlbinlog --start-datetime="2024-01-01 00:00:00" \
--stop-datetime="2024-01-02 00:00:00" \
mysql-bin.000001 > incremental_backup.sql
2. 恢复策略
bash
# 全量恢复
mysql -u root -p ds_admin < backup_20240101_120000.sql
# 增量恢复
mysql -u root -p ds_admin < incremental_backup.sql
监控与维护
1. 性能监控
sql
-- 查看慢查询
SHOW VARIABLES LIKE 'slow_query_log';
SHOW VARIABLES LIKE 'long_query_time';
-- 查看表状态
SHOW TABLE STATUS LIKE 'ds_tbl_order';
-- 查看索引使用情况
SHOW INDEX FROM ds_tbl_order;
2. 数据清理
sql
-- 清理过期日志数据
DELETE FROM ds_sys_access_logs WHERE create_at < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY));
-- 清理软删除数据
DELETE FROM ds_user WHERE is_deleted = 1 AND update_at < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 90 DAY));
设计原则
1. 数据库设计原则
- 规范化: 遵循第三范式,减少数据冗余
- 字段设计: 选择合适的数据类型和长度
- 命名规范: 使用统一的命名规范
- 软删除: 使用
is_deleted
和deleted_at
字段实现软删除
2. 开发规范
- 模型定义: 所有模型继承BaseModel
- 关联关系: 合理定义模型关联关系
- 数据验证: 使用验证器进行数据验证
- 事务处理: 重要操作使用数据库事务
数据库初始化
安装流程
执行核心表结构
bashmysql -u root -p ds_admin < php-server/public/install/db/deshang.sql
执行积分商品表
bashmysql -u root -p ds_admin < php-server/public/install/db/points-goods.sql
执行短视频表
bashmysql -u root -p ds_admin < php-server/public/install/db/video.sql
执行知识管理表
bashmysql -u root -p ds_admin < php-server/public/install/db/kms.sql
初始化地区数据
bashmysql -u root -p ds_admin < php-server/public/install/db/initarea.sql
表前缀替换
安装过程中,#__
占位符会被替换为实际配置的表前缀:
php
// 配置示例
DB_PREFIX=ds_
// 实际表名
ds_user, ds_tbl_goods, ds_tbl_order 等
数据库维护
定期维护任务
数据清理
- 清理过期的日志数据
- 清理软删除的数据
- 清理临时文件
索引优化
- 分析慢查询日志
- 优化索引结构
- 重建碎片化索引
备份策略
- 每日增量备份
- 每周全量备份
- 异地备份存储
性能监控
sql
-- 查看表大小
SELECT
table_name,
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS 'Size (MB)'
FROM information_schema.tables
WHERE table_schema = 'ds_admin'
ORDER BY (data_length + index_length) DESC;
-- 查看慢查询
SHOW VARIABLES LIKE 'slow_query_log';
SHOW VARIABLES LIKE 'long_query_time';
-- 查看连接数
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Max_used_connections';
相关链接
最后更新:2024-01-20
维护者:DSPlatform技术团队