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

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.rechargeamount.vo.RechargeAmountPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.rechargeamount.vo.RechargeAmountRespVO;
import cn.iocoder.yudao.module.member.controller.admin.rechargeamount.vo.RechargeAmountSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.rechargeamount.RechargeAmountDO;
import cn.iocoder.yudao.module.member.service.rechargeamount.RechargeAmountService;
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.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/recharge-amount")
@Validated
public class RechargeAmountController {

    @Resource
    private RechargeAmountService rechargeAmountService;

    @PostMapping("/create")
    @Operation(summary = "创建充值金额配置")
    public CommonResult<Long> createRechargeAmount(@Valid @RequestBody RechargeAmountSaveReqVO createReqVO) {
        return success(rechargeAmountService.createRechargeAmount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充值金额配置")
    public CommonResult<Boolean> updateRechargeAmount(@Valid @RequestBody RechargeAmountSaveReqVO updateReqVO) {
        rechargeAmountService.updateRechargeAmount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除充值金额配置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteRechargeAmount(@RequestParam("id") Long id) {
        rechargeAmountService.deleteRechargeAmount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得充值金额配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<RechargeAmountRespVO> getRechargeAmount(@RequestParam("id") Long id) {
        RechargeAmountDO rechargeAmount = rechargeAmountService.getRechargeAmount(id);
        return success(BeanUtils.toBean(rechargeAmount, RechargeAmountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充值金额配置分页")
    public CommonResult<PageResult<RechargeAmountRespVO>> getRechargeAmountPage(@Valid RechargeAmountPageReqVO pageReqVO) {
        PageResult<RechargeAmountDO> pageResult = rechargeAmountService.getRechargeAmountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RechargeAmountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充值金额配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:recharge-amount:export')")
    @OperateLog(type = EXPORT)
    public void exportRechargeAmountExcel(@Valid RechargeAmountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RechargeAmountDO> list = rechargeAmountService.getRechargeAmountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "充值金额配置.xls", "数据", RechargeAmountRespVO.class,
                        BeanUtils.toBean(list, RechargeAmountRespVO.class));
    }

}