/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.coldchain.modular.monitordevice.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.coldchain.modular.monitordevice.entity.MonitorDevice;
import vip.xiaonuo.coldchain.modular.monitordevice.param.MonitorDeviceAddParam;
import vip.xiaonuo.coldchain.modular.monitordevice.param.MonitorDeviceEditParam;
import vip.xiaonuo.coldchain.modular.monitordevice.param.MonitorDeviceIdParam;
import vip.xiaonuo.coldchain.modular.monitordevice.param.MonitorDevicePageParam;
import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;
import vip.xiaonuo.coldchain.modular.monitordevicetype.entity.CountEntity;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;

import java.util.List;

/**
 * 采集器管理控制器
 *
 * @author 黄渊昊
 * @date 2024/11/13 16:55
 */
@Tag(name = "采集器管理控制器")
@RestController
@Validated
public class MonitorDeviceController {

    @Resource
    private MonitorDeviceService monitorDeviceService;

    /**
     * 获取采集器管理分页
     *
     * @author 黄渊昊
     * @date 2024/11/13 16:55
     */
    @Operation(summary = "获取采集器管理分页")
    @SaCheckPermission("/coldchain/monitordevice/page")
    @GetMapping("/coldchain/monitordevice/page")
    public CommonResult<Page<?>> page(MonitorDevicePageParam monitorDevicePageParam) {
        return CommonResult.data(monitorDeviceService.page(monitorDevicePageParam));
    }

    /**
     * 添加采集器管理
     *
     * @author 黄渊昊
     * @date 2024/11/13 16:55
     */
    @Operation(summary = "添加采集器管理")
    @CommonLog("添加采集器管理")
    @SaCheckPermission("/coldchain/monitordevice/add")
    @PostMapping("/coldchain/monitordevice/add")
    public CommonResult<String> add(@RequestBody @Valid MonitorDeviceAddParam monitorDeviceAddParam) {
        monitorDeviceService.add(monitorDeviceAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑采集器管理
     *
     * @author 黄渊昊
     * @date 2024/11/13 16:55
     */
    @Operation(summary = "编辑采集器管理")
    @CommonLog("编辑采集器管理")
    @SaCheckPermission("/coldchain/monitordevice/edit")
    @PostMapping("/coldchain/monitordevice/edit")
    public CommonResult<String> edit(@RequestBody @Valid MonitorDeviceEditParam monitorDeviceEditParam) {
        monitorDeviceService.edit(monitorDeviceEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除采集器管理
     *
     * @author 黄渊昊
     * @date 2024/11/13 16:55
     */
    @Operation(summary = "删除采集器管理")
    @CommonLog("删除采集器管理")
    @SaCheckPermission("/coldchain/monitordevice/delete")
    @PostMapping("/coldchain/monitordevice/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                       List<MonitorDeviceIdParam> monitorDeviceIdParamList) {
        monitorDeviceService.delete(monitorDeviceIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取采集器管理详情
     *
     * @author 黄渊昊
     * @date 2024/11/13 16:55
     */
    @Operation(summary = "获取采集器管理详情")
    @SaCheckPermission("/coldchain/monitordevice/detail")
    @GetMapping("/coldchain/monitordevice/detail")
    public CommonResult<MonitorDevice> detail(@Valid MonitorDeviceIdParam monitorDeviceIdParam) {
        return CommonResult.data(monitorDeviceService.detail(monitorDeviceIdParam));
    }

    /**
     * 获取监控设备列表
     *
     * @param 是否查询所有
     */
    @Operation(summary = "获取监控设备列表")
    @SaCheckPermission("/coldchain/monitordevice/list")
    @GetMapping("/coldchain/monitordevice/list")
    public CommonResult<List<MonitorDevice>> list(Boolean isAll,String sensorType) {
        return CommonResult.data(monitorDeviceService.myList(isAll,sensorType));
    }

    /**
    * 检查设备名是否已注册
    */
    @Operation(summary = "检查设备名是否已注册")
    @SaCheckPermission("/coldchain/monitordevice/getByDeviceName")
    @GetMapping("/coldchain/monitordevice/getByDeviceName")
    public CommonResult<String> getByDeviceName(String deviceName) {
        boolean b = monitorDeviceService.getByDeviceName(deviceName);
        if (b) {
            return CommonResult.error("设备名" + deviceName + "已注册使用");
        }
        return CommonResult.ok();
    }

    /**
     * 检查设备编码是否已注册
     */
    @Operation(summary = "检查设备编码是否已注册")
    @SaCheckPermission("/coldchain/monitordevice/getByDeviceCode")
    @GetMapping("/coldchain/monitordevice/getByDeviceCode")
    public CommonResult<String> getByDeviceCode(String deviceCode) {
        boolean b = monitorDeviceService.getByDeviceCode(deviceCode);
        if (b) {
            return CommonResult.error("设备编码" + deviceCode + "已注册使用：{}");
        }
        return CommonResult.ok();
    }

    /**
     * 获取首页统计信息
     */
    @Operation(summary = "获取首页统计信息")
    @SaCheckPermission("/coldchain/monitordevice/getIndexCount")
    @GetMapping("/coldchain/monitordevice/getIndexCount")
    public CommonResult<CountEntity> getIndexCount() {
        return CommonResult.data(monitorDeviceService.getIndexCount());
    }

    /**
     * 下载Excel模板
     */
    @Operation(summary = "下载Excel模板")
    @GetMapping("/coldchain/monitordevice/downloadTemplate")
    public CommonResult<String> downloadTemplate(HttpServletResponse response) {
       monitorDeviceService.downloadTemplate(response);
       return CommonResult.ok();
    }

    /**
     * 导入数据
     */
    @Operation(summary = "导入数据")
    @PostMapping("/coldchain/monitordevice/import")
    public CommonResult<String> importMonitorDevice(MultipartFile file) {
        monitorDeviceService.importMonitorDevice(file);
        return CommonResult.ok();
    }

}
