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

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.admincard.vo.AdminCardPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.admincard.vo.AdminCardRespVO;
import cn.iocoder.yudao.module.member.controller.admin.admincard.vo.AdminCardSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.admincard.AdminCardDO;
import cn.iocoder.yudao.module.member.service.admincard.AdminCardService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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/admin-card")
@Validated
public class AdminCardController {

    @Resource
    private AdminCardService adminCardService;

    @PostMapping("/create")
    @Operation(summary = "创建管理员卡")
    //@PreAuthorize("@ss.hasPermission('member:admin-card:create')")
    public CommonResult<Long> createAdminCard(@Valid @RequestBody AdminCardSaveReqVO createReqVO) {
        return success(adminCardService.createAdminCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新管理员卡")
    //@PreAuthorize("@ss.hasPermission('member:admin-card:update')")
    public CommonResult<Boolean> updateAdminCard(@Valid @RequestBody AdminCardSaveReqVO updateReqVO) {
        adminCardService.updateAdminCard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除管理员卡")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('member:admin-card:delete')")
    public CommonResult<Boolean> deleteAdminCard(@RequestParam("id") Long id) {
        adminCardService.deleteAdminCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得管理员卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('member:admin-card:query')")
    public CommonResult<AdminCardRespVO> getAdminCard(@RequestParam("id") Long id) {
        AdminCardDO adminCard = adminCardService.getAdminCard(id);
        return success(BeanUtils.toBean(adminCard, AdminCardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得管理员卡分页")
    //@PreAuthorize("@ss.hasPermission('member:admin-card:query')")
    public CommonResult<PageResult<AdminCardRespVO>> getAdminCardPage(@Valid AdminCardPageReqVO pageReqVO) {
        PageResult<AdminCardDO> pageResult = adminCardService.getAdminCardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdminCardRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得管理员卡不分页")
    //@PreAuthorize("@ss.hasPermission('member:admin-card:query')")
    public CommonResult<List<AdminCardRespVO>> getAdminCardList() {
        List<AdminCardDO> pageResult = adminCardService.getAdminCardList();
        return success(BeanUtils.toBean(pageResult, AdminCardRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出管理员卡 Excel")
    //@PreAuthorize("@ss.hasPermission('member:admin-card:export')")
    @OperateLog(type = EXPORT)
    public void exportAdminCardExcel(@Valid AdminCardPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdminCardDO> list = adminCardService.getAdminCardPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "饭卡管理.xls", "数据", AdminCardRespVO.class,
                        BeanUtils.toBean(list, AdminCardRespVO.class));
    }

}