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

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.activitytabledish.vo.ActivityTableDishPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activitytabledish.vo.ActivityTableDishRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activitytabledish.vo.ActivityTableDishSaveReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activitytabledish.vo.ActivityUpdateDishVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activitytabledish.ActivityTableDishDO;
import cn.iocoder.yudao.module.member.service.activitytabledish.ActivityTableDishService;
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-table-dish")
@Validated
public class ActivityTableDishController {

    @Resource
    private ActivityTableDishService activityTableDishService;

    @PostMapping("/create")
    @Operation(summary = "创建活动用餐桌子配置")
    @PreAuthorize("@ss.hasPermission('member:activity-table-dish:create')")
    public CommonResult<Long> createActivityTableDish(@Valid @RequestBody ActivityTableDishSaveReqVO createReqVO) {
        return success(activityTableDishService.createActivityTableDish(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动用餐桌子配置")
    @PreAuthorize("@ss.hasPermission('member:activity-table-dish:update')")
    public CommonResult<Boolean> updateActivityTableDish(@Valid @RequestBody ActivityTableDishSaveReqVO updateReqVO) {
        activityTableDishService.updateActivityTableDish(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得活动用餐桌子配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-table-dish:query')")
    public CommonResult<ActivityTableDishRespVO> getActivityTableDish(@RequestParam("id") Long id) {
        ActivityTableDishDO activityTableDish = activityTableDishService.getActivityTableDish(id);
        return success(BeanUtils.toBean(activityTableDish, ActivityTableDishRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动用餐桌子配置分页")
    @PreAuthorize("@ss.hasPermission('member:activity-table-dish:query')")
    public CommonResult<PageResult<ActivityTableDishRespVO>> getActivityTableDishPage(@Valid ActivityTableDishPageReqVO pageReqVO) {
        PageResult<ActivityTableDishDO> pageResult = activityTableDishService.getActivityTableDishPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityTableDishRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得所有活动用餐桌子")
    @PreAuthorize("@ss.hasPermission('member:activity-table-dish:query')")
    public CommonResult<List<ActivityTableDishRespVO>> getActivityTableDishList(@Valid ActivityTableDishPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityTableDishDO> list = activityTableDishService.getActivityTableDishPage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, ActivityTableDishRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动用餐桌子配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-table-dish:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityTableDishExcel(@Valid ActivityTableDishPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityTableDishDO> list = activityTableDishService.getActivityTableDishPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动用餐桌子配置.xls", "数据", ActivityTableDishRespVO.class,
                        BeanUtils.toBean(list, ActivityTableDishRespVO.class));
    }

    @PutMapping("/updateDish")
    @Operation(summary = "批量更新活动用餐桌子套餐菜品")
    public CommonResult<Boolean> updateDish(@Valid @RequestBody ActivityUpdateDishVO updateReqVO) {
        return success(activityTableDishService.updateDish(updateReqVO));
    }
}