diff --git a/back-end_code/src/main/java/com/shop/controller/GoodsController.java b/back-end_code/src/main/java/com/shop/controller/GoodsController.java index 0873d84ff66d64540144811e7d3c43794e2c427c..dea94cb4c58092ed1095c3efd0761a818d7747bf 100644 --- a/back-end_code/src/main/java/com/shop/controller/GoodsController.java +++ b/back-end_code/src/main/java/com/shop/controller/GoodsController.java @@ -85,8 +85,7 @@ public class GoodsController { public Object updateGoodsState(@RequestBody Map json){ String message = "error"; JSONObject jsonObject = new JSONObject(); - int result = goodsService.updateGoodsState(json.get("seller_username"),Integer.parseInt(json.get("item_id")), Integer.parseInt(json.get("new_state"))); - + int result = goodsService.updateGoodsState(json.get("seller_username"),Integer.parseInt(json.get("item_id")), json.get("new_state")); if (result >= 1) { message = "success"; }else { @@ -96,7 +95,7 @@ public class GoodsController { return jsonObject; } - //在goods表中查找所有正在出售的商品 + //买家在goods表中查找所有正在出售的商品 @GetMapping(value = "/searchSellingGoods") public Object searchSellingGoods(){ try { @@ -109,6 +108,24 @@ public class GoodsController { } } + //卖家在goods表中查找自己正在出售的商品 + @GetMapping(value = "/seller/searchSellingGoods") + public Object sellerSearchSellingGoods(@RequestParam String seller_username){ + JSONObject jsonObject = new JSONObject(); + List goods= goodsService.sellerSearchSellingGoods(seller_username); + jsonObject.put("goods",goods); + return Result.ok(jsonObject); + } + + //卖家在goods表中查找自己售罄的商品 + @GetMapping(value = "/seller/searchSelloutGoods") + public Object sellerSearchSellOutGoods(@RequestParam String seller_username){ + JSONObject jsonObject = new JSONObject(); + List goods= goodsService.sellerSearchSellOutGoods(seller_username); + jsonObject.put("goods",goods); + return Result.ok(jsonObject); + } + //在goods表中查找欢迎界面商品 @GetMapping(value = "/searchWelcomeGoods") public Object searchWelcomeGoods(){ @@ -123,34 +140,13 @@ public class GoodsController { } - //在goods表中查询历史商品 + //卖家查看自己下架商品 @GetMapping(value = "/searchHistoryGoods") public Object searchHistoryGoods(@RequestParam String seller_username){ - String message = "error"; JSONObject jsonObject = new JSONObject(); List goodsList = goodsService.searchHistoryGoods(seller_username); -// List list = new ArrayList(); -// for (int i=0;i < goodsList.size();i++){ -// Goods good = (Goods) goodsList.get(i); -// GO go = goService.searchGOByItemId(good.getItem_id()); -// Order order = orderService.searchOrderByOrderId(seller_username, go.getOrder_id()); -// Order_Goods order_goods = new Order_Goods( -// order.getOrder_id(), -// order.getBuyer_realname(), -// order.getBuyer_phonenumber(), -// order.getBuyer_address(), -// order.getFinish_time(), -// good.getItem_id(), -// good.getGoods_name(), -// good.getGoods_price(), -// good.getGoods_img(), -// good.getGoods_discribe()); -// list.add(order_goods); -// } - message = "success"; - jsonObject.put("message",message); jsonObject.put("goods",goodsList); - return jsonObject; + return Result.ok(jsonObject); } //查询此用户是否有正在出售的商品 diff --git a/back-end_code/src/main/java/com/shop/controller/OrderController.java b/back-end_code/src/main/java/com/shop/controller/OrderController.java index bd962c8592e4ed4b70312106ed1f1f8645ad4063..3a8b9a0e88eb6cca52236557ca5ec49537f11627 100644 --- a/back-end_code/src/main/java/com/shop/controller/OrderController.java +++ b/back-end_code/src/main/java/com/shop/controller/OrderController.java @@ -58,66 +58,92 @@ public class OrderController { //用户下单 @PostMapping("/Orders") public Object addOrder(@RequestBody JSONObject jsonObject){ - Order order = new Order(jsonObject); - long newId = YitIdHelper.nextId(); - System.out.println("生成的id:"+newId); - order.setOrder_id(newId); + Iterator orders = jsonObject.getJSONArray("orders").iterator(); boolean cart = jsonObject.getBoolean("cart"); - Address address = addressService.searchAddressById(order.getAddress().getAddress_id()); - if (address == null || !order.getBuyer_username().equals(address.getUsername())){ + Map Seller_orderId = new HashMap(); + String buyer_username = jsonObject.getString("buyer_username"); + Address address = addressService.searchAddressById(jsonObject.getJSONObject("address").getInteger("address_id")); + if (address == null || !buyer_username.equals(address.getUsername()) || !buyer_username.equals(jsonObject.getJSONObject("address").getString("username"))){ return Result.fail("地址有误"); } - order.setCreate_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); - orderService.addOrder(order); - order = orderService.searchNewOrder(order); - List orderDetailList = new ArrayList(); - Iterator objectIterator = jsonObject.getJSONArray("goods").iterator(); + List orderList = new ArrayList(); + String seller_username = null; + String remark = null; + Order order = null; OrderDetail orderDetail = null ; - Integer item_id = null ; - Integer buy_stock = null ; - Float buy_amount = 0.0f; - Snapshot snapshot = null ; - JSONObject jb = null; Goods good = null; + Snapshot snapshot = null; + Integer item_id = null; + Integer buy_stock = null; + Long orderId = null; + Float buy_amount = 0.0f; + JSONObject orderjson = null; + JSONObject goodjson = null; StringBuilder sb = new StringBuilder(); - while (objectIterator.hasNext()) { - jb = (JSONObject) objectIterator.next(); - item_id = jb.getInteger("item_id"); - good = goodsService.searchGoods(item_id); - if (good == null){ - sb.append("商品ID: ").append(item_id).append(" 下单失败").append(" ,原因:该商品不存在\n"); - continue; + while (orders.hasNext()){ + orderjson = (JSONObject) orders.next(); + seller_username = orderjson.getString("seller_username"); + remark = orderjson.getString("remark"); + Iterator goods = orderjson.getJSONArray("goods").iterator(); + List orderDetailList = new ArrayList(); + while (goods.hasNext()){ + goodjson = (JSONObject) goods.next(); + item_id = goodjson.getInteger("item_id"); + buy_stock = goodjson.getInteger("buy_stock"); + good = goodsService.searchGoods(item_id); + if (good == null){ + sb.append("商品ID: ").append(item_id).append(" 下单失败").append(" ,原因:该商品不存在\n"); + continue; + } + if (good.getGoods_state() != GoodState.Selling){ + sb.append("商品: ").append(good.getGoods_name()).append(" 下单失败").append(" ,原因:该商品非售\n"); + continue; + } + if (buy_stock > good.getGoods_stock()) { + sb.append("商品: ").append(good.getGoods_name()).append(" 下单失败").append(" ,原因:该商品购买数量超库存\n"); + continue; + } + if(Seller_orderId.containsKey(seller_username) && seller_username.equals(good.getSeller_username())){ + orderId = Seller_orderId.get(seller_username); + order = orderService.searchOrderByOrderId(orderId); + }else { + if (!seller_username.equals(good.getSeller_username())) + sb.append("商品: ").append(good.getGoods_name()).append(" 下单出问题").append(" ,原因:该商品卖家非").append(seller_username).append("已自动分配为").append(good.getSeller_username()+"\n"); + orderId = YitIdHelper.nextId(); + order = new Order(orderId,buyer_username,address,remark,good.getSeller_username(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); + orderService.addOrder(order); + Seller_orderId.put(good.getSeller_username(),orderId); + orderService.setAmountByOrderId(orderId,good.getSeller_username(),0.0f); + } + if (order.getBuy_amount() == null){ + buy_amount = 0.0f; + }else { + buy_amount = order.getBuy_amount(); + } + snapshot = snapshotService.searchNewSnapshotByGoodId(item_id); + orderDetailService.addOrderDetail(order.getOrder_id(),snapshot,buy_stock); + orderDetail = orderDetailService.searchNewOrderDetailByOrderId(order.getOrder_id()); + orderDetailList.add(orderDetail); + buy_amount += orderDetail.getBuy_sum(); + goodsService.updateGoodStock(good.getItem_id(),good.getGoods_stock()-buy_stock); + if (cart){ + cartService.deleteCart(order.getBuyer_username(), good.getItem_id()); + } + orderService.setAmountByOrderId(orderId,good.getSeller_username(),buy_amount); + sb.append("商品: ").append(good.getGoods_name()).append(" 下单成功\n"); + order = null; + good = null; + snapshot = null; + orderDetail = null; } - if (good.getGoods_state() != GoodState.Selling){ - sb.append("商品: ").append(good.getGoods_name()).append(" 下单失败").append(" ,原因:该商品非售\n"); - continue; - } - buy_stock = jb.getInteger("buy_stock"); - if (buy_stock > good.getGoods_stock()) { - sb.append("商品: ").append(good.getGoods_name()).append(" 下单失败").append(" ,原因:该商品购买数量超库存\n"); - continue; - } - snapshot = snapshotService.searchNewSnapshotByGoodId(item_id); - orderDetailService.addOrderDetail(order.getOrder_id(),snapshot,buy_stock); - orderDetail = orderDetailService.searchNewOrderDetailByOrderId(order.getOrder_id()); - buy_amount += orderDetail.getBuy_sum(); - orderDetailList.add(orderDetail); - cartService.deleteCart(order.getBuyer_username(), good.getItem_id()); - goodsService.updateGoodStock(good.getItem_id(),good.getGoods_stock()-buy_stock); - if (cart){ - cartService.deleteCart(order.getBuyer_username(), good.getItem_id()); - } - sb.append("商品: ").append(good.getGoods_name()).append(" 下单成功\n"); - good = null; - snapshot = null; - orderDetail = null; } - if (jsonObject.getFloat("buy_amount") == null) - orderService.setAmountByOrderId(order.getOrder_id(),order.getSeller_username(), buy_amount); - order = orderService.searchOrderByOrderId(order.getOrder_id()); + for(String key:Seller_orderId.keySet()){ + order = orderService.searchOrderByOrderId(Seller_orderId.get(key)); + orderList.add(order); + } jsonObject.clear(); jsonObject.put("result",sb.toString()); - jsonObject.put("order" ,order); + jsonObject.put("orders" ,orderList); return Result.ok(jsonObject); } diff --git a/back-end_code/src/main/java/com/shop/entity/Order.java b/back-end_code/src/main/java/com/shop/entity/Order.java index c842f4771bd1ae35cdc21b6d003fee3112a981ff..e6241e63a33c6af2c2a2bb1f0eba5c503fba3923 100644 --- a/back-end_code/src/main/java/com/shop/entity/Order.java +++ b/back-end_code/src/main/java/com/shop/entity/Order.java @@ -28,10 +28,28 @@ public class Order { super(); } - public Order(Long order_id, String buyer_username, Address address) { + public Order(Long order_id, String buyer_username, Address address, String remark, String seller_username, String create_time) { this.order_id = order_id; this.buyer_username = buyer_username; this.address = address; + this.remark = remark; + this.seller_username = seller_username; + this.create_time = create_time; + } + + public Order(Long order_id, String buyer_username, Address address, String seller_username, String create_time) { + this.order_id = order_id; + this.buyer_username = buyer_username; + this.address = address; + this.seller_username = seller_username; + this.create_time = create_time; + } + + public Order(Long order_id, String buyer_username, Address address, String create_time) { + this.order_id = order_id; + this.buyer_username = buyer_username; + this.address = address; + this.create_time = create_time; } public Order(JSONObject jsonObject) { diff --git a/back-end_code/src/main/java/com/shop/mapper/GoodsMapper.java b/back-end_code/src/main/java/com/shop/mapper/GoodsMapper.java index 9c715a8f5b25d0c124a7dd07219c6454b48a036e..dc7d27dabcd44a7b9d621f073ebc097f4ed4df6d 100644 --- a/back-end_code/src/main/java/com/shop/mapper/GoodsMapper.java +++ b/back-end_code/src/main/java/com/shop/mapper/GoodsMapper.java @@ -1,5 +1,6 @@ package com.shop.mapper; +import com.shop.Enum.GoodState; import com.shop.entity.Goods; import org.springframework.stereotype.Repository; @@ -37,7 +38,7 @@ public interface GoodsMapper { * @param new_state * @return */ - int updateGoodsState(String seller_username,int item_id,int new_state); + int updateGoodsState(String seller_username, int item_id, GoodState new_state); /** *根据username查找此用户是否有正在出售的商品 @@ -45,18 +46,33 @@ public interface GoodsMapper { * @return */ Goods isHaveSellingGoods(String username); + /** - * 在Goods表中查找所有正在出售的商品 + * 买家在Goods表中查找所有正在出售的商品 * @return */ List searchSellingGoods(); /** - * 在Goods表中查找所有商品 + * 卖家在Goods表中查找所有正在出售的商品 + * @param seller_username + * @return + */ + List sellerSearchSellingGoods(String seller_username); + + /** + * 在Goods表中查找售罄商品 + * @param seller_username + * @return + */ + List sellerSearchSellOutGoods(String seller_username); + + /** + * 卖家在Goods表中查找下架商品 * @param seller_username * @return */ - List searchHistoryGoods(String seller_username); + List sellerSearchHistoryGoods(String seller_username); /** * 在Goods表中查找所有商品(welcome) diff --git a/back-end_code/src/main/java/com/shop/service/GoodsService.java b/back-end_code/src/main/java/com/shop/service/GoodsService.java index 134dcdba3e6008ec1b3531b6b06241a0e551bf07..c64945f028c44565f9b2aa5949d4315c49f3ebff 100644 --- a/back-end_code/src/main/java/com/shop/service/GoodsService.java +++ b/back-end_code/src/main/java/com/shop/service/GoodsService.java @@ -1,5 +1,6 @@ package com.shop.service; +import com.shop.Enum.GoodState; import com.shop.entity.Category; import com.shop.entity.Goods; import com.shop.mapper.CategoryMapper; @@ -23,15 +24,17 @@ public class GoodsService { } public Goods getNewItem(String goods_name, String goods_img,String goods_describe){ return goodsMapper.getNewItem(goods_name,goods_img,goods_describe);} public Goods searchGoods(int item_id){ return goodsMapper.searchGoods(item_id); } - public int updateGoodsState(String seller_username,int item_id,int new_state){return goodsMapper.updateGoodsState(seller_username,item_id, new_state);} - public List searchSellingGoods(){return goodsMapper.searchSellingGoods();}//一页8条 - public List searchFreezingGoods(String seller_username){return goodsMapper.searchFreezingGoods(seller_username);}//一页10条 - public List searchHistoryGoods(String seller_username){ return goodsMapper.searchHistoryGoods(seller_username);}//一页10条 + public int updateGoodsState(String seller_username,int item_id,String new_state){return goodsMapper.updateGoodsState(seller_username,item_id, GoodState.toenum(new_state));} + public List searchSellingGoods(){return goodsMapper.searchSellingGoods();} + public List sellerSearchSellingGoods(String seller_username){return goodsMapper.sellerSearchSellingGoods(seller_username);} + public List searchFreezingGoods(String seller_username){return goodsMapper.searchFreezingGoods(seller_username);} + public List sellerSearchSellOutGoods(String seller_username){ return goodsMapper.sellerSearchSellOutGoods(seller_username);} + public List searchHistoryGoods(String seller_username){ return goodsMapper.sellerSearchHistoryGoods(seller_username);} public int updateGoods(Goods newgoods){ if (newgoods.getGoods_stock() == 0){ - goodsMapper.updateGoodsState(newgoods.getSeller_username(),newgoods.getItem_id(),3); + goodsMapper.updateGoodsState(newgoods.getSeller_username(),newgoods.getItem_id(),GoodState.Sell_out); }else if (newgoods.getGoods_stock() > 0){ - goodsMapper.updateGoodsState(newgoods.getSeller_username(),newgoods.getItem_id(),1); + goodsMapper.updateGoodsState(newgoods.getSeller_username(),newgoods.getItem_id(),GoodState.Sell_out); } return goodsMapper.updateGoods(newgoods);} public List searchWelcomeGoods(){return goodsMapper.searchWelcomeGoods();} @@ -69,9 +72,9 @@ public class GoodsService { public int updateGoodStock(int item_id,int new_stock){ Goods good = searchGoods(item_id); if (new_stock > 0 ){ - updateGoodsState(good.getSeller_username(),item_id,1); + updateGoodsState(good.getSeller_username(),item_id,"Selling"); }else if (new_stock == 0){ - updateGoodsState(good.getSeller_username(),item_id,3); + updateGoodsState(good.getSeller_username(),item_id,"Sell_out"); } return goodsMapper.updateGoodStock(item_id,new_stock);} public List searchGoodsByCategoryAndKeyName(int goods_category,String key_name){ diff --git a/back-end_code/src/main/resources/mapping/GoodsMapper.xml b/back-end_code/src/main/resources/mapping/GoodsMapper.xml index 9e2df59a52320c9a42e7f157cb91dd5cd5af119c..7339dd1b154f5da25449575193a304d9ee68512f 100644 --- a/back-end_code/src/main/resources/mapping/GoodsMapper.xml +++ b/back-end_code/src/main/resources/mapping/GoodsMapper.xml @@ -69,7 +69,7 @@ - + @@ -113,7 +113,7 @@ update goods - goodsState = #{new_state,jdbcType=INTEGER}, + goodsState = #{new_state,jdbcType=VARCHAR}, where itemId = #{item_id,jdbcType=INTEGER} @@ -121,22 +121,32 @@ + + + + + + - + select * from goods where sellerUsername = #{seller_username} and @@ -146,17 +156,17 @@ diff --git a/back-end_code/src/main/resources/mapping/OrderMapper.xml b/back-end_code/src/main/resources/mapping/OrderMapper.xml index 685fb70688b2915c731777ff2d02a3be4889808b..96be4028ffa2a512dafd35214d1526c5f4e2c4e4 100644 --- a/back-end_code/src/main/resources/mapping/OrderMapper.xml +++ b/back-end_code/src/main/resources/mapping/OrderMapper.xml @@ -35,6 +35,7 @@ + @@ -62,6 +63,9 @@ addressId, + + remark, + buyAmount, @@ -83,6 +87,9 @@ #{address.address_id,jdbcType=INTEGER}, + + #{remark,jdbcType=VARCHAR}, + #{buy_amount ,jdbcType=REAL},