package org.wfc.system.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.RestController;
import org.wfc.common.core.domain.R;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.system.domain.SysDevice;
import org.wfc.system.service.ISysDeviceService;

import java.util.List;

/**
 * <p>
 * 客户平台-设备表 前端控制器
 * </p>
 *
 * @author sys
 * @since 2025-01-07
 */
@RestController
@RequestMapping("/device")
public class SysDeviceController extends BaseController {

    @Autowired
    private ISysDeviceService sysDeviceService;

    @GetMapping("/page")
    public TableDataInfo page(SysDevice sysDevice) {
        startPage();
        List<SysDevice> list = sysDeviceService.list(Wrappers.<SysDevice>lambdaQuery()
                .like(SysDevice::getName, sysDevice.getName())
                .like(SysDevice::getMac, sysDevice.getMac()));
        return getDataTable(list);
    }

    @GetMapping("/list")
    public AjaxResult list(SysDevice sysDevice) {
        List<SysDevice> list = sysDeviceService.list(Wrappers.<SysDevice>lambdaQuery()
                .like(SysDevice::getName, sysDevice.getName())
                .like(SysDevice::getMac, sysDevice.getMac()));
        return success(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return success(sysDeviceService.getById(id));
    }

    @PostMapping
    public AjaxResult add(@RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.save(sysDevice));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.updateById(sysDevice));
    }

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysDeviceService.removeByIds(CollUtil.newArrayList(ids)));
    }

    @PostMapping("/deviceJob")
    public AjaxResult deviceJob() {
        return toAjax(sysDeviceService.deviceJob());
    }

    @PostMapping("/settingJob")
    public AjaxResult settingJob() {
        return toAjax(sysDeviceService.settingJob());
    }

    @PostMapping("/testJob")
    public R<String> testJob() {
        return R.ok(sysDeviceService.testJob());
    }
}
