-- phpMyAdmin SQL Dump
-- version 5.2.2
-- https://www.phpmyadmin.net/
--
-- Máy chủ: localhost:3306
-- Thời gian đã tạo: Th12 22, 2025 lúc 10:29 PM
-- Phiên bản máy phục vụ: 10.11.11-MariaDB-cll-lve
-- Phiên bản PHP: 8.3.23

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Cơ sở dữ liệu: `taxidien_telegrambot`
--

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `admin_logs`
--

CREATE TABLE `admin_logs` (
  `id` int(11) NOT NULL,
  `admin_username` varchar(100) NOT NULL,
  `action` varchar(50) NOT NULL COMMENT 'add_money, subtract_money, ban_user, unban_user',
  `target` bigint(20) NOT NULL COMMENT 'Telegram ID của user bị tác động',
  `amount` decimal(14,2) DEFAULT NULL COMMENT 'Số tiền (nếu có)',
  `note` text DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Đang đổ dữ liệu cho bảng `admin_logs`
--

INSERT INTO `admin_logs` (`id`, `admin_username`, `action`, `target`, `amount`, `note`, `created_at`) VALUES
(1, 'admin', 'subtract_money', 5966807953, 5000.00, 'Trừ tiền của Thuỷ Jeny', '2025-12-22 05:26:33'),
(2, 'admin', 'ban_user', 5966807953, NULL, 'Ban user Thuỷ Jeny', '2025-12-22 05:26:43'),
(3, 'admin', 'unban_user', 5966807953, NULL, 'Unban user Thuỷ Jeny', '2025-12-22 05:26:51'),
(4, 'admin', 'subtract_money', 5966807953, 60003.00, 'Trừ tiền của Thuỷ Jeny', '2025-12-22 06:44:06'),
(5, 'admin', 'broadcast', 0, NULL, 'Gửi thông báo: 0/3 thành công, 3 thất bại', '2025-12-22 08:54:46'),
(6, 'admin', 'broadcast', 0, NULL, 'Gửi thông báo: 0/3 thành công, 3 thất bại', '2025-12-22 08:55:06'),
(7, 'admin', 'broadcast', 0, NULL, 'Gửi thông báo: 0/3 thành công, 3 thất bại', '2025-12-22 08:55:49'),
(8, 'admin', 'broadcast', 0, NULL, 'Gửi thông báo: 3/3 thành công, 0 thất bại', '2025-12-22 09:11:25'),
(9, 'admin', 'broadcast', 0, NULL, 'Gửi thông báo: 3/3 thành công, 0 thất bại', '2025-12-22 09:15:16'),
(10, 'admin', 'broadcast', 0, NULL, 'Gửi thông báo: 7/7 thành công, 0 thất bại', '2025-12-23 03:15:03');

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `categories`
--

CREATE TABLE `categories` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text DEFAULT NULL,
  `icon` varchar(50) DEFAULT '?',
  `status` enum('active','inactive') DEFAULT 'active',
  `sort_order` int(11) DEFAULT 0,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Đang đổ dữ liệu cho bảng `categories`
--

INSERT INTO `categories` (`id`, `name`, `description`, `icon`, `status`, `sort_order`, `created_at`) VALUES
(6, 'ChatGpt Plus', '', '🌐', 'active', 1, '2025-12-22 05:56:27'),
(7, 'Veo3 Ultral', '', '🔥', 'active', 0, '2025-12-23 03:17:27');

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `customers`
--

CREATE TABLE `customers` (
  `id` int(11) NOT NULL,
  `telegram_id` bigint(20) NOT NULL,
  `username` varchar(255) DEFAULT NULL,
  `first_name` varchar(255) DEFAULT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `last_active` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `is_banned` tinyint(1) DEFAULT 0 COMMENT '0=Active, 1=Banned'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Đang đổ dữ liệu cho bảng `customers`
--

INSERT INTO `customers` (`id`, `telegram_id`, `username`, `first_name`, `last_name`, `phone`, `created_at`, `last_active`, `is_banned`) VALUES
(1, 5966807953, 'ThuyJeny886', 'Thuỷ Jeny', '', NULL, '2025-12-22 03:42:33', '2025-12-22 09:30:44', 0),
(104, 7195813286, 'laomom112', 'Lão', 'Mõm', NULL, '2025-12-22 06:41:02', '2025-12-22 06:43:02', 0),
(110, 1963208840, 'NhuTho886', 'Như Thọ - 像托', '', NULL, '2025-12-22 06:44:42', '2025-12-23 03:29:08', 0),
(132, 903197466, 'trung_tran', 'Trung', 'Trần', NULL, '2025-12-22 12:09:30', '2025-12-22 12:09:56', 0),
(141, 5696044698, 'truonsin1349', 'Trần Trường', 'Sinh', NULL, '2025-12-22 12:43:54', '2025-12-23 03:15:36', 0),
(150, 5582437613, 'adam02202', 'Thanh', 'Nhã', NULL, '2025-12-22 18:45:48', '2025-12-23 03:07:46', 0),
(158, 7245821297, '', 'Ssshher', '', NULL, '2025-12-23 01:35:22', '2025-12-23 01:35:40', 0);

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `orders`
--

CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `username` varchar(255) DEFAULT NULL,
  `product_id` int(11) DEFAULT NULL,
  `account_id` int(11) DEFAULT NULL,
  `amount` decimal(14,2) DEFAULT NULL,
  `status` enum('pending','paid','cancelled') DEFAULT 'pending',
  `payment_data` text DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `pending_qr`
--

CREATE TABLE `pending_qr` (
  `id` int(11) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `chat_id` bigint(20) NOT NULL,
  `message_id` int(11) NOT NULL,
  `amount` decimal(14,2) NOT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `products`
--

CREATE TABLE `products` (
  `id` int(11) NOT NULL,
  `category_id` int(11) DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  `description` text DEFAULT NULL,
  `price` decimal(14,2) NOT NULL,
  `storage` varchar(50) DEFAULT NULL,
  `bandwidth` varchar(50) DEFAULT NULL,
  `status` enum('active','inactive') DEFAULT 'active',
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Đang đổ dữ liệu cho bảng `products`
--

INSERT INTO `products` (`id`, `category_id`, `name`, `description`, `price`, `storage`, `bandwidth`, `status`, `created_at`) VALUES
(7, 6, 'Code ChatGpt Plus 1 Tháng', '', 15000.00, '', '', 'active', '2025-12-22 06:02:26'),
(8, 6, 'ChatGpt Plus Chính Chủ 1 Tháng', '', 200000.00, '', '', 'active', '2025-12-22 06:16:53'),
(9, 6, 'ChatGpt Go (KBH)', '', 80000.00, '', '', 'active', '2025-12-22 06:17:27'),
(10, 6, 'ChatGpt Giáo Viên (KBH)', '', 79000.00, '', '', 'active', '2025-12-22 06:17:55'),
(11, 7, 'Veo3 Ultral 45K Credit ', '', 55000.00, '', '', 'active', '2025-12-23 03:17:59');

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `product_accounts`
--

CREATE TABLE `product_accounts` (
  `id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `account_info` text NOT NULL,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `additional_info` text DEFAULT NULL,
  `status` enum('available','sold','reserved') DEFAULT 'available',
  `sold_to_user_id` bigint(20) DEFAULT NULL,
  `sold_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Đang đổ dữ liệu cho bảng `product_accounts`
--

INSERT INTO `product_accounts` (`id`, `product_id`, `account_info`, `username`, `password`, `additional_info`, `status`, `sold_to_user_id`, `sold_at`, `created_at`) VALUES
(11, 7, '', 'chatgpt.com/p/C5YAHDDYBUZ96ZAP', '', NULL, 'sold', 5966807953, '2025-12-22 06:11:13', '2025-12-22 06:03:46'),
(12, 7, '', 'chatgpt.com/p/CN2ZB85PS5XVLH6X', '', NULL, 'available', NULL, NULL, '2025-12-22 06:03:46'),
(13, 7, '', 'chatgpt.com/p/CXXJLF5J7BH3NFNW', '', NULL, 'available', NULL, NULL, '2025-12-22 06:03:46'),
(14, 7, '', 'chatgpt.com/p/UXBEMZ7LT9GEW56L', '', NULL, 'available', NULL, NULL, '2025-12-22 06:03:46'),
(15, 7, '', 'chatgpt.com/p/AYWMLEQGQZT683CD', '', NULL, 'available', NULL, NULL, '2025-12-22 06:03:46'),
(16, 8, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:32'),
(17, 8, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:32'),
(18, 8, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:32'),
(19, 8, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:32'),
(20, 8, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:32'),
(21, 8, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:32'),
(22, 9, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:49'),
(23, 9, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:49'),
(24, 9, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:49'),
(25, 9, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:49'),
(26, 10, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:56'),
(27, 10, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:56'),
(28, 10, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:56'),
(29, 10, '', 'Liên hệ @NhuTho886', '', NULL, 'available', NULL, NULL, '2025-12-22 06:18:56'),
(30, 11, '', 'Liên hệ @NhuTho886 Nhận Account', '1', NULL, 'available', NULL, NULL, '2025-12-23 03:20:34'),
(31, 11, '', 'Liên hệ @NhuTho886 Nhận Account', '2', NULL, 'available', NULL, NULL, '2025-12-23 03:20:34'),
(32, 11, '', 'Liên hệ @NhuTho886 Nhận Account', '3', NULL, 'available', NULL, NULL, '2025-12-23 03:20:34'),
(33, 11, '', 'Liên hệ @NhuTho886 Nhận Account', '4', NULL, 'available', NULL, NULL, '2025-12-23 03:20:34'),
(34, 11, '', 'Liên hệ @NhuTho886 Nhận Account', '5', NULL, 'available', NULL, NULL, '2025-12-23 03:20:34'),
(35, 11, '', 'Liên hệ @NhuTho886 Nhận Account', '6', NULL, 'available', NULL, NULL, '2025-12-23 03:20:34');

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `settings`
--

CREATE TABLE `settings` (
  `id` int(11) NOT NULL,
  `bot_token` varchar(255) NOT NULL,
  `bank_token` varchar(255) DEFAULT NULL,
  `bank_account` varchar(100) DEFAULT NULL,
  `bank_password` varchar(255) DEFAULT NULL,
  `webhook_url` varchar(255) DEFAULT NULL,
  `auto_check_bank` tinyint(1) DEFAULT 1,
  `auto_approve` tinyint(1) DEFAULT 1,
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

--
-- Đang đổ dữ liệu cho bảng `settings`
--

INSERT INTO `settings` (`id`, `bot_token`, `bank_token`, `bank_account`, `bank_password`, `webhook_url`, `auto_check_bank`, `auto_approve`, `updated_at`) VALUES
(1, '8329078139:AAHPgKDlgTQnc4OkjB9cFDE9dJM92WpqDZI', '', '', '', '', 0, 0, '2025-12-22 09:26:49');

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `topups`
--

CREATE TABLE `topups` (
  `id` int(11) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `amount` decimal(14,2) NOT NULL,
  `bank_tx_id` varchar(100) NOT NULL,
  `description` text DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Đang đổ dữ liệu cho bảng `topups`
--

INSERT INTO `topups` (`id`, `user_id`, `amount`, `bank_tx_id`, `description`, `created_at`) VALUES
(11, 5966807953, 10000.00, '2687', '5966807953 FT25356952883866 GD 5356IBT1kJXZAWL5 221225-12:42:53', '2025-12-22 06:43:14'),
(12, 5966807953, 10000.00, '2685', '5966807953 GD 5356IBT1dJ7X8ZFK 221225-11:36:16', '2025-12-22 06:43:15'),
(13, 5966807953, 10000.00, '2684', '5966807953 GD 5356IBT1dJ7XSUBG 221225-11:19:03', '2025-12-22 06:43:16'),
(14, 5966807953, 10000.00, '2683', '5966807953 GD 5356IBT1dJ7XCYK3 221225-11:09:56', '2025-12-22 06:43:17'),
(15, 5966807953, 10000.00, '2682', '5966807953 GD 5356IBT1dJ7X11QB 221225-11:05:27', '2025-12-22 06:43:17'),
(16, 5966807953, 5000.00, '2681', 'ID5966807953 GD 5356IBT1dJ733EDD 221225-10:44:21', '2025-12-22 06:43:18'),
(17, 111061713623, 3000.00, '2669', '111061713623-01202738245-DONATE161220251041', '2025-12-22 06:43:19'),
(18, 111060797883, 5000.00, '2668', '111060797883-01202738245-DONATE161220250333', '2025-12-22 06:43:20'),
(19, 1963208840, 10000.00, '2689', '1963208840 FT25356700035857 GD 5356IBT1kJXLWJUQ 221225-15:29:02', '2025-12-22 08:30:00');

-- --------------------------------------------------------

--
-- Cấu trúc đóng vai cho view `v_product_stats`
-- (See below for the actual view)
--
CREATE TABLE `v_product_stats` (
`id` int(11)
,`product_name` varchar(255)
,`category_name` varchar(255)
,`category_icon` varchar(50)
,`price` decimal(14,2)
,`total_accounts` bigint(21)
,`available_accounts` decimal(22,0)
,`sold_accounts` decimal(22,0)
,`reserved_accounts` decimal(22,0)
);

-- --------------------------------------------------------

--
-- Cấu trúc bảng cho bảng `wallets`
--

CREATE TABLE `wallets` (
  `id` int(11) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `balance` decimal(14,2) NOT NULL DEFAULT 0.00,
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Đang đổ dữ liệu cho bảng `wallets`
--

INSERT INTO `wallets` (`id`, `user_id`, `balance`, `updated_at`) VALUES
(1, 5966807953, 0.00, '2025-12-22 06:44:05'),
(6, 111061713623, 3000.00, '2025-12-22 06:43:19'),
(7, 111060797883, 5000.00, '2025-12-22 06:43:20'),
(8, 1963208840, 10000.00, '2025-12-22 08:30:00');

--
-- Chỉ mục cho các bảng đã đổ
--

--
-- Chỉ mục cho bảng `admin_logs`
--
ALTER TABLE `admin_logs`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_admin` (`admin_username`),
  ADD KEY `idx_action` (`action`),
  ADD KEY `idx_target` (`target`),
  ADD KEY `idx_created` (`created_at`);

--
-- Chỉ mục cho bảng `categories`
--
ALTER TABLE `categories`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_status` (`status`),
  ADD KEY `idx_sort` (`sort_order`);

--
-- Chỉ mục cho bảng `customers`
--
ALTER TABLE `customers`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `telegram_id` (`telegram_id`),
  ADD KEY `idx_telegram` (`telegram_id`),
  ADD KEY `idx_is_banned` (`is_banned`);

--
-- Chỉ mục cho bảng `orders`
--
ALTER TABLE `orders`
  ADD PRIMARY KEY (`id`),
  ADD KEY `product_id` (`product_id`),
  ADD KEY `idx_user` (`user_id`),
  ADD KEY `idx_status` (`status`),
  ADD KEY `idx_account` (`account_id`),
  ADD KEY `idx_created` (`created_at`);

--
-- Chỉ mục cho bảng `pending_qr`
--
ALTER TABLE `pending_qr`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_user` (`user_id`),
  ADD KEY `idx_created` (`created_at`);

--
-- Chỉ mục cho bảng `products`
--
ALTER TABLE `products`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_category` (`category_id`),
  ADD KEY `idx_status` (`status`);

--
-- Chỉ mục cho bảng `product_accounts`
--
ALTER TABLE `product_accounts`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_product` (`product_id`),
  ADD KEY `idx_status` (`status`);

--
-- Chỉ mục cho bảng `settings`
--
ALTER TABLE `settings`
  ADD PRIMARY KEY (`id`);

--
-- Chỉ mục cho bảng `topups`
--
ALTER TABLE `topups`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_bank_tx` (`bank_tx_id`),
  ADD KEY `idx_user` (`user_id`),
  ADD KEY `idx_created` (`created_at`);

--
-- Chỉ mục cho bảng `wallets`
--
ALTER TABLE `wallets`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uk_user` (`user_id`),
  ADD KEY `idx_balance` (`balance`);

--
-- AUTO_INCREMENT cho các bảng đã đổ
--

--
-- AUTO_INCREMENT cho bảng `admin_logs`
--
ALTER TABLE `admin_logs`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;

--
-- AUTO_INCREMENT cho bảng `categories`
--
ALTER TABLE `categories`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;

--
-- AUTO_INCREMENT cho bảng `customers`
--
ALTER TABLE `customers`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=176;

--
-- AUTO_INCREMENT cho bảng `orders`
--
ALTER TABLE `orders`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

--
-- AUTO_INCREMENT cho bảng `pending_qr`
--
ALTER TABLE `pending_qr`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT cho bảng `products`
--
ALTER TABLE `products`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;

--
-- AUTO_INCREMENT cho bảng `product_accounts`
--
ALTER TABLE `product_accounts`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=36;

--
-- AUTO_INCREMENT cho bảng `settings`
--
ALTER TABLE `settings`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT cho bảng `topups`
--
ALTER TABLE `topups`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=20;

--
-- AUTO_INCREMENT cho bảng `wallets`
--
ALTER TABLE `wallets`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;

-- --------------------------------------------------------

--
-- Cấu trúc cho view `v_product_stats`
--
DROP TABLE IF EXISTS `v_product_stats`;

CREATE ALGORITHM=UNDEFINED DEFINER=`cpses_tadgua0lyf`@`localhost` SQL SECURITY DEFINER VIEW `v_product_stats`  AS SELECT `p`.`id` AS `id`, `p`.`name` AS `product_name`, `c`.`name` AS `category_name`, `c`.`icon` AS `category_icon`, `p`.`price` AS `price`, count(`pa`.`id`) AS `total_accounts`, sum(case when `pa`.`status` = 'available' then 1 else 0 end) AS `available_accounts`, sum(case when `pa`.`status` = 'sold' then 1 else 0 end) AS `sold_accounts`, sum(case when `pa`.`status` = 'reserved' then 1 else 0 end) AS `reserved_accounts` FROM ((`products` `p` left join `categories` `c` on(`p`.`category_id` = `c`.`id`)) left join `product_accounts` `pa` on(`p`.`id` = `pa`.`product_id`)) GROUP BY `p`.`id`, `p`.`name`, `c`.`name`, `c`.`icon`, `p`.`price` ;

--
-- Ràng buộc đối với các bảng kết xuất
--

--
-- Ràng buộc cho bảng `orders`
--
ALTER TABLE `orders`
  ADD CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES `product_accounts` (`id`) ON DELETE SET NULL;

--
-- Ràng buộc cho bảng `products`
--
ALTER TABLE `products`
  ADD CONSTRAINT `products_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL;

--
-- Ràng buộc cho bảng `product_accounts`
--
ALTER TABLE `product_accounts`
  ADD CONSTRAINT `product_accounts_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
