package cn.iocoder.yudao.module.system.controller.app.dishestype;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.dishestype.vo.DishesTypePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishestype.vo.DishesTypeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dishestype.vo.DishesTypeSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishestype.DishesTypeDO;
import cn.iocoder.yudao.module.system.service.dishestype.DishesTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 菜品分类")
@RestController
@RequestMapping("/app/t/dishes-type")
@Validated
public class DishesTypeAppController {

    @Resource
    private DishesTypeService dishesTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建菜品分类")
    public CommonResult<Long> createDishesType(@Valid @RequestBody DishesTypeSaveReqVO createReqVO) {
        return success(dishesTypeService.createDishesType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜品分类")
    public CommonResult<Boolean> updateDishesType(@Valid @RequestBody DishesTypeSaveReqVO updateReqVO) {
        dishesTypeService.updateDishesType(updateReqVO);
        return success(true);
    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除菜品分类")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('t:dishes-type:delete')")
//    public CommonResult<Boolean> deleteDishesType(@RequestParam("id") Long id) {
//        dishesTypeService.deleteDishesType(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得菜品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('t:dishes-type:query')")
    public CommonResult<DishesTypeRespVO> getDishesType(@RequestParam("id") Long id) {
        DishesTypeDO dishesType = dishesTypeService.getDishesType(id);
        return success(BeanUtils.toBean(dishesType, DishesTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得菜品分类分页")
//    @PreAuthorize("@ss.hasPermission('t:dishes-type:query')")
    public CommonResult<PageResult<DishesTypeRespVO>> getDishesTypePage(@Valid DishesTypePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DishesTypeDO> pageResult = dishesTypeService.getDishesTypePageApp(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DishesTypeRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出菜品分类 Excel")
//    @PreAuthorize("@ss.hasPermission('t:dishes-type:export')")
//    @OperateLog(type = EXPORT)
//    public void exportDishesTypeExcel(@Valid DishesTypePageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<DishesTypeDO> list = dishesTypeService.getDishesTypePage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "菜品分类.xls", "数据", DishesTypeRespVO.class,
//                        BeanUtils.toBean(list, DishesTypeRespVO.class));
//    }



}