DROP TABLE IF EXISTS vhost.tw_user;

CREATE TABLE vhost.tw_user (
    uid varchar(36) NOT NULL COMMENT "사용자 UUID",
    id varchar(30) NOT NULL,
    passwd varchar(100) NOT NULL,
    name varchar(20) NOT NULL COMMENT "사용자명",
    email varchar(50) NOT NULL,
    phone varchar(20) NULL COMMENT '연락처',
    mobile varchar(20) NULL COMMENT '핸드폰',
    role varchar(255) NOT NULL DEFAULT 'user' COMMENT '사용자등급',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 사용,unuse: 사용않함',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    UNIQUE KEY (id),
    UNIQUE KEY (email)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = '사용자 정보';
-- insert into tw_user (uid,id,passwd,name,email,role,status) select uuid(),id,passwd,name,email,role,status from cfmgr.user;
DROP TABLE IF EXISTS vhost.tw_user_profile;

DROP TABLE IF EXISTS vhost.tw_user_sns;

CREATE TABLE vhost.tw_user_sns (
    uid int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    user_uid varchar(36) NULL COMMENT '사용자 정보',
    site varchar(20) NOT NULL COMMENT 'Site: GOOGLE,FACEBOOK 등등',
    id varchar(255) NOT NULL COMMENT 'sns 로그인 인중후 Return ID값',
    name varchar(50) NOT NULL,
    email varchar(50) NOT NULL,
    detail text NOT NULL COMMENT 'JSON형식 원본값',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 사용,unuse: 사용않함',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    PRIMARY KEY (uid),
    UNIQUE KEY (site, id),
    CONSTRAINT FOREIGN KEY (user_uid) REFERENCES tw_user (uid) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = 'SNS 로그인 후 정보';

DROP TABLE IF EXISTS vhost.tw_category;

CREATE TABLE vhost.tw_category (
    uid varchar(50) NOT NULL,
    grpno int(10) unsigned NOT NULL DEFAULT 1 COMMENT 'Group번호: 상위가없을시 기본 uid와 같음,항상 숫자여야함',
    grporder int(5) unsigned NOT NULL DEFAULT 1 COMMENT 'Group순서: 상위가없을시 1부터시작',
    grpdepth int(3) unsigned NOT NULL DEFAULT 1 COMMENT 'Group깊이: 상위가없을시 1부터시작 , 상위 grpdepth+1씩 추가필요',
    parent varchar(50) DEFAULT NULL COMMENT '부모UID',
    name varchar(255) NOT NULL COMMENT '범주명',
    linkurl varchar(100) NOT NULL DEFAULT '/front/board' COMMENT 'Front Link URL',
    isaccess varchar(30) NOT NULL DEFAULT 'guest' COMMENT '접근권한',
    isread varchar(30) NOT NULL DEFAULT 'guest' COMMENT '읽기권한',
    iswrite varchar(30) NOT NULL DEFAULT 'guest' COMMENT '쓰기권한',
    isreply varchar(30) NOT NULL DEFAULT 'guest' COMMENT '답글권한',
    isupload varchar(30) NOT NULL DEFAULT 'guest' COMMENT 'Upload권한',
    isdownload varchar(30) NOT NULL DEFAULT 'guest' COMMENT 'Download권한',
    head text DEFAULT NULL COMMENT '위 내용',
    tail text DEFAULT NULL COMMENT '아래 내용',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 표시,unuse: 표시않함',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = '범주';

DROP TABLE IF EXISTS vhost.tw_board;
-- 1. 게시물 추가전 grpno에 해당하는 max(grporder)+1씩증가 작업
-- update tw_board set grporder=grporder+1 where grpno=그룹번호 and grporder > 선택한 grpno
-- 2. 게시물 추가시 작업
-- insert tw_board grpno=그룹번호,grporder=grporder+1,grpdepth=grpdepth+1
-- 3. 게시물 조회시 작업
-- select * from tw_board order by grpno desc,grporder asc
CREATE TABLE vhost.tw_board (
    uid int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    grpno int(10) UNSIGNED NOT NULL DEFAULT 1 COMMENT 'Group번호: 상위가없을시 기본 uid와 같음,항상 숫자여야함',
    grporder int(5) UNSIGNED NOT NULL DEFAULT 1 COMMENT 'Group순서: 상위가없을시 1부터시작',
    grpdepth int(3) UNSIGNED NOT NULL DEFAULT 1 COMMENT 'Group깊이: 상위가없을시 1부터시작 , 상위 grpdepth+1씩 추가필요',
    parent int(10) UNSIGNED NULL COMMENT '부모UID',
    category varchar(20) NOT NULL COMMENT '구분',
    user_uid varchar(36) NULL COMMENT '작성자 정보',
    title varchar(255) NOT NULL COMMENT '제목',
    content text NOT NULL COMMENT '내용',
    passwd varchar(20) NULL COMMENT '작성자 암호',
    board_file varchar(255) NULL COMMENT '파일명',
    view_cnt int(5) NOT NULL DEFAULT 0 COMMENT '조회수',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 사용, unuse: 사용않함 등등',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    CONSTRAINT tw_board_ibfk_1 FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = '게시물 정보';

DROP TABLE IF EXISTS vhost.tw_sitepage;

CREATE TABLE vhost.tw_sitepage (
    uid int(10) unsigned NOT NULL AUTO_INCREMENT,
    category varchar(20) NOT NULL COMMENT '구분',
    user_uid varchar(36) DEFAULT NULL COMMENT '작성자 정보',
    title varchar(255) NOT NULL COMMENT '제목',
    content text NOT NULL COMMENT '내용',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 사용,unuse: 사용않함 등등',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    KEY user_uid (user_uid),
    CONSTRAINT tw_sitepage_ibfk_1 FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT '사이트페이지';

DROP TABLE IF EXISTS vhost.tw_device;

CREATE TABLE vhost.tw_device (
    uid varchar(36) NOT NULL,
    category varchar(20) NOT NULL COMMENT '형식:server(HP,KVM,Container),cpu,memory,disk(ssd,nvme),os(linux,windows),공인ip등등',
    name varchar(255) NOT NULL,
    cost int(10) unsigned NOT NULL DEFAULT 0 COMMENT '원가',
    price int(10) unsigned NOT NULL DEFAULT 0 COMMENT '판매가',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 판매중 unuse: 판매중지, soldout:매진, outofstock:재고부족 등등',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT '장비 정보';

DROP TABLE IF EXISTS vhost.tw_product;

CREATE TABLE vhost.tw_product (
    uid varchar(36) NOT NULL,
    category varchar(20) NOT NULL COMMENT '범주_UID',
    user_uid varchar(36) DEFAULT NULL COMMENT '생산자 정보',
    type varchar(10) NOT NULL DEFAULT 'rental' COMMENT 'rental: 월임대, onetime:판매,일회성',
    name varchar(255) NOT NULL COMMENT '상품명',
    photo varchar(255) DEFAULT NULL COMMENT '상품이미지',
    cost int(10) unsigned NOT NULL DEFAULT 0 COMMENT '원가',
    price int(10) unsigned NOT NULL DEFAULT 0 COMMENT '판매가',
    sale int(10) unsigned NOT NULL DEFAULT 0 COMMENT '할인가',
    stock int(5) unsigned NOT NULL DEFAULT 1 COMMENT '재고수량',
    view_cnt int(4) unsigned NOT NULL DEFAULT 1 COMMENT '조회수',
    content text NOT NULL COMMENT '상품내용',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 판매, unuse: 판매중지, soldout:매진, outofstock:재고부족 등등',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    KEY user_uid (user_uid),
    CONSTRAINT tw_product_ibfk_1 FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = '상품 정보';

DROP TABLE IF EXISTS vhost.tw_product_device;

CREATE TABLE vhost.tw_product_device (
    uid int(10) unsigned NOT NULL AUTO_INCREMENT,
    product_uid varchar(36) NOT NULL COMMENT '상품 정보',
    device_uid varchar(36) NOT NULL COMMENT '장비 정보',
    device_cnt int(3) unsigned NOT NULL DEFAULT 1 COMMENT '장비 갯수',
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    PRIMARY KEY (uid),
    KEY product_uid (product_uid),
    KEY device_uid (device_uid),
    CONSTRAINT tw_product_device_ibfk_1 FOREIGN KEY (product_uid) REFERENCES tw_product (uid) ON DELETE CASCADE,
    CONSTRAINT tw_product_device_ibfk_2 FOREIGN KEY (device_uid) REFERENCES tw_device (uid) ON DELETE RESTRICT
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT '상품장비연결';

DROP TABLE IF EXISTS vhost.tw_order;

CREATE TABLE vhost.tw_order (
    uid varchar(36) NOT NULL,
    product_uid varchar(36) DEFAULT NULL COMMENT '상품 정보',
    user_uid varchar(36) DEFAULT NULL COMMENT '사용자 정보',
    name varchar(255) NOT NULL COMMENT '상품명',
    cost int(10) unsigned NOT NULL COMMENT '구매원가',
    sale int(10) unsigned NOT NULL DEFAULT 0 COMMENT '할인가',
    price int(10) unsigned NOT NULL COMMENT '결제액',
    quantity varchar(255) NOT NULL COMMENT '수량',
    type varchar(10) NOT NULL DEFAULT 'rental' COMMENT 'rental: 월임대,onetime:판매,일회성',
    paymentday int(2) unsigned DEFAULT NULL COMMENT '결제일',
    status varchar(10) NOT NULL DEFAULT 'unuse' COMMENT 'use: 주문요청/장바구니,unuse: 주문완료/서비스',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    KEY product_uid (product_uid),
    KEY user_uid (user_uid),
    CONSTRAINT tw_order_ibfk_1 FOREIGN KEY (product_uid) REFERENCES tw_product (uid),
    CONSTRAINT tw_order_ibfk_2 FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT 'Order 정보';

DROP TABLE IF EXISTS vhost.tw_service;

CREATE TABLE vhost.tw_service (
    uid varchar(36) NOT NULL,
    product_uid varchar(36) DEFAULT NULL COMMENT '상품 정보',
    user_uid varchar(36) DEFAULT NULL COMMENT '구매자 정보',
    type varchar(10) NOT NULL DEFAULT 'rental' COMMENT 'rental: 월임대,onetime:판매,일회성',
    price int(10) unsigned NOT NULL COMMENT '결제액',
    paymentday int(2) unsigned DEFAULT NULL COMMENT '결제일',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 사용,unuse: 해지',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    KEY user_uid (user_uid),
    CONSTRAINT tw_service_ibfk_1 FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT '서비스정보';

DROP TABLE IF EXISTS vhost.tw_service_product;

CREATE TABLE vhost.tw_service_product (
    uid int(10) unsigned NOT NULL,
    service_uid varchar(36) DEFAULT NULL COMMENT '서비스상품 정보',
    product_uid varchar(36) DEFAULT NULL COMMENT '상품 정보',
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    KEY product_uid (product_uid),
    KEY service_uid (service_uid),
    CONSTRAINT tw_service_product_ibfk_1 FOREIGN KEY (service_uid) REFERENCES tw_service (uid),
    CONSTRAINT tw_service_product_ibfk_2 FOREIGN KEY (product_uid) REFERENCES tw_product (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT '주문-서비스정보';

DROP TABLE IF EXISTS vhost.tw_billing;

CREATE TABLE vhost.tw_billing (
    uid int(10) unsigned NOT NULL,
    user_uid varchar(36) NOT NULL COMMENT '사용자 정보',
    type varchar(10) DEFAULT NULL COMMENT 'Card: 카드결제,
	Deposit:무통장입금',
    email varchar(50) NOT NULL,
    phone varchar(20) DEFAULT NULL COMMENT '연락처',
    title varchar(255) NOT NULL COMMENT '청구서제목',
    price int(10) unsigned NOT NULL COMMENT '청구금액',
    response text DEFAULT NULL COMMENT '결제처리결과',
    status varchar(10) NOT NULL DEFAULT 'unuse' COMMENT 'use: 납부완료,
	unuse: 미납 등등',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    KEY user_uid (user_uid),
    CONSTRAINT tw_billing_ibfk_1 FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT '청구서정보';

DROP TABLE IF EXISTS vhost.tw_service_billing;

CREATE TABLE vhost.tw_service_billing (
    uid int(10) unsigned NOT NULL,
    service_uid varchar(36) NOT NULL COMMENT '서비스 정보',
    billing_uid int(10) unsigned NOT NULL COMMENT '청구서 정보',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    PRIMARY KEY (uid),
    KEY service_uid (service_uid),
    KEY billing_uid (billing_uid),
    CONSTRAINT tw_service_billing_ibfk_1 FOREIGN KEY (service_uid) REFERENCES tw_service (uid),
    CONSTRAINT tw_service_billing_ibfk_2 FOREIGN KEY (billing_uid) REFERENCES tw_billing (uid)
) ENGINE = InnoDB CHARSET = utf8 COLLATE = utf8_general_ci COMMENT '서비스-청구서정보';