CREATE TABLE `qimall_addons_business_bonus_level`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '商城ID',
  `level` int(10) NULL DEFAULT 0 COMMENT '等级(权重)，数字越大，等级越高',
  `level_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '等级名称',
  `bonus_rate` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '分红比例，5%，填5',
  `status` tinyint(4) NULL DEFAULT 1 COMMENT '状态：-1删除，0禁用，1启用',
  `created_at` int(11) NULL DEFAULT 0 COMMENT '创建时间',
  `updated_at` int(11) NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `mall_id`(`mall_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '招商员等级' ROW_FORMAT = Dynamic;

CREATE TABLE `qimall_addons_business_bonus_level_info`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '商城ID',
  `business_bonus_level` int(11) NULL DEFAULT 0 COMMENT '招商员等级',
  `benefit_intro` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '福利介绍',
  `status` tinyint(4) NULL DEFAULT 1 COMMENT '状态：-1删除，0禁用，1启用',
  `created_at` int(11) NULL DEFAULT 0 COMMENT '创建时间',
  `updated_at` int(11) NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `mall_id`(`mall_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '招商员等级附加信息表' ROW_FORMAT = Dynamic;

CREATE TABLE `qimall_addons_business_bonus_reward`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'updated_at',
  `mall_id` int(11) NULL DEFAULT 0 COMMENT '商城ID',
  `user_id` int(11) NULL DEFAULT 0 COMMENT '会员ID',
  `order_id` int(11) NULL DEFAULT 0 COMMENT '订单ID',
  `order_no` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '订单号',
  `settle_base_on` tinyint(4) NULL DEFAULT 1 COMMENT '结算依据：1订单金额，2利润金额',
  `settle_amount` decimal(12, 2) NULL DEFAULT 0.00 COMMENT '结算金额',
  `bonus_rate` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '分红比例，5%，填5',
  `bonus_amount` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '分红金额',
  `bonus_status` tinyint(4) NULL DEFAULT 0 COMMENT '结算状态：0待结算，1已结算',
  `bonus_time` int(11) NULL DEFAULT 0 COMMENT '分红时间',
  `settle_time` int(11) NULL DEFAULT 0 COMMENT '结算时间，跑任务用',
  `bonus_type` tinyint(4) NULL DEFAULT 1 COMMENT '分红类型：1招商分红',
  `from_type` tinyint(4) NULL DEFAULT 0 COMMENT '来源类型：1门店，2多商户',
  `store_id` int(11) NULL DEFAULT 0 COMMENT '门店、多商户的ID',
  `status` tinyint(4) NULL DEFAULT 1 COMMENT '状态：-1删除，0禁用，1启用',
  `created_at` int(11) NULL DEFAULT 0 COMMENT '创建时间',
  `updated_at` int(11) NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `mall_id`(`mall_id`) USING BTREE,
  INDEX `user_id`(`user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '招商分红奖励明细表' ROW_FORMAT = Dynamic;

CREATE TABLE `qimall_addons_business_bonus_store`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) NULL DEFAULT 0 COMMENT '商城ID',
  `store_id` int(11) NULL DEFAULT 0 COMMENT '门店、多商户的ID',
  `store_type` tinyint(4) NULL DEFAULT 0 COMMENT '类型：1门店，2多商户',
  `store_user_id` int(11) NULL DEFAULT 0 COMMENT '店主、商户user_id',
  `business_user_id` int(11) NULL DEFAULT 0 COMMENT '招商员user_id',
  `history_income` decimal(12, 2) NULL DEFAULT 0.00 COMMENT '累计收入',
  `history_earn` decimal(12, 2) NULL DEFAULT 0.00 COMMENT '累计利润',
  `status` tinyint(4) NULL DEFAULT 1 COMMENT '状态：-1删除，0禁用，1启用',
  `store_created_at` int(11) NULL DEFAULT 0 COMMENT '店铺创建时间',
  `created_at` int(11) NULL DEFAULT 0 COMMENT '创建时间',
  `updated_at` int(11) NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `mall_id`(`mall_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '门店、多商户列表' ROW_FORMAT = Dynamic;

CREATE TABLE `qimall_addons_business_bonus_upgrade_apply`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) NULL DEFAULT 0 COMMENT '商城ID',
  `user_id` int(11) NULL DEFAULT 0 COMMENT '申请人会员ID',
  `business_bonus_level` int(11) NULL DEFAULT 0 COMMENT '申请的招商员等级',
  `audit_status` tinyint(4) NULL DEFAULT 0 COMMENT '审核状态：0待审核，1审核通过，2驳回',
  `audit_time` int(11) NULL DEFAULT 0 COMMENT '审核时间',
  `audit_admin` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '审核的管理员的用户名',
  `apply_time` int(11) NULL DEFAULT 0 COMMENT '申请时间',
  `faid_reason` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '驳回理由',
  `status` tinyint(4) NULL DEFAULT 1 COMMENT '状态：-1删除，0禁用，1启用',
  `created_at` int(11) NULL DEFAULT 0 COMMENT '创建时间',
  `updated_at` int(11) NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `mall_id`(`mall_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '招商员升级申请' ROW_FORMAT = Dynamic;

CREATE TABLE `qimall_addons_business_bonus_upgrade_condition`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) NULL DEFAULT 0 COMMENT '商城ID',
  `business_bonus_level` int(11) NULL DEFAULT 0 COMMENT '招商员等级',
  `upgrade_type` tinyint(4) NULL DEFAULT 0 COMMENT '升级方式：0未知，1无条件，2前端申请，3购买指定商品，4后台添加，5后台调整',
  `goods_id` tinytext CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '指定的商品ID，多个用英文逗号隔开',
  `goods_buy_number` int(11) NULL DEFAULT 1 COMMENT '购买商品数量：0全部商品，1任一商品，2其二商品, 以此类推',
  `is_audit` tinyint(4) NULL DEFAULT 1 COMMENT '前端申请时，是否需要审核，0否，1是',
  `status` tinyint(4) NULL DEFAULT 1 COMMENT '状态：-1删除，0禁用，1启用',
  `created_at` int(11) NULL DEFAULT 0 COMMENT '创建时间',
  `updated_at` int(11) NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `mall_id`(`mall_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '招商员升级条件' ROW_FORMAT = Dynamic;

CREATE TABLE `qimall_addons_business_bonus_upgrade_record`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '商城ID',
  `user_id` int(11) NULL DEFAULT 0 COMMENT '会员ID',
  `business_bonus_level` int(11) NULL DEFAULT 0 COMMENT '招商分红等级',
  `bonus_rate` decimal(12, 2) NULL DEFAULT 0.00 COMMENT '升级后的分红比例，仅记录作用',
  `upgrade_type` tinyint(4) NULL DEFAULT 0 COMMENT '升级方式：0未知，1无条件，2前端申请，3购买指定商品，4后台添加，5后台调整',
  `upgrade_time` int(11) NULL DEFAULT 0 COMMENT '升级时间',
  `extra_data` text COMMENT '额外信息，json格式，根据不同升级方式保存不同信息',
  `status` int(11) NULL DEFAULT 1 COMMENT '状态：-1删除，0禁用，1启用',
  `created_at` int(11) NULL DEFAULT 0 COMMENT '创建时间',
  `updated_at` int(11) NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `mall_id`(`mall_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '升级记录' ROW_FORMAT = Dynamic;

CREATE TABLE `qimall_addons_business_bonus_user`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `mall_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '商城ID',
  `user_id` int(11) NULL DEFAULT 0 COMMENT '用户ID',
  `business_bonus_level` int(11) NULL DEFAULT 0 COMMENT '招商分红等级',
  `history_bonus_amount` decimal(12, 2) NULL DEFAULT 0.00 COMMENT '历史累计分红',
  `register_time` int(11) NULL DEFAULT 0 COMMENT '成为招商员时间',
  `store_number` int(11) NULL DEFAULT 0 COMMENT '推广的门店数量',
  `mch_number` int(11) NULL DEFAULT 0 COMMENT '推广的供应商数量',
  `status` tinyint(4) NULL DEFAULT 1 COMMENT '状态：-1删除，0禁用，1启用',
  `created_at` int(11) NULL DEFAULT 0 COMMENT '创建时间',
  `updated_at` int(11) NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `mall_id`(`mall_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '招商员列表' ROW_FORMAT = Dynamic;

INSERT INTO `qimall_crontab_task`(`name`, `command`, `type`, `rule`, `is_warning`, `status`, `created_at`, `updated_at`) VALUES ('招商分红：结算分红', 'business-bonus/settle-bonus/send-bonus', 2, '*/3 * * * * *', 0, 1, 1658731380, 1658731380);

