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

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.rechargeorder.vo.*;
import co.yixiang.yshop.module.order.dal.dataobject.rechargeorder.RechargeOrderDO;
import co.yixiang.yshop.module.order.convert.rechargeorder.RechargeOrderConvert;
import co.yixiang.yshop.module.order.service.rechargeorder.RechargeOrderService;

@Tag(name = "管理后台 - 充值订单")
@RestController
@RequestMapping("/order/recharge-order")
@Validated
public class RechargeOrderController {

    @Resource
    private RechargeOrderService rechargeOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建充值订单")
    @PreAuthorize("@ss.hasPermission('order:recharge-order:create')")
    public CommonResult<String> createRechargeOrder(@Valid @RequestBody RechargeOrderCreateReqVO createReqVO) {
        return success(rechargeOrderService.createRechargeOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充值订单")
    @PreAuthorize("@ss.hasPermission('order:recharge-order:update')")
    public CommonResult<Boolean> updateRechargeOrder(@Valid @RequestBody RechargeOrderUpdateReqVO updateReqVO) {
        rechargeOrderService.updateRechargeOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除充值订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('order:recharge-order:delete')")
    public CommonResult<Boolean> deleteRechargeOrder(@RequestParam("id") String id) {
        rechargeOrderService.deleteRechargeOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得充值订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:recharge-order:query')")
    public CommonResult<RechargeOrderRespVO> getRechargeOrder(@RequestParam("id") String id) {
        RechargeOrderDO rechargeOrder = rechargeOrderService.getRechargeOrder(id);
        return success(RechargeOrderConvert.INSTANCE.convert(rechargeOrder));
    }

    @GetMapping("/list")
    @Operation(summary = "获得充值订单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:recharge-order:query')")
    public CommonResult<List<RechargeOrderRespVO>> getRechargeOrderList(@RequestParam("ids") Collection<String> ids) {
        List<RechargeOrderDO> list = rechargeOrderService.getRechargeOrderList(ids);
        return success(RechargeOrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充值订单分页")
    @PreAuthorize("@ss.hasPermission('order:recharge-order:query')")
    public CommonResult<PageResult<RechargeOrderRespVO>> getRechargeOrderPage(@Valid RechargeOrderPageReqVO pageVO) {
        return success(rechargeOrderService.getRechargeOrderPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充值订单 Excel")
    @PreAuthorize("@ss.hasPermission('order:recharge-order:export')")
    @OperateLog(type = EXPORT)
    public void exportRechargeOrderExcel(@Valid RechargeOrderExportReqVO exportReqVO,
                                         HttpServletResponse response) throws IOException {
        List<RechargeOrderDO> list = rechargeOrderService.getRechargeOrderList(exportReqVO);
        // 导出 Excel
        List<RechargeOrderExcelVO> datas = RechargeOrderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "充值订单.xls", "数据", RechargeOrderExcelVO.class, datas);
    }

    @PostMapping("/refund")
    @Operation(summary = "批量退款")
    public CommonResult<Boolean> refund() throws Exception {
        rechargeOrderService.refund();
        return success(Boolean.TRUE);
    }
}
