DROP TABLE IF EXISTS shoppingmall.tw_user;

CREATE TABLE shoppingmall.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 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_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 shoppingmall.tw_user_sns;

CREATE TABLE shoppingmall.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 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='SNS 로그인 후 정보';


DROP TABLE IF EXISTS shoppingmall.tw_category;
-- 1. 게시물 추가전 grpno에 해당하는 max(grporder)+1씩증가 작업
-- update tw_category set grporder=grporder+1 where grpno=그룹번호 and grporder > 선택한 grpno
-- 2. 게시물 추가시 작업
-- insert tw_category grpno=그룹번호,grporder=grporder+1,grpdepth=grpdepth+1
-- 3. 게시물 조회시 작업
-- select * from tw_category order by grpno desc,grporder asc
CREATE TABLE shoppingmall.tw_category (
    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_uid int(10) UNSIGNED NULL COMMENT '부모UID',
    name varchar(255) NOT NULL COMMENT '분류명',
    linkurl varchar(100) NOT NULL DEFAULT '/front/board' COMMENT 'Front Link URL',
    photo varchar(255) NULL COMMENT '이미지',
    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 NULL COMMENT '위 내용',
    tail text 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 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='분류';

DROP TABLE IF EXISTS shoppingmall.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 shoppingmall.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_uid int(10) UNSIGNED NULL COMMENT '부모UID',
	category_uid int(10) UNSIGNED NOT NULL COMMENT '분류 UID',
    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 FOREIGN KEY (category_uid) REFERENCES tw_category (uid),
    CONSTRAINT FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='게시물 정보';

DROP TABLE IF EXISTS shoppingmall.tw_sitepage;
CREATE TABLE shoppingmall.tw_sitepage (
    uid int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	category_uid int(10) UNSIGNED NOT NULL COMMENT '분류 UID',
    user_uid varchar(36) 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),
    CONSTRAINT FOREIGN KEY (category_uid) REFERENCES tw_category (uid),
    CONSTRAINT FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='사이트페이지 정보';

DROP TABLE IF EXISTS shoppingmall.tw_product;
CREATE TABLE shoppingmall.tw_product (
    uid varchar(36) NOT NULL,
    category_uid int(10) UNSIGNED NOT NULL COMMENT '상품분류',
    user_uid varchar(36) NULL COMMENT '생산자 정보',
    name varchar(255) NOT NULL COMMENT '상품명',
    photo varchar(255) 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 '판매가',
    stock int(5) UNSIGNED DEFAULT 1 NULL COMMENT '재고수량',
    view_cnt int(4) UNSIGNED DEFAULT 1 NOT NULL DEFAULT 0 COMMENT '조회수',
    content text NOT NULL COMMENT '상품정보',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 판매, unuse: 판매않함, standby: 판매준비, hold: 판매중지 soldout: 상품부족 등등',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    CONSTRAINT FOREIGN KEY (category_uid) REFERENCES tw_category (uid),
    CONSTRAINT FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='상품 정보';

DROP TABLE IF EXISTS shoppingmall.tw_order;
CREATE TABLE shoppingmall.tw_order (
    uid varchar(36) NOT NULL,
    product_uid varchar(36) NULL COMMENT '상품 정보',
    user_uid varchar(36) 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 '수량',
    status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 결제하기, unuse: 주문취소, paid:결제완료 등등',
    updated_at timestamp NULL DEFAULT NULL,
    created_at timestamp NOT NULL DEFAULT current_timestamp(),
    deleted_at timestamp NULL DEFAULT NULL,
    PRIMARY KEY (uid),
    CONSTRAINT FOREIGN KEY (product_uid) REFERENCES tw_product (uid),
    CONSTRAINT FOREIGN KEY (user_uid) REFERENCES tw_user (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='Order 정보';

DROP TABLE IF EXISTS shoppingmall.tw_payment;
CREATE TABLE shoppingmall.tw_payment (
    uid int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    TID varchar(20) NOT NULL COMMENT 'PG사 결제 거래고유번호 (전표출력 및 결제취소에 사용됩니다)'
    ORDERNO varchar(50) NOT NULL COMMENT '주문번호',
    AMOUNT int(10) UNSIGNED NOT NULL COMMENT '결제 된 금액',
    QUOTA varchar(2) NOT NULL COMMENT '카드 할부결제시 할부기간 (00:일시불, 01:1개월)',
    ACCEPTNO varchar(10) NOT NULL COMMENT '승인번호',
    ACCEPTDATE varchar(20) NOT NULL COMMENT '승인일시',
    RESULTCODE varchar(4) NOT NULL COMMENT '결제결과코드',
    RESULTMSG varchar(100) NOT NULL COMMENT '결제결과메세지',
    ETC1 varchar(100) NULL COMMENT '결제 요청시 입력한 값',
    ETC2 varchar(100) NULL COMMENT '결제 요청시 입력한 값',
    ETC3 varchar(100) NULL COMMENT '결제 요청시 입력한 값',
    ETC4 varchar(100) NULL COMMENT '결제 요청시 입력한 값',
    ETC5 varchar(100) NULL COMMENT '결제 요청시 입력한 값',
    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 (tid),
    CONSTRAINT FOREIGN KEY (orderno) REFERENCES tw_order (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='Payment 정보';