当前位置:网站首页>6. package management business development

6. package management business development

2022-06-24 10:16:00 xjhqre

Package management business development

1、 preparation

Prepare the required classes and interfaces

  1. Entity class SetmealDish
  2. DTO SetmealDto
  3. Mapper Interface SetmealDishMapper
  4. Business layer interface SetmealDishService
  5. Business layer implementation classes SetmealDishServiceImpl
  6. Control layer SetmealController

2、 New package

Execute the process :

  1. page (backend/page/comboladd.html) send out ajax request , Request the server to obtain the package classification data and display it in the drop-down box
  2. Page sending ajax request , Get the dish classification data and add it to the service side
  3. Page sending ajax request , Request server , Query the corresponding dish data according to the dish classification and display it in the add dish window
  4. The page sends a request for image upload , Request the server to save the picture to the server
  5. The page sends a request for image download , Echo the uploaded picture
  6. Click the save button , send out ajax request , The data related to the package is displayed in json Submit to the server in the form of

2.1、 Query the menu classification list

stay DishController establish list Method

/** *  Query the corresponding dish data according to the conditions  * @param dish * @return */
@GetMapping("/list")
public R<List<Dish>> list(Dish dish) {
    

    //  Paparazzi query criteria 
    LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
    //  The query status is 1 The dishes 
    queryWrapper.eq(Dish::getStatus, 1);

    //  Add sorting criteria 
    queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

    List<Dish> list = dishService.list(queryWrapper);

    return R.success(list);
}

2.2、 Alibaba cloud OSS Picture echo ( Optional )

modify backend/page/combo/add.html The following pages , Change to your Alibaba cloud address plus the image name

image-20220619153241714

2.3、 Save new information

2.3.1、SetmealController

package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/** * @Author: xjhqre * @DateTime: 2022/6/19 15:36 */
@RequestMapping("/setmeal")
@RestController
@Slf4j
public class SetmealController {
    

    @Autowired
    SetmealService setmealService;

    @Autowired
    SetmealDishService setmealDishService;

    /** *  New package  * @param setmealDto * @return */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
    
        log.info(" New package information :{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success(" Successfully added package ");
    }
}

2.3.2、SetmealService

//  New package , At the same time, you need to save the relationship between packages and dishes 
void saveWithDish(SetmealDto setmealDto);

2.3.3、SetmealServiceImpl

/** *  New package , At the same time, you need to save the relationship between packages and dishes  * @param setmealDto */
@Override
@Transactional
public void saveWithDish(SetmealDto setmealDto) {
    
    //  Save the basic information of the package , operation setmeal, perform insert operation 
    this.save(setmealDto);

    List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
    setmealDishes = setmealDishes.stream().map(item -> {
    
        item.setSetmealId(setmealDto.getId());
        return item;
    }).collect(Collectors.toList());

    //  Save the related information of packages and dishes , operation setmeal_dish perform insert operation 
    setmealDishService.saveBatch(setmealDishes);
}

2.4、 Test the new package

3、 Paging query of package information

Execute the process :

  1. page (backend/page/combo/list.html) send out ajax request , Paging query parameters (page、pageSize.name) Submit to the server , Get paging data
  2. Page send request , Request the server to download pictures , For page image display

3.1、 Method 1 : The teacher's method

stay SetmealController establish page Method

/** *  Paging query of package information  * @param page * @param pageSize * @param name * @return */
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name) {
    
    //  Page builder object 
    Page<Setmeal> pageInfo = new Page<>(page, pageSize);
    Page<SetmealDto> dtoPage = new Page<>();

    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    //  Add query criteria , according to name Conduct like Fuzzy query 
    queryWrapper.like(name != null, Setmeal::getName, name);
    //  Add sorting criteria , Sort by update time in descending order 
    queryWrapper.orderByDesc(Setmeal::getUpdateTime);

    setmealService.page(pageInfo, queryWrapper);

    //  Object Copy 
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<Setmeal> records = pageInfo.getRecords();

    List<SetmealDto> list = records.stream().map(item -> {
    
        SetmealDto setmealDto = new SetmealDto();
        //  Object Copy 
        BeanUtils.copyProperties(item, setmealDto);
        //  classification id
        Long categoryId = item.getCategoryId();
        //  According to the classification id Query classification objects 
        Category category = categoryService.getById(categoryId);
        if (category != null) {
    
            //  Category name 
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
        }
        return setmealDto;
    }).collect(Collectors.toList());

    dtoPage.setRecords(list);
    return R.success(pageInfo);
}

3.2、 Method 2 : Join table query method

3.2.1、SetmealController

/** *  Paging query of package information  * @param page * @param pageSize * @param name * @return */
@GetMapping("/page")
public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
    
    //  Paging constructor 
    Page<SetmealDto> pageInfo = new Page<>(page, pageSize);

    setmealService.querySetmealPageWithCategoryName(pageInfo, name);

    return R.success(pageInfo);
}

3.2.2、SetmealService

//  Query the package paging information with category name 
void querySetmealPageWithCategoryName(Page<SetmealDto> pageInfo, String name);

3.2.3、SetmealServiceImpl

/** *  Query the package paging information with category name  * @param pageInfo * @param name */
@Override
public void querySetmealPageWithCategoryName(Page<SetmealDto> pageInfo, String name) {
    
    List<SetmealDto> records = setmealMapper.querySetmealPageWithCategoryName(pageInfo, name == null ? "" : name);
    pageInfo.setRecords(records);
}

3.2.4、SetmealMapper

@Select("SELECT a.*, b.`name` AS category_name FROM setmeal a JOIN category b ON a.`category_id` = b.`id` WHERE a.`name` LIKE CONCAT('%',#{name},'%') AND a.`is_deleted` = 0 ORDER BY sort")
List<SetmealDto> querySetmealPageWithCategoryName(Page<SetmealDto> pageInfo, String name);

3.3、 Package management page Alibaba cloud OSS Picture shows

modify backend/page/combo/list.html There are two places in the document

image-20220619171621422

image-20220619171653708

3.4、 Test paging query

image-20220619173331821

4、 Delete package information

  1. The front end sends a delete request , The parameter form is ids=id1,id2,...
  2. The back end receives requests , according to id Delete the table first setmeal_dish Table contents , In the delete setmeal Table contents

The deletion method is logical deletion

4.1 modify Setmeal and SetmealDish Entity class

Modify the... Of two entity classes isDeleted attribute , add @TableLogic(value = "0", delval = "1") annotation . For logical deletion . After marking this note , call mybatis-plus The deletion method given in the framework , Will turn into pairs is_deleted Field update method

// Whether or not to delete 
@TableLogic(value = "0", delval = "1")
private Integer isDeleted;

4.2、SetmealController

establish delete Method

/** *  Delete package  * @param ids * @return */
@DeleteMapping
public R<String> delete(@RequestParam Long[] ids) {
    
    log.info(" To delete the package ids:{}", Arrays.toString(ids));
    
    setmealService.removeSetmealWithDish(ids);
    
    return R.success(" Package data deleted successfully ");
}

4.3、SetmealService

//  Delete package , At the same time, you need to delete the associated data of packages and dishes 
void removeSetmealWithDish(Long[] ids);

4.4、SetmealServiceImpl

/** *  Delete package , At the same time, you need to delete the associated data of packages and dishes  * @param ids */
@Override
@Transactional
public void removeSetmealWithDish(Long[] ids) {
    
    //  Query package status , Determine if delete... Is available 
    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.in(Setmeal::getId, ids);
    queryWrapper.eq(Setmeal::getStatus, 1);
    int count = this.count(queryWrapper);
    if (count > 0) {
    
        //  The current package is for sale , Can't delete 
        throw new CustomException(" The package is on sale , Can't delete ");
    }

    //  Delete if available , Delete the table first setmeal_dish Information about the associated dishes in 
    // delete from setmeal_dish where setmeal_id in ()
    LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
    setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
    setmealDishService.remove(setmealDishLambdaQueryWrapper);

    //  Then delete the package information 
    this.removeByIds(Arrays.asList(ids));
}

4.5、 Test delete package

5、 Modify package information ( Optional )

5.1、 Package data echo

Click Modify , The front page will send a query request , with id Parameters

image-20220619195308895

We need to return a SetmealDto Object of type

5.1.1、SetmealController

/** *  according to id Query package information and corresponding dish information  * @param id * @return */
@GetMapping("/{id}")
public R<SetmealDto> get(@PathVariable Long id) {
    
    SetmealDto setmealDto = setmealService.getByIdWithDish(id);
    return R.success(setmealDto);
}

5.1.2、SetmealService

//  according to id Query package information and corresponding dish information 
SetmealDto getByIdWithDish(Long id);

5.1.3、SetmealServiceImpl

/** *  according to id Query package information and corresponding dish information  * @param id * @return */
@Override
@Transactional
public SetmealDto getByIdWithDish(Long id) {
    
    //  Query the basic information of the package 
    Setmeal setmeal = this.getById(id);

    SetmealDto setmealDto = new SetmealDto();
    BeanUtils.copyProperties(setmeal, setmealDto);

    //  Query the menu information corresponding to the current package 
    // select * from setmeal_dish where setmeal_id = id;
    LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(SetmealDish::getSetmealId, id);
    List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

    setmealDto.setSetmealDishes(setmealDishes);

    return setmealDto;
}

5.1.4、 Alibaba cloud OSS Picture echo ( Optional )

modify backend/page/combo/add.html The document contains the following

image-20220619200351111

5.1.5、 Test data echo

5.2、 Save modification information

Delete first setmeal All corresponding dish, Adding

5.2.1、SetmealController

/** *  Modify package  * @param setmealDto * @return */
@PutMapping
public R<String> update(@RequestBody SetmealDto setmealDto) {
    
    log.info(" Save and modify package information :{}", setmealDto);
    setmealService.updateSetmealWithDish(setmealDto);
    return R.success(" Package modification succeeded ");
}

5.2.2、SetmealService

//  Update package information 
void updateSetmealWithDish(SetmealDto setmealDto);

5.2.3、SetmealServiceImpl

/** *  Update package information  * @param setmealDto */
@Override
public void updateSetmealWithDish(SetmealDto setmealDto) {
    
    //  to update setmeal Table basic information 
    this.updateById(setmealDto);

    //  Delete the original corresponding dish information 
    LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
    setmealDishService.remove(queryWrapper);

    //  Add new dish information 
    List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
    setmealDishes = setmealDishes.stream().peek(item -> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());
    setmealDishService.saveBatch(setmealDishes);
}

5.2.4、 Test modification package

6、 Start selling in batches / Stop selling package ( Optional )

Front end page request :

image-20220619215306929

6.1、SetmealController

/** *  Start selling in batches / Stop selling package  * @param status * @param ids * @return */
@PostMapping("/status/{status}")
public R<String> status(@PathVariable("status") int status, @RequestParam Long[] ids) {
    
    log.info(" Start and stop selling in batches :{}", status, Arrays.toString(ids));
    setmealService.updateStatusByIds(status, ids);
    return R.success(" Successful start and stop of batch sales ");
}

6.2、SetmealService

//  Start and stop package in batch 
void updateStatusByIds(int status, Long[] ids);

6.3、SetmealServiceImpl

do not know why mybat-plus There is no method for batch modification .

Here I directly update in the loop for convenience .

Another way is to mapper Batch update in the file .

I don't know which method is more elegant . If there is a big guy who knows, I hope I can answer it in the comment area .

/** *  Start and stop package in batch  * @param status * @param ids */
@Override
public void updateStatusByIds(int status, Long[] ids) {
    
    //  Query all corresponding id Of setmeal
    List<Setmeal> setmeals = setmealMapper.selectBatchIds(Arrays.asList(ids));

    //  Loop through updates 
    setmeals.forEach(item -> {
    
        item.setStatus(status);
        setmealMapper.updateById(item);
    });
}

6.4、 Test the start and stop of batch sales

原网站

版权声明
本文为[xjhqre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240916020957.html