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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.combo.vo.ComboPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.combo.vo.ComboRespVO;
import cn.iocoder.yudao.module.member.controller.admin.combo.vo.ComboSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.combo.ComboDO;
import cn.iocoder.yudao.module.member.service.combo.ComboService;
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.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.*;


@Tag(name = "管理后台 - 套餐")
@RestController
@RequestMapping("/combo")
@Validated
public class ComboController {

    @Resource
    private ComboService comboService;

    @PostMapping("/create")
    @Operation(summary = "创建套餐")
    @PreAuthorize("@ss.hasPermission('t:combo:create')")
    public CommonResult<Long> createCombo(@Valid @RequestBody ComboSaveReqVO createReqVO) {
        return success(comboService.createCombo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新套餐")
    @PreAuthorize("@ss.hasPermission('t:combo:update')")
    public CommonResult<Boolean> updateCombo(@Valid @RequestBody ComboSaveReqVO updateReqVO) {
        comboService.updateCombo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:combo:delete')")
    public CommonResult<Boolean> deleteCombo(@RequestParam("id") Long id) {
        comboService.deleteCombo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:combo:query')")
    public CommonResult<ComboRespVO> getCombo(@RequestParam("id") Long id) {
        ComboDO combo = comboService.getCombo(id);

        return success(BeanUtils.toBean(combo, ComboRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得套餐分页")
    @PreAuthorize("@ss.hasPermission('t:combo:query')")
    public CommonResult<PageResult<ComboRespVO>> getComboPage(@Valid ComboPageReqVO pageReqVO) {
        PageResult<ComboDO> pageResult = comboService.getComboPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ComboRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出套餐 Excel")
    @PreAuthorize("@ss.hasPermission('t:combo:export')")
    @OperateLog(type = EXPORT)
    public void exportComboExcel(@Valid ComboPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ComboDO> list = comboService.getComboPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "套餐.xls", "数据", ComboRespVO.class,
                        BeanUtils.toBean(list, ComboRespVO.class));
    }

}