From 005063d2e32da7d80fab5f3e908eafe42bb33265 Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Thu, 29 Aug 2024 23:50:44 +0800 Subject: [PATCH] =?UTF-8?q?wxApp=E5=90=8E=E7=AB=AF=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=95=86=E5=93=81=E8=A1=A8=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GoodsController.java | 42 +++++++++++ .../controller/OrderController.java | 12 ++++ .../controller/UserController.java | 49 +++++++++++++ .../com/bigdata/wxappserver/entity/Goods.java | 2 +- .../wxappserver/service/GoodsService.java | 35 ++++++++++ .../wxappserver/service/OrderService.java | 68 +++++++++++++----- .../wxappserver/service/UserService.java | 70 +++++++++++++++++++ src/main/resources/application.yml | 4 +- 8 files changed, 263 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/bigdata/wxappserver/controller/GoodsController.java create mode 100644 src/main/java/com/bigdata/wxappserver/controller/UserController.java diff --git a/src/main/java/com/bigdata/wxappserver/controller/GoodsController.java b/src/main/java/com/bigdata/wxappserver/controller/GoodsController.java new file mode 100644 index 0000000..93ca66f --- /dev/null +++ b/src/main/java/com/bigdata/wxappserver/controller/GoodsController.java @@ -0,0 +1,42 @@ +package com.bigdata.wxappserver.controller; + +import com.alibaba.fastjson.JSONObject; +import com.bigdata.wxappserver.service.GoodsService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/08/29/19:53 + * @Description: + */ +@RestController +@RequestMapping("goods") +@Slf4j +public class GoodsController { + @Autowired + GoodsService goodsService; + + @RequestMapping("addOrUpdate") + public JSONObject addOrUpdate(@RequestBody JSONObject jsonObject){ + return goodsService.addOrUpdate(jsonObject); + } + @RequestMapping("delete") + public JSONObject delete(@RequestParam("id")Integer id){ + return goodsService.delete(id); +// return new JSONObject().fluentPut("success",goodsService.removeById(id)); + } + @RequestMapping("getById") + public JSONObject queryById(@RequestParam("id") Integer id){ + return goodsService.queryById(id); + } + + @RequestMapping("loadData") + public JSONObject list(){ + return new JSONObject().fluentPut("data",goodsService.list()); + } + +} diff --git a/src/main/java/com/bigdata/wxappserver/controller/OrderController.java b/src/main/java/com/bigdata/wxappserver/controller/OrderController.java index 54dbdaa..15eaa02 100644 --- a/src/main/java/com/bigdata/wxappserver/controller/OrderController.java +++ b/src/main/java/com/bigdata/wxappserver/controller/OrderController.java @@ -1,10 +1,12 @@ package com.bigdata.wxappserver.controller; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.bigdata.wxappserver.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** @@ -29,6 +31,16 @@ public class OrderController { @RequestMapping("loadData") public JSONObject list(@RequestBody JSONObject jsonObject){ return orderService.list(jsonObject); + + } + @RequestMapping("getById") + public JSONObject queryById(@RequestParam("id") Integer id){ + return orderService.queryById(id); + } + @RequestMapping("delete") + public JSONObject delete(@RequestParam("id")Integer id){ +// return new JSONObject().fluentPut("success",orderService.removeById(id)); + return orderService.delete(id); } } diff --git a/src/main/java/com/bigdata/wxappserver/controller/UserController.java b/src/main/java/com/bigdata/wxappserver/controller/UserController.java new file mode 100644 index 0000000..d732ab1 --- /dev/null +++ b/src/main/java/com/bigdata/wxappserver/controller/UserController.java @@ -0,0 +1,49 @@ +package com.bigdata.wxappserver.controller; + +import com.alibaba.fastjson.JSONObject; +import com.bigdata.wxappserver.service.UserService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/08/29/22:59 + * @Description: + */ + +@RequestMapping("user") +@RestController +@Slf4j +public class UserController { + + + @Autowired + UserService userService; + + @RequestMapping("addOrUpdate") + public JSONObject addOrUpdate(@RequestBody JSONObject jsonObject){ + return userService.addOrUpdate(jsonObject); + } + + @RequestMapping("loadData") + public JSONObject list(@RequestBody JSONObject jsonObject){ + return new JSONObject().fluentPut("data",userService.list()); + + } + @RequestMapping("getById") + public JSONObject queryById(@RequestParam("id") Integer id){ + return userService.queryById(id); + } + @RequestMapping("delete") + public JSONObject delete(@RequestParam("id")Integer id){ + return userService.delete(id); + + } + +} diff --git a/src/main/java/com/bigdata/wxappserver/entity/Goods.java b/src/main/java/com/bigdata/wxappserver/entity/Goods.java index 82c3636..e2b7f28 100644 --- a/src/main/java/com/bigdata/wxappserver/entity/Goods.java +++ b/src/main/java/com/bigdata/wxappserver/entity/Goods.java @@ -29,7 +29,7 @@ public class Goods extends Base { /** * 商品图片路径 */ - private String image; + private String goodImage; /** * 商品描述 */ diff --git a/src/main/java/com/bigdata/wxappserver/service/GoodsService.java b/src/main/java/com/bigdata/wxappserver/service/GoodsService.java index caca4e4..ba5e495 100644 --- a/src/main/java/com/bigdata/wxappserver/service/GoodsService.java +++ b/src/main/java/com/bigdata/wxappserver/service/GoodsService.java @@ -1,10 +1,45 @@ package com.bigdata.wxappserver.service; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.bigdata.wxappserver.entity.Goods; +import com.bigdata.wxappserver.entity.Order; +import com.bigdata.wxappserver.entity.User; import com.bigdata.wxappserver.mapper.GoodsMapper; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; @Service public class GoodsService extends ServiceImpl { + @Transactional(rollbackFor = Exception.class) + public JSONObject addOrUpdate(JSONObject jsonObject) { + Goods good = jsonObject.toJavaObject(Goods.class); + if (good == null) { + return new JSONObject().fluentPut("success", false).fluentPut("message", "参数错误"); + } + // TODO 判空 + boolean success = saveOrUpdate(good); + return new JSONObject().fluentPut("message", "success").fluentPut("success", success); + } + + public JSONObject queryById(Integer id) { + if(id==null){ + return new JSONObject().fluentPut("success",false).fluentPut("message","缺少ID"); + } + Goods goods = getById(id); + return new JSONObject().fluentPut("data",goods); + } + @Transactional(rollbackFor = Exception.class) + public JSONObject delete(Integer id) { + if(id==null){ + return new JSONObject().fluentPut("success",false).fluentPut("message","缺少Id"); + } + boolean success = removeById(id); + if(success){ + return new JSONObject().fluentPut("success",true); + }else{ + return new JSONObject().fluentPut("success",false).fluentPut("message","出现未知错误,请联系管理员!"); + } + } } diff --git a/src/main/java/com/bigdata/wxappserver/service/OrderService.java b/src/main/java/com/bigdata/wxappserver/service/OrderService.java index 7cd1e1a..b069419 100644 --- a/src/main/java/com/bigdata/wxappserver/service/OrderService.java +++ b/src/main/java/com/bigdata/wxappserver/service/OrderService.java @@ -6,15 +6,22 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bigdata.wxappserver.entity.Goods; import com.bigdata.wxappserver.entity.Order; +import com.bigdata.wxappserver.entity.User; import com.bigdata.wxappserver.enums.OrderStatusEnum; import com.bigdata.wxappserver.mapper.OrderMapper; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; +import java.util.Collections; import java.util.List; +import static com.fasterxml.jackson.databind.type.LogicalType.Collection; + /** * Created with IntelliJ IDEA. * @@ -25,35 +32,41 @@ import java.util.List; @Service public class OrderService extends ServiceImpl { + @Autowired + UserService userService; + @Transactional(rollbackFor = Exception.class) public JSONObject addOrUpdate(JSONObject jsonObject) { Order order = jsonObject.toJavaObject(Order.class); - if (order==null){ - return new JSONObject().fluentPut("success",false).fluentPut("message","参数错误"); + if (order == null) { + return new JSONObject().fluentPut("success", false).fluentPut("message", "参数错误"); + } + if (!StringUtils.hasLength(order.getAddress())) { + User user = userService.getById(order.getUserId()); + order.setAddress(user.getAddress()); } // TODO 判空 boolean success = saveOrUpdate(order); - return new JSONObject().fluentPut("message","success").fluentPut("success",success); + return new JSONObject().fluentPut("message", "success").fluentPut("success", success); } - public JSONObject list(JSONObject jsonObject){ + public JSONObject list(JSONObject jsonObject) { String userId = jsonObject.getString("userId"); - Integer currentPage = jsonObject.getInteger("currentPage"); - Integer pageSize = jsonObject.getInteger("pageSize"); - IPage page=new Page<>(currentPage,pageSize); - LambdaQueryWrapper wrapper=new LambdaQueryWrapper<>(); - wrapper.eq(userId!=null,Order::getUserId,userId); - IPage orderIPage = page(page, wrapper); - dealList(orderIPage.getRecords()); - JSONObject returnData=new JSONObject(); - returnData.fluentPut("data",orderIPage.getRecords()) - .fluentPut("total",orderIPage.getTotal()) - .fluentPut("current",orderIPage.getCurrent()); +// Integer currentPage = jsonObject.getInteger("currentPage"); +// Integer pageSize = jsonObject.getInteger("pageSize"); +// IPage page = new Page<>(currentPage, pageSize); + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(userId != null, Order::getUserId, userId); +// IPage orderIPage = page(page, wrapper); + List list = list(wrapper); + dealList(list); + JSONObject returnData = new JSONObject(); + returnData.fluentPut("data", list); return returnData; } private void dealList(List records) { - if(CollectionUtils.isEmpty(records)){ + if (CollectionUtils.isEmpty(records)) { return; } for (Order record : records) { @@ -62,4 +75,27 @@ public class OrderService extends ServiceImpl { } + public JSONObject queryById(Integer id) { + if (id == null) { + return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少ID"); + } + Order order = getById(id); + dealList(Collections.singletonList(order)); + return new JSONObject().fluentPut("data", order); + } + + + @Transactional(rollbackFor = Exception.class) + public JSONObject delete(Integer id) { + if (id == null) { + return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少Id"); + } + boolean success = removeById(id); + if (success) { + return new JSONObject().fluentPut("success", true); + } else { + return new JSONObject().fluentPut("success", false).fluentPut("message", "出现未知错误,请联系管理员!"); + } + } + } diff --git a/src/main/java/com/bigdata/wxappserver/service/UserService.java b/src/main/java/com/bigdata/wxappserver/service/UserService.java index f84de4d..4d83bd8 100644 --- a/src/main/java/com/bigdata/wxappserver/service/UserService.java +++ b/src/main/java/com/bigdata/wxappserver/service/UserService.java @@ -1,10 +1,80 @@ package com.bigdata.wxappserver.service; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.bigdata.wxappserver.entity.User; import com.bigdata.wxappserver.mapper.UserMapper; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; @Service public class UserService extends ServiceImpl { + @Transactional(rollbackFor = Exception.class) + public JSONObject addOrUpdate(JSONObject jsonObject) { + User user = jsonObject.toJavaObject(User.class); + if (user == null) { + return new JSONObject().fluentPut("success", false).fluentPut("message", "参数错误"); + } + List userList = list(); + List openIdList = userList.stream().map(User::getOpenId).collect(Collectors.toList()); + if (user.getId() == null && !CollectionUtils.isEmpty(openIdList) && openIdList.contains(user.getOpenId())) { + return new JSONObject().fluentPut("message", "该用户已存在").fluentPut("success", false); + } + // TODO 判空 + boolean success = saveOrUpdate(user); + return new JSONObject().fluentPut("success", success); + } + + public JSONObject list(JSONObject jsonObject) { + String userId = jsonObject.getString("userId"); + Integer currentPage = jsonObject.getInteger("currentPage"); + Integer pageSize = jsonObject.getInteger("pageSize"); + IPage page = new Page<>(currentPage, pageSize); + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(userId != null, User::getId, userId); + IPage userIPage = page(page, wrapper); + dealList(userIPage.getRecords()); + JSONObject returnData = new JSONObject(); + returnData.fluentPut("data", userIPage.getRecords()) + .fluentPut("total", userIPage.getTotal()) + .fluentPut("current", userIPage.getCurrent()); + return returnData; + } + + private void dealList(List records) { + if (CollectionUtils.isEmpty(records)) { + return; + } + } + + @Transactional(rollbackFor = Exception.class) + public JSONObject delete(Integer id) { + if (id == null) { + return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少Id"); + } + boolean success = removeById(id); + if (success) { + return new JSONObject().fluentPut("success", true); + } else { + return new JSONObject().fluentPut("success", false).fluentPut("message", "出现未知错误,请联系管理员!"); + } + } + + public JSONObject queryById(Integer id) { + if (id == null) { + return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少ID"); + } + User user = getById(id); + return new JSONObject().fluentPut("data", user); + } + } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d22acf9..6377e90 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,9 +7,9 @@ spring: username: root password: nWZpHMb8mNxWE5Xk server: - port: 8088 + port: 8080 -# Mybatis-plus?? +# Mybatis-plus配置 mybatis-plus: mapper-locations: classpath*:/mapper/*Mapper.xml configuration: