/*增加店铺表，同时为相关表添加店铺id*/

/*增加店铺表*/
CREATE TABLE `sale_shop`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '店铺id',
  `name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '店铺名称',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '店铺状态',
  `merchant_id` bigint NOT NULL DEFAULT 0 COMMENT '商户id',

  `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '店铺表' ROW_FORMAT = Dynamic;



ALTER TABLE product_property ADD COLUMN shop_id bigint COMMENT '店铺id';  --  商品属性项表加店铺id
ALTER TABLE product_property_value ADD COLUMN shop_id bigint COMMENT '店铺id';  --  商品属性值表加店铺id
ALTER TABLE product_spu ADD COLUMN shop_id bigint COMMENT '店铺id';  --  商品SPU表加店铺id
ALTER TABLE product_sku ADD COLUMN shop_id bigint COMMENT '店铺id';  --  商品SKU表加店铺id
ALTER TABLE trade_order ADD COLUMN shop_id bigint COMMENT '店铺id';  --  订单主表加店铺id
ALTER TABLE product_comment ADD COLUMN shop_id bigint COMMENT '店铺id';  --  商品评价表加店铺id
ALTER TABLE trade_after_sale ADD COLUMN shop_id bigint COMMENT '店铺id';  --  售后订单表加店铺id
ALTER TABLE trade_delivery_express_template ADD COLUMN shop_id bigint COMMENT '店铺id';  --  快递运费模板主表加店铺id
ALTER TABLE trade_delivery_pick_up_store ADD COLUMN shop_id bigint COMMENT '店铺id';  --  自提门店表加店铺id

/*增加商户表*/
CREATE TABLE `sale_merchant`  (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '商户id',
  `name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '商户名称',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '商户状态，关联商户状态表',
  `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '简介',
  `contact` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '负责人名称',
  `address` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '详细地址',
  `contact_number` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '负责人电话',
  `website` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '官网',
  `complaints_hotline` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '维权电话',
  `customer_service_hotline` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '客服电话',
  `email` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '邮箱',
  `business_license_picture` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '营业执照图片路径',
  `expire_time` datetime NULL COMMENT '服务到期时间',
  
  `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '店铺表' ROW_FORMAT = Dynamic;

ALTER TABLE product_property ADD COLUMN merchant_id bigint COMMENT '商户id';  --  商品属性项表加商户id
ALTER TABLE product_property_value ADD COLUMN merchant_id bigint COMMENT '商户id';  --  商品属性值表加商户id
ALTER TABLE product_spu ADD COLUMN merchant_id bigint COMMENT '商户id';  --  商品SPU表加商户id
ALTER TABLE product_sku ADD COLUMN merchant_id bigint COMMENT '商户id';  --  商品SKU表加商户id
ALTER TABLE trade_order ADD COLUMN merchant_id bigint COMMENT '商户id';  --  订单主表加商户id
ALTER TABLE product_comment ADD COLUMN merchant_id bigint COMMENT '商户id';  --  商品评价表加商户id
ALTER TABLE trade_after_sale ADD COLUMN merchant_id bigint COMMENT '商户id';  --  售后订单表加商户id
ALTER TABLE trade_delivery_express_template ADD COLUMN merchant_id bigint COMMENT '商户id';  --  快递运费模板主表加商户id
ALTER TABLE trade_delivery_pick_up_store ADD COLUMN merchant_id bigint COMMENT '商户id';  --  自提门店表加商户id


ALTER TABLE product_spu ADD COLUMN hidden bit(1) NOT NULL DEFAULT b'0' COMMENT '是否隐藏。隐藏：1，不隐藏：0';  --  控制商品是否显示。


/*增加店铺状态表*/
CREATE TABLE `sale_shop_status`  (
  `id` bigint NOT NULL COMMENT 'id',
  `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '名称',

  `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '店铺状态表' ROW_FORMAT = Dynamic;

INSERT INTO sale_shop_status (id,name) VALUES(1,'营业中');
INSERT INTO sale_shop_status (id,name) VALUES(2,'暂停营业');
INSERT INTO sale_shop_status (id,name) VALUES(3,'停用');

commit;

/*增加用户类型表*/
CREATE TABLE `system_user_category`  (
  `id` bigint NOT NULL COMMENT 'id',
  `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '名称',

  `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '店铺状态表' ROW_FORMAT = Dynamic;

INSERT INTO system_user_category (id,name) VALUES(1,'系统用户');
INSERT INTO system_user_category (id,name) VALUES(5,'店铺用户');
commit;

/*用户表、角色、菜单表 添加用户类型字段。*/
ALTER TABLE system_users ADD COLUMN category_id bigint NULL COMMENT '用户类型'; -- 用户表添加用户类型字段
ALTER TABLE system_role ADD COLUMN category_id bigint NULL COMMENT '用户类型'; -- 角色表添加用户类型字段
ALTER TABLE system_menu ADD COLUMN category_id bigint NULL COMMENT '用户类型'; -- 菜单表添加用户类型字段

/*用户表system_users还要添加店铺ID、商户ID字段*/
ALTER TABLE system_users ADD COLUMN shop_id bigint NULL COMMENT '店铺id';
ALTER TABLE system_users ADD COLUMN merchant_id bigint NULL COMMENT '商户id';

ALTER TABLE `member_user` MODIFY COLUMN `future_member_worth_value` bigint(0) NULL DEFAULT NULL COMMENT '未来身价值，未来身价值=当前身价值+预增加身价值' AFTER `pre_added_member_worth_value`;
ALTER TABLE `sale_merchant` ADD COLUMN `logo_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT 'logo地址' AFTER `id`;
ALTER TABLE `sale_shop` ADD COLUMN `logo_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT 'logo地址' AFTER `id`;


-- 修改spu以及sku表字段
ALTER TABLE `product_sku` ADD COLUMN `Promotion_fee` int(0) NULL DEFAULT NULL COMMENT '推广费, 单位: 分' AFTER `price`;
ALTER TABLE `product_sku` ADD COLUMN `mark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '备注' AFTER `Promotion_fee`;

ALTER TABLE `product_spu` ADD COLUMN `producer_area` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '产地' AFTER `description`;
ALTER TABLE `product_spu` ADD COLUMN `spu_type` bit(1) NULL DEFAULT NULL COMMENT '商品类别' AFTER `producer_area`;






ALTER TABLE `member_user` MODIFY COLUMN `future_member_worth_value` bigint(0) NULL DEFAULT NULL COMMENT '未来身价值，未来身价值=当前身价值+预增加身价值' AFTER `pre_added_member_worth_value`;
ALTER TABLE `sale_merchant` ADD COLUMN `logo_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT 'logo地址' AFTER `id`;
ALTER TABLE `sale_shop` ADD COLUMN `logo_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT 'logo地址' AFTER `id`;

-- 扩展商品sku表 推广费金额、推广费占比
ALTER TABLE product_sku ADD COLUMN promotion_expenses int NULL COMMENT '推广费（以分为单位）';
ALTER TABLE product_sku ADD COLUMN promotion_expenses_proportion int NULL COMMENT '推广费占比，推广费占比=推广费/销售价*100';

-- 扩展商户表，增加“区域”字段，对应页面是省、市、区 级联菜单
ALTER TABLE `sale_merchant` ADD COLUMN `area_id` bigint NULL DEFAULT NULL COMMENT '区域id，对应文件系统 src/main/resources/area.csv 中的id一列';


-- 扩展商户表，增加“区域”字段，对应页面是省、市、区 级联菜单
ALTER TABLE `system_users` ADD COLUMN `area_id` bigint NULL DEFAULT NULL COMMENT '区域id，对应文件系统 src/main/resources/area.csv 中的id一列';
ALTER TABLE `sale_shop` ADD COLUMN `website` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '店铺域名' AFTER `deleted`;

ALTER TABLE `sale_merchant` MODIFY COLUMN `website` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '域名' AFTER `contact_number`;

ALTER TABLE `sale_merchant` ADD COLUMN `business_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '商户官网' AFTER `website`;

-- 2024 04 01 给商户模板diy页面以及模板页面添加商户ID以及店铺ID

ALTER TABLE `promotion_diy_template` ADD COLUMN `merchant_id` bigint(0) NULL DEFAULT NULL COMMENT '商户ID' AFTER `deleted`;

ALTER TABLE `promotion_diy_template` ADD COLUMN `shop_id` bigint(0) NULL DEFAULT NULL COMMENT '店铺ID' AFTER `merchant_id`;

ALTER TABLE `promotion_diy_page` ADD COLUMN `shop_id` bigint(0) NULL DEFAULT NULL COMMENT '店铺ID' AFTER `deleted`;

ALTER TABLE `promotion_diy_page` ADD COLUMN `merchant_id` bigint(0) NULL DEFAULT NULL COMMENT '商户ID' AFTER `shop_id`;



