package com.ruoyi.ems.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.ems.domain.EmsDeviceComponent;
import com.ruoyi.ems.service.IEmsDeviceComponentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 能源设备部件Controller
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/device/component")
@Api(value = "DeviceController", description = "设备部件管理")
public class DeviceComponentController extends BaseController {
    @Autowired
    private IEmsDeviceComponentService componentService;

    /**
     * 查询能源设备部件列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmsDeviceComponent component) {
        startPage();
        List<EmsDeviceComponent> list = componentService.selectList(component);
        return getDataTable(list);
    }

    /**
     * 查询能源设备部件列表
     *
     * @param deviceCode 设备编号
     * @return AjaxResult
     */
    @GetMapping("/listByDevice")
    public AjaxResult list(@RequestParam(name = "deviceCode") String deviceCode) {
        List<EmsDeviceComponent> list = componentService.selectByDeviceCode(deviceCode);
        return success(list);
    }

    /**
     * 获取能源设备部件详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(componentService.selectById(id));
    }

    /**
     * 获取能源设备部件详细信息
     */
    @GetMapping(value = "/getByCode")
    public AjaxResult getByCode(@RequestParam("compoCode") String compoCode) {
        return success(componentService.selectByCode(compoCode));
    }

    /**
     * 新增能源设备部件
     */
    @Log(title = "能源设备部件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsDeviceComponent component) {
        return toAjax(componentService.insertComponent(component));
    }

    /**
     * 修改能源设备部件
     */
    @Log(title = "能源设备部件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsDeviceComponent component) {
        return toAjax(componentService.updateComponent(component));
    }

    /**
     * 删除能源设备部件
     */
    @Log(title = "能源设备部件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(componentService.deleteComponentByIds(ids));
    }
}
