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

import co.yixiang.yshop.module.order.service.rechargeconfig.RechargeConfigFacade;
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.rechargeconfig.vo.*;
import co.yixiang.yshop.module.order.dal.dataobject.rechargeconfig.RechargeConfigDO;
import co.yixiang.yshop.module.order.convert.rechargeconfig.RechargeConfigConvert;
import co.yixiang.yshop.module.order.service.rechargeconfig.RechargeConfigService;

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

    @Resource
    private RechargeConfigService rechargeConfigService;

    @Resource
    private RechargeConfigFacade rechargeConfigFacade;

    @GetMapping("/get-all-config")
    @Operation(summary = "获取所有配置")
    public CommonResult<RechargeAllConfigVO> getAllConfig() {
        return success(rechargeConfigFacade.getAllConfig(null));
    }

    @PostMapping("/update-all-config")
    @Operation(summary = "更新所有配置")
    public CommonResult<Boolean> updateAllConfig(@Valid @RequestBody RechargeAllConfigVO rechargeAllConfigVO) {
        rechargeConfigFacade.updateAllConfig(rechargeAllConfigVO);
        return success(Boolean.TRUE);
    }


    @PostMapping("/create")
    @Operation(summary = "创建充值配置")
    @PreAuthorize("@ss.hasPermission('order:recharge-config:create')")
    public CommonResult<Long> createRechargeConfig(@Valid @RequestBody RechargeConfigCreateReqVO createReqVO) {
        return success(rechargeConfigService.createRechargeConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充值配置")
    @PreAuthorize("@ss.hasPermission('order:recharge-config:update')")
    public CommonResult<Boolean> updateRechargeConfig(@Valid @RequestBody RechargeConfigUpdateReqVO updateReqVO) {
        rechargeConfigService.updateRechargeConfig(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得充值配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:recharge-config:query')")
    public CommonResult<RechargeConfigRespVO> getRechargeConfig(@RequestParam("id") Long id) {
        RechargeConfigDO rechargeConfig = rechargeConfigService.getRechargeConfig(id);
        return success(RechargeConfigConvert.INSTANCE.convert(rechargeConfig));
    }

    @GetMapping("/list")
    @Operation(summary = "获得充值配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:recharge-config:query')")
    public CommonResult<List<RechargeConfigRespVO>> getRechargeConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<RechargeConfigDO> list = rechargeConfigService.getRechargeConfigList(ids);
        return success(RechargeConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充值配置分页")
    @PreAuthorize("@ss.hasPermission('order:recharge-config:query')")
    public CommonResult<PageResult<RechargeConfigRespVO>> getRechargeConfigPage(@Valid RechargeConfigPageReqVO pageVO) {
        PageResult<RechargeConfigDO> pageResult = rechargeConfigService.getRechargeConfigPage(pageVO);
        return success(RechargeConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充值配置 Excel")
    @PreAuthorize("@ss.hasPermission('order:recharge-config:export')")
    @OperateLog(type = EXPORT)
    public void exportRechargeConfigExcel(@Valid RechargeConfigExportReqVO exportReqVO,
                                          HttpServletResponse response) throws IOException {
        List<RechargeConfigDO> list = rechargeConfigService.getRechargeConfigList(exportReqVO);
        // 导出 Excel
        List<RechargeConfigExcelVO> datas = RechargeConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "充值配置.xls", "数据", RechargeConfigExcelVO.class, datas);
    }

}
