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

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

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

import cn.iocoder.yudao.module.member.controller.admin.money.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.money.MoneyDO;
import cn.iocoder.yudao.module.member.service.money.MoneyService;

@Tag(name = "管理后台 - 金额")
@RestController
@RequestMapping("/member/money")
@Validated
public class MoneyController {

    @Resource
    private MoneyService moneyService;

    @PostMapping("/create")
    @Operation(summary = "创建金额")
    @PreAuthorize("@ss.hasPermission('member:money:create')")
    public CommonResult<Long> createMoney(@Valid @RequestBody MoneySaveReqVO createReqVO) {
        return success(moneyService.createMoney(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新金额")
    @PreAuthorize("@ss.hasPermission('member:money:update')")
    public CommonResult<Boolean> updateMoney(@Valid @RequestBody MoneySaveReqVO updateReqVO) {
        moneyService.updateMoney(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除金额")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:money:delete')")
    public CommonResult<Boolean> deleteMoney(@RequestParam("id") Long id) {
        moneyService.deleteMoney(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得金额")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:money:query')")
    public CommonResult<MoneyRespVO> getMoney(@RequestParam("id") Long id) {
        MoneyDO money = moneyService.getMoney(id);
        return success(BeanUtils.toBean(money, MoneyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得金额分页")
    @PreAuthorize("@ss.hasPermission('member:money:query')")
    public CommonResult<PageResult<MoneyRespVO>> getMoneyPage(@Valid MoneyPageReqVO pageReqVO) {
        PageResult<MoneyDO> pageResult = moneyService.getMoneyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoneyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出金额 Excel")
    @PreAuthorize("@ss.hasPermission('member:money:export')")
    @OperateLog(type = EXPORT)
    public void exportMoneyExcel(@Valid MoneyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoneyDO> list = moneyService.getMoneyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "金额.xls", "数据", MoneyRespVO.class,
                        BeanUtils.toBean(list, MoneyRespVO.class));
    }

}