package com.sun.gifthouse.Controller; import com.sun.gifthouse.Utils.GoodsUtil; import com.sun.gifthouse.exception.GlobalException; import com.sun.gifthouse.po.Goods; import com.sun.gifthouse.po.ShoppingInfo; import com.sun.gifthouse.po.User; import com.sun.gifthouse.result.CodeMsg; import com.sun.gifthouse.result.Result; import com.sun.gifthouse.service.GoodsService; import com.sun.gifthouse.service.UserService; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; @Controller public class ApplicationController { @Resource private UserService userService; @Resource private GoodsService goodsService; @RequestMapping("/login") @ResponseBody public Result<Boolean> login(User user){ if (user == null || user.getUsername() == null || user.getUsername().isEmpty() || user.getPassword() == null || user.getPassword().isEmpty()) throw new GlobalException(CodeMsg.NO_USER); userService.login(user); return Result.success(true); } @GetMapping("/getUser") @ResponseBody public Result<User> getUser(String username){ User user = userService.getUser(username); return Result.success(user); } @GetMapping("/getGoods") @ResponseBody public Result<List> getGoods(){ List<Object> list = goodsService.getAllGoods(); LoggerFactory.getLogger("12").info("success"); return Result.success(list); } @GetMapping("/buy") @ResponseBody public Result<Boolean> buy(String username, Integer goodsId){ goodsService.buy(username, goodsId); return Result.success(true); } @GetMapping("/getShops") @ResponseBody public Result<List> getShops(String username){ List<ShoppingInfo> list = goodsService.getShops(username); return Result.success(list); } }
GlobalException
package com.sun.gifthouse.exception; import com.sun.gifthouse.result.CodeMsg; public class GlobalException extends RuntimeException{ private static final long serialVersionUID = 1L; private CodeMsg cm; public GlobalException(CodeMsg cm) { super(cm.toString()); this.cm = cm; } public CodeMsg getCm() { return cm; } public static long getSerialversionuid() { return serialVersionUID; } }
GlobalExceptionHandler
package com.sun.gifthouse.exception; import com.sun.gifthouse.result.CodeMsg; import com.sun.gifthouse.result.Result; import org.springframework.validation.BindException; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.util.List; @ControllerAdvice @ResponseBody public class GlobalExceptionHandler { @ExceptionHandler(value = Exception.class) public Result<String> exceptionHandler(HttpServletRequest request, Exception e){ e.printStackTrace(); if(e instanceof GlobalException) { GlobalException ex = (GlobalException)e; return Result.error(ex.getCm()); } if(e instanceof BindException) { BindException ex = (BindException)e; List<ObjectError> errors = ex.getAllErrors(); ObjectError error = errors.get(0); String msg = error.getDefaultMessage(); return Result.error(CodeMsg.BIND_ERROR.fillArgs(msg)); }else { return Result.error(CodeMsg.SERVER_ERROR); } } }
Goods
package com.sun.gifthouse.po; import lombok.Data; @Data public class Goods { private Integer id; private String discription; private Integer number; private Integer points; public Goods(Integer id, String discription, Integer number, Integer points) { this.id = id; this.discription = discription; this.number = number; this.points = points; } }
ShoppingInfo
package com.sun.gifthouse.po; import lombok.Data; @Data public class ShoppingInfo { private Integer id; private Integer goodsId; private String discription; private Integer number; private Integer points; private String username; public ShoppingInfo(Integer id, Integer goodsId, String discription, Integer number, Integer points, String username) { this.id = id; this.goodsId = goodsId; this.discription = discription; this.number = number; this.points = points; this.username = username; } }
User
package com.sun.gifthouse.po; import lombok.Data; @Data public class User { private String username; private String password; private Integer points; public User(String username, String password, Integer points) { this.username = username; this.password = password; this.points = points; } }
CodeMsg
package com.sun.gifthouse.result; public class CodeMsg { private int code; private String msg; //通用异常 5001XX public static CodeMsg SUCCESS = new CodeMsg(0, "success"); public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务器异常"); public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s"); //User模块 5002XX public static CodeMsg NO_USER = new CodeMsg(500201,"账号不存在"); public static CodeMsg NO_USERNAME = new CodeMsg(500202, "该用户名不存在"); public static CodeMsg ERROR_PASSWORD = new CodeMsg(500203,"密码错误"); //Shop模块 5003XX public static CodeMsg INSUFFICIENT_POINTS = new CodeMsg(500301,"积分不足"); public static CodeMsg MOBILE_ERROR = new CodeMsg(500213,"手机号格式错误"); public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214,"手机号不存在"); public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215,"密码错误"); public static CodeMsg WRONG_ANSWER = new CodeMsg(500216,"答案错误"); public static CodeMsg USER_EXIST = new CodeMsg(500218,"用户已存在"); CodeMsg(int code, String msg){ this.code = code; this.msg = msg; } public int getCode() { return code; } public String getMsg() { return msg; } public CodeMsg fillArgs(Object...args ) { int code = this.code; String message = String.format(this.msg, args); return new CodeMsg(code, message); } @Override public String toString() { return "CodeMsg [code=" + code + ", msg=" + msg + "]"; } }
Result
package com.sun.gifthouse.result; public class Result <T>{ private int code; private String msg; private T data; //成功的时候调用 public static <T> Result<T> success(T data) { return new Result<T>(data); } //失败的时候调用 public static <T> Result<T> error(CodeMsg codeMsg){ return new Result<T>(codeMsg); } Result(T data){ this.code = 0; this.msg = "success"; this.data = data; } Result(CodeMsg codeMsg){ this.code = codeMsg.getCode(); this.msg = codeMsg.getMsg(); } public int getCode() { return code; } public String getMsg() { return msg; } public T getData() { return data; } }
GoodsService
package com.sun.gifthouse.service; import com.sun.gifthouse.Utils.GoodsUtil; import com.sun.gifthouse.Utils.ShopUtil; import com.sun.gifthouse.Utils.UserUtil; import com.sun.gifthouse.exception.GlobalException; import com.sun.gifthouse.po.Goods; import com.sun.gifthouse.po.ShoppingInfo; import com.sun.gifthouse.result.CodeMsg; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class GoodsService { public List<Object> getAllGoods() { return GoodsUtil.getAllGoods(); } public void buy(String username, Integer goodsId) { ShopUtil.buy(username, goodsId); } public List<ShoppingInfo> getShops(String username) { if (UserUtil.getUser(username) == null) throw new GlobalException(CodeMsg.NO_USERNAME); return ShopUtil.getShops(username); } }
UserService
package com.sun.gifthouse.service; import com.sun.gifthouse.Utils.UserUtil; import com.sun.gifthouse.po.User; import org.springframework.stereotype.Service; @Service public class UserService { public void login(User user) { UserUtil.login(user); } public User getUser(String username) { return UserUtil.getUser(username); } }
GoodsUtil
package com.sun.gifthouse.Utils; import com.sun.gifthouse.po.Goods; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.ConcurrentHashMap; public class GoodsUtil { private static final ConcurrentHashMap<Integer, Goods> goodsMap = new ConcurrentHashMap<>();; static { goodsMap.put(1, new Goods(1, "面纸", 1000, 100)); goodsMap.put(2, new Goods(2, "毛巾", 1000, 200)); goodsMap.put(3, new Goods(3, "洗衣液", 200, 500)); goodsMap.put(4, new Goods(4, "饮料", 100, 1000)); } public static List<Object> getAllGoods() { LoggerFactory.getLogger("12").info(goodsMap.values().toArray().toString()); return Arrays.asList(goodsMap.values().toArray()); } public static Goods getGoods(Integer id){ return goodsMap.get(id); } public static void buy(String username, Integer goodsId) { } public static void setGoods(Integer goodsId, Goods goods) { goodsMap.replace(goodsId, goods); } }
ShopUtils
package com.sun.gifthouse.Utils; import com.sun.gifthouse.exception.GlobalException; import com.sun.gifthouse.po.Goods; import com.sun.gifthouse.po.ShoppingInfo; import com.sun.gifthouse.po.User; import com.sun.gifthouse.result.CodeMsg; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ShopUtil { private static final ConcurrentHashMap<Integer, ShoppingInfo> shops = new ConcurrentHashMap<>(); public static void buy(String username, Integer goodsId) { User user = UserUtil.getUser(username); Goods goods = GoodsUtil.getGoods(goodsId); if(user.getPoints() < goods.getPoints()) throw new GlobalException(CodeMsg.INSUFFICIENT_POINTS); user.setPoints(user.getPoints() - goods.getPoints()); shops.put(shops.size(), new ShoppingInfo(shops.size(), goodsId, goods.getDiscription(), goods.getNumber(), goods.getPoints(), user.getUsername())); goods.setNumber(goods.getNumber() - 1); UserUtil.setUser(user.getUsername(), user); GoodsUtil.setGoods(goodsId, goods); } public static List<ShoppingInfo> getShops(String username) { List<ShoppingInfo> list = new ArrayList<>(); Iterator<Map.Entry<Integer, ShoppingInfo>> it = shops.entrySet().iterator(); while (it.hasNext()){ Map.Entry<Integer, ShoppingInfo> entry = it.next(); LoggerFactory.getLogger("11").info(entry.toString()); if (username.equals(entry.getValue().getUsername())) list.add(entry.getValue()); } return list; } }
UserUtil
package com.sun.gifthouse.Utils; import com.sun.gifthouse.exception.GlobalException; import com.sun.gifthouse.po.User; import com.sun.gifthouse.result.CodeMsg; import java.util.HashMap; import java.util.concurrent.ConcurrentHashMap; public class UserUtil { private static final ConcurrentHashMap<String, User> users = new ConcurrentHashMap<>(); static { users.put("111", new User("111", "111", 1000)); users.put("222", new User("222", "222", 2000)); } public static void login(User user) { if (!users.containsKey(user.getUsername())){ throw new GlobalException(CodeMsg.NO_USERNAME); } User data = users.get(user.getUsername()); if (!user.getPassword().equals(data.getPassword())) throw new GlobalException(CodeMsg.ERROR_PASSWORD); } public static User getUser(String username) { if (!users.containsKey(username)){ throw new GlobalException(CodeMsg.NO_USER); } return users.get(username); } public static void setUser(String username, User user) { users.replace(username, user); } }
server.port=9090