package com.zsElectric.boot.business.controller;

import cn.idev.excel.EasyExcel;
import com.zsElectric.boot.business.model.dto.ChargeOrderInfoExportDTO;
import com.zsElectric.boot.business.service.ChargeOrderInfoService;
import com.zsElectric.boot.business.service.ThirdPartyChargingService;
import com.zsElectric.boot.charging.entity.ThirdPartyStationInfo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.ChargeOrderInfoForm;
import com.zsElectric.boot.business.model.query.ChargeOrderInfoQuery;
import com.zsElectric.boot.business.model.vo.ChargeOrderInfoVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsElectric.boot.core.web.PageResult;
import com.zsElectric.boot.core.web.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 充电订单信息前端控制层
 *
 * @author zsElectric
 * @since 2025-12-17 19:13
 */
@Tag(name = "充电订单信息接口")
@RestController
@RequestMapping("/api/v1/charge-order-info")
@RequiredArgsConstructor
public class ChargeOrderInfoController  {

    private final ChargeOrderInfoService chargeOrderInfoService;

    @Operation(summary = "充电订单信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:charge-order-info:query')")
    public PageResult<ChargeOrderInfoVO> getChargeOrderInfoPage(ChargeOrderInfoQuery queryParams ) {
        IPage<ChargeOrderInfoVO> result = chargeOrderInfoService.getChargeOrderInfoPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "获取充电订单信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:charge-order-info:edit')")
    public Result<ChargeOrderInfoForm> getChargeOrderInfoForm(
        @Parameter(description = "充电订单信息ID") @PathVariable Long id
    ) {
        ChargeOrderInfoForm formData = chargeOrderInfoService.getChargeOrderInfoFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改充电订单信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:charge-order-info:edit')")
    public Result<Void> updateChargeOrderInfo(
            @Parameter(description = "充电订单信息ID") @PathVariable Long id,
            @RequestBody @Validated ChargeOrderInfoForm formData
    ) {
        boolean result = chargeOrderInfoService.updateChargeOrderInfo(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除充电订单信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:charge-order-info:delete')")
    public Result<Void> deleteChargeOrderInfos(
        @Parameter(description = "充电订单信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = chargeOrderInfoService.deleteChargeOrderInfos(ids);
        return Result.judge(result);
    }

    @Operation(summary = "导出充电订单信息")
    @PostMapping("/export")
    @PreAuthorize("@ss.hasPerm('business:charge-order-info:export')")
    public void exportChargeOrderInfo(@RequestBody ChargeOrderInfoQuery queryParams, HttpServletResponse response) throws IOException {
        String fileName = "充电订单信息.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));

        List<ChargeOrderInfoExportDTO> exportList = chargeOrderInfoService.listExportChargeOrderInfo(queryParams);
        EasyExcel.write(response.getOutputStream(), ChargeOrderInfoExportDTO.class).sheet("充电订单信息")
                .doWrite(exportList);
    }

    @Operation(summary = "修复未处理的充电订单", description = "修复状态为5(未成功充电)且实际充电量>0的订单，重新计算费用并扣减余额")
    @PostMapping("/repair-unprocessed")
    @PreAuthorize("@ss.hasPerm('business:charge-order-info:edit')")
    public Result<String> repairUnprocessedOrders() {
        String result = chargeOrderInfoService.repairUnprocessedOrders();
        return Result.success(result);
    }

    @Operation(summary = "根据订单号修复订单", description = "根据充电订单号修复订单，重新处理订单状态变更、数据修改及余额扣减")
    @PostMapping("/repair-by-order-no/{chargeOrderNo}")
    @PreAuthorize("@ss.hasPerm('business:charge-order-info:edit')")
    public Result<String> repairOrderByOrderNo(
            @Parameter(description = "充电订单号") @PathVariable String chargeOrderNo
    ) {
        String result = chargeOrderInfoService.repairOrderByOrderNo(chargeOrderNo);
        return Result.success(result);
    }
}
