package org.dromara.facility.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.facility.domain.dto.photovoltaicpanel.FacPhotovoltaicPanelCreateByGeoJsonReq;
import org.dromara.facility.domain.dto.photovoltaicpanel.FacPhotovoltaicPanelCreateReq;
import org.dromara.facility.domain.dto.photovoltaicpanel.FacPhotovoltaicPanelQueryReq;
import org.dromara.facility.domain.dto.photovoltaicpanel.FacPhotovoltaicPanelUpdateReq;
import org.dromara.facility.domain.vo.photovoltaicpanel.FacPhotovoltaicPanelVo;
import org.dromara.facility.service.IFacPhotovoltaicPanelService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设施-光伏板
 *
 * @author lcj
 * @date 2025-04-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/facility/photovoltaicPanel")
public class FacPhotovoltaicPanelController extends BaseController {

    private final IFacPhotovoltaicPanelService facPhotovoltaicPanelService;

    /**
     * 查询设施-光伏板列表
     */
    @SaCheckPermission("facility:photovoltaicPanel:list")
    @GetMapping("/list")
    public TableDataInfo<FacPhotovoltaicPanelVo> list(FacPhotovoltaicPanelQueryReq req, PageQuery pageQuery) {
        return facPhotovoltaicPanelService.queryPageList(req, pageQuery);
    }

    /**
     * 导出设施-光伏板列表
     */
    @SaCheckPermission("facility:photovoltaicPanel:export")
    @Log(title = "设施-光伏板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FacPhotovoltaicPanelQueryReq req, HttpServletResponse response) {
        List<FacPhotovoltaicPanelVo> list = facPhotovoltaicPanelService.queryList(req);
        ExcelUtil.exportExcel(list, "设施-光伏板", FacPhotovoltaicPanelVo.class, response);
    }

    /**
     * 获取设施-光伏板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("facility:photovoltaicPanel:query")
    @GetMapping("/{id}")
    public R<FacPhotovoltaicPanelVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long id) {
        return R.ok(facPhotovoltaicPanelService.queryById(id));
    }

    /**
     * 新增设施-光伏板
     */
    @SaCheckPermission("facility:photovoltaicPanel:add")
    @Log(title = "设施-光伏板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody FacPhotovoltaicPanelCreateReq req) {
        return R.ok(facPhotovoltaicPanelService.insertByBo(req));
    }

    /**
     * 通过GeoJson新增设施-光伏板
     */
    @SaCheckPermission("facility:photovoltaicPanel:add")
    @Log(title = "设施-光伏板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/geoJson")
    public R<Void> insertByGeoJson(@RequestBody FacPhotovoltaicPanelCreateByGeoJsonReq geoJson) {
        return toAjax(facPhotovoltaicPanelService.insertByGeoJson(geoJson));
    }

    /**
     * 修改设施-光伏板
     */
    @SaCheckPermission("facility:photovoltaicPanel:edit")
    @Log(title = "设施-光伏板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FacPhotovoltaicPanelUpdateReq req) {
        return toAjax(facPhotovoltaicPanelService.updateByBo(req));
    }

    /**
     * 删除设施-光伏板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("facility:photovoltaicPanel:remove")
    @Log(title = "设施-光伏板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(facPhotovoltaicPanelService.deleteWithValidByIds(List.of(ids), true));
    }
}
