package com.yida.data.device.consume.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.consume.EduConsumeDevice;
import com.yida.data.device.consume.service.EduConsumeDeviceService;
import com.yida.data.device.vo.consume.ConsumeDeviceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 消费机设备Controller
 *
 * @author ZYJ
 * @date 2023-03-14 18:02:27
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("consumeDevice")
@Api(tags = "消费机设备后台api")
public class EduConsumeDeviceController {

    private final EduConsumeDeviceService eduConsumeDeviceService;

    @ApiOperation("消费机设备列表分页查询")
    @GetMapping("/listDevicePage")
    public ResultBean<Page<ConsumeDeviceVO>> listDevicePage(
            @ApiParam("设备编号或名称") String number,
            @ApiParam("设备状态，0-在线，1-离线") Integer state,
            @ApiParam("学校id") Long schoolId,
            @ApiParam("当前页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("页面大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return ResultBean.buildSuccess(eduConsumeDeviceService.listDevicePage(pageNum, pageSize, number, state, schoolId));
    }

    @ApiOperation("保存消费机设备")
    @PostMapping("/saveConsumeDevice")
    public ResultBean<String> saveDevice(@RequestBody EduConsumeDevice device) {
        eduConsumeDeviceService.saveDevice(device);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("获取设备详情")
    @GetMapping("/getDeviceInfo")
    public ResultBean<ConsumeDeviceVO> getDeviceInfo(@RequestParam Long deviceId) {
        EduConsumeDevice consumeDevice = eduConsumeDeviceService.getById(deviceId);
        ConsumeDeviceVO deviceVO = new ConsumeDeviceVO();
        if (Objects.nonNull(consumeDevice)) {
            BeanUtils.copyProperties(consumeDevice, deviceVO);
        }
        return ResultBean.buildSuccess(deviceVO);
    }

    @ApiOperation("批量删除消费机设备")
    @PostMapping("/deleteDeviceBatch")
    public ResultBean<String> deleteDeviceBatch(@RequestBody List<Long> ids) {
        eduConsumeDeviceService.removeByIds(ids);
        return ResultBean.buildSuccess();
    }
}
