Преглед на файлове

feat: 酒店优惠策略制定查找匹配

YPLee преди 6 години
родител
ревизия
4f56abffa9

+ 31 - 0
src/main/java/com/example/hotel/bl/coupon/CouponService.java

@@ -0,0 +1,31 @@
+package com.example.hotel.bl.coupon;
+
+import com.example.hotel.po.Coupon;
+import com.example.hotel.vo.CouponVO;
+import com.example.hotel.vo.HotelTargetMoneyCouponVO;
+import com.example.hotel.vo.OrderVO;
+
+import java.util.List;
+
+public interface CouponService {
+    /**
+     * 返回某一订单可用的优惠策略列表
+     * @param orderVO
+     * @return
+     */
+    List<Coupon> matchOrderCoupon(OrderVO orderVO);
+
+    /**
+     * 查看某个酒店提供的所有优惠策略(包括失效的)
+     * @param hotelId
+     * @return
+     */
+    List<Coupon> getHotelAllCoupon(Integer hotelId);
+
+    /**
+     * 添加酒店满减优惠策略
+     * @param couponVO
+     * @return
+     */
+    CouponVO addHotelTargetMoneyCoupon(HotelTargetMoneyCouponVO couponVO);
+}

+ 11 - 0
src/main/java/com/example/hotel/bl/coupon/CouponStrategy.java

@@ -0,0 +1,11 @@
+package com.example.hotel.bl.coupon;
+
+import com.example.hotel.po.Coupon;
+
+import com.example.hotel.vo.OrderVO;
+
+public interface CouponStrategy {
+
+    boolean isMatchCoupon(OrderVO orderVO, Coupon coupon);
+
+}

+ 5 - 0
src/main/java/com/example/hotel/bl/hotel/HotelService.java

@@ -2,6 +2,7 @@ package com.example.hotel.bl.hotel;
 
 import com.example.hotel.po.HotelRoom;
 import com.example.hotel.util.ServiceException;
+import com.example.hotel.vo.CouponVO;
 import com.example.hotel.vo.HotelVO;
 
 import java.util.List;
@@ -45,4 +46,8 @@ public interface HotelService {
      */
     int getRoomCurNum(Integer hotelId,String roomType);
 
+
+
+
+
 }

+ 1 - 0
src/main/java/com/example/hotel/bl/order/OrderService.java

@@ -1,6 +1,7 @@
 package com.example.hotel.bl.order;
 
 import com.example.hotel.po.Order;
+import com.example.hotel.vo.CouponVO;
 import com.example.hotel.vo.OrderVO;
 import com.example.hotel.vo.ResponseVO;
 

+ 79 - 0
src/main/java/com/example/hotel/blImpl/coupon/CouponServiceImpl.java

@@ -0,0 +1,79 @@
+package com.example.hotel.blImpl.coupon;
+
+import com.example.hotel.bl.coupon.CouponService;
+import com.example.hotel.bl.coupon.CouponStrategy;
+import com.example.hotel.data.coupon.CouponMapper;
+import com.example.hotel.po.Coupon;
+import com.example.hotel.vo.CouponVO;
+import com.example.hotel.vo.HotelTargetMoneyCouponVO;
+import com.example.hotel.vo.OrderVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+@Service
+public class CouponServiceImpl implements CouponService {
+
+
+    private final  TargetMoneyCouponStrategyImpl targetMoneyCouponStrategy;
+
+    private final  TimeCouponStrategyImpl timeCouponStrategy;
+    private final CouponMapper couponMapper;
+
+    private static List<CouponStrategy> strategyList = new ArrayList<>();
+
+    @Autowired
+    public CouponServiceImpl(TargetMoneyCouponStrategyImpl targetMoneyCouponStrategy,
+                             TimeCouponStrategyImpl timeCouponStrategy,
+                             CouponMapper couponMapper) {
+        this.couponMapper = couponMapper;
+        this.targetMoneyCouponStrategy = targetMoneyCouponStrategy;
+        this.timeCouponStrategy = timeCouponStrategy;
+        strategyList.add(targetMoneyCouponStrategy);
+        strategyList.add(timeCouponStrategy);
+    }
+
+
+
+    @Override
+    public List<Coupon> matchOrderCoupon(OrderVO orderVO) {
+
+        List<Coupon> hotelCoupons = getHotelAllCoupon(orderVO.getHotelId());
+
+        List<Coupon> availAbleCoupons = new ArrayList<>();
+
+        for (int i = 0; i < hotelCoupons.size(); i++) {
+            for (CouponStrategy strategy : strategyList) {
+                if (strategy.isMatchCoupon(orderVO, hotelCoupons.get(i))) {
+                    availAbleCoupons.add(hotelCoupons.get(i));
+                }
+            }
+        }
+
+        return availAbleCoupons;
+    }
+
+    @Override
+    public List<Coupon> getHotelAllCoupon(Integer hotelId) {
+        List<Coupon> hotelCoupons = couponMapper.selectByHotelId(hotelId);
+        return hotelCoupons;
+    }
+
+    @Override
+    public CouponVO addHotelTargetMoneyCoupon(HotelTargetMoneyCouponVO couponVO) {
+        Coupon coupon = new Coupon();
+        coupon.setCouponName(couponVO.getName());
+        coupon.setDescription(couponVO.getDescription());
+        coupon.setCouponType(couponVO.getType());
+        coupon.setTargetMoney(couponVO.getTargetMoney());
+        coupon.setHotelId(couponVO.getHotelId());
+        coupon.setDiscountMoney(couponVO.getDiscountMoney());
+        coupon.setStatus(1);
+        int result = couponMapper.insertCoupon(coupon);
+        couponVO.setId(result);
+        return couponVO;
+    }
+}

+ 29 - 0
src/main/java/com/example/hotel/blImpl/coupon/TargetMoneyCouponStrategyImpl.java

@@ -0,0 +1,29 @@
+package com.example.hotel.blImpl.coupon;
+
+import com.example.hotel.bl.coupon.CouponStrategy;
+import com.example.hotel.po.Coupon;
+import com.example.hotel.vo.CouponVO;
+import com.example.hotel.vo.HotelTargetMoneyCouponVO;
+import com.example.hotel.vo.OrderVO;
+import org.springframework.stereotype.Service;
+
+@Service
+public class TargetMoneyCouponStrategyImpl implements CouponStrategy {
+
+
+    /**
+     * 判断某个订单是否满足某种满减金额优惠策略
+     * @param orderVO
+     * @param coupon
+     * @return
+     */
+    @Override
+    public boolean isMatchCoupon(OrderVO orderVO, Coupon coupon) {
+
+        if (coupon.getCouponType() == 3 && orderVO.getPrice() >= coupon.getTargetMoney()) {
+            return true;
+        }
+
+        return false;
+    }
+}

+ 21 - 0
src/main/java/com/example/hotel/blImpl/coupon/TimeCouponStrategyImpl.java

@@ -0,0 +1,21 @@
+package com.example.hotel.blImpl.coupon;
+
+import com.example.hotel.bl.coupon.CouponStrategy;
+import com.example.hotel.po.Coupon;
+import com.example.hotel.vo.OrderVO;
+import org.springframework.stereotype.Service;
+
+@Service
+public class TimeCouponStrategyImpl implements CouponStrategy {
+
+
+    /**
+     * 判断某个订单是否满足某种限时优惠策略
+     * @param orderVO
+     * @return
+     */
+    @Override
+    public boolean isMatchCoupon(OrderVO orderVO, Coupon coupon) {
+        return false;
+    }
+}

+ 1 - 0
src/main/java/com/example/hotel/blImpl/hotel/HotelServiceImpl.java

@@ -13,6 +13,7 @@ import com.example.hotel.po.Hotel;
 import com.example.hotel.po.HotelRoom;
 import com.example.hotel.po.User;
 import com.example.hotel.util.ServiceException;
+import com.example.hotel.vo.CouponVO;
 import com.example.hotel.vo.HotelVO;
 import com.example.hotel.vo.RoomVO;
 import org.springframework.beans.factory.annotation.Autowired;

+ 50 - 0
src/main/java/com/example/hotel/controller/coupon/CouponController.java

@@ -0,0 +1,50 @@
+package com.example.hotel.controller.coupon;
+
+import com.example.hotel.bl.coupon.CouponService;
+import com.example.hotel.vo.CouponVO;
+import com.example.hotel.vo.HotelTargetMoneyCouponVO;
+import com.example.hotel.vo.OrderVO;
+import com.example.hotel.vo.ResponseVO;
+import com.sun.tools.corba.se.idl.constExpr.Or;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+@RestController
+@RequestMapping("/api/coupon")
+public class CouponController {
+
+    @Autowired
+    private CouponService couponService;
+
+    @PostMapping("/hotelTargetMoney")
+    public ResponseVO addHotelTargetMoneyCoupon(@RequestBody HotelTargetMoneyCouponVO hotelTargetMoneyCouponVO) {
+
+        CouponVO couponVO = couponService.addHotelTargetMoneyCoupon(hotelTargetMoneyCouponVO);
+
+        return ResponseVO.buildSuccess(couponVO);
+    }
+
+    @GetMapping("/hotelAllCoupons")
+    public ResponseVO getHotelAllCoupons(@RequestParam Integer hotelId) {
+        return ResponseVO.buildSuccess(couponService.getHotelAllCoupon(hotelId));
+    }
+
+    @GetMapping("/orderMatchCoupons")
+    public ResponseVO getOrderMatchCoupons(@RequestParam Integer userId,
+                                           @RequestParam Integer hotelId,
+                                           @RequestParam Double orderPrice,
+                                           @RequestParam Integer roomNum,
+                                           @RequestParam String checkIn,
+                                           @RequestParam String checkOut) {
+        OrderVO requestOrderVO = new OrderVO();
+        requestOrderVO.setUserId(userId);
+        requestOrderVO.setHotelId(hotelId);
+        requestOrderVO.setPrice(orderPrice);
+        requestOrderVO.setRoomNum(roomNum);
+        requestOrderVO.setCheckInDate(checkIn);
+        requestOrderVO.setCheckOutDate(checkOut);
+        return ResponseVO.buildSuccess(couponService.matchOrderCoupon(requestOrderVO));
+    }
+
+
+}

+ 15 - 0
src/main/java/com/example/hotel/data/coupon/CouponMapper.java

@@ -0,0 +1,15 @@
+package com.example.hotel.data.coupon;
+
+import com.example.hotel.po.Coupon;
+import org.apache.ibatis.annotations.Mapper;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Mapper
+@Repository
+public interface CouponMapper {
+    int insertCoupon(Coupon coupon);
+
+    List<Coupon> selectByHotelId(Integer hotelId);
+}

+ 75 - 25
src/main/java/com/example/hotel/po/Coupon.java

@@ -1,10 +1,8 @@
 package com.example.hotel.po;
-
 import java.sql.Timestamp;
+import java.time.LocalDateTime;
+
 
-/**
- * Created by liying on 2019/4/16.
- */
 public class Coupon {
     /**
      * 优惠券id
@@ -14,40 +12,60 @@ public class Coupon {
      * 优惠券描述
      */
     private String description;
+
+    /**
+     * 如果为-1 代表是网站推出的优惠
+     */
+    private Integer hotelId;
+
+    /**
+     * 优惠券类型 1生日特惠 2多间特惠 3满减优惠 4限时优惠
+     */
+    private Integer couponType;
     /**
      * 优惠券名称
      */
-    private String name;
+    private String couponName;
     /**
      * 优惠券使用门槛
      */
-    private double targetAmount;
+    private double targetMoney;
+
+    /**
+     * 折扣
+     */
+    private double discount;
     /**
      * 优惠券优惠金额
      */
-    private double discountAmount;
+    private double discountMoney;
     /**
      * 可用时间
      */
-    private Timestamp startTime;
+    private LocalDateTime startTime;
     /**
      * 失效时间
      */
-    private Timestamp endTime;
+    private LocalDateTime endTime;
 
-    public Timestamp getStartTime() {
+    /**
+     * 优惠券状态 是否已经失效 1可用 0失效
+     */
+    private Integer status;
+
+    public LocalDateTime getStartTime() {
         return startTime;
     }
 
-    public void setStartTime(Timestamp startTime) {
+    public void setStartTime(LocalDateTime startTime) {
         this.startTime = startTime;
     }
 
-    public Timestamp getEndTime() {
+    public LocalDateTime getEndTime() {
         return endTime;
     }
 
-    public void setEndTime(Timestamp endTime) {
+    public void setEndTime(LocalDateTime endTime) {
         this.endTime = endTime;
     }
 
@@ -67,28 +85,60 @@ public class Coupon {
         this.description = description;
     }
 
-    public String getName() {
-        return name;
+    public String getCouponName() {
+        return couponName;
+    }
+
+    public void setCouponName(String couponName) {
+        this.couponName = couponName;
+    }
+
+    public double getTargetMoney() {
+        return targetMoney;
+    }
+
+    public void setTargetMoney(double targetMoney) {
+        this.targetMoney = targetMoney;
+    }
+
+    public double getDiscountMoney() {
+        return discountMoney;
+    }
+
+    public void setDiscountMoney(double discountMoney) {
+        this.discountMoney = discountMoney;
+    }
+
+    public Integer getHotelId() {
+        return hotelId;
+    }
+
+    public void setHotelId(Integer hotelId) {
+        this.hotelId = hotelId;
+    }
+
+    public Integer getCouponType() {
+        return couponType;
     }
 
-    public void setName(String name) {
-        this.name = name;
+    public void setCouponType(Integer couponType) {
+        this.couponType = couponType;
     }
 
-    public double getTargetAmount() {
-        return targetAmount;
+    public Integer getStatus() {
+        return status;
     }
 
-    public void setTargetAmount(double targetAmount) {
-        this.targetAmount = targetAmount;
+    public void setStatus(Integer status) {
+        this.status = status;
     }
 
-    public double getDiscountAmount() {
-        return discountAmount;
+    public double getDiscount() {
+        return discount;
     }
 
-    public void setDiscountAmount(double discountAmount) {
-        this.discountAmount = discountAmount;
+    public void setDiscount(double discount) {
+        this.discount = discount;
     }
 
     public Coupon() {

+ 49 - 0
src/main/java/com/example/hotel/vo/CouponVO.java

@@ -0,0 +1,49 @@
+package com.example.hotel.vo;
+
+public class CouponVO {
+    private Integer id;
+    private String description;
+    private Integer status;
+    private String name;
+    private Integer type;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public Integer getStatus() {
+        return status;
+    }
+
+    public void setStatus(Integer status) {
+        this.status = status;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getType() {
+        return type;
+    }
+
+    public void setType(Integer type) {
+        this.type = type;
+    }
+}

+ 38 - 0
src/main/java/com/example/hotel/vo/HotelTargetMoneyCouponVO.java

@@ -0,0 +1,38 @@
+package com.example.hotel.vo;
+
+/**
+ * 酒店满减金额优惠 eg 满300减100
+ */
+public class HotelTargetMoneyCouponVO extends CouponVO {
+
+    private Integer hotelId;
+
+    private Integer targetMoney;
+
+    private Integer discountMoney;
+
+    public Integer getHotelId() {
+        return hotelId;
+    }
+
+    public void setHotelId(Integer hotelId) {
+        this.hotelId = hotelId;
+    }
+
+    public Integer getTargetMoney() {
+        return targetMoney;
+    }
+
+    public void setTargetMoney(Integer targetMoney) {
+        this.targetMoney = targetMoney;
+    }
+
+    public Integer getDiscountMoney() {
+        return discountMoney;
+    }
+
+    public void setDiscountMoney(Integer discountMoney) {
+        this.discountMoney = discountMoney;
+    }
+
+}

+ 45 - 0
src/main/java/com/example/hotel/vo/TimeCouponVO.java

@@ -0,0 +1,45 @@
+package com.example.hotel.vo;
+
+import java.time.LocalDateTime;
+
+public class TimeCouponVO extends CouponVO {
+    private LocalDateTime startTime;
+    private LocalDateTime endTime;
+
+    //自行决定是打折还是直接减金额 如果选择打折就把targetMoney变为负数 否则就把discount变为负数
+
+    private double discount;
+    private Integer targetMoney;
+
+    public LocalDateTime getEndTime() {
+        return endTime;
+    }
+
+    public void setEndTime(LocalDateTime endTime) {
+        this.endTime = endTime;
+    }
+
+    public LocalDateTime getStartTime() {
+        return startTime;
+    }
+
+    public void setStartTime(LocalDateTime startTime) {
+        this.startTime = startTime;
+    }
+
+    public double getDiscount() {
+        return discount;
+    }
+
+    public void setDiscount(double discount) {
+        this.discount = discount;
+    }
+
+    public Integer getTargetMoney() {
+        return targetMoney;
+    }
+
+    public void setTargetMoney(Integer targetMoney) {
+        this.targetMoney = targetMoney;
+    }
+}

+ 29 - 0
src/main/resources/dataImpl/coupon/CouponMapper.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.example.hotel.data.coupon.CouponMapper">
+
+    <insert id="insertCoupon" parameterType="com.example.hotel.po.Coupon"
+            useGeneratedKeys="true" keyProperty="id">
+        insert into coupon(description,couponName,target_money,discount_money,start_time,end_time,hotelId,couponType,discount,status)
+        values(#{description},#{couponName},#{targetMoney},#{discountMoney},#{startTime},#{endTime},#{hotelId},#{couponType},#{discount},#{status})
+    </insert>
+
+    <select id="selectByHotelId" resultMap="Coupon">
+        select * from Coupon where hotelId=#{hotelId}
+    </select>
+
+
+    <resultMap id="Coupon" type="com.example.hotel.po.Coupon">
+        <result column="description" property="description"></result>
+        <result column="id" property="id"></result>
+        <result column="couponName" property="couponName"></result>
+        <result column="hotelId" property="hotelId"></result>
+        <result column="couponType" property="couponType"></result>
+        <result column="discount" property="discount"></result>
+        <result column="status" property="status"></result>
+        <result column="target_money" property="targetMoney"></result>
+        <result column="discount_money" property="discountMoney"></result>
+        <result column="start_time" property="startTime"></result>
+        <result column="end_time" property="endTime"></result>
+    </resultMap>
+</mapper>