天天看點

電商平台-優惠券設計與架構

說明:現在電商白熱化的程度,無論是生鮮電商還是其他的電商等等,都會有促銷的這個體系,目的就是增加訂單量與知名度等等

           那麼對于Java開源生鮮電商平台而言,我們采用優惠券的這種方式進行促銷。(補貼價格戰對燒錢而言非常的恐怖的,太燒錢了)

1. 優惠券基礎資訊表

說明:任何一個優惠券或者說代金券都是有一個基礎的說明,比如:優惠券名稱,類型,價格,有效期,狀态,說明等等基礎資訊。

CREATE TABLE `coupon` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自動增加ID',
  `region_id` bigint(20) DEFAULT NULL COMMENT '所屬區域',
  `type` int(11) DEFAULT NULL COMMENT '所屬類型,1為滿減',
  `name` varchar(32) DEFAULT NULL COMMENT '優惠券名稱',
  `img` varchar(64) DEFAULT NULL COMMENT '圖檔的URL位址',
  `start_time` datetime DEFAULT NULL COMMENT '優惠券開始時間',
  `end_time` datetime DEFAULT NULL COMMENT '優惠券結束時間',
  `money` decimal(11,2) DEFAULT NULL COMMENT '優惠券金額,用整數,固定值目前。',
  `status` int(11) DEFAULT NULL COMMENT '狀态,0表示未開始,1表示進行中,-1表示結束',
  `remarks` varchar(512) DEFAULT NULL COMMENT '優惠券的說明',
  `create_time` datetime DEFAULT NULL COMMENT '建立時間',
  `full_money` decimal(12,2) DEFAULT NULL COMMENT '金額滿',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='優惠券基礎配置表';      

說明:業務說可以規定某個區域,做優惠券,而且是納新後才有,這樣增加買家使用者。價格可以後端進行設定。

           狀态的意義在于,使用者需要注冊完成後,然後主動認領才有效,為什麼要這樣設計呢?目的隻有一個:讓使用者在對APP這個軟體玩一會兒,增加熟悉程度.

2. 優惠券領取記錄表

說明:我們需要記錄那個買家,什麼時候進行的領取,領取的的時間,券的額度是多少等等,是否已經使用等資訊

CREATE TABLE `coupon_receive` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自動增加ID',
  `buyer_id` bigint(20) DEFAULT NULL COMMENT '買家ID',
  `coupon_id` bigint(20) DEFAULT NULL COMMENT '優惠券編号',
  `coupon_money` decimal(12,2) DEFAULT NULL COMMENT '券額',
  `create_time` datetime DEFAULT NULL COMMENT '領取時間',
  `full_money` decimal(12,2) DEFAULT NULL COMMENT '金額滿',
  `status` int(11) DEFAULT NULL COMMENT '狀态,1為已使用,0為已領取未使用,-1為已過期',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='優惠券領取記錄表';      

3.優惠券消費記錄表

說明:優惠券消費記錄表,是需要知道那個買家,那個優惠券,那個訂單使用了優惠券,這邊有個特别注意的地方是,這個優惠券的執行在支付成功後的回調。

CREATE TABLE `coupon_logs` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自動增加ID',
  `buyer_id` bigint(20) DEFAULT NULL COMMENT '買家ID',
  `coupon_receive_id` bigint(20) DEFAULT NULL COMMENT '優惠券id',
  `order_number` varchar(64) DEFAULT NULL COMMENT '訂單号',
  `order_original_amount` decimal(12,2) DEFAULT NULL COMMENT '原訂單金額',
  `coupon_amount` decimal(11,2) DEFAULT NULL COMMENT '優惠券的金額',
  `order_final_amount` decimal(12,2) DEFAULT NULL COMMENT '抵扣優惠券之後的訂單金額',
  `create_time` datetime DEFAULT NULL COMMENT '領取時間',
  `status` int(2) DEFAULT '0' COMMENT '日志狀态: 預設為0,支付回調成功後為1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='優惠券消費記錄表';      

說明:相對而言,優惠券的難度不算大,重點的是業務方面的指導與學習,包括資料庫的架構與設計等等,還有就是思路的學習。

相關核心代碼如下:

APP需要背景提供以下幾個接口:

3.1 查詢所有買家的優惠券。

3.2 判斷買家是否可以領取優惠券。

3.3 買家主動領取優惠券

/**
 * 優惠券controller
 */
@RestController
@RequestMapping("/buyer/coupon")
public class CouponController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(CouponController.class);

    @Autowired
    private CouponReceiveService couponReceiveService;

    @Autowired
    private UsersService usersService;

    /**
     * 查詢買家所有優惠券
     * 
     * @param request
     * @param response
     * @param buyerId
     * @return
     */
    @RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
    public JsonResult getCouponList(HttpServletRequest request, HttpServletResponse response, Long buyerId) {
        try {
            if (buyerId == null) {
                return new JsonResult(JsonResultCode.FAILURE, "買家不存在", "");
            }
            List<CouponReceive> result = couponReceiveService.selectAllByBuyerId(buyerId);
            return new JsonResult(JsonResultCode.SUCCESS, "查詢成功", result);
        } catch (Exception ex) {
            logger.error("[CouponController][getCouponList] exception", ex);
            return new JsonResult(JsonResultCode.FAILURE, "系統錯誤,請稍後重試", "");
        }
    }

    /**
     * 判斷買家是否可以領取優惠券
     * 
     * @param request
     * @param response
     * @param buyerId
     * @return
     */
    @RequestMapping(value = "/judge", method = { RequestMethod.GET, RequestMethod.POST })
    public JsonResult judgeReceive(HttpServletRequest request, HttpServletResponse response, Long buyerId) {
        try {
            // 判斷目前使用者是否可用
            Users users = usersService.getUsersById(buyerId);
            if (users == null) {
                logger.info("OrderController.judgeReceive.buyerId " + buyerId);
                return new JsonResult(JsonResultCode.FAILURE, "你的賬号有誤,請重新登入", "");
            }
            int status = users.getStatus();
            if (UserStatus.FORBIDDEN == status) {
                return new JsonResult(JsonResultCode.FAILURE, "你的賬号已經被禁用了,請聯系公司客服", "");
            }
            List<Coupon> result = couponReceiveService.selectByBuyerId(buyerId);
            if (CollectionUtils.isEmpty(result)) {
                result = new ArrayList<Coupon>();
            }
            return new JsonResult(JsonResultCode.SUCCESS, "查詢成功", result);
        } catch (Exception ex) {
            logger.error("[CouponController][judgeReceive] exception", ex);
            return new JsonResult(JsonResultCode.FAILURE, "系統錯誤,請稍後重試", "");
        }
    }

    /**
     * 買家領取優惠券
     * 
     * @param request
     * @param response
     * @param buyerId
     * @return
     */
    @RequestMapping(value = "/add", method = { RequestMethod.GET, RequestMethod.POST })
    public JsonResult saveCoupon(HttpServletRequest request, HttpServletResponse response, Long buyerId,
            Long couponId) {
        try {
            // 判斷目前使用者是否可用
            Users users = usersService.getUsersById(buyerId);
            if (users == null) {
                logger.info("OrderController.saveCoupon.buyerId " + buyerId);
                return new JsonResult(JsonResultCode.FAILURE, "你的賬号有誤,請重新登入", "");
            }
            //判斷目前使用者的狀态是否可用
            int status = users.getStatus();
            if (UserStatus.FORBIDDEN == status) {
                return new JsonResult(JsonResultCode.FAILURE, "你的賬号已經被禁用了,請聯系公司客服", "");
            }
            if (couponId == null) {
                return new JsonResult(JsonResultCode.SUCCESS, "活動已經結束", "");
            }
            //新增
            int result = couponReceiveService.insert(buyerId, couponId);
            if (result == -1) {
                return new JsonResult(JsonResultCode.SUCCESS, "領取失敗,已經領取過優惠券了", "");
            } else if (result == 0) {
                return new JsonResult(JsonResultCode.FAILURE, "領取失敗,活動已經結束", "");
            } else {
                return new JsonResult(JsonResultCode.SUCCESS, "領取成功", "");
            }
        } catch (Exception ex) {
            logger.error("[CouponController][saveCoupon] exception", ex);
            return new JsonResult(JsonResultCode.FAILURE, "系統錯誤,請稍後重試", "");
        }
    }
}      

最終總結:使用者優惠券會發放在買家的APP中的個人中心裡面,然後進行點選檢視與領取,然後在支付的時候會自動顯示出優惠券的資料,非常的靈活與友善。

繼續閱讀