package cn.iocoder.yudao.module.system.controller.admin.restaurant;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.restaurant.vo.CateringPlanPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.restaurant.vo.CateringPlanRespVO;
import cn.iocoder.yudao.module.system.controller.admin.restaurant.vo.CateringPlanSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.restaurant.CateringPlanDO;
import cn.iocoder.yudao.module.system.service.restaurant.CateringPlanService;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ImportResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

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

@Tag(name = "管理后台 - 餐饮计划")
@RestController
@RequestMapping("/restaurant/cateringPlan")
@Validated
public class CateringPlanController {

    @Resource
    private CateringPlanService cateringPlanService;

    @PostMapping("/createCateringPlan")
    @Operation(summary = "创建餐饮计划")
    public CommonResult<Long> createCateringPlan(@Valid @RequestBody CateringPlanSaveReqVO createReqVO) {
        return success(cateringPlanService.createCateringPlan(createReqVO));
    }

    @PutMapping("/updateCateringPlan")
    @Operation(summary = "更新餐饮计划")
    public CommonResult<Boolean> updateCateringPlan(@Valid @RequestBody CateringPlanSaveReqVO updateReqVO) {
        cateringPlanService.updateCateringPlan(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除餐饮计划")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCateringPlan(@RequestBody Long[] ids) {
        cateringPlanService.deleteCateringPlan(ids);
        return success(true);
    }

    @GetMapping("/getCateringPlan")
    @Operation(summary = "获得餐饮计划详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CateringPlanRespVO> getCateringPlan(@RequestParam("id") Long id) {
        CateringPlanRespVO cateringPlan = cateringPlanService.getCateringPlan(id);
        return success(cateringPlan);
    }

    @GetMapping("/getCateringPlanPage")
    @Operation(summary = "获得餐饮计划分页")
    @TenantIgnore
    public CommonResult<PageResult<CateringPlanDO>> getCateringPlanPage(@Valid CateringPlanPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<CateringPlanDO> pageResult = cateringPlanService.getCateringPlanPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/exportCateringPlanItemExcelDate")
    @Operation(summary = "导出餐饮计划Excel数据")
    @TenantIgnore
    public CommonResult<CateringPlanRespVO> exportCateringPlanItemExcelDate(@RequestParam("id") Long id) {
        return success(cateringPlanService.exportCateringPlanItemExcelDate(id));
    }

    @GetMapping("/import-template")
    @Operation(summary = "下载餐饮计划导入模板")
    @TenantIgnore
    public void importTemplate(HttpServletResponse response) throws IOException {
        cateringPlanService.exportImportTemplate(response);
    }

    @PostMapping("/import")
    @Operation(summary = "导入餐饮计划")
    @TenantIgnore
    public CommonResult<ImportResultVO> importCateringPlan(@RequestParam("file") MultipartFile file,
                                                           @RequestParam("restaurantId") Long restaurantId) throws Exception {
        return success(cateringPlanService.importCateringPlan(file, restaurantId));
    }
}