【开源】SpringBoot框架开发固始鹅块销售系统-五、核心代码

时间:2024-03-10 08:19:43

5.1 查询鹅块

@RequestMapping(value = "/getSellPage", method = RequestMethod.GET)
@ApiOperation(value = "查询商品")
public Result<IPage<Product>> getSellPage(@ModelAttribute Product product,@ModelAttribute PageVo page){
    QueryWrapper<Product> qw = new QueryWrapper<>();
    User currUser = securityUtil.getCurrUser();
    QueryWrapper<SupermarketBill> billQw = new QueryWrapper<>();
    billQw.eq("user_id",currUser.getId());
    billQw.eq("status","未付款");
    List<SupermarketBill> billList = iSupermarketBillService.list(billQw);
    if(!ZwzNullUtils.isNull(product.getTitle())) {
        qw.like("title",product.getTitle());
    }
    if(!ZwzNullUtils.isNull(product.getType())) {
        qw.eq("type",product.getType());
    }
    IPage<Product> data = iProductService.page(PageUtil.initMpPage(page),qw);
    for (Product vo : data.getRecords()) {
        vo.setNumber(BigDecimal.ZERO);
    }
    for (Product vo : data.getRecords()) {
        for (SupermarketBill bill : billList) {
            if(Objects.equals(vo.getId(),bill.getProductId())) {
                vo.setNumber(vo.getNumber().add(bill.getNumber()));
            }
        }
    }
    return new ResultUtil<IPage<Product>>().setData(data);
}

5.2 查询鹅块类型

@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
@ApiOperation(value = "查询商品类型")
public Result<IPage<ProductType>> getByPage(@ModelAttribute ProductType productType ,@ModelAttribute PageVo page){
    QueryWrapper<ProductType> qw = new QueryWrapper<>();
    if(!ZwzNullUtils.isNull(productType.getTitle())) {
        qw.like("title",productType.getTitle());
    }
    if(!ZwzNullUtils.isNull(productType.getStatus())) {
        qw.eq("status",productType.getStatus());
    }
    IPage<ProductType> data = iProductTypeService.page(PageUtil.initMpPage(page),qw);
    return new ResultUtil<IPage<ProductType>>().setData(data);
}

5.3 鹅块下单

@RequestMapping(value = "/addOne", method = RequestMethod.GET)
@ApiOperation(value = "下单")
public Result<Object> addOne(@RequestParam String id){
    Product p = iProductService.getById(id);
    if(p == null) {
        return ResultUtil.error("商品不存在");
    }
    User currUser = securityUtil.getCurrUser();
    /**
        * 判断是否下单
        */
    QueryWrapper<SupermarketBill> billQw = new QueryWrapper<>();
    billQw.eq("product_id",id);
    billQw.eq("user_id",currUser.getId());
    billQw.eq("status","未付款");
    billQw.last("limit 1");
    SupermarketBill bill = iSupermarketBillService.getOne(billQw);
    if(bill != null) {
        bill.setNumber(bill.getNumber().add(BigDecimal.ONE));
        bill.setSum(bill.getPrice().multiply(bill.getNumber()));
        iSupermarketBillService.saveOrUpdate(bill);
        return ResultUtil.success();
    }
    /**
        * 下单
        */
    SupermarketBill b = new SupermarketBill();
    b.setProductId(p.getId());
    b.setProductName(p.getTitle());
    b.setProductImage(p.getImage());
    b.setUserId(currUser.getId());
    b.setUserName(currUser.getNickname());
    b.setPrice(p.getPrice());
    b.setNumber(BigDecimal.ONE);
    b.setSum(p.getPrice());
    b.setStatus("未付款");
    iSupermarketBillService.saveOrUpdate(b);
    return ResultUtil.success();
}

5.4 撤销鹅块下单

@RequestMapping(value = "/cancelOne", method = RequestMethod.GET)
@ApiOperation(value = "撤销下单")
public Result<Object> cancelOne(@RequestParam String id) {
    Product p = iProductService.getById(id);
    if (p == null) {
        return ResultUtil.error("商品不存在");
    }
    User currUser = securityUtil.getCurrUser();
    QueryWrapper<SupermarketBill> billQw = new QueryWrapper<>();
    billQw.eq("product_id",id);
    billQw.eq("user_id",currUser.getId());
    billQw.eq("status","未付款");
    billQw.last("limit 1");
    SupermarketBill bill = iSupermarketBillService.getOne(billQw);
    if(bill != null && bill.getNumber().compareTo(BigDecimal.ONE) > 0) {
        bill.setNumber(bill.getNumber().subtract(BigDecimal.ONE));
        bill.setSum(bill.getPrice().multiply(bill.getNumber()));
        iSupermarketBillService.saveOrUpdate(bill);
    }
    return ResultUtil.success();
}

5.5 新增留言

@RequestMapping(value = "/insert", method = RequestMethod.POST)
@ApiOperation(value = "新增留言")
public Result<Message> insert(Message message){
    User currUser = securityUtil.getCurrUser();
    message.setUserId(currUser.getId());
    message.setUserName(currUser.getNickname());
    message.setTime(DateUtil.now());
    message.setReplyContent("");
    message.setReplyName("");
    message.setReplyId("");
    message.setReplyTime("");
    iMessageService.saveOrUpdate(message);
    return new ResultUtil<Message>().setData(message);
}