package co.yixiang.yshop.module.order.controller.admin.rechargepackage;

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 co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.order.controller.admin.rechargepackage.vo.*;
import co.yixiang.yshop.module.order.dal.dataobject.rechargepackage.RechargePackageDO;
import co.yixiang.yshop.module.order.convert.rechargepackage.RechargePackageConvert;
import co.yixiang.yshop.module.order.service.rechargepackage.RechargePackageService;

@Tag(name = "管理后台 - 充值套餐配置")
@RestController
@RequestMapping("/order/recharge-package")
@Validated
public class RechargePackageController {

    @Resource
    private RechargePackageService rechargePackageService;

    @PostMapping("/create")
    @Operation(summary = "创建充值套餐配置")
    @PreAuthorize("@ss.hasPermission('order:recharge-package:create')")
    public CommonResult<Long> createRechargePackage(@Valid @RequestBody RechargePackageCreateReqVO createReqVO) {
        return success(rechargePackageService.createRechargePackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充值套餐配置")
    @PreAuthorize("@ss.hasPermission('order:recharge-package:update')")
    public CommonResult<Boolean> updateRechargePackage(@Valid @RequestBody RechargePackageUpdateReqVO updateReqVO) {
        rechargePackageService.updateRechargePackage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除充值套餐配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('order:recharge-package:delete')")
    public CommonResult<Boolean> deleteRechargePackage(@RequestParam("id") Long id) {
        rechargePackageService.deleteRechargePackage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得充值套餐配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:recharge-package:query')")
    public CommonResult<RechargePackageRespVO> getRechargePackage(@RequestParam("id") Long id) {
        RechargePackageDO rechargePackage = rechargePackageService.getRechargePackage(id);
        return success(RechargePackageConvert.INSTANCE.convert(rechargePackage));
    }

    @GetMapping("/list")
    @Operation(summary = "获得充值套餐配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:recharge-package:query')")
    public CommonResult<List<RechargePackageRespVO>> getRechargePackageList(@RequestParam("ids") Collection<Long> ids) {
        List<RechargePackageDO> list = rechargePackageService.getRechargePackageList(ids);
        return success(RechargePackageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充值套餐配置分页")
    @PreAuthorize("@ss.hasPermission('order:recharge-package:query')")
    public CommonResult<PageResult<RechargePackageRespVO>> getRechargePackagePage(@Valid RechargePackagePageReqVO pageVO) {
        PageResult<RechargePackageDO> pageResult = rechargePackageService.getRechargePackagePage(pageVO);
        return success(RechargePackageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充值套餐配置 Excel")
    @PreAuthorize("@ss.hasPermission('order:recharge-package:export')")
    @OperateLog(type = EXPORT)
    public void exportRechargePackageExcel(@Valid RechargePackageExportReqVO exportReqVO,
                                           HttpServletResponse response) throws IOException {
        List<RechargePackageDO> list = rechargePackageService.getRechargePackageList(exportReqVO);
        // 导出 Excel
        List<RechargePackageExcelVO> datas = RechargePackageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "充值套餐配置.xls", "数据", RechargePackageExcelVO.class, datas);
    }

}
