package cn.edu.hactcm.cotroller;

import cn.edu.hactcm.common.R;
import cn.edu.hactcm.dto.SetmealDto;
import cn.edu.hactcm.entity.Category;
import cn.edu.hactcm.entity.Setmeal;
import cn.edu.hactcm.service.CategoryService;
import cn.edu.hactcm.service.SetmealDishService;
import cn.edu.hactcm.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @Transactional
    public R<String> saveMealDish(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功！");
    }

    /**
     * 分页展示套餐信息，实现思路同分页展示菜品管理相关信息（/dish/page）
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page,int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage);
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal setmeal : setmealPage.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            list.add(setmealDto);
        }

        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteWithDish(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 套餐查询展示
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> get(Setmeal setmeal){

        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != null,Setmeal::getCategoryId,categoryId)
                .eq(status != null,Setmeal::getStatus,status)
                .orderByDesc(Setmeal::getCreateTime);

        List<Setmeal> setmeals = setmealService.list(queryWrapper);
        return R.success(setmeals);
    }

}
