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

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.member.controller.admin.activityadddish.vo.ActivityAddDishPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityadddish.vo.ActivityAddDishRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activityadddish.vo.ActivityAddDishSaveBatchVO;
import cn.iocoder.yudao.module.member.controller.admin.activityadddish.vo.ActivityAddDishSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityadddish.ActivityAddDishDO;
import cn.iocoder.yudao.module.member.service.activityadddish.ActivityAddDishService;
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("/member/activity-add-dish")
@Validated
public class ActivityAddDishController {

    @Resource
    private ActivityAddDishService activityAddDishService;

    @PostMapping("/create")
    @Operation(summary = "创建活动用餐桌子加菜")
    @PreAuthorize("@ss.hasPermission('member:activity-add-dish:create')")
    public CommonResult<Long> createActivityAddDish(@Valid @RequestBody ActivityAddDishSaveReqVO createReqVO) {
        return success(activityAddDishService.createActivityAddDish(createReqVO));
    }

    @PostMapping("/createBatch")
    @Operation(summary = "创建活动用餐桌子加菜批量")
    @PreAuthorize("@ss.hasPermission('member:activity-add-dish:create')")
    public CommonResult<Boolean> createActivityAddDish(@Valid @RequestBody ActivityAddDishSaveBatchVO createReqVO) {
        activityAddDishService.createActivityAddDishBatch(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动用餐桌子加菜")
    @PreAuthorize("@ss.hasPermission('member:activity-add-dish:update')")
    public CommonResult<Boolean> updateActivityAddDish(@Valid @RequestBody ActivityAddDishSaveReqVO updateReqVO) {
        activityAddDishService.updateActivityAddDish(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得活动用餐桌子加菜")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-add-dish:query')")
    public CommonResult<ActivityAddDishRespVO> getActivityAddDish(@RequestParam("id") Long id) {
        ActivityAddDishDO activityAddDish = activityAddDishService.getActivityAddDish(id);
        return success(BeanUtils.toBean(activityAddDish, ActivityAddDishRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动用餐桌子加菜分页")
    @PreAuthorize("@ss.hasPermission('member:activity-add-dish:query')")
    public CommonResult<PageResult<ActivityAddDishRespVO>> getActivityAddDishPage(@Valid ActivityAddDishPageReqVO pageReqVO) {
        PageResult<ActivityAddDishDO> pageResult = activityAddDishService.getActivityAddDishPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityAddDishRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动用餐桌子加菜 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-add-dish:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityAddDishExcel(@Valid ActivityAddDishPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityAddDishDO> list = activityAddDishService.getActivityAddDishPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动用餐桌子加菜.xls", "数据", ActivityAddDishRespVO.class,
                        BeanUtils.toBean(list, ActivityAddDishRespVO.class));
    }

}