CREATE TABLE `la_distributor`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `name` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称',
  `dist_num` varchar(24) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '分销编号',
  `contact` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '联系方式',
  `valid_start_time` int(10) UNSIGNED NOT NULL COMMENT '分销码有效期开始时间',
  `valid_end_time` int(10) UNSIGNED NOT NULL COMMENT '分销码有效期结束时间',
  `binding_time` int(5) UNSIGNED NOT NULL DEFAULT 0 COMMENT '绑定有效时间：单位为天',
  `group_id` int(11) NOT NULL COMMENT '组别id',
  `status` tinyint(1) UNSIGNED NOT NULL COMMENT '状态:1-启用;0-停用;',
  `update_time` int(10) UNSIGNED NOT NULL COMMENT '更新时间',
  `is_delete` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除:1-是;0-否',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

alter table la_order Add column distributor_id int(10) NOT NULL DEFAULT 0 COMMENT '分销商主键id';

alter table la_user Add column distributor_id int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '分销商主键id';
alter table la_user Add column bind_time int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '分销商绑定时间';

INSERT INTO `la_crontab`(`id`, `name`, `types`, `command`, `rules`, `remark`, `error`, `status`, `strategy`, `concurrent`, `is_delete`, `start_time`, `end_time`, `task_time`, `task_max_time`, `create_time`, `update_time`, `delete_time`) VALUES (13, '检查用户分销绑定', 'default', 'myJob.checkUserBindingTime()', '0 0 0 * * ?', '', '', 1, 1, 0, 0, 1691631720, 1691631720, 7, 0, 1691629897, 1691631737, 0);

CREATE TABLE `la_distribute_group` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `group_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '组别名称',
  `remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '分销商组别表' ROW_FORMAT = Dynamic;

INSERT INTO `la_system_auth_menu`(`id`, `pid`, `menu_type`, `menu_name`, `menu_icon`, `menu_sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`) VALUES (782, 0, 'M', '分销管理', 'el-icon-Aim', 1, '', 'distributor', '', '', '', 1, 1, 0, 1689920771, 1689926131);
INSERT INTO `la_system_auth_menu`(`id`, `pid`, `menu_type`, `menu_name`, `menu_icon`, `menu_sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`) VALUES (783, 782, 'C', '分销商', '', 0, '', 'index', 'distributor/index', '', '', 1, 1, 0, 1689920862, 1691459376);
INSERT INTO `la_system_auth_menu`(`id`, `pid`, `menu_type`, `menu_name`, `menu_icon`, `menu_sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`) VALUES (784, 782, 'C', '分销组别管理', '', 0, '', 'distributor_group/index', 'distributor_group/index', '', '', 1, 1, 0, 1693390769, 1693391490);
INSERT INTO `la_system_auth_menu`(`id`, `pid`, `menu_type`, `menu_name`, `menu_icon`, `menu_sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`) VALUES (785, 784, 'A', '删除', '', 0, 'system:distributorGroup:del', '', '', '', '', 1, 1, 0, 1693448765, 1693448765);

#分销商佣金
DROP TABLE IF EXISTS `la_distributor_commission`;
CREATE TABLE `la_distributor_commission`  (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
`distributor_id` bigint NOT NULL COMMENT '分销商id',
`order_id` bigint NOT NULL COMMENT '订单id',
`commission_rate` decimal(10, 2) NOT NULL COMMENT '服务抽佣比',
`commission` decimal(10, 2) NOT NULL COMMENT '佣金',
`create_time` datetime NOT NULL COMMENT '创建时间',
`status` int NOT NULL COMMENT '佣金状态：0-冻结 1-可提现',
`is_delete` int NOT NULL COMMENT '是否删除：0-未删除 1已删除',
PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '分销商佣金表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `la_distributor_withdraw`;
CREATE TABLE `la_distributor_withdraw`  (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
`distributor_id` bigint NOT NULL COMMENT '分销商id',
`transaction_id` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方平台交易流水号',
`withdraw_money` decimal(10, 2) NOT NULL COMMENT '提现金额',
`create_time` datetime NOT NULL COMMENT '创建时间',
`status` int NOT NULL COMMENT '提现状态：0-申请中 1-已结算 2-拒绝',
`is_delete` int NOT NULL COMMENT '是否删除：0-未删除 1已删除',
PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '分销商提现表' ROW_FORMAT = Dynamic;

ALTER TABLE `la_distributor`
    ADD COLUMN `user_id` bigint NOT NULL COMMENT '用户id' AFTER `is_delete`,
ADD COLUMN `commission_rate` decimal(10, 2) NOT NULL COMMENT '抽佣比例' AFTER `user_id`,
ADD COLUMN `can_withdraw_commission` decimal(10, 2) NOT NULL COMMENT '可提现佣金' AFTER `commission_rate`,
ADD COLUMN `already_withdraw` decimal(10, 2) NOT NULL COMMENT '已提现佣金' AFTER `can_withdraw_commission`;

ALTER TABLE `la_goods`
    ADD COLUMN `commission_rate` decimal(10, 2) NOT NULL COMMENT '抽佣比例' AFTER `is_delete`;

ALTER TABLE `la_distributor`
    ADD COLUMN `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间' AFTER `status`;

#服务时间
DROP TABLE IF EXISTS `la_goods_time`;
CREATE TABLE `la_goods_time`
(
    `id`            int NOT NULL AUTO_INCREMENT COMMENT ' id',
    `name`          varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '名称',
    `type`          tinyint(1) NOT NULL DEFAULT 0 COMMENT '模板类型 0-自定义 1-自动间隔',
    `time`          int NULL DEFAULT NULL COMMENT '提前预约时间',
    `start_time`    varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '开始时间',
    `end_time`      varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '结束时间',
    `service_time`  int NULL DEFAULT NULL COMMENT '服务时间（分钟）',
    `interval_time` int NULL DEFAULT NULL COMMENT '间隔时间（分钟）',
    `status`        tinyint(1) NOT NULL DEFAULT 0 COMMENT '状态 0-开启 1-关闭',
    `create_time`   datetime                                                     NOT NULL COMMENT '创建时间',
    `is_delete`     tinyint(1) NULL DEFAULT 0 COMMENT '是否删除 0-未删除 1-已删除',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci COMMENT = '服务时间表' ROW_FORMAT = Dynamic;

ALTER TABLE `la_order_time`
    MODIFY COLUMN `start_time` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '开始时间' AFTER `goods_time_id`,
    MODIFY COLUMN `end_time` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '结束时间' AFTER `start_time`;

ALTER TABLE `la_distributor_commission`
    CHANGE COLUMN `commission_rate` `goods_commission_rate` decimal(10, 2) NOT NULL COMMENT '服务抽佣比' AFTER `order_id`,
    ADD COLUMN `distributor_commission_rate` decimal(10, 2) NOT NULL COMMENT '分销商抽佣比' AFTER `goods_commission_rate`;

ALTER TABLE `la_goods`
    ADD COLUMN `goods_time_id` int NOT NULL COMMENT '服务时间id' AFTER `goods_commission_rate`;

ALTER TABLE `la_order_time`
    ADD COLUMN `goods_time_id` int NULL DEFAULT NULL COMMENT '服务时间id' AFTER `id`;

#分销商佣金
ALTER TABLE `la_order_goods`
ADD COLUMN `distributor_commission_rate` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '分销商抽佣比例' AFTER `goods_snap`,
ADD COLUMN `goods_commission_rate` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '服务抽佣比例' AFTER `distributor_commission_rate`;
