package cn.iocoder.yudao.module.member.controller.admin.activityrestaurant;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.activityrestaurant.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.activityrestaurant.ActivityRestaurantDO;
import cn.iocoder.yudao.module.member.service.activityrestaurant.ActivityRestaurantService;

@Tag(name = "管理后台 - 活动用餐")
@RestController
@RequestMapping("/member/activity-restaurant")
@Validated
public class ActivityRestaurantController {

    @Resource
    private ActivityRestaurantService activityRestaurantService;

    @PostMapping("/create")
    @Operation(summary = "创建活动用餐")
    @PreAuthorize("@ss.hasPermission('member:activity-restaurant:create')")
    public CommonResult<Long> createActivityRestaurant(@Valid @RequestBody ActivityRestaurantSaveReqVO createReqVO) {
        return success(activityRestaurantService.createActivityRestaurant(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动用餐")
    @PreAuthorize("@ss.hasPermission('member:activity-restaurant:update')")
    public CommonResult<Boolean> updateActivityRestaurant(@Valid @RequestBody ActivityRestaurantSaveReqVO updateReqVO) {
        activityRestaurantService.updateActivityRestaurant(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动用餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:activity-restaurant:delete')")
    public CommonResult<Boolean> deleteActivityRestaurant(@RequestParam("id") Long id) {
        activityRestaurantService.deleteActivityRestaurant(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动用餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-restaurant:query')")
    public CommonResult<ActivityRestaurantRespVO> getActivityRestaurant(@RequestParam("id") Long id) {
        ActivityRestaurantDO activityRestaurant = activityRestaurantService.getActivityRestaurant(id);
        return success(BeanUtils.toBean(activityRestaurant, ActivityRestaurantRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动用餐分页")
    @PreAuthorize("@ss.hasPermission('member:activity-restaurant:query')")
    public CommonResult<PageResult<ActivityRestaurantRespVO>> getActivityRestaurantPage(@Valid ActivityRestaurantPageReqVO pageReqVO) {
        PageResult<ActivityRestaurantDO> pageResult = activityRestaurantService.getActivityRestaurantPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityRestaurantRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动用餐 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-restaurant:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityRestaurantExcel(@Valid ActivityRestaurantPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityRestaurantDO> list = activityRestaurantService.getActivityRestaurantPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动用餐.xls", "数据", ActivityRestaurantRespVO.class,
                        BeanUtils.toBean(list, ActivityRestaurantRespVO.class));
    }

}