-- 结伴客系统数据库初始化脚本
-- 创建时间: 2024-01-15
-- 版本: 1.0.0

USE jiebandata;

-- 清空现有测试数据（可选，谨慎使用）
-- TRUNCATE TABLE admins;
-- TRUNCATE TABLE users;
-- TRUNCATE TABLE merchants;

-- 插入管理员用户数据
INSERT INTO admins (username, password, email, nickname, role, status) VALUES 
('superadmin', '$2b$10$rOzJqJzX9zX9zX9zX9zX9u', 'superadmin@jiebanke.com', '超级管理员', 'super_admin', 1),
('admin', '$2b$10$rOzJqJzX9zX9zX9zX9zX9u', 'admin@jiebanke.com', '管理员', 'admin', 1),
('operator', '$2b$10$rOzJqJzX9zX9zX9zX9zX9u', 'operator@jiebanke.com', '操作员', 'operator', 1)
ON DUPLICATE KEY UPDATE updated_at = CURRENT_TIMESTAMP;

-- 插入测试用户数据
INSERT INTO users (openid, nickname, gender, phone, email, created_at, updated_at) VALUES 
('wx_test_user_001', '测试用户张三', 'male', '13800138001', 'zhangsan@example.com', NOW(), NOW()),
('wx_test_user_002', '测试用户李四', 'female', '13800138002', 'lisi@example.com', NOW(), NOW()),
('wx_test_user_003', '测试用户王五', 'male', '13800138003', 'wangwu@example.com', NOW(), NOW()),
('wx_test_user_004', '测试用户赵六', 'female', '13800138004', 'zhaoliu@example.com', NOW(), NOW()),
('wx_test_user_005', '测试用户钱七', 'male', '13800138005', 'qianqi@example.com', NOW(), NOW()),
('wx_test_user_006', '测试用户孙八', 'female', '13800138006', 'sunba@example.com', NOW(), NOW()),
('wx_test_user_007', '测试用户周九', 'male', '13800138007', 'zhoujiu@example.com', NOW(), NOW()),
('wx_test_user_008', '测试用户吴十', 'female', '13800138008', 'wushi@example.com', NOW(), NOW()),
('wx_test_user_009', '测试用户郑十一', 'male', '13800138009', 'zhengshiyi@example.com', NOW(), NOW()),
('wx_test_user_010', '测试用户王十二', 'female', '13800138010', 'wangshier@example.com', NOW(), NOW())
ON DUPLICATE KEY UPDATE updated_at = NOW();

-- 插入测试商家数据
INSERT INTO merchants (user_id, merchant_type, business_name, contact_person, contact_phone, address, status, created_at, updated_at) VALUES 
(1, 'flower_shop', '鲜花小店', '张老板', '13800138100', '北京市朝阳区鲜花大街1号', 'approved', NOW(), NOW()),
(2, 'farm_owner', '快乐农场', '李农场主', '13800138101', '上海市浦东新区农场路88号', 'approved', NOW(), NOW()),
(3, 'activity_organizer', '户外活动俱乐部', '王教练', '13800138102', '广州市天河区体育西路123号', 'approved', NOW(), NOW()),
(4, 'flower_shop', '浪漫花坊', '赵花艺师', '13800138103', '深圳市南山区科技园路456号', 'approved', NOW(), NOW()),
(5, 'farm_owner', '有机农场', '钱农民', '13800138104', '杭州市西湖区龙井路789号', 'pending', NOW(), NOW())
ON DUPLICATE KEY UPDATE updated_at = NOW();

-- 插入测试商品数据
INSERT INTO products (merchant_id, name, description, price, image_url, category, status, created_at, updated_at) VALUES 
(1, '红玫瑰花束', '新鲜红玫瑰11朵精美包装', 199.00, '/images/flowers/rose.jpg', 'flowers', 'active', NOW(), NOW()),
(1, '百合花束', '白色百合花清新淡雅', 159.00, '/images/flowers/lily.jpg', 'flowers', 'active', NOW(), NOW()),
(2, '有机蔬菜礼盒', '当季有机蔬菜新鲜配送', 88.00, '/images/farm/vegetables.jpg', 'farm', 'active', NOW(), NOW()),
(2, '散养鸡蛋', '农家散养新鲜鸡蛋', 25.00, '/images/farm/eggs.jpg', 'farm', 'active', NOW(), NOW()),
(3, '户外徒步活动', '周末户外徒步一日游', 150.00, '/images/activities/hiking.jpg', 'activity', 'active', NOW(), NOW()),
(4, '康乃馨花束', '粉色康乃馨温馨祝福', 129.00, '/images/flowers/carnation.jpg', 'flowers', 'active', NOW(), NOW())
ON DUPLICATE KEY UPDATE updated_at = NOW();

-- 插入测试订单数据
INSERT INTO orders (user_id, merchant_id, order_number, total_amount, status, delivery_address, ordered_at) VALUES 
(1, 1, 'ORDER202401150001', 199.00, 'completed', '北京市朝阳区测试地址1号', NOW()),
(2, 2, 'ORDER202401150002', 113.00, 'paid', '上海市浦东新区测试地址2号', NOW()),
(3, 3, 'ORDER202401150003', 150.00, 'shipped', '广州市天河区测试地址3号', NOW()),
(4, 1, 'ORDER202401150004', 328.00, 'pending', '深圳市南山区测试地址4号', NOW()),
(5, 4, 'ORDER202401150005', 129.00, 'cancelled', '杭州市西湖区测试地址5号', NOW())
ON DUPLICATE KEY UPDATE updated_at = NOW();

-- 插入订单商品关联数据
INSERT INTO order_items (order_id, product_id, quantity, unit_price) VALUES 
(1, 1, 1, 199.00),
(2, 3, 1, 88.00),
(2, 4, 1, 25.00),
(3, 5, 1, 150.00),
(4, 1, 1, 199.00),
(4, 2, 1, 129.00),
(5, 6, 1, 129.00)
ON DUPLICATE KEY UPDATE unit_price = VALUES(unit_price);

-- 插入支付记录数据
INSERT INTO payments (order_id, payment_method, transaction_id, amount, status, paid_at) VALUES 
(1, 'wechat', 'WX202401150001', 199.00, 'success', NOW()),
(2, 'alipay', 'AL202401150002', 113.00, 'success', NOW()),
(3, 'balance', 'BAL202401150003', 150.00, 'success', NOW()),
(4, 'wechat', 'WX202401150004', 328.00, 'pending', NULL),
(5, 'alipay', 'AL202401150005', 129.00, 'failed', NULL)
ON DUPLICATE KEY UPDATE updated_at = NOW();

-- 查询确认数据插入情况
SELECT '管理员数据统计:' AS info, COUNT(*) AS count FROM admins
UNION ALL
SELECT '用户数据统计:', COUNT(*) FROM users 
UNION ALL
SELECT '商家数据统计:', COUNT(*) FROM merchants
UNION ALL
SELECT '商品数据统计:', COUNT(*) FROM products
UNION ALL
SELECT '订单数据统计:', COUNT(*) FROM orders
UNION ALL
SELECT '支付记录统计:', COUNT(*) FROM payments;

-- 显示最近插入的管理员用户
SELECT '最近管理员:' AS type, username, nickname, role, status FROM admins ORDER BY id DESC LIMIT 3;

-- 显示最近插入的普通用户
SELECT '最近用户:' AS type, nickname, gender, phone, email FROM users ORDER BY id DESC LIMIT 5;

-- 显示商家列表
SELECT '商家列表:' AS type, business_name, contact_person, contact_phone, status FROM merchants ORDER BY id;

-- 显示商品列表
SELECT '商品列表:' AS type, name, price, category, status FROM products ORDER BY merchant_id, id;

-- 显示订单统计
SELECT '订单状态统计:' AS status, COUNT(*) AS count FROM orders GROUP BY status;

COMMIT;

SELECT '数据库初始化完成！' AS message;