DROP TABLE IF EXISTS `la_coupon`;
CREATE TABLE `la_coupon`  (
      `id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
      `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '优惠券名称',
      `money` decimal(10, 2) NOT NULL COMMENT '优惠券面额(元)',
      `get_type` tinyint(1) NULL DEFAULT NULL COMMENT '发放类型：1-用户领取；2-系统赠送；3-渠道领取；4-注册赠券；5-付费会员专享',
      `channel_type` tinyint(1) NULL DEFAULT NULL COMMENT '渠道领取为3时：渠道指定：1-分销员 2-指定用户',
      `use_goods_type` tinyint(1) NULL DEFAULT NULL COMMENT '适用商品类型:1-全部商品；2-品类商品；3-指定商品；',
      `condition_type` tinyint(1) NULL DEFAULT NULL COMMENT '使用条件类型：1-无门槛；2-订单满足金额',
      `condition_money` decimal(10, 2) NULL DEFAULT NULL COMMENT '使用条件类型为2时：该字段为订单满足金额可使用',
      `send_time_start` datetime NULL DEFAULT NULL COMMENT '发放开始时间',
      `send_time_end` datetime NULL DEFAULT NULL COMMENT '发放结束时间',
      `use_time_start` datetime NULL DEFAULT NULL COMMENT '使用开始时间',
      `use_time_end` datetime NULL DEFAULT NULL COMMENT '使用结束时间；',
      `send_total_type` tinyint(1) NULL DEFAULT NULL COMMENT '发送总量类型：1-不限制；2-限制张数',
      `send_total` int NULL DEFAULT NULL COMMENT '发送总量类型为2时：该字段为限制的张数',
      `get_num` int NULL DEFAULT NULL COMMENT '领取限制的数量',
      `status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '优惠券状态：1-生效；2-失效；3-已结束',
      `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '使用规则',
      `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
      `update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
      `is_delete` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否删除；1-是；0-否',
      PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '优惠券表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `la_goods_category_coupon`;
CREATE TABLE `la_goods_category_coupon`  (
     `id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
     `goods_cat_id` int NOT NULL COMMENT '品类id',
     `coupon_id` int NOT NULL COMMENT '优惠券id',
     `create_time` datetime NOT NULL COMMENT '创建时间',
     `is_delete` tinyint NULL DEFAULT 0 COMMENT '是否删除；1-是；0-否',
     PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '品类与优惠券关联表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `la_goods_coupon`;
CREATE TABLE `la_goods_coupon`  (
    `id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `goods_id` int NOT NULL COMMENT '服务主键id',
    `coupon_id` int NOT NULL COMMENT '优惠券主键id',
    `create_time` datetime NOT NULL COMMENT '创建时间',
    `is_delete` tinyint NULL DEFAULT 0 COMMENT '是否删除；1-是；0-否',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '指定商品与优惠券关联表' ROW_FORMAT = DYNAMIC;

DROP TABLE IF EXISTS `la_channel_coupon`;
CREATE TABLE `la_channel_coupon`  (
    `id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `coupon_id` int NOT NULL COMMENT '优惠券主键id',
    `channel_type` tinyint NOT NULL COMMENT '渠道类型：1-指定分销员 2-指定用户',
    `appoint_id` int NOT NULL COMMENT '指定人员id: 分销员或具体用户的id',
    `create_time` datetime NOT NULL COMMENT '创建时间',
    `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '发放状态：1-发放中 2-取消发放',
    `is_delete` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否删除；1-是；0-否',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '渠道与指定分销人员关联表' ROW_FORMAT = DYNAMIC;

DROP TABLE IF EXISTS `la_user_coupon`;
CREATE TABLE `la_user_coupon`  (
    `id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `user_id` int NOT NULL COMMENT '用户主键id',
    `coupon_id` int NOT NULL COMMENT '优惠券主键id',
    `use_status` tinyint NOT NULL DEFAULT 1 COMMENT '使用状态：1未使用、2已使用、3过期',
    `create_time` datetime NOT NULL COMMENT '领取时间',
    `is_delete` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否删除；1-是；0-否',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户与优惠券关联表' ROW_FORMAT = DYNAMIC;

ALTER TABLE `la_order`
ADD COLUMN `user_coupon_id` bigint NULL DEFAULT NULL COMMENT '用户领取优惠券id' AFTER `staff_mobile`,
ADD COLUMN `deduction_money` decimal(10, 2) NULL DEFAULT NULL COMMENT '抵扣金额' AFTER `user_coupon_id`;

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 (20, '发送和停用优惠券', 'default', 'couponCrontab.sendCoupon', '*/10 * * * * ?', '每个小时执行一次发券和停券', '', 1, 1, 1, 0, 1695008480, 1695008480, 4, 82, 1694675840, 1694765251, 0);
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 (21, '删除用户领取过期五天的优惠券', 'default', 'couponCrontab.deleteUserGetCoupon', '0 * * * * ?', '每天12点执行一次', '', 1, 1, 0, 0, 1695008460, 1695008460, 2, 0, 1694676074, 1694676977, 0);