package cn.iocoder.yudao.module.mes.controller.admin.energydevice;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.HourEnergyValueVO;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.energydevice.EnergyDeviceCheckRecordDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.energydevice.EnergyDeviceDO;
import cn.iocoder.yudao.module.mes.service.energydevice.EnergyDeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 能源设备")
@RestController
@RequestMapping("/mes/energy-device")
@Validated
public class EnergyDeviceController {

    @Resource
    private EnergyDeviceService energyDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建能源设备")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:create')")
    public CommonResult<Long> createEnergyDevice(@Valid @RequestBody EnergyDeviceSaveReqVO createReqVO) {
        return success(energyDeviceService.createEnergyDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新能源设备")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:update')")
    public CommonResult<Boolean> updateEnergyDevice(@Valid @RequestBody EnergyDeviceSaveReqVO updateReqVO) {
        energyDeviceService.updateEnergyDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除能源设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:energy-device:delete')")
    public CommonResult<Boolean> deleteEnergyDevice(@RequestParam("id") Long id) {
        energyDeviceService.deleteEnergyDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得能源设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:query')")
    public CommonResult<EnergyDeviceRespVO> getEnergyDevice(@RequestParam("id") Long id) {
        EnergyDeviceRespVO energyDeviceRespVO = energyDeviceService.getEnergyDevice(id);
        return success(energyDeviceRespVO);
    }
    @GetMapping("/getList")
    @Operation(summary = "获得能源设备列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<EnergyDeviceDO>> getList(@Valid EnergyDevicePageReqVO pageReqVO) {
        List<EnergyDeviceDO> list = energyDeviceService.getEnergyDevicePage(pageReqVO).getList();
        return success(list);
    }
    @GetMapping("/page")
    @Operation(summary = "获得能源设备分页")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:query')")
    public CommonResult<PageResult<EnergyDeviceRespVO>> getEnergyDevicePage(@Valid EnergyDevicePageReqVO pageReqVO) {
        PageResult<EnergyDeviceDO> pageResult = energyDeviceService.getEnergyDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EnergyDeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出能源设备 Excel")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEnergyDeviceExcel(@Valid EnergyDevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EnergyDeviceDO> list = energyDeviceService.getEnergyDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "能源设备.xls", "数据", EnergyDeviceRespVO.class,
                        BeanUtils.toBean(list, EnergyDeviceRespVO.class));
    }

    @GetMapping("/queryDataRecords")
    @Operation(summary = "查询数据记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:create')")
    public CommonResult<List<EnergyDeviceRespVO>> queryDataRecords(@Valid EnergyDeviceConsumptionReqVO deviceConsumptionReqVO) {
        return success(energyDeviceService.queryDataRecords(deviceConsumptionReqVO));
    }

    @GetMapping("/record-export-excel")
    @Operation(summary = "导出数据记录 Excel")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void recordExportExcel(@Valid EnergyDeviceConsumptionReqVO deviceConsumptionReqVO,
                                        HttpServletResponse response) throws IOException {
        List<EnergyDeviceRespVO> energyDeviceRespVOS = energyDeviceService.queryDataRecords(deviceConsumptionReqVO);
        List<EnergyDeviceRecordRespVO> energyDeviceRecordRespVOS = BeanUtils.toBean(energyDeviceRespVOS, EnergyDeviceRecordRespVO.class);
        // 设置响应头
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode("能耗报表.xls", "UTF-8"));
        response.setHeader("Content-Encoding", "identity");
        // 导出 Excel
        ExcelUtils.write(response, "能耗报表.xls", "数据", EnergyDeviceRecordRespVO.class, energyDeviceRecordRespVOS);
    }

    // ==================== 子表（抄表记录） ====================

    @GetMapping("/energy-device-check-record/page")
    @Operation(summary = "获得抄表记录分页")
    @Parameter(name = "deviceId", description = "能源设备Id")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:query')")
    public CommonResult<PageResult<EnergyDeviceCheckRecordDO>> getEnergyDeviceCheckRecordPage(PageParam pageReqVO,
                                                                                        @RequestParam("deviceId") Long deviceId) {
        return success(energyDeviceService.getEnergyDeviceCheckRecordPage(pageReqVO, deviceId));
    }

    @PostMapping("/energy-device-check-record/create")
    @Operation(summary = "创建抄表记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:create')")
    public CommonResult<Long> createEnergyDeviceCheckRecord(@Valid @RequestBody EnergyDeviceCheckRecordDO energyDeviceCheckRecord) {
        return success(energyDeviceService.createEnergyDeviceCheckRecord(energyDeviceCheckRecord));
    }

    @PutMapping("/energy-device-check-record/update")
    @Operation(summary = "更新抄表记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:update')")
    public CommonResult<Boolean> updateEnergyDeviceCheckRecord(@Valid @RequestBody EnergyDeviceCheckRecordDO energyDeviceCheckRecord) {
        energyDeviceService.updateEnergyDeviceCheckRecord(energyDeviceCheckRecord);
        return success(true);
    }

    @DeleteMapping("/energy-device-check-record/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除抄表记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:delete')")
    public CommonResult<Boolean> deleteEnergyDeviceCheckRecord(@RequestParam("id") Long id) {
        energyDeviceService.deleteEnergyDeviceCheckRecord(id);
        return success(true);
    }

	@GetMapping("/energy-device-check-record/get")
	@Operation(summary = "获得抄表记录")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:energy-device:query')")
	public CommonResult<EnergyDeviceCheckRecordDO> getEnergyDeviceCheckRecord(@RequestParam("id") Long id) {
	    return success(energyDeviceService.getEnergyDeviceCheckRecord(id));
	}


    @GetMapping("/lastEnergyStatistics")
    @Operation(summary = "获得近七小时能耗记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:query')")
    public CommonResult<List<HourEnergyValueVO>> lastEnergyStatistics(@RequestParam("deviceTypeId") Long deviceTypeId,
                                                                      @RequestParam("orgId") Long orgId) {

        List<HourEnergyValueVO> hourEnergyValueVOS = energyDeviceService.lastEnergyStatistics(deviceTypeId, orgId);

        return success(hourEnergyValueVOS);
    }

    @GetMapping("/latestSevenDaysStatistics")
    @Operation(summary = "获得近七天能耗记录")
    @PreAuthorize("@ss.hasPermission('mes:energy-device:query')")
    public CommonResult<List<DayEnergyValueVO>> latestSevenDaysStatistics(
                                    @RequestParam Long deviceTypeId,
                                    @RequestParam Long orgId) {
        return CommonResult.success(energyDeviceService.latestSevenDaysStatistics(deviceTypeId, orgId));
    }


}