// com/ruoyi/ems/controller/ChargingBillController.java
package com.ruoyi.ems.controller;

import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ems.model.ChargingBillVO;
import com.ruoyi.ems.model.QueryChargingBill;
import com.ruoyi.ems.service.IChargingBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商户能源账单Controller
 *
 * @author ruoyi
 * @date 2024-12-20
 */
@RestController
@RequestMapping("/charging/bill")
@Api(value = "ChargingBillController", description = "计费账单服务")
public class ChargingBillController extends BaseController {

    @Autowired
    private IChargingBillService chargingBillService;

    /**
     * 查询总览数据
     */
    //@RequiresPermissions("ems:charging:list")
    @GetMapping("/overview")
    @ApiOperation("查询总览数据")
    public AjaxResult getOverview(QueryChargingBill queryParam) {
        try {
            ChargingBillVO bill = chargingBillService.selectOverviewTotal(queryParam);
            return success(bill);
        }
        catch (Exception e) {
            logger.error("查询总览数据异常", e);
            return error("系统异常: " + e.getMessage());
        }
    }

    /**
     * 查询总览数据
     */
    @RequiresPermissions("ems:charging:list")
    @GetMapping("/overview/list")
    @ApiOperation("查询总览数据")
    public AjaxResult getOverviewList(QueryChargingBill queryParam) {
        try {
            List<ChargingBillVO> list = chargingBillService.selectOverviewList(queryParam);
            return success(list);
        }
        catch (Exception e) {
            logger.error("查询总览数据异常", e);
            return error("系统异常: " + e.getMessage());
        }
    }

    /**
     * 查询个户综合账单
     */
    @RequiresPermissions("ems:charging:list")
    @GetMapping("/seller")
    @ApiOperation("查询商户用能账单")
    public AjaxResult getIndividualBill(QueryChargingBill queryParam) {
        try {
            ChargingBillVO bill = chargingBillService.selectSellerEmsBill(queryParam);
            return success(bill);
        }
        catch (Exception e) {
            logger.error("查询个户账单异常", e);
            return error("系统异常: " + e.getMessage());
        }
    }

    /**
     * 查询历史账单列表
     */
    //@RequiresPermissions("ems:charging:list")
    @GetMapping("/seller/history")
    @ApiOperation("查询历史账单列表")
    public TableDataInfo getSellerHistoryBills(QueryChargingBill queryParam) {
        startPage();
        List<ChargingBillVO> list = chargingBillService.selectSellerHistoryBills(queryParam);
        return getDataTable(list);
    }

    /**
     * 导出账单
     */
    @RequiresPermissions("ems:charging:export")
    @Log(title = "计费账单", businessType = BusinessType.EXPORT)
    @PostMapping("/seller/export")
    @ApiOperation("导出账单")
    public void exportSellerBill(HttpServletResponse response, QueryChargingBill queryParam) {
        List<ChargingBillVO> list = chargingBillService.selectSellerHistoryBills(queryParam);
        ExcelUtil<ChargingBillVO> util = new ExcelUtil<>(ChargingBillVO.class);
        String fileName = String.format("计费账单_%s.xlsx", System.currentTimeMillis());
        util.exportExcel(response, list, fileName);
    }
}