栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

最新Springboot+mp核心技术外卖入门实战项目(三)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

最新Springboot+mp核心技术外卖入门实战项目(三)

有关套餐的实体类

@Data
public class Setmeal implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    //分类id
    private Long categoryId;
    //套餐名称
    private String name;
    //套餐价格
    private BigDecimal price;
    //状态 0:停用 1:启用
    private Integer status;
    //编码
    private String code;
    //描述信息
    private String description;
    //图片
    private String image;

    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT_UPDATe)
    private LocalDateTime updateTime;
    
    @TableField(fill = FieldFill.INSERT)
    private Long createUser;
    
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;
}

@Data
public class SetmealDto extends Setmeal {

    private List setmealDishes;

    private String categoryName;
}

@Data
public class SetmealDish implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    
    //套餐id
    private Long setmealId;
    //菜品id
    private Long dishId;
    //菜品名称 (冗余字段)
    private String name;
    //菜品原价
    private BigDecimal price;
    //份数
    private Integer copies;
    //排序
    private Integer sort;

    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    @TableField(fill = FieldFill.INSERT)
    private Long createUser;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;

    //是否删除
    private Integer deleted;
}

@Data
public class SetmealDto extends Setmeal {

    private List setmealDishes;

    private String categoryName;
}

已经写出的接口

public interface IDishService extends IService {

    //新增菜品,同时插入菜品对应的口味数据,需要操作dish和dish_flavor两张表
    void saveWithFlavor(DishDto dishDto);

    Page page(int page, int pageSize, String name);

    //根据id查询菜品信息和对应的口味信息
    DishDto getByIdWithFlavor(Long id);

    void updateWithFlavor(DishDto dishDto);

    //根据条件查询对应的菜品数据
    List list(Dish dish);
}

服务层实现类

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl implements ISetmealService {

    @Autowired
    private ISetmealDishService setmealDishService;

    @Autowired
    private ICategoryService categoryService;

    
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);//因为这两个之间有映射关系 对应属性会自动保存
        List setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联关系,操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    
    @Override
    public Page page(int page, int pageSize, String name) {
        Page pageInfo = new Page<>(page, pageSize);
        Page dtoPage = new Page<>();
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name)
                    .orderByDesc(Setmeal::getUpdateTime);
        this.page(pageInfo, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List records = pageInfo.getRecords();

        List list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return dtoPage;
    }

    
    @Override
    @Transactional
    public void removeWithDish(List ids) {
        //select count(*) from setmeal where id in(1, 2, 3) and status = 1
        //查询套餐状态,确定是否可用删除
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids)
                    .eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除,抛出一个业务异常
            throw new CustomException("套餐正在售卖中,不能删除");
        }
        //可以删除就先删除套餐表中的数据 -- setmeal
        this.removeByIds(ids);

        //删除关系表中的数据 -- setmeal_dish
        //此时不能直接使用setmealDishService.removeByIds(ids) 当前套餐id并不是关系表中的id
        //delete from setmeal_dish where setmeal_id in (1, 2, 3)
        LambdaQueryWrapper LambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(LambdaQueryWrapper);
    }
}

套餐controller层

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private ISetmealDishService setmealDishService;

    @ApiOperation(value = "保存", notes = "新增套餐")
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息:{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    @ApiOperation(value = "查询", notes = "套餐分页查询")
    @GetMapping("/page")
    public R page(int page, int pageSize, String name){
        Page pageInfo = setmealService.page(page, pageSize, name);
        return R.success(pageInfo);
    }

    @ApiOperation(value = "删除", notes = "删除单个或多个套餐")
    @DeleteMapping
    public R delete(@RequestParam List ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
}

我们在完善套餐功能时还在dish中增加了一个功能,因为增加套餐时需要查询正在售卖的菜品,停售的菜品是不能加入到套餐中的

//根据条件查询对应的菜品数据
    List list(Dish dish);


    @Override
    public List list(Dish dish) {
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId())
                    .eq(Dish::getStatus, 1)//添加条件 状态为1(起售)
                    .orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        return this.list(queryWrapper);
    }

@ApiOperation(value = "查询", notes = "根据条件查询对应的菜品数据")
    @GetMapping("/list")
    public R> list(Dish dish){
        List list = dishService.list(dish);
        return R.success(list);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号