|
|
@@ -4,16 +4,11 @@ import com.example.cinema.bl.VIPService;
|
|
|
import com.example.cinema.data.VIPCardMapper;
|
|
|
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.
|
|
|
@@ -29,7 +24,8 @@ public class VIPServiceImpl implements VIPService {
|
|
|
vipCard.setUserId(userId);
|
|
|
vipCard.setBalance(0);
|
|
|
try {
|
|
|
- return ResponseVO.buildSuccess(vipCardMapper.insertOneCard(vipCard));
|
|
|
+ int id = vipCardMapper.insertOneCard(vipCard);
|
|
|
+ return ResponseVO.buildSuccess(vipCardMapper.selectCardById(id));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseVO.buildFailure("失败");
|
|
|
@@ -48,42 +44,38 @@ public class VIPServiceImpl implements VIPService {
|
|
|
|
|
|
@Override
|
|
|
public ResponseVO getVIPInfo() {
|
|
|
- List<ChargeStrategy> strategies = new ArrayList<>();
|
|
|
- strategies.add(new ReturnStrategy());
|
|
|
VIPInfoVO vipInfoVO = new VIPInfoVO();
|
|
|
- vipInfoVO.setStrategies(strategies);
|
|
|
+ vipInfoVO.setDescription(VIPCard.description);
|
|
|
vipInfoVO.setPrice(VIPCard.price);
|
|
|
return ResponseVO.buildSuccess(vipInfoVO);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ResponseVO charge(VIPCardForm vipCardForm) {
|
|
|
- if (vipCardForm.getStrategyName() == null) {
|
|
|
- return ResponseVO.buildFailure("充值优惠策略不能为空");
|
|
|
+
|
|
|
+ VIPCard vipCard = vipCardMapper.selectCardById(vipCardForm.getVipId());
|
|
|
+ if (vipCard == null) {
|
|
|
+ return ResponseVO.buildFailure("会员卡不存在");
|
|
|
}
|
|
|
- ChargeStrategy chargeStrategy;
|
|
|
+ double balance = vipCard.calculate(vipCardForm.getAmount());
|
|
|
+ vipCard.setBalance(vipCard.getBalance() + balance);
|
|
|
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("会员卡不存在");
|
|
|
+ vipCardMapper.updateCardBalance(vipCardForm.getVipId(), balance);
|
|
|
+ return ResponseVO.buildSuccess(vipCard);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return ResponseVO.buildFailure("失败");
|
|
|
}
|
|
|
- double balance = chargeStrategy.calculate(vipCardForm.getAmount());
|
|
|
- vipCard.setBalance(vipCard.getBalance()+balance);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseVO getCardByUserId(int userId) {
|
|
|
try {
|
|
|
- return ResponseVO.buildSuccess(vipCardMapper.updateCardBalance(vipCardForm.getVipId(),balance));
|
|
|
+ VIPCard vipCard = vipCardMapper.selectCardByUserId(userId);
|
|
|
+ if(vipCard==null){
|
|
|
+ return ResponseVO.buildFailure("用户卡不存在");
|
|
|
+ }
|
|
|
+ return ResponseVO.buildSuccess(ResponseVO.buildSuccess(vipCard));
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseVO.buildFailure("失败");
|