package com.sunyard.chsm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.constant.AuditLogConst;
import com.sunyard.chsm.dto.DeviceGroupDTO;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.service.DeviceGroupService;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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 javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 设备组管理接口
 *
 * @author liulu
 * @since 2024/10/21
 */
@RestController("sp_DeviceGroupController")
@RequestMapping("/device/group")
public class DeviceGroupController {

    @Resource
    private DeviceGroupService deviceGroupService;

    /**
     * 分页查询设备组列表
     *
     * @param query 查询条件
     * @return 分页列表
     */
    @GetMapping("/pageList")
    public R<Page<DeviceGroupDTO.DGView>> servicePageList(DeviceGroupDTO.Query query) {

        Page<DeviceGroupDTO.DGView> page = deviceGroupService.selectPageList(query);

        return R.data(page);
    }

    /**
     * 添加设备组
     *
     * @param save 参数
     * @return id
     */
    @PostMapping
    @AuditControllerLog(description = "添加设备组", operateType = AuditLogConst.ADD)
    public R<String> save(@Valid @RequestBody DeviceGroupDTO.DGSave save) {
        Long id = deviceGroupService.save(save);
        return R.data(String.valueOf(id));
    }

    /**
     * 修改设备组
     *
     * @param update 参数
     * @return void
     */
    @PutMapping
    @AuditControllerLog(description = "修改设备组", operateType = AuditLogConst.UPDATE)
    public R<Void> update(@Valid @RequestBody DeviceGroupDTO.DGSave update) {
        deviceGroupService.update(update);
        return R.ok();
    }

    /**
     * 设置组内设备负载
     *
     * @param loadWeight 参数
     * @return void
     */
    @PostMapping("/weight")
    @AuditControllerLog(description = "修改设备负载", operateType = AuditLogConst.UPDATE)
    public R<Void> updateLoadWeight(@Valid @RequestBody DeviceGroupDTO.LoadWeight loadWeight) {
        deviceGroupService.updateLoadWeight(loadWeight);
        return R.ok();
    }

    /**
     * 删除设备组
     *
     * @param id id
     * @return void
     */
    @DeleteMapping
    @AuditControllerLog(description = "删除设备组", operateType = AuditLogConst.DELETE)
    public R<Void> delete(Long id) {
        deviceGroupService.delete(id);
        return R.ok();
    }

}
