package com.sz.admin.monitor.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sz.admin.monitor.pojo.dto.substation.SubstationCreateDTO;
import com.sz.admin.monitor.pojo.dto.substation.SubstationListDTO;
import com.sz.admin.monitor.pojo.dto.substation.SubstationUpdateDTO;

import com.sz.admin.monitor.pojo.po.Substation;
import com.sz.admin.monitor.pojo.vo.substation.SubstationVO;
import com.sz.admin.monitor.service.SubstationService;
import com.sz.admin.monitor.service.SubstationService;
import com.sz.core.common.entity.*;
import com.sz.core.util.BeanCopyUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: SubstationController
 * Package: com.sz.admin.monitor.controller
 * Description:
 */
@Tag(name = "变电站表")
@RestController
@RequestMapping("ry-substation")
@RequiredArgsConstructor
@Profile({"dev", "local", "preview"})
public class SubstationController {
    private final SubstationService substationService;

    @Operation(summary = "新增")
    @SaCheckPermission(value = "ry.substation.create")
    @PostMapping
    public ApiResult<Void> create(@RequestBody SubstationCreateDTO dto) {
        substationService.create(dto);
        return ApiResult.success();
    }

    @Operation(summary = "修改")
    @SaCheckPermission(value = "ry.substation.update")
    @PutMapping
    public ApiResult<Void> update(@RequestBody SubstationUpdateDTO dto) {
        substationService.update(dto);
        return ApiResult.success();
    }

    @Operation(summary = "删除")
    @SaCheckPermission(value = "ry.substation.remove")
    @DeleteMapping
    public ApiResult<Void> remove(@RequestBody SelectIdsDTO dto) {
        substationService.remove(dto);
        return ApiResult.success();
    }

    @Operation(summary = "列表查询")
    @SaCheckPermission(value = "ry.substation.query_table")
    @GetMapping
    public ApiResult<PageResult<SubstationVO>> list(SubstationListDTO dto) {
        return ApiPageResult.success(substationService.page(dto));
    }

    @Operation(summary = "详情")
    @SaCheckPermission(value = "ry.substation.query_table")
    @GetMapping("/{id}")
    public ApiResult<SubstationVO> detail(@PathVariable Object id) {
        return ApiResult.success(substationService.detail(id));
    }

    @Operation(summary = "导入")
    @Parameters({
        @Parameter(name = "file", description = "上传文件", schema = @Schema(type = "string", format = "binary"), required = true),
    })
    @SaCheckPermission(value = "ry.substation.import")
    @PostMapping("/import")
    public void importExcel(@ModelAttribute ImportExcelDTO dto) {
        substationService.importExcel(dto);
    }

    @Operation(summary = "导出")
    @SaCheckPermission(value = "ry.substation.export")
    @PostMapping("/export")
    public void exportExcel(@RequestBody SubstationListDTO dto, HttpServletResponse response) {
        substationService.exportExcel(dto, response);
    }
    // 获取所有的列表
    @Operation(summary = "获取所有的变电站")
    @GetMapping("/list")
    public ApiResult<List<SubstationVO>> getAllList(){
        List<Substation> substationList = substationService.list();
        return ApiResult.success(BeanCopyUtils.copyList(substationList, SubstationVO.class));
    }


}
