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

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.activitymenu.vo.ActivityMenuPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activitymenu.vo.ActivityMenuRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activitymenu.vo.ActivityMenuSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activitymenu.ActivityMenuDO;
import cn.iocoder.yudao.module.member.service.activitymenu.ActivityMenuService;
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-menu")
@Validated
public class ActivityMenuController {

    @Resource
    private ActivityMenuService activityMenuService;

    @PostMapping("/create")
    @Operation(summary = "创建活动套餐")
    @PreAuthorize("@ss.hasPermission('member:activity-menu:create')")
    public CommonResult<Long> createActivityMenu(@Valid @RequestBody ActivityMenuSaveReqVO createReqVO) {
        return success(activityMenuService.createActivityMenu(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动套餐")
    @PreAuthorize("@ss.hasPermission('member:activity-menu:update')")
    public CommonResult<Boolean> updateActivityMenu(@Valid @RequestBody ActivityMenuSaveReqVO updateReqVO) {
        activityMenuService.updateActivityMenu(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得活动套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:activity-menu:query')")
    public CommonResult<ActivityMenuRespVO> getActivityMenu(@RequestParam("id") Long id) {
        ActivityMenuDO activityMenu = activityMenuService.getActivityMenu(id);
        return success(BeanUtils.toBean(activityMenu, ActivityMenuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动套餐分页")
    @PreAuthorize("@ss.hasPermission('member:activity-menu:query')")
    public CommonResult<PageResult<ActivityMenuRespVO>> getActivityMenuPage(@Valid ActivityMenuPageReqVO pageReqVO) {
        PageResult<ActivityMenuDO> pageResult = activityMenuService.getActivityMenuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityMenuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动套餐 Excel")
    @PreAuthorize("@ss.hasPermission('member:activity-menu:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityMenuExcel(@Valid ActivityMenuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityMenuDO> list = activityMenuService.getActivityMenuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动套餐.xls", "数据", ActivityMenuRespVO.class,
                        BeanUtils.toBean(list, ActivityMenuRespVO.class));
    }

}