package com.ruoyi.ems.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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.domain.CaMeterD;
import com.ruoyi.ems.service.ICaMeterDService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 碳计量日Controller
 */
@RestController
@RequestMapping("/caMeterD")
@Api(value = "CaMeterDController", description = "碳计量日数据访问接口")
public class CaMeterDController extends BaseController {

    @Autowired
    private ICaMeterDService caMeterDService;

    /**
     * 查询碳计量列表（支持按日/月/年，分页）
     */
    @RequiresPermissions("ca-analysis:emission:list")
    @GetMapping("/list")
    @ApiOperation("查询碳计量列表（支持按日/月/年）")
    public TableDataInfo list(
        CaMeterD caMeterD,
        @ApiParam(value = "时间类型：day-按日, month-按月, year-按年", defaultValue = "year")
        @RequestParam(value = "timeType", required = false, defaultValue = "year") String timeType) {
        startPage();
        List<CaMeterD> list = caMeterDService.selectCaMeterDListByTimeType(caMeterD, timeType);
        return getDataTable(list);
    }

    /**
     * 查询全部数据（不分页，用于统计）
     */
    @RequiresPermissions("ca-analysis:emission:list")
    @GetMapping("/list/all")
    @ApiOperation("查询碳计量全部数据（用于统计，支持按日/月/年）")
    public AjaxResult listAll(
        CaMeterD caMeterD,
        @ApiParam(value = "时间类型：day-按日, month-按月, year-按年", defaultValue = "year")
        @RequestParam(value = "timeType", required = false, defaultValue = "year") String timeType) {
        List<CaMeterD> list = caMeterDService.selectSumCaMeterDListByTimeType(caMeterD, timeType);
        return success(list);
    }

    /**
     * 查询平均值
     */
    @RequiresPermissions("ca-analysis:emission:list")
    @GetMapping("/area/avg/ca")
    @ApiOperation("查询碳计量平均值")
    public AjaxResult qryAvgCa(CaMeterD caMeterD) {
        List<CaMeterD> list = caMeterDService.selectAvgCaMeterDList(caMeterD);
        return success(list);
    }

    /**
     * 导出碳计量列表
     */
    @RequiresPermissions("ca-analysis:emission:export")
    @Log(title = "碳计量日", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CaMeterD caMeterD,
        @RequestParam(value = "timeType", required = false, defaultValue = "year") String timeType) {
        List<CaMeterD> list = caMeterDService.selectCaMeterDListByTimeType(caMeterD, timeType);
        ExcelUtil<CaMeterD> util = new ExcelUtil<CaMeterD>(CaMeterD.class);
        util.exportExcel(response, list, "碳计量数据");
    }

    @RequiresPermissions("ca-analysis:emission:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(caMeterDService.selectCaMeterDById(id));
    }

    @RequiresPermissions("ca-analysis:emission:add")
    @Log(title = "碳计量日", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CaMeterD caMeterD) {
        return toAjax(caMeterDService.insertCaMeterD(caMeterD));
    }

    @RequiresPermissions("ca-analysis:emission:edit")
    @Log(title = "碳计量日", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CaMeterD caMeterD) {
        return toAjax(caMeterDService.updateCaMeterD(caMeterD));
    }

    @RequiresPermissions("ca-analysis:emission:remove")
    @Log(title = "碳计量日", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(caMeterDService.deleteCaMeterDByIds(ids));
    }
}