parent
9b0c64d2f7
commit
97f1c5999a
26 changed files with 957 additions and 61 deletions
@ -0,0 +1,215 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.controller; |
||||
|
||||
import cn.hutool.core.bean.BeanUtil; |
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.huoran.common.exception.YamiShopBindException; |
||||
import com.huoran.common.response.ServerResponseEntity; |
||||
import com.huoran.common.utils.SecurityUtils; |
||||
import com.huoran.nakadai.entity.app.dto.MyOrderDto; |
||||
import com.huoran.nakadai.entity.app.dto.OrderCountData; |
||||
import com.huoran.nakadai.entity.app.dto.OrderItemDto; |
||||
import com.huoran.nakadai.entity.app.dto.OrderShopDto; |
||||
import com.huoran.nakadai.entity.enums.OrderStatus; |
||||
import com.huoran.nakadai.entity.model.Order; |
||||
import com.huoran.nakadai.entity.model.OrderItem; |
||||
import com.huoran.nakadai.entity.model.ShopDetail; |
||||
import com.huoran.nakadai.entity.model.UserAddrOrder; |
||||
import com.huoran.nakadai.service.*; |
||||
import com.huoran.nakadai.utils.Arith; |
||||
import com.huoran.nakadai.utils.PageParam; |
||||
import io.swagger.annotations.Api; |
||||
|
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import java.util.Collections; |
||||
|
||||
import java.util.List; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* @author lanhai |
||||
*/ |
||||
@RestController |
||||
@RequestMapping("/myOrder") |
||||
@Api(tags = "小程序我的订单接口(用户采购信息)") |
||||
@AllArgsConstructor |
||||
public class MyOrderController { |
||||
|
||||
private final OrderService orderService; |
||||
|
||||
private final ProductService productService; |
||||
|
||||
private final SkuService skuService; |
||||
|
||||
private final MyOrderService myOrderService; |
||||
|
||||
private final ShopDetailService shopDetailService; |
||||
|
||||
private final OrderItemService orderItemService; |
||||
|
||||
|
||||
/** |
||||
* 订单详情信息接口 |
||||
*/ |
||||
@GetMapping("/orderDetail") |
||||
@ApiOperation(value = "根据订单号获取订单详情信息") |
||||
public ServerResponseEntity<OrderShopDto> orderDetail(@RequestParam(value = "orderNumber") String orderNumber) { |
||||
|
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
OrderShopDto orderShopDto = new OrderShopDto(); |
||||
|
||||
Order order = orderService.getOrderByOrderNumber(orderNumber); |
||||
|
||||
if (order == null) { |
||||
throw new RuntimeException("该订单不存在"); |
||||
} |
||||
if (!Objects.equals(order.getUserId(), userId)) { |
||||
throw new RuntimeException("你没有权限获取该订单信息"); |
||||
} |
||||
|
||||
ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(order.getShopId()); |
||||
// UserAddrOrder userAddrOrder = userAddrOrderService.getById(order.getAddrOrderId());
|
||||
// UserAddrDto userAddrDto = BeanUtil.copyProperties(userAddrOrder, UserAddrDto.class);
|
||||
List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber); |
||||
List<OrderItemDto> orderItemList = BeanUtil.copyToList(orderItems, OrderItemDto.class); |
||||
|
||||
orderShopDto.setShopId(shopDetail.getShopId()); |
||||
orderShopDto.setShopName(shopDetail.getShopName()); |
||||
orderShopDto.setActualTotal(order.getActualTotal()); |
||||
// orderShopDto.setUserAddrDto(userAddrDto);
|
||||
orderShopDto.setOrderItemDtos(orderItemList); |
||||
orderShopDto.setTransfee(order.getFreightAmount()); |
||||
orderShopDto.setReduceAmount(order.getReduceAmount()); |
||||
orderShopDto.setCreateTime(order.getCreateTime()); |
||||
orderShopDto.setRemarks(order.getRemarks()); |
||||
orderShopDto.setStatus(order.getStatus()); |
||||
|
||||
double total = 0.0; |
||||
Integer totalNum = 0; |
||||
for (OrderItemDto orderItem : orderShopDto.getOrderItemDtos()) { |
||||
total = Arith.add(total, orderItem.getProductTotalAmount()); |
||||
totalNum += orderItem.getProdCount(); |
||||
} |
||||
orderShopDto.setTotal(total); |
||||
orderShopDto.setTotalNum(totalNum); |
||||
|
||||
return ServerResponseEntity.success(orderShopDto); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 订单列表接口 |
||||
*/ |
||||
@GetMapping("/myOrder") |
||||
@ApiOperation(value = "根据订单状态获取订单列表信息,状态为0时获取所有订单 1:待付款 2:待发货 3:待收货 4:待评价 5:成功 6:失败") |
||||
public ServerResponseEntity<IPage<MyOrderDto>> myOrder(@RequestParam(value = "status") Integer status, PageParam<MyOrderDto> page) { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
IPage<MyOrderDto> myOrderDtoIpage = myOrderService.pageMyOrderByUserIdAndStatus(page, userId, status); |
||||
return ServerResponseEntity.success(myOrderDtoIpage); |
||||
} |
||||
|
||||
/** |
||||
* 取消订单 |
||||
*/ |
||||
@PostMapping("/cancel") |
||||
@ApiOperation(value = "根据订单号取消订单") |
||||
public ServerResponseEntity<String> cancel(@RequestParam("orderNumber") String orderNumber) { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
Order order = orderService.getOrderByOrderNumber(orderNumber); |
||||
if (!Objects.equals(order.getUserId(), userId)) { |
||||
throw new YamiShopBindException("你没有权限获取该订单信息"); |
||||
} |
||||
if (!Objects.equals(order.getStatus(), OrderStatus.UNPAY.value())) { |
||||
throw new YamiShopBindException("订单已支付,无法取消订单"); |
||||
} |
||||
List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber); |
||||
order.setOrderItems(orderItems); |
||||
// 取消订单
|
||||
orderService.cancelOrders(Collections.singletonList(order)); |
||||
|
||||
// 清除缓存
|
||||
for (OrderItem orderItem : orderItems) { |
||||
productService.removeProductCacheByProdId(orderItem.getProdId()); |
||||
skuService.removeSkuCacheBySkuId(orderItem.getSkuId(), orderItem.getProdId()); |
||||
} |
||||
return ServerResponseEntity.success(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 确认收货 |
||||
*/ |
||||
@PostMapping("/receipt") |
||||
@ApiOperation(value = "根据订单号确认收货") |
||||
public ServerResponseEntity<String> receipt(@RequestParam("orderNumber") String orderNumber) { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
Order order = orderService.getOrderByOrderNumber(orderNumber); |
||||
if (!Objects.equals(order.getUserId(), userId)) { |
||||
throw new YamiShopBindException("你没有权限获取该订单信息"); |
||||
} |
||||
if (!Objects.equals(order.getStatus(), OrderStatus.CONSIGNMENT.value())) { |
||||
throw new YamiShopBindException("订单未发货,无法确认收货"); |
||||
} |
||||
List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber); |
||||
order.setOrderItems(orderItems); |
||||
// 确认收货
|
||||
orderService.confirmOrder(Collections.singletonList(order)); |
||||
|
||||
for (OrderItem orderItem : orderItems) { |
||||
productService.removeProductCacheByProdId(orderItem.getProdId()); |
||||
skuService.removeSkuCacheBySkuId(orderItem.getSkuId(), orderItem.getProdId()); |
||||
} |
||||
return ServerResponseEntity.success(); |
||||
} |
||||
|
||||
/** |
||||
* 删除订单 |
||||
*/ |
||||
@PostMapping("/deleteOrder") |
||||
@ApiOperation(value = "根据订单号删除订单") |
||||
public ServerResponseEntity<String> delete(@RequestParam("orderNumber") String orderNumber) { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
|
||||
Order order = orderService.getOrderByOrderNumber(orderNumber); |
||||
if (order == null) { |
||||
throw new YamiShopBindException("该订单不存在"); |
||||
} |
||||
if (!Objects.equals(order.getUserId(), userId)) { |
||||
throw new YamiShopBindException("你没有权限获取该订单信息"); |
||||
} |
||||
if (!Objects.equals(order.getStatus(), OrderStatus.SUCCESS.value()) && !Objects.equals(order.getStatus(), OrderStatus.CLOSE.value())) { |
||||
throw new YamiShopBindException("订单未完成或未关闭,无法删除订单"); |
||||
} |
||||
|
||||
// 删除订单
|
||||
orderService.deleteOrders(Collections.singletonList(order)); |
||||
|
||||
return ServerResponseEntity.success("删除成功"); |
||||
} |
||||
|
||||
/** |
||||
* 获取我的订单订单数量 |
||||
*/ |
||||
@GetMapping("/orderCount") |
||||
@ApiOperation(value = "获取我的订单订单数量") |
||||
public ServerResponseEntity<OrderCountData> getOrderCount() { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
OrderCountData orderCountMap = orderService.getOrderCount(userId); |
||||
return ServerResponseEntity.success(orderCountMap); |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,287 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.controller; |
||||
|
||||
import cn.hutool.core.bean.BeanUtil; |
||||
import cn.hutool.core.date.DatePattern; |
||||
import cn.hutool.core.date.DateUtil; |
||||
import cn.hutool.core.io.IORuntimeException; |
||||
import cn.hutool.core.io.IoUtil; |
||||
import cn.hutool.poi.excel.ExcelUtil; |
||||
import cn.hutool.poi.excel.ExcelWriter; |
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.google.common.base.Objects; |
||||
import com.huoran.common.exception.YamiShopBindException; |
||||
import com.huoran.common.response.ServerResponseEntity; |
||||
import com.huoran.common.utils.SecurityUtils; |
||||
import com.huoran.common.utils.TokenUtils; |
||||
import com.huoran.nakadai.entity.enums.OrderStatus; |
||||
import com.huoran.nakadai.entity.model.Order; |
||||
import com.huoran.nakadai.entity.model.OrderItem; |
||||
import com.huoran.nakadai.entity.model.UserAddrOrder; |
||||
import com.huoran.nakadai.entity.param.DeliveryOrderParam; |
||||
import com.huoran.nakadai.entity.param.OrderParam; |
||||
import com.huoran.nakadai.service.*; |
||||
import com.huoran.nakadai.utils.PageParam; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.format.annotation.DateTimeFormat; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.servlet.ServletOutputStream; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
import java.util.Arrays; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author lanhai |
||||
*/ |
||||
@Slf4j |
||||
@RestController |
||||
@RequestMapping("/platformOrder") |
||||
@Api(tags = "供应商平台后台订单管理") |
||||
@AllArgsConstructor |
||||
public class PlatformOrderController { |
||||
|
||||
@Autowired |
||||
private OrderService orderService; |
||||
|
||||
@Autowired |
||||
private OrderItemService orderItemService; |
||||
|
||||
@Autowired |
||||
private ProductService productService; |
||||
|
||||
@Autowired |
||||
private SkuService skuService; |
||||
|
||||
|
||||
/** |
||||
* 分页获取 |
||||
*/ |
||||
@GetMapping("/page") |
||||
@ApiOperation(value = "商机线索列表") |
||||
public ServerResponseEntity<IPage<Order>> page(OrderParam orderParam, PageParam<Order> page, HttpServletRequest request) { |
||||
orderParam.setShopId(SecurityUtils.getCurrentInfo().getShopId()); |
||||
IPage<Order> orderPage = orderService.pageOrdersDetailByOrderParam(page, orderParam); |
||||
return ServerResponseEntity.success(orderPage); |
||||
} |
||||
|
||||
/** |
||||
* 获取信息 |
||||
*/ |
||||
@GetMapping("/orderInfo/{orderNumber}") |
||||
public ServerResponseEntity<Order> info(@PathVariable("orderNumber") String orderNumber, HttpServletRequest request) { |
||||
Integer shopId = TokenUtils.getShopIdByJwtToken(request); |
||||
Order order = orderService.getOrderByOrderNumber(orderNumber); |
||||
if (!com.google.common.base.Objects.equal(shopId, order.getShopId())) { |
||||
throw new YamiShopBindException("您没有权限获取该订单信息"); |
||||
} |
||||
List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber); |
||||
order.setOrderItems(orderItems); |
||||
// UserAddrOrder userAddrOrder = userAddrOrderService.getById(order.getAddrOrderId());
|
||||
// order.setUserAddrOrder(userAddrOrder);
|
||||
return ServerResponseEntity.success(order); |
||||
} |
||||
|
||||
/** |
||||
* 发货 |
||||
*/ |
||||
@PutMapping("/delivery") |
||||
public ServerResponseEntity<Void> delivery(@RequestBody DeliveryOrderParam deliveryOrderParam, HttpServletRequest request) { |
||||
Integer shopId = TokenUtils.getShopIdByJwtToken(request); |
||||
Order order = orderService.getOrderByOrderNumber(deliveryOrderParam.getOrderNumber()); |
||||
if (!Objects.equal(shopId, order.getShopId())) { |
||||
throw new YamiShopBindException("您没有权限修改该订单信息"); |
||||
} |
||||
|
||||
Order orderParam = new Order(); |
||||
orderParam.setOrderId(order.getOrderId()); |
||||
orderParam.setDvyId(deliveryOrderParam.getDvyId()); |
||||
orderParam.setDvyFlowId(deliveryOrderParam.getDvyFlowId()); |
||||
orderParam.setDvyTime(new Date()); |
||||
orderParam.setStatus(OrderStatus.CONSIGNMENT.value()); |
||||
orderParam.setUserId(order.getUserId()); |
||||
|
||||
orderService.delivery(orderParam); |
||||
|
||||
List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(deliveryOrderParam.getOrderNumber()); |
||||
for (OrderItem orderItem : orderItems) { |
||||
productService.removeProductCacheByProdId(orderItem.getProdId()); |
||||
skuService.removeSkuCacheBySkuId(orderItem.getSkuId(),orderItem.getProdId()); |
||||
} |
||||
return ServerResponseEntity.success(); |
||||
} |
||||
|
||||
/** |
||||
* 打印待发货的订单表 |
||||
* |
||||
* @param order |
||||
* @param consignmentName 发件人姓名 |
||||
* @param consignmentMobile 发货人手机号 |
||||
* @param consignmentAddr 发货地址 |
||||
*/ |
||||
@GetMapping("/waitingConsignmentExcel") |
||||
public void waitingConsignmentExcel(Order order, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, |
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, String consignmentName, String consignmentMobile, |
||||
String consignmentAddr, HttpServletResponse response, HttpServletRequest request) { |
||||
Integer shopId = TokenUtils.getShopIdByJwtToken(request); |
||||
order.setShopId(shopId); |
||||
order.setStatus(OrderStatus.PADYED.value()); |
||||
List<Order> orders = orderService.listOrdersDetailByOrder(order, startTime, endTime); |
||||
|
||||
//通过工具类创建writer
|
||||
ExcelWriter writer = ExcelUtil.getBigWriter(); |
||||
Sheet sheet = writer.getSheet(); |
||||
sheet.setColumnWidth(0, 20 * 256); |
||||
sheet.setColumnWidth(1, 20 * 256); |
||||
sheet.setColumnWidth(2, 20 * 256); |
||||
sheet.setColumnWidth(3, 60 * 256); |
||||
sheet.setColumnWidth(4, 60 * 256); |
||||
sheet.setColumnWidth(7, 60 * 256); |
||||
sheet.setColumnWidth(8, 60 * 256); |
||||
sheet.setColumnWidth(9, 60 * 256); |
||||
// 待发货
|
||||
String[] hearder = {"订单编号", "收件人", "手机", "收货地址", "商品名称", "数量", "发件人姓名", "发件人手机号", "发货地址", "备注"}; |
||||
writer.merge(hearder.length - 1, "发货信息整理"); |
||||
writer.writeRow(Arrays.asList(hearder)); |
||||
|
||||
int row = 1; |
||||
for (Order dbOrder : orders) { |
||||
UserAddrOrder addr = dbOrder.getUserAddrOrder(); |
||||
String addrInfo = addr.getProvince() + addr.getCity() + addr.getArea() + addr.getAddr(); |
||||
List<OrderItem> orderItems = dbOrder.getOrderItems(); |
||||
row++; |
||||
for (OrderItem orderItem : orderItems) { |
||||
// 第0列开始
|
||||
int col = 0; |
||||
writer.writeCellValue(col++, row, dbOrder.getOrderNumber()); |
||||
writer.writeCellValue(col++, row, addr.getReceiver()); |
||||
writer.writeCellValue(col++, row, addr.getMobile()); |
||||
writer.writeCellValue(col++, row, addrInfo); |
||||
writer.writeCellValue(col++, row, orderItem.getProdName()); |
||||
writer.writeCellValue(col++, row, orderItem.getProdCount()); |
||||
writer.writeCellValue(col++, row, consignmentName); |
||||
writer.writeCellValue(col++, row, consignmentMobile); |
||||
writer.writeCellValue(col++, row, consignmentAddr); |
||||
writer.writeCellValue(col++, row, dbOrder.getRemarks()); |
||||
} |
||||
} |
||||
writeExcel(response, writer); |
||||
} |
||||
|
||||
/** |
||||
* 已销售订单 |
||||
* |
||||
* @param order |
||||
*/ |
||||
@GetMapping("/soldExcel") |
||||
public void soldExcel(Order order, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, |
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, HttpServletResponse response,HttpServletRequest request) { |
||||
Integer shopId = TokenUtils.getShopIdByJwtToken(request); |
||||
order.setShopId(shopId); |
||||
order.setIsPayed(1); |
||||
List<Order> orders = orderService.listOrdersDetailByOrder(order, startTime, endTime); |
||||
|
||||
//通过工具类创建writer
|
||||
ExcelWriter writer = ExcelUtil.getBigWriter(); |
||||
// 待发货
|
||||
String[] hearder = {"订单编号", "下单时间", "收件人", "手机", "收货地址", "商品名称", "数量", "订单应付", "订单运费", "订单实付"}; |
||||
Sheet sheet = writer.getSheet(); |
||||
sheet.setColumnWidth(0, 20 * 256); |
||||
sheet.setColumnWidth(1, 20 * 256); |
||||
sheet.setColumnWidth(3, 20 * 256); |
||||
sheet.setColumnWidth(4, 60 * 256); |
||||
sheet.setColumnWidth(5, 60 * 256); |
||||
|
||||
writer.merge(hearder.length - 1, "销售信息整理"); |
||||
writer.writeRow(Arrays.asList(hearder)); |
||||
|
||||
int row = 1; |
||||
for (Order dbOrder : orders) { |
||||
UserAddrOrder addr = dbOrder.getUserAddrOrder(); |
||||
String addrInfo = addr.getProvince() + addr.getCity() + addr.getArea() + addr.getAddr(); |
||||
List<OrderItem> orderItems = dbOrder.getOrderItems(); |
||||
int firstRow = row + 1; |
||||
int lastRow = row + orderItems.size(); |
||||
int col = -1; |
||||
// 订单编号
|
||||
mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getOrderNumber()); |
||||
// 下单时间
|
||||
mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getCreateTime()); |
||||
// 收件人
|
||||
mergeIfNeed(writer, firstRow, lastRow, ++col, col, addr.getReceiver()); |
||||
// "手机"
|
||||
mergeIfNeed(writer, firstRow, lastRow, ++col, col, addr.getMobile()); |
||||
// "收货地址"
|
||||
mergeIfNeed(writer, firstRow, lastRow, ++col, col, addrInfo); |
||||
int prodNameCol = ++col; |
||||
int prodCountCol = ++col; |
||||
for (OrderItem orderItem : orderItems) { |
||||
row++; |
||||
// 商品名称
|
||||
writer.writeCellValue(prodNameCol, row, orderItem.getProdName()); |
||||
// 数量
|
||||
writer.writeCellValue(prodCountCol, row, orderItem.getProdCount()); |
||||
} |
||||
// 订单应付
|
||||
mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getTotal()); |
||||
// 订单运费
|
||||
mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getFreightAmount()); |
||||
// 订单实付
|
||||
mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getActualTotal()); |
||||
|
||||
} |
||||
writeExcel(response, writer); |
||||
} |
||||
|
||||
/** |
||||
* 如果需要合并的话,就合并 |
||||
*/ |
||||
private void mergeIfNeed(ExcelWriter writer, int firstRow, int lastRow, int firstColumn, int lastColumn, Object content) { |
||||
if (content instanceof Date) { |
||||
content = DateUtil.format((Date) content, DatePattern.NORM_DATETIME_PATTERN); |
||||
} |
||||
if (lastRow - firstRow > 0 || lastColumn - firstColumn > 0) { |
||||
writer.merge(firstRow, lastRow, firstColumn, lastColumn, content, false); |
||||
} else { |
||||
writer.writeCellValue(firstColumn, firstRow, content); |
||||
} |
||||
|
||||
} |
||||
|
||||
private void writeExcel(HttpServletResponse response, ExcelWriter writer) { |
||||
//response为HttpServletResponse对象
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
||||
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
|
||||
response.setHeader("Content-Disposition", "attachment;filename=1.xls"); |
||||
|
||||
ServletOutputStream servletOutputStream = null; |
||||
try { |
||||
servletOutputStream = response.getOutputStream(); |
||||
writer.flush(servletOutputStream); |
||||
servletOutputStream.flush(); |
||||
} catch (IORuntimeException | IOException e) { |
||||
log.error("写出Excel错误:", e); |
||||
} finally { |
||||
IoUtil.close(writer); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,250 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.controller; |
||||
|
||||
import cn.hutool.core.collection.CollectionUtil; |
||||
import cn.hutool.core.map.MapUtil; |
||||
import com.google.common.collect.Lists; |
||||
import com.huoran.common.response.ServerResponseEntity; |
||||
import com.huoran.common.utils.SecurityUtils; |
||||
import com.huoran.nakadai.entity.app.dto.*; |
||||
import com.huoran.nakadai.entity.app.param.ChangeShopCartParam; |
||||
import com.huoran.nakadai.entity.app.param.ShopCartParam; |
||||
import com.huoran.nakadai.entity.model.Basket; |
||||
import com.huoran.nakadai.entity.model.Product; |
||||
import com.huoran.nakadai.entity.model.Sku; |
||||
import com.huoran.nakadai.event.ShopCartEvent; |
||||
import com.huoran.nakadai.service.BasketService; |
||||
import com.huoran.nakadai.service.ProductService; |
||||
import com.huoran.nakadai.service.SkuService; |
||||
import com.huoran.nakadai.utils.Arith; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.validation.Valid; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Objects; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @author lanhai |
||||
*/ |
||||
@RestController |
||||
@RequestMapping("/shopCart") |
||||
@Api(tags = "购物车管理") |
||||
@AllArgsConstructor |
||||
public class ShopCartController { |
||||
|
||||
private final BasketService basketService; |
||||
|
||||
private final ProductService productService; |
||||
|
||||
private final SkuService skuService; |
||||
|
||||
private final ApplicationContext applicationContext; |
||||
|
||||
/** |
||||
* 获取用户购物车信息 |
||||
* |
||||
* @param basketIdShopCartParamMap 购物车参数对象列表 |
||||
* @return |
||||
*/ |
||||
@PostMapping("/info") |
||||
@ApiOperation(value = "获取用户购物车信息,参数为用户选中的活动项数组,以购物车id为key") |
||||
public ServerResponseEntity<List<ShopCartDto>> info(@RequestBody Map<Long, ShopCartParam> basketIdShopCartParamMap) { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
|
||||
// 更新购物车信息,
|
||||
if (MapUtil.isNotEmpty(basketIdShopCartParamMap)) { |
||||
basketService.updateBasketByShopCartParam(userId, basketIdShopCartParamMap); |
||||
} |
||||
|
||||
// 拿到购物车的所有item
|
||||
List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId); |
||||
return ServerResponseEntity.success(basketService.getShopCarts(shopCartItems)); |
||||
|
||||
} |
||||
|
||||
@PostMapping("/deleteItem") |
||||
@ApiOperation("通过购物车id删除用户购物车物品") |
||||
public ServerResponseEntity<Void> deleteItem(@RequestBody List<Integer> basketIds) { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
basketService.deleteShopCartItemsByBasketIds(userId, basketIds); |
||||
return ServerResponseEntity.success(); |
||||
} |
||||
|
||||
@PostMapping("/deleteAll") |
||||
@ApiOperation("清空用户购物车所有物品") |
||||
public ServerResponseEntity<String> deleteAll() { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
basketService.deleteAllShopCartItems(userId); |
||||
return ServerResponseEntity.success("删除成功"); |
||||
} |
||||
|
||||
@PostMapping("/changeItem") |
||||
@ApiOperation(value = "添加、修改用户购物车物品,通过商品id(prodId)、skuId、店铺Id(shopId),添加/修改用户购物车商品,并传入改变的商品个数(count)," + |
||||
"当count为正值时,增加商品数量,当count为负值时,将减去商品的数量,当最终count值小于0时,会将商品从购物车里面删除") |
||||
public ServerResponseEntity<String> addItem(@Valid @RequestBody ChangeShopCartParam param) { |
||||
|
||||
if (param.getCount() == 0) { |
||||
return ServerResponseEntity.showFailMsg("输入更改数量"); |
||||
} |
||||
|
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId); |
||||
Product prodParam = productService.getProductByProdId(param.getProdId()); |
||||
Sku skuParam = skuService.getSkuBySkuId(param.getSkuId()); |
||||
|
||||
// 当商品状态不正常时,不能添加到购物车
|
||||
if (prodParam.getStatus() != 1 || skuParam.getStatus() != 1) { |
||||
return ServerResponseEntity.showFailMsg("当前商品已下架"); |
||||
} |
||||
for (ShopCartItemDto shopCartItemDto : shopCartItems) { |
||||
if (Objects.equals(param.getSkuId(), shopCartItemDto.getSkuId())) { |
||||
Basket basket = new Basket(); |
||||
basket.setUserId(userId); |
||||
basket.setBasketCount(param.getCount() + shopCartItemDto.getProdCount()); |
||||
basket.setBasketId(shopCartItemDto.getBasketId()); |
||||
|
||||
// 防止购物车变成负数
|
||||
if (basket.getBasketCount() <= 0) { |
||||
basketService.deleteShopCartItemsByBasketIds(userId, Collections.singletonList(basket.getBasketId())); |
||||
return ServerResponseEntity.success(); |
||||
} |
||||
|
||||
// 当sku实际库存不足时,不能添加到购物车
|
||||
if (skuParam.getStocks() < basket.getBasketCount() && shopCartItemDto.getProdCount() > 0) { |
||||
return ServerResponseEntity.showFailMsg("库存不足"); |
||||
} |
||||
basketService.updateShopCartItem(basket); |
||||
return ServerResponseEntity.success(); |
||||
} |
||||
} |
||||
|
||||
// 防止购物车已被删除的情况下,添加了负数的商品
|
||||
if (param.getCount() < 0) { |
||||
return ServerResponseEntity.showFailMsg("商品已从购物车移除"); |
||||
} |
||||
// 当sku实际库存不足时,不能添加到购物车
|
||||
if (skuParam.getStocks() < param.getCount()) { |
||||
return ServerResponseEntity.showFailMsg("库存不足"); |
||||
} |
||||
// 所有都正常时
|
||||
basketService.addShopCartItem(param,userId); |
||||
return ServerResponseEntity.success("添加成功"); |
||||
} |
||||
|
||||
@GetMapping("/prodCount") |
||||
@ApiOperation(value = "获取所有购物车商品数量") |
||||
public ServerResponseEntity<Integer> prodCount() { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId); |
||||
if (CollectionUtil.isEmpty(shopCartItems)) { |
||||
return ServerResponseEntity.success(0); |
||||
} |
||||
Integer totalCount = shopCartItems.stream().map(ShopCartItemDto::getProdCount).reduce(0, Integer::sum); |
||||
return ServerResponseEntity.success(totalCount); |
||||
} |
||||
|
||||
@GetMapping("/expiryProdList") |
||||
@ApiOperation(value = "获取购物车失效商品列表") |
||||
public ServerResponseEntity<List<ShopCartExpiryItemDto>> expiryProdList() { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
List<ShopCartItemDto> shopCartItems = basketService.getShopCartExpiryItems(userId); |
||||
//根据店铺ID划分item
|
||||
Map<Integer, List<ShopCartItemDto>> shopCartItemDtoMap = shopCartItems.stream().collect(Collectors.groupingBy(ShopCartItemDto::getShopId)); |
||||
|
||||
// 返回一个店铺对应的所有信息
|
||||
List<ShopCartExpiryItemDto> shopcartExpiryitems = Lists.newArrayList(); |
||||
|
||||
for (Integer key : shopCartItemDtoMap.keySet()) { |
||||
ShopCartExpiryItemDto shopCartExpiryItemDto = new ShopCartExpiryItemDto(); |
||||
shopCartExpiryItemDto.setShopId(key); |
||||
List<ShopCartItemDto> shopCartItemDtos = Lists.newArrayList(); |
||||
for (ShopCartItemDto tempShopCartItemDto : shopCartItemDtoMap.get(key)) { |
||||
shopCartExpiryItemDto.setShopName(tempShopCartItemDto.getShopName()); |
||||
shopCartItemDtos.add(tempShopCartItemDto); |
||||
} |
||||
shopCartExpiryItemDto.setShopCartItemDtoList(shopCartItemDtos); |
||||
shopcartExpiryitems.add(shopCartExpiryItemDto); |
||||
} |
||||
|
||||
return ServerResponseEntity.success(shopcartExpiryitems); |
||||
} |
||||
|
||||
@PostMapping("/cleanExpiryProdList") |
||||
@ApiOperation(value = "清空用户失效商品") |
||||
public ServerResponseEntity<Void> cleanExpiryProdList() { |
||||
String userId = SecurityUtils.getCurrentInfo().getAccountId(); |
||||
basketService.cleanExpiryProdList(userId); |
||||
return ServerResponseEntity.success(); |
||||
} |
||||
|
||||
@PostMapping("/totalPay") |
||||
@ApiOperation(value = "获取选中购物项总计、选中的商品数量,参数为购物车id数组") |
||||
public ServerResponseEntity<ShopCartAmountDto> getTotalPay(@RequestBody List<Long> basketIds) { |
||||
|
||||
// 拿到购物车的所有item
|
||||
List<ShopCartItemDto> dbShopCartItems = basketService.getShopCartItems(SecurityUtils.getCurrentInfo().getAccountId()); |
||||
|
||||
List<ShopCartItemDto> chooseShopCartItems = dbShopCartItems |
||||
.stream() |
||||
.filter(shopCartItemDto -> { |
||||
for (Long basketId : basketIds) { |
||||
if (Objects.equals(basketId,shopCartItemDto.getBasketId())) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
}) |
||||
.collect(Collectors.toList()); |
||||
|
||||
// 根据店铺ID划分item
|
||||
Map<Integer, List<ShopCartItemDto>> shopCartMap = chooseShopCartItems.stream().collect(Collectors.groupingBy(ShopCartItemDto::getShopId)); |
||||
|
||||
double total = 0.0; |
||||
int count = 0; |
||||
double reduce = 0.0; |
||||
for (Integer shopId : shopCartMap.keySet()) { |
||||
//获取店铺的所有商品项
|
||||
List<ShopCartItemDto> shopCartItemDtoList = shopCartMap.get(shopId); |
||||
// 构建每个店铺的购物车信息
|
||||
ShopCartDto shopCart = new ShopCartDto(); |
||||
shopCart.setShopId(shopId); |
||||
|
||||
applicationContext.publishEvent(new ShopCartEvent(shopCart, shopCartItemDtoList)); |
||||
|
||||
List<ShopCartItemDiscountDto> shopCartItemDiscounts = shopCart.getShopCartItemDiscounts(); |
||||
|
||||
for (ShopCartItemDiscountDto shopCartItemDiscount : shopCartItemDiscounts) { |
||||
List<ShopCartItemDto> shopCartItems = shopCartItemDiscount.getShopCartItems(); |
||||
|
||||
for (ShopCartItemDto shopCartItem : shopCartItems) { |
||||
count = shopCartItem.getProdCount() + count; |
||||
total = Arith.add(shopCartItem.getProductTotalAmount(), total); |
||||
} |
||||
} |
||||
} |
||||
ShopCartAmountDto shopCartAmountDto = new ShopCartAmountDto(); |
||||
shopCartAmountDto.setCount(count); |
||||
shopCartAmountDto.setTotalMoney(total); |
||||
shopCartAmountDto.setSubtractMoney(reduce); |
||||
shopCartAmountDto.setFinalMoney(Arith.sub(shopCartAmountDto.getTotalMoney(), shopCartAmountDto.getSubtractMoney())); |
||||
|
||||
return ServerResponseEntity.success(shopCartAmountDto); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,51 @@ |
||||
/* |
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||
* |
||||
* https://www.mall4j.com/
|
||||
* |
||||
* 未经允许,不可做商业用途! |
||||
* |
||||
* 版权所有,侵权必究! |
||||
*/ |
||||
|
||||
package com.huoran.nakadai.controller; |
||||
|
||||
import cn.hutool.core.bean.BeanUtil; |
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.huoran.common.response.ServerResponseEntity; |
||||
import com.huoran.nakadai.entity.app.dto.SkuDto; |
||||
import com.huoran.nakadai.entity.model.Sku; |
||||
import com.huoran.nakadai.service.SkuService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author lanhai |
||||
*/ |
||||
@RestController |
||||
@RequestMapping("/sku") |
||||
@Api(tags = "sku规格管理") |
||||
@AllArgsConstructor |
||||
public class SkuController { |
||||
|
||||
private final SkuService skuService; |
||||
|
||||
|
||||
@GetMapping("/getSkuList") |
||||
@ApiOperation(value = "通过prodId获取商品全部规格列表") |
||||
public ServerResponseEntity<List<SkuDto>> getSkuListByProdId(Integer prodId) { |
||||
List<Sku> skus = skuService.list(new LambdaQueryWrapper<Sku>() |
||||
.eq(Sku::getStatus, 1) |
||||
.eq(Sku::getIsDelete, 0) |
||||
.eq(Sku::getProdId, prodId) |
||||
); |
||||
List<SkuDto> skuDtoList = BeanUtil.copyToList(skus, SkuDto.class); |
||||
return ServerResponseEntity.success(skuDtoList); |
||||
} |
||||
} |
Loading…
Reference in new issue