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.inverter.FacInverterCreateByGeoJsonReq;
import org.dromara.facility.domain.dto.inverter.FacInverterCreateReq;
import org.dromara.facility.domain.dto.inverter.FacInverterQueryReq;
import org.dromara.facility.domain.dto.inverter.FacInverterUpdateReq;
import org.dromara.facility.domain.vo.inverter.FacInverterVo;
import org.dromara.facility.service.IFacInverterService;
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/inverter")
public class FacInverterController extends BaseController {

    private final IFacInverterService facInverterService;

    /**
     * 查询设施-逆变器列表
     */
    @SaCheckPermission("facility:inverter:list")
    @GetMapping("/list")
    public TableDataInfo<FacInverterVo> list(FacInverterQueryReq req, PageQuery pageQuery) {
        return facInverterService.queryPageList(req, pageQuery);
    }

    /**
     * 导出设施-逆变器列表
     */
    @SaCheckPermission("facility:inverter:export")
    @Log(title = "设施-逆变器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FacInverterQueryReq req, HttpServletResponse response) {
        List<FacInverterVo> list = facInverterService.queryList(req);
        ExcelUtil.exportExcel(list, "设施-逆变器", FacInverterVo.class, response);
    }

    /**
     * 获取设施-逆变器详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("facility:inverter:query")
    @GetMapping("/{id}")
    public R<FacInverterVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(facInverterService.queryById(id));
    }

    /**
     * 新增设施-逆变器
     */
    @SaCheckPermission("facility:inverter:add")
    @Log(title = "设施-逆变器", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody FacInverterCreateReq req) {
        return R.ok(facInverterService.insertByBo(req));
    }

    /**
     * 通过GeoJson新增设施-逆变器
     */
    @SaCheckPermission("facility:inverter:add")
    @Log(title = "设施-逆变器", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/geoJson")
    public R<Void> insertByGeoJson(@RequestBody FacInverterCreateByGeoJsonReq req) {
        return toAjax(facInverterService.insertByGeoJson(req));
    }

    /**
     * 修改设施-逆变器
     */
    @SaCheckPermission("facility:inverter:edit")
    @Log(title = "设施-逆变器", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FacInverterUpdateReq req) {
        return toAjax(facInverterService.updateByBo(req));
    }

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

}
