-- MySQL 8.0.35

CREATE
DATABASE IF NOT EXISTS `building_cdwms` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE
`building_cdwms`;

CREATE TABLE IF NOT EXISTS `wh_warehouse`
(
  `id`
  bigint
  NOT
  NULL
  COMMENT
  '主键',
  `warehouse_name`
  varchar
(
  128
) NOT NULL COMMENT '仓库名称',
  `warehouse_type_code` varchar
(
  32
) NOT NULL COMMENT '仓库类型编码(来源数据字典，例如：RAW=原料仓、FINISHED=成品仓、HAZ_TMP=危废暂存仓等)',
  `capacity` decimal
(
  16,
  2
) NOT NULL DEFAULT 0 COMMENT '最大容量',
  `capacity_unit_code` varchar
(
  16
) NOT NULL DEFAULT 'TON' COMMENT '容量单位编码(来源字典，例如：TON=吨、CBM=立方米)',
  `manager_id` bigint DEFAULT NULL COMMENT '责任人 Id; sys_user.id',
  `creator_id` bigint NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id` bigint NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `modify_time` datetime NOT NULL COMMENT '修改时间',
  `deleted` tinyint
(
  1
) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY
(
  `id`
),
  KEY `idx_wh_warehouse_type_code`
(
  `warehouse_type_code`
)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_general_ci COMMENT='仓库信息';

-- 仓库存量（实时库存）
-- 出入库单（入库单+出库单，且每笔订单只包含一种物料）
CREATE TABLE IF NOT EXISTS `wh_stock_order`
(
  `id`
  bigint
  NOT
  NULL
  COMMENT
  '主键',
  `batch_no`
  varchar
(
  64
) DEFAULT NULL COMMENT '出入库编号',
  `order_id` bigint NOT NULL COMMENT '上游业务订单Id（如销售订单、采购订单等）',
  `order_type` varchar
(
  16
) NOT NULL COMMENT '单据类型(字典)：IN=入库单、OUT=出库单',
  `warehouse_id` bigint NOT NULL COMMENT '仓库 Id; wh_warehouse.id',
  `order_no` varchar
(
  32
) DEFAULT NULL COMMENT '关联业务单号（可选，用于展示用单号）',
  `customer_id` bigint DEFAULT NULL COMMENT '客户 Id; tb_customer.id，仅出库单使用',
  `operate_time` datetime NOT NULL COMMENT '出入库时间（入库=到货/入库时间，出库=发货时间）',
  `locator` varchar
(
  64
) DEFAULT NULL COMMENT '仓内存放位置（货架/库位等，入库使用为主）',
  `material_id` bigint NOT NULL COMMENT '物料 Id; tb_material.id',
  `quantity` decimal
(
  16,
  3
) NOT NULL DEFAULT 0 COMMENT '数量',
  `remark` varchar
(
  255
) DEFAULT NULL COMMENT '备注信息',
  `responsible_id` bigint DEFAULT NULL COMMENT '经办人/责任人 Id; sys_user.id',
  `creator_id` bigint NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id` bigint NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `modify_time` datetime NOT NULL COMMENT '修改时间',
  `deleted` tinyint
(
  1
) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY
(
  `id`
),
  UNIQUE KEY `uk_wh_stock_order_order_id_type`
(
  `order_id`,
  `order_type`
),
  KEY `idx_wh_stock_order_wh`
(
  `warehouse_id`
),
  KEY `idx_wh_stock_order_type`
(
  `order_type`
),
  KEY `idx_wh_stock_order_material`
(
  `material_id`
),
  KEY `idx_wh_stock_order_customer`
(
  `customer_id`
),
  CONSTRAINT `fk_wh_stock_order_wh` FOREIGN KEY
(
  `warehouse_id`
) REFERENCES `wh_warehouse`
(
  `id`
),
  CONSTRAINT `fk_wh_stock_order_material` FOREIGN KEY
(
  `material_id`
) REFERENCES `tb_material`
(
  `id`
),
  CONSTRAINT `fk_wh_stock_order_customer` FOREIGN KEY
(
  `customer_id`
) REFERENCES `tb_customer`
(
  `id`
)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_general_ci COMMENT='出入库单（单物料）';

-- 库存预警记录
CREATE TABLE IF NOT EXISTS `wh_stock_alert`
(
  `id`
  bigint
  NOT
  NULL
  COMMENT
  '主键',
  `warehouse_id`
  bigint
  NOT
  NULL
  COMMENT
  '仓库 Id',
  `material_id`
  bigint
  NOT
  NULL
  COMMENT
  '物料 Id',
  `alert_type`
  varchar
(
  16
) NOT NULL COMMENT '预警类型(字典)：LOW=低库存预警、HIGH=高库存预警',
  `current_quantity` decimal
(
  16,
  3
) NOT NULL COMMENT '触发时库存',
  `status` varchar
(
  16
) NOT NULL DEFAULT 'UNRESOLVED' COMMENT '处理状态(字典)：UNRESOLVED=未处理、RESOLVED=已处理',
  `suggestion` varchar
(
  255
) DEFAULT NULL COMMENT '处理建议',
  `alert_time` datetime NOT NULL COMMENT '预警时间',
  `creator_id` bigint NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id` bigint NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `modify_time` datetime NOT NULL COMMENT '修改时间',
  `deleted` tinyint
(
  1
) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY
(
  `id`
),
  KEY `idx_wh_stock_alert_wh_material`
(
  `warehouse_id`,
  `material_id`
),
  CONSTRAINT `fk_wh_stock_alert_wh` FOREIGN KEY
(
  `warehouse_id`
) REFERENCES `wh_warehouse`
(
  `id`
),
  CONSTRAINT `fk_wh_stock_alert_material` FOREIGN KEY
(
  `material_id`
) REFERENCES `tb_material`
(
  `id`
)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_general_ci COMMENT='库存预警记录';

-- 盘点任务
CREATE TABLE IF NOT EXISTS `wh_stocktaking_task`
(
  `id`
  bigint
  NOT
  NULL
  COMMENT
  '主键',
  `task_no`
  varchar
(
  32
) NOT NULL COMMENT '盘点编号',
  `warehouse_id` bigint NOT NULL COMMENT '盘点仓库 Id',
  `checker_id` bigint DEFAULT NULL COMMENT '盘点人 Id; sys_user.id',
  `reviewer_id` bigint DEFAULT NULL COMMENT '复核人 Id; sys_user.id',
  `task_time` datetime NOT NULL COMMENT '盘点时间',
  `item_count` int NOT NULL DEFAULT 0 COMMENT '盘点项数',
  `abnormal_count` int NOT NULL DEFAULT 0 COMMENT '差异项数',
  `remark` varchar
(
  255
) DEFAULT NULL COMMENT '备注',
  `creator_id` bigint NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id` bigint NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `modify_time` datetime NOT NULL COMMENT '修改时间',
  `deleted` tinyint
(
  1
) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY
(
  `id`
),
  UNIQUE KEY `uk_wh_stocktaking_task_no`
(
  `task_no`
),
  KEY `idx_wh_stocktaking_task_wh`
(
  `warehouse_id`
),
  CONSTRAINT `fk_wh_stocktaking_task_wh` FOREIGN KEY
(
  `warehouse_id`
) REFERENCES `wh_warehouse`
(
  `id`
)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_general_ci COMMENT='库存盘点任务';

-- 盘点明细
CREATE TABLE IF NOT EXISTS `wh_stocktaking_item`
(
  `id`
  bigint
  NOT
  NULL
  COMMENT
  '主键',
  `task_id`
  bigint
  NOT
  NULL
  COMMENT
  '盘点任务 Id; wh_stocktaking_task.id',
  `material_id`
  bigint
  NOT
  NULL
  COMMENT
  '物料 Id',
  `system_quantity`
  decimal
(
  16,
  3
) NOT NULL DEFAULT 0 COMMENT '系统库存',
  `actual_quantity` decimal
(
  16,
  3
) NOT NULL DEFAULT 0 COMMENT '实际库存',
  `difference_quantity` decimal
(
  16,
  3
) NOT NULL DEFAULT 0 COMMENT '差异数量',
  `status` varchar
(
  16
) NOT NULL DEFAULT 'NORMAL' COMMENT '盘点结果状态(字典)：NORMAL=无差异、ABNORMAL=有差异',
  `suggestion` varchar
(
  255
) DEFAULT NULL COMMENT '处理建议',
  `creator_id` bigint NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id` bigint NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `modify_time` datetime NOT NULL COMMENT '修改时间',
  `deleted` tinyint
(
  1
) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY
(
  `id`
),
  KEY `idx_wh_stocktaking_item_task`
(
  `task_id`
),
  KEY `idx_wh_stocktaking_item_material`
(
  `material_id`
),
  CONSTRAINT `fk_wh_stocktaking_item_task` FOREIGN KEY
(
  `task_id`
) REFERENCES `wh_stocktaking_task`
(
  `id`
),
  CONSTRAINT `fk_wh_stocktaking_item_material` FOREIGN KEY
(
  `material_id`
) REFERENCES `tb_material`
(
  `id`
)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_general_ci COMMENT='库存盘点明细';


CREATE TABLE `fin_money_account`
(
  `id`          bigint         NOT NULL COMMENT 'Id',
  `account_no`  varchar(64)    NOT NULL COMMENT '账户编号',
  `type`        varchar(32)    NOT NULL COMMENT '账户类型编码(来源数据字典，例如：COMPANY=企业账户、PERSONAL=个人账户)',
  `user_id`     bigint                  DEFAULT NULL COMMENT '用户Id; 个人账户使用',
  `company_id`  bigint                  DEFAULT NULL COMMENT '企业Id; 企业账户使用',
  `money`       decimal(14, 2) NOT NULL DEFAULT '0.00' COMMENT '余额',
  `revenue`     decimal(14, 2) NOT NULL DEFAULT '0.00' COMMENT '营收',
  `creator_id`  bigint         NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id` bigint         NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time` datetime       NOT NULL COMMENT '创建时间',
  `modify_time` datetime       NOT NULL COMMENT '修改时间',
  `deleted`     tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `uk_fin_money_account_no` (`account_no`),
  KEY           `idx_fin_money_account_type` (`type`),
  KEY           `idx_fin_money_account_user` (`user_id`),
  KEY           `idx_fin_money_account_company` (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='资金账户表';

CREATE TABLE `fin_money_detail`
(
  `id`               bigint                                                       NOT NULL COMMENT 'Id',
  `order_id`         bigint                                                        DEFAULT NULL COMMENT '订单 Id',
  `money_account_id` bigint                                                       NOT NULL COMMENT '资金账户 Id',
  `balance_before`   decimal(14, 2)                                               NOT NULL COMMENT '变动前余额',
  `delta`            decimal(14, 2)                                               NOT NULL COMMENT '变动金额; 有正负',
  `balance_after`    decimal(14, 2)                                               NOT NULL COMMENT '变动后余额',
  `type`             varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '变动类型(字典 money_change_category)：recharge=充值、platform_pay=平台打款、order_adjust=订单调账、order_deduct=订单扣款、order_refund=订单退款',
  `memo`             varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
  `creator_id`       bigint                                                       NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id`      bigint                                                       NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time`      datetime                                                     NOT NULL COMMENT '创建时间',
  `modify_time`      datetime                                                     NOT NULL COMMENT '修改时间',
  `deleted`          tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='资金明细';

CREATE TABLE `fin_money_bill`
(
  `id`             bigint         NOT NULL COMMENT 'Id',
  `company_id`     bigint         NOT NULL COMMENT '企业 Id',
  `user_id`        bigint         NOT NULL COMMENT '个人 Id',
  `type`           varchar(32)    NOT NULL COMMENT '账户类型编码(company=企业账户、personal=个人账户)',
  `start_time`     date           NOT NULL COMMENT '账单开始时间',
  `end_time`       date           NOT NULL COMMENT '账单结束时间',
  `bill_period`    date                                                          DEFAULT NULL COMMENT '账期',
  `goods_id`       bigint         NOT NULL COMMENT '产品id',
  `goods_name`     varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '产品名称',
  `order_count`    int            NOT NULL COMMENT '订单数',
  `total_weight`   int                                                           DEFAULT '0' COMMENT '总质量; 单位：千克',
  `car_count`      int                                                           DEFAULT '0' COMMENT '总车数',
  `discount_money` decimal(14, 2) NOT NULL                                       DEFAULT '0.00' COMMENT '优惠金额',
  `total_money`    decimal(14, 2) NOT NULL COMMENT '账单金额',
  `creator_id`     bigint         NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id`    bigint         NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time`    datetime       NOT NULL COMMENT '创建时间',
  `modify_time`    datetime       NOT NULL COMMENT '修改时间',
  `deleted`        tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='对账单';

CREATE TABLE `fin_expense`
(
  `id`             bigint   NOT NULL COMMENT 'Id',
  `name`           varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '付费项名称',
  `price`          decimal(14, 2)                                                DEFAULT '0.00' COMMENT '单价; 单位：元',
  `money_strategy` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  DEFAULT NULL COMMENT '计费策略; 字典代码：money_strategy',
  `canuse`         tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否可用; 0-->否、1-->是',
  `scope`          varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  DEFAULT NULL COMMENT '适用范围; all -->所有客户 customer_type-->指定客户类型 customer -->指定客户',
  `company_ids`    varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '公司ids',
  `goods_scope`    varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  DEFAULT NULL COMMENT '产品范围;字典代码：goods_scope all -->所有产品  goods -->指定产品',
  `goods_ids`      varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '产品ids',
  `station_scope`  varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  DEFAULT NULL COMMENT '站点范围;字典代码：station_scope all -->所有站点  stations -->指定站点',
  `station_ids`    varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '站点ids',
  `billing_type`   varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  DEFAULT NULL COMMENT '计费类型 字典代码：billing_type',
  `basic_weight`   decimal(14, 2)                                                DEFAULT '0.00' COMMENT '基础磅重',
  `basic_price`    decimal(14, 2)                                                DEFAULT '0.00' COMMENT '基础单价; 单位：元',
  `every_weight`   decimal(14, 2)                                                DEFAULT '0.00' COMMENT '每档磅重',
  `every_price`    decimal(14, 2)                                                DEFAULT '0.00' COMMENT '每档单价; 单位：元',
  `creator_id`     bigint   NOT NULL COMMENT '创建人 Id; sys_user.id',
  `create_time`    datetime NOT NULL COMMENT '创建时间',
  `modifier_id`    bigint   NOT NULL COMMENT '修改人 Id; sys_user.id',
  `modify_time`    datetime NOT NULL COMMENT '修改时间',
  `deleted`        tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='费用配置';

CREATE TABLE `fin_discount`
(
  `id`          bigint   NOT NULL COMMENT 'Id',
  `company_id`  bigint                                                        DEFAULT NULL COMMENT '公司id',
  `fee_type`    varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  DEFAULT NULL COMMENT '费用类型：goods 产品  service 服务费',
  `expense_id`  bigint                                                        DEFAULT NULL COMMENT '服务费id',
  `goods_ids`   varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '产品ids',
  `type`        varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  DEFAULT NULL COMMENT '优惠类型：discount 优惠 raise加价',
  `money`       decimal(14, 2)                                                DEFAULT '0.00' COMMENT '变化金额',
  `start_time`  datetime NOT NULL COMMENT '开始时间',
  `end_time`    datetime NOT NULL COMMENT '结束时间',
  `remark`      text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '备注',
  `creator_id`  bigint   NOT NULL COMMENT '创建人 Id; sys_user.id',
  `modifier_id` bigint   NOT NULL COMMENT '修改人 Id; sys_user.id',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `modify_time` datetime NOT NULL COMMENT '修改时间',
  `deleted`     tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除; 0-->未删除、1-->已删除',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='优惠管理';


