package com.usky.vpp.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.vpp.domain.VppEnergyReadingMonthly;
import com.usky.vpp.domain.VppSettlementBill;
import com.usky.vpp.service.VppSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 虚拟电厂 - Settlement 接口
 * 网关前缀: /prod-api/service-vpp
 */
@RestController
@RequestMapping("/settlement")
public class SettlementController {

    @Autowired
    private VppSettlementService vppSettlementService;

    /**
     * 分页查询powermeter数据
     * <ul>
     *   <li>id           主键ID 精确匹配</li>
     *   <li>customerName 客户名称 模糊匹配</li>
     *   <li>calcCycle    计算周期 模糊匹配</li>
     *   <li>current      页码</li>
     *   <li>size         页大小</li>
     * </ul>
     */
    @GetMapping(value = "/reading")
    public ApiResult<CommonPage<VppEnergyReadingMonthly>> pageReading(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "customerName", required = false) String customerName,
            @RequestParam(value = "calcCycle", required = false) String calcCycle,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        return ApiResult.success(vppSettlementService.pageReading(id, customerName, calcCycle, current, size));
    }

    /**
     * 导出月度电量核算数据（Excel）
     * <ul>
     *   <li>scope = "current"：导出当前页，需传 current + size</li>
     *   <li>scope = "all"：导出当前租户全部未删除月度电量核算记录</li>
     *   <li>scope = "filtered"：导出筛选结果，需至少传一个筛选条件</li>
     * </ul>
     */
    @GetMapping(value = "/reading/export")
    public void exportReading(
            @RequestParam("scope") String scope,
            @RequestParam("fileName") String fileName,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "customerName", required = false) String customerName,
            @RequestParam(value = "calcCycle", required = false) String calcCycle,
            @RequestParam(value = "current", required = false) Integer current,
            @RequestParam(value = "size", required = false) Integer size,
            HttpServletResponse response) {
        vppSettlementService.exportReading(scope, fileName, id, customerName, calcCycle, current, size, response);
    }

    @PostMapping(value = "/reading/calculate")
    public ApiResult<Void> calculateReading(@RequestBody(required = false) Object body) {
        return ApiResult.success();
    }

    @GetMapping(value = "/bill")
    public ApiResult<CommonPage<VppSettlementBill>> pageBill(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "customerName", required = false) String customerName,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        return ApiResult.success(vppSettlementService.pageBill(id, customerName, current, size));
    }

    /**
     * 导出电费账单数据（Excel）
     * <ul>
     *   <li>scope = "current"：导出当前页，需传 current + size</li>
     *   <li>scope = "all"：导出当前租户全部未删除电费账单</li>
     *   <li>scope = "filtered"：导出筛选结果，需至少传一个筛选条件</li>
     * </ul>
     */
    @GetMapping(value = "/bill/export")
    public void exportBill(
            @RequestParam("scope") String scope,
            @RequestParam("fileName") String fileName,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "customerName", required = false) String customerName,
            @RequestParam(value = "current", required = false) Integer current,
            @RequestParam(value = "size", required = false) Integer size,
            HttpServletResponse response) {
        vppSettlementService.exportBill(scope, fileName, id, customerName, current, size, response);
    }

    @PostMapping(value = "/bill")
    public ApiResult<Object> generateBill(@RequestBody(required = false) Object body) {
        return ApiResult.success(null);
    }

    @GetMapping(value = "/bill/{id}")
    public ApiResult<Object> getBill(@PathVariable("id") Long id, @RequestParam(required = false) java.util.Map<String, Object> params) {
        return ApiResult.success(null);
    }

    @GetMapping(value = "/bill/{id}/download")
    public ApiResult<Object> downloadBill(@PathVariable("id") Long id) {
        return ApiResult.success(null);
    }

    @GetMapping(value = "/payment")
    public ApiResult<Object> pagePayment(@RequestParam(required = false) java.util.Map<String, Object> params) {
        return ApiResult.success(null);
    }

    @PostMapping(value = "/payment")
    public ApiResult<Object> recordPayment(@RequestBody(required = false) Object body) {
        return ApiResult.success(null);
    }
}
