|
|
@@ -1,13 +1,19 @@
|
|
|
package com.example.cinema.bl;
|
|
|
|
|
|
import com.example.cinema.data.VIPCardMapper;
|
|
|
-import com.example.cinema.form.VIPCardForm;
|
|
|
+import com.example.cinema.vo.VIPCardForm;
|
|
|
import com.example.cinema.po.VIPCard;
|
|
|
import com.example.cinema.strategy.ChargeStrategy;
|
|
|
+import com.example.cinema.strategy.ReturnStrategy;
|
|
|
import com.example.cinema.vo.ResponseVO;
|
|
|
+import com.example.cinema.vo.VIPInfoVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
/**
|
|
|
* Created by liying on 2019/4/14.
|
|
|
*/
|
|
|
@@ -17,27 +23,66 @@ public class VIPServiceImpl implements VIPService {
|
|
|
VIPCardMapper vipCardMapper;
|
|
|
|
|
|
@Override
|
|
|
- public ResponseVO addVIPCard(VIPCardForm vipCardForm, ChargeStrategy chargeStrategy) {
|
|
|
- VIPCard vipCard=new VIPCard();
|
|
|
- vipCard.setUserId(vipCardForm.getUserId());
|
|
|
- if(vipCardForm.getAmount()<VIPCard.price){
|
|
|
- return ResponseVO.buildFailure("充值金额不足,无法完成");
|
|
|
- }
|
|
|
- vipCard.setBalance(chargeStrategy.calculate(vipCardForm.getAmount())-VIPCard.price);
|
|
|
+ public ResponseVO addVIPCard(int userId) {
|
|
|
+ VIPCard vipCard = new VIPCard();
|
|
|
+ vipCard.setUserId(userId);
|
|
|
+ vipCard.setBalance(0);
|
|
|
try {
|
|
|
return ResponseVO.buildSuccess(vipCardMapper.insertOneCard(vipCard));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseVO.buildFailure("失败");
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ResponseVO getCardById(int id) {
|
|
|
- VIPCard vipCard=vipCardMapper.selectCardById(id);
|
|
|
try {
|
|
|
- return ResponseVO.buildSuccess(vipCardMapper.selectCardById(id));
|
|
|
+ return ResponseVO.buildSuccess(vipCardMapper.selectCardById(id));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return ResponseVO.buildFailure("失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseVO getVIPInfo() {
|
|
|
+ List<ChargeStrategy> strategies = new ArrayList<>();
|
|
|
+ strategies.add(new ReturnStrategy());
|
|
|
+ VIPInfoVO vipInfoVO = new VIPInfoVO();
|
|
|
+ vipInfoVO.setStrategies(strategies);
|
|
|
+ vipInfoVO.setPrice(VIPCard.price);
|
|
|
+ return ResponseVO.buildSuccess(vipInfoVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseVO charge(VIPCardForm vipCardForm) {
|
|
|
+ if (vipCardForm.getStrategyName() == null) {
|
|
|
+ return ResponseVO.buildFailure("充值优惠策略不能为空");
|
|
|
+ }
|
|
|
+ ChargeStrategy chargeStrategy;
|
|
|
+ try {
|
|
|
+ Class c = Class.forName(vipCardForm.getStrategyName());
|
|
|
+ //如果是抽象类或接口
|
|
|
+ if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) {
|
|
|
+ return ResponseVO.buildFailure("充值优惠策略不存在");
|
|
|
+ }
|
|
|
+ chargeStrategy = (ChargeStrategy) c.newInstance();
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return ResponseVO.buildFailure("充值优惠策略不存在");
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ chargeStrategy = new ReturnStrategy();
|
|
|
+ } catch (InstantiationException e) {
|
|
|
+ chargeStrategy = new ReturnStrategy();
|
|
|
+ }
|
|
|
+ VIPCard vipCard=vipCardMapper.selectCardById(vipCardForm.getVipId());
|
|
|
+ if(vipCard==null){
|
|
|
+ return ResponseVO.buildFailure("会员卡不存在");
|
|
|
+ }
|
|
|
+ double balance = chargeStrategy.calculate(vipCardForm.getAmount());
|
|
|
+ vipCard.setBalance(vipCard.getBalance()+balance);
|
|
|
+ try {
|
|
|
+ return ResponseVO.buildSuccess(vipCardMapper.updateCardBalance(vipCardForm.getVipId(),balance));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseVO.buildFailure("失败");
|