OrderCouponController.java 4.12 KB
package com.yohoufo.promotion.controller;


import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.promotion.UserCouponsListBo;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.promotion.service.ICouponService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 全部是给订单的接口
 */
@RestController
@RequestMapping(value = "/orderCoupon")
public class OrderCouponController {


    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    ICouponService couponService;

    /**
     * 使用优惠券(订单使用)
     * @param uid
     * @param couponCodes
     * @param orderCode
     * @return
     */
    @RequestMapping(params = "method=ufo.coupons.use")
    @ResponseBody
    public ApiResponse useCoupon(@RequestParam(value = "uid") Integer uid,
                                 @RequestParam(value = "orderCode") long orderCode,
                                 @RequestParam(value = "couponCodes") List<String> couponCodes) {

        logger.info("use coupons, uid: {},couponCodes: {}, orderCode :{}", uid, couponCodes, orderCode);
        boolean useSuccess = couponService.useCoupon(uid, couponCodes, orderCode);
        logger.info("use coupons, uid: {},couponCodes: {}, orderCode :{},result: {}", uid, couponCodes, orderCode, useSuccess);
        return new ApiResponse.ApiResponseBuilder().code(200).data(useSuccess).build();
    }


    /**
     * 使用优惠券(订单退还)
     * @param uid
     * @param couponCodes
     * @param orderCode
     * @return
     */
    @RequestMapping(params = "method=ufo.coupons.cancel")
    @ResponseBody
    public ApiResponse cancelCoupon(@RequestParam(value = "uid") Integer uid,
                                    @RequestParam(value = "orderCode") long orderCode,
                                    @RequestParam(value = "couponCodes") List<String> couponCodes) {

        logger.info("cancel coupons, uid: {},couponCodes: {}, orderCode :{}", uid, couponCodes, orderCode);
        boolean cancelSuccess = couponService.cancelCoupon(uid, couponCodes, orderCode);
        logger.info("cancel coupons, uid: {},couponCodes: {}, orderCode :{},result: {}", uid, couponCodes, orderCode, cancelSuccess);
        return new ApiResponse.ApiResponseBuilder().code(200).data(cancelSuccess).build();
    }


    /**
     * 优惠券列表(订单使用)
     * @param uid
     * @return
     */
    @RequestMapping(params = "method=ufo.coupons.listNoUsed")
    @ResponseBody
    public ApiResponse queryUserNoUsedCoupons(@RequestParam(value = "uid") Integer uid) {

        logger.info("use coupons, uid: {}", uid);
        UserCouponsListBo couponBoList = couponService.queryUserNoUsedCoupons(uid);
        logger.info("use coupons, uid: {}, success", uid);
        return new ApiResponse.ApiResponseBuilder().code(200).data(couponBoList).build();
    }


    /**
     * 优惠券列表(订单使用)
     *
     * @param uid
     * @return
     */
    @RequestMapping(params = "method=ufo.coupons.checkAndGet")
    @ResponseBody
    public ApiResponse checkAndGetCoupons(@RequestParam(value = "uid") int uid,
                                          @RequestParam(value = "couponCodes") List<String> couponCodes) {

        logger.info("checkAndGet coupons, uid: {},couponCodes:{}", uid, couponCodes);
        if (uid <= 0 || CollectionUtils.isEmpty(couponCodes)) {
            throw new ServiceException(ServiceError.PROMOTION_REQUEST_PAREMENT_ERROR);
        }
        UserCouponsListBo couponBoList = couponService.checkAndGetCoupons(uid, couponCodes);
        logger.info("checkAndGet coupons, uid: {},couponCodes:{},success", uid, couponCodes);
        return new ApiResponse.ApiResponseBuilder().code(200).data(couponBoList).build();
    }
}