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

import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodel.DeviceModelDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mes.controller.admin.energytype.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.energytype.EnergyTypeDO;
import cn.iocoder.yudao.module.mes.service.energytype.EnergyTypeService;

@Tag(name = "管理后台 - 能耗类型")
@RestController
@RequestMapping("/mes/energy-type")
@Validated
public class EnergyTypeController {

    @Resource
    private EnergyTypeService energyTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建能耗类型")
    @PreAuthorize("@ss.hasPermission('mes:energy-type:create')")
    public CommonResult<Long> createEnergyType(@Valid @RequestBody EnergyTypeSaveReqVO createReqVO) {
        return success(energyTypeService.createEnergyType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新能耗类型")
    @PreAuthorize("@ss.hasPermission('mes:energy-type:update')")
    public CommonResult<Boolean> updateEnergyType(@Valid @RequestBody EnergyTypeSaveReqVO updateReqVO) {
        energyTypeService.updateEnergyType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除能耗类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:energy-type:delete')")
    public CommonResult<Boolean> deleteEnergyType(@RequestParam("id") Long id) {
        energyTypeService.deleteEnergyType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得能耗类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:energy-type:query')")
    public CommonResult<EnergyTypeRespVO> getEnergyType(@RequestParam("id") Long id) {
        EnergyTypeDO energyType = energyTypeService.getEnergyType(id);
        return success(BeanUtils.toBean(energyType, EnergyTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得能耗类型分页")
    @PreAuthorize("@ss.hasPermission('mes:energy-type:query')")
    public CommonResult<PageResult<EnergyTypeRespVO>> getEnergyTypePage(@Valid EnergyTypePageReqVO pageReqVO) {
        PageResult<EnergyTypeDO> pageResult = energyTypeService.getEnergyTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EnergyTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出能耗类型 Excel")
    @PreAuthorize("@ss.hasPermission('mes:energy-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEnergyTypeExcel(@Valid EnergyTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EnergyTypeDO> list = energyTypeService.getEnergyTypePage(pageReqVO).getList();
        // 设置响应头
        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
        String fileName = String.format("能耗类型_%s.xls", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));

        // 导出 Excel
        ExcelUtils.write(response, fileName, "数据", EnergyTypeRespVO.class,
                        BeanUtils.toBean(list, EnergyTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得能耗类型列表")
    public CommonResult<List<EnergyTypeDO>> getEnergyTypeDOList(@RequestParam(name = "orgId",required = false) Long orgId) {
        List<EnergyTypeDO> energyTypeDOList = energyTypeService.getEnergyTypeList();
        return success(energyTypeDOList);

    }

}