-- 创建物资表
CREATE TABLE IF NOT EXISTS materials (
  id VARCHAR(36) NOT NULL PRIMARY KEY COMMENT '物资ID',
  code VARCHAR(20) NOT NULL UNIQUE COMMENT '物资编号',
  name VARCHAR(100) NOT NULL COMMENT '物资名称',
  category VARCHAR(20) NOT NULL COMMENT '物资类别',
  unit VARCHAR(20) NOT NULL COMMENT '单位',
  stock_quantity INT NOT NULL DEFAULT 0 COMMENT '库存数量',
  warning_quantity INT NOT NULL DEFAULT 0 COMMENT '预警数量',
  status VARCHAR(20) NOT NULL DEFAULT 'normal' COMMENT '状态',
  supplier VARCHAR(100) NULL COMMENT '供应商',
  remark TEXT NULL COMMENT '备注',
  update_time DATETIME NOT NULL COMMENT '更新时间',
  created_at DATETIME NOT NULL COMMENT '创建时间',
  updated_at DATETIME NOT NULL COMMENT '更新时间',
  deleted_at DATETIME NULL COMMENT '删除时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='物资表';

-- 创建仓库交易记录表
CREATE TABLE IF NOT EXISTS warehouse_transactions (
  id VARCHAR(36) NOT NULL PRIMARY KEY COMMENT '交易记录ID',
  material_id VARCHAR(36) NOT NULL COMMENT '物资ID',
  type VARCHAR(10) NOT NULL COMMENT '操作类型：in(入库)，out(出库)',
  quantity INT NOT NULL COMMENT '操作数量',
  operator VARCHAR(50) NOT NULL COMMENT '操作人',
  remark TEXT NULL COMMENT '备注',
  created_at DATETIME NOT NULL COMMENT '创建时间',
  updated_at DATETIME NOT NULL COMMENT '更新时间',
  deleted_at DATETIME NULL COMMENT '删除时间',
  INDEX idx_material_id (material_id),
  FOREIGN KEY (material_id) REFERENCES materials(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='仓库交易记录表';

-- 插入测试数据
INSERT INTO materials (id, code, name, category, unit, stock_quantity, warning_quantity, status, supplier, remark, update_time, created_at, updated_at) VALUES
('1', 'FEED001', '牛用精饲料', 'feed', '袋', 250, 50, 'normal', '绿源饲料公司', '高蛋白配方', '2024-04-10 09:30:00', '2024-04-01 09:30:00', '2024-04-10 09:30:00'),
('2', 'FEED002', '粗饲料', 'feed', '吨', 12, 5, 'low', '草原饲料厂', '优质牧草', '2024-04-09 14:20:00', '2024-04-02 14:20:00', '2024-04-09 14:20:00'),
('3', 'MED001', '牛瘟疫苗', 'medicine', '盒', 0, 10, 'out', '动保生物公司', '每盒10支', '2024-04-08 10:15:00', '2024-04-03 10:15:00', '2024-04-08 10:15:00'),
('4', 'MED002', '驱虫药', 'medicine', '瓶', 85, 20, 'normal', '兽药批发中心', '广谱驱虫', '2024-04-10 11:45:00', '2024-04-04 11:45:00', '2024-04-10 11:45:00'),
('5', 'EQU001', '牛用耳标', 'equipment', '个', 3500, 500, 'normal', '畜牧设备公司', 'RFID电子耳标', '2024-04-07 16:00:00', '2024-04-05 16:00:00', '2024-04-07 16:00:00'),
('6', 'EQU002', '体温计', 'equipment', '支', 15, 5, 'normal', '医疗器械公司', '兽用电子体温计', '2024-04-06 13:30:00', '2024-04-06 13:30:00', '2024-04-06 13:30:00'),
('7', 'FEED003', '矿物质添加剂', 'feed', 'kg', 35, 10, 'normal', '营养添加剂厂', '补充微量元素', '2024-04-05 10:15:00', '2024-04-07 10:15:00', '2024-04-05 10:15:00'),
('8', 'MED003', '抗生素', 'medicine', '盒', 5, 10, 'low', '兽药批发中心', '需处方使用', '2024-04-04 15:45:00', '2024-04-08 15:45:00', '2024-04-04 15:45:00'),
('9', 'EQU003', '消毒设备', 'equipment', '台', 3, 1, 'normal', '畜牧设备公司', '自动喷雾消毒机', '2024-04-03 09:30:00', '2024-04-09 09:30:00', '2024-04-03 09:30:00'),
('10', 'OTH001', '防护服', 'other', '套', 120, 30, 'normal', '劳保用品公司', '一次性使用', '2024-04-02 14:20:00', '2024-04-10 14:20:00', '2024-04-02 14:20:00');

-- 插入交易记录测试数据
INSERT INTO warehouse_transactions (id, material_id, type, quantity, operator, remark, created_at, updated_at) VALUES
('1', '1', 'in', 250, '管理员', '采购入库', '2024-04-10 09:30:00', '2024-04-10 09:30:00'),
('2', '2', 'in', 20, '管理员', '采购入库', '2024-04-02 14:20:00', '2024-04-02 14:20:00'),
('3', '2', 'out', 8, '操作员A', '领用出库', '2024-04-09 14:20:00', '2024-04-09 14:20:00'),
('4', '3', 'in', 50, '管理员', '采购入库', '2024-04-03 10:15:00', '2024-04-03 10:15:00'),
('5', '3', 'out', 50, '操作员B', '领用出库', '2024-04-08 10:15:00', '2024-04-08 10:15:00'),
('6', '4', 'in', 100, '管理员', '采购入库', '2024-04-04 11:45:00', '2024-04-04 11:45:00'),
('7', '4', 'out', 15, '操作员A', '领用出库', '2024-04-10 11:45:00', '2024-04-10 11:45:00'),
('8', '5', 'in', 3500, '管理员', '采购入库', '2024-04-05 16:00:00', '2024-04-05 16:00:00'),
('9', '6', 'in', 15, '管理员', '采购入库', '2024-04-06 13:30:00', '2024-04-06 13:30:00'),
('10', '7', 'in', 35, '管理员', '采购入库', '2024-04-07 10:15:00', '2024-04-07 10:15:00');