DROP TABLE IF EXISTS `qimall_addons_circle_distribution`;
CREATE TABLE `qimall_addons_circle_distribution`  (
                                               `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                                               `mall_id` int(11) UNSIGNED NOT NULL,
                                               `user_id` int(11) UNSIGNED NOT NULL,
                                               `remarks` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '备注',
                                               `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态[-1:删除;0:禁用;1启用]',
                                               `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
                                               `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '修改时间',
                                               `level` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '分销商等级',
                                               `upgrade_level_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '分销商等级升级时间',
                                               `total_commission` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '累计佣金',
                                               `total_score` int(11) NOT NULL DEFAULT 0 COMMENT '累计积分',
                                               `upgrade_status` smallint(6) NOT NULL DEFAULT 0 COMMENT '1条件升级 2 购买指定商品升级 3手动升级',
                                               PRIMARY KEY (`id`) USING BTREE,
                                               INDEX `mall_id`(`mall_id`) USING BTREE,
                                               INDEX `user_id`(`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '分销商信息' ROW_FORMAT = Dynamic;


DROP TABLE IF EXISTS `qimall_addons_circle_distribution_apply`;
CREATE TABLE `qimall_addons_circle_distribution_apply`  (
                                                     `id` int(11) NOT NULL AUTO_INCREMENT,
                                                     `mall_id` int(11) NOT NULL,
                                                     `user_id` int(11) NOT NULL,
                                                     `mobile` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
                                                     `name` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
                                                     `remark` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '申请备注',
                                                     `check_remark` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '审核备注',
                                                     `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态[-1:删除;0:禁用;1启用]',
                                                     `check_status` tinyint(3) NOT NULL DEFAULT 0 COMMENT '0待审核1审核通过2不通过',
                                                     `created_at` int(11) NOT NULL DEFAULT 0,
                                                     `updated_at` int(11) NOT NULL DEFAULT 0,
                                                     PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '分销商申请记录' ROW_FORMAT = Dynamic;


CREATE TABLE `qimall_addons_circle_distribution_commission_basis_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) NOT NULL DEFAULT '0' COMMENT '商城ID',
  `source_table` varchar(255) NOT NULL DEFAULT '0' COMMENT '来源表',
  `source_table_id` int(11) NOT NULL DEFAULT '0' COMMENT '来源表id',
  `type` varchar(255) NOT NULL COMMENT '积分：score；佣金：commission',
  `from_type` varchar(255) NOT NULL DEFAULT '' COMMENT '来源类型',
  `basic_config` text COMMENT '基础配置',
  `reward_config` text COMMENT '等级配置',
  `goods_config` text COMMENT '商品独立配置',
  `relation_ship` varchar(2000) DEFAULT NULL COMMENT '关系链等级',
  `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态[-1:删除;0:禁用;1启用]',
  `created_at` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
  `updated_at` int(11) NOT NULL DEFAULT '0' COMMENT '修改时间',
  `user_id` int(11) NOT NULL DEFAULT '0' COMMENT '发放佣金对应的用户ID ',
  `sequence` int(11) NOT NULL DEFAULT '0' COMMENT '领取奖励的顺序',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_mall_user` (`mall_id`,`user_id`) USING BTREE,
  KEY `idx_source` (`source_table_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='分销商奖励计算依据';


CREATE TABLE `qimall_addons_circle_distribution_commission_item_setting` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) NOT NULL COMMENT '商城ID',
  `is_enable` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否独立设置：0:否;1是',
  `item_id` int(11) NOT NULL DEFAULT '0' COMMENT '项目id',
  `item_type` varchar(255) NOT NULL DEFAULT '0' COMMENT '项目类型：商品goods',
  `settings` tinyint(3) NOT NULL DEFAULT '0' COMMENT '分销佣金设置:0:按商品设置;1:按规格设置',
  `commission_type` varchar(255) NOT NULL DEFAULT '0' COMMENT '分佣类型：积分score;佣金：commission',
  `commission_value` json DEFAULT NULL COMMENT '独立设置内容',
  `is_percent` tinyint(1) NOT NULL DEFAULT '0' COMMENT '类型 0百分比 1 固定金额',
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态[-1:删除;0:禁用;1启用]',
  `created_at` int(11) NOT NULL DEFAULT '0',
  `updated_at` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `mall_id` (`mall_id`) USING BTREE
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='分销商项目分佣设置';

CREATE TABLE `qimall_addons_circle_distribution_goods_setting` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `mall_id` int(10) NOT NULL DEFAULT '0' COMMENT '商城ID',
  `goods_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '商品id',
  `content` text COMMENT '独立配置详情',
  `status` tinyint(4) DEFAULT '1' COMMENT '状态[-1:删除;0:禁用;1启用]',
  `created_at` int(10) unsigned NOT NULL DEFAULT '0',
  `updated_at` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `mall_id+goods_id` (`mall_id`,`goods_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='分销管理-商品单独设置表';


INSERT INTO `qimall_global_var_map` (`name`,`title`,`type`,`is_addons`,`status`,`created_at`,`updated_at`)
VALUES('CircleDistribution',	'分销循环','FromTypeMap','1','1','1666166113','1666166113');

ALTER TABLE `qimall_addons_circle_distribution`
MODIFY COLUMN `total_score`  decimal(12,2) NOT NULL DEFAULT 0 COMMENT '累计积分' AFTER `total_commission`;