/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.modbus.device.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dm.modbus.device.domain.DmModbusDevice;
import me.zhengjie.modules.dm.modbus.device.service.DmModbusDeviceService;
import me.zhengjie.modules.dm.modbus.device.service.dto.DmModbusDeviceQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author wld
* @date 2022-05-20
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "通道下的设备管理管理")
@RequestMapping("/api/dmModbusDevice")
public class DmModbusDeviceController {

    private final DmModbusDeviceService dmModbusDeviceService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dmModbusDevice:list')")
    public void download(HttpServletResponse response, DmModbusDeviceQueryCriteria criteria) throws IOException {
        dmModbusDeviceService.download(dmModbusDeviceService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询通道下的设备管理")
    @ApiOperation("查询通道下的设备管理")
    @PreAuthorize("@el.check('dmModbusDevice:list')")
    public ResponseEntity<Object> query(DmModbusDeviceQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(dmModbusDeviceService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增通道下的设备管理")
    @ApiOperation("新增通道下的设备管理")
    @PreAuthorize("@el.check('dmModbusDevice:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DmModbusDevice resources){
        return new ResponseEntity<>(dmModbusDeviceService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改通道下的设备管理")
    @ApiOperation("修改通道下的设备管理")
    @PreAuthorize("@el.check('dmModbusDevice:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DmModbusDevice resources){
        dmModbusDeviceService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除通道下的设备管理")
    @ApiOperation("删除通道下的设备管理")
    @PreAuthorize("@el.check('dmModbusDevice:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        dmModbusDeviceService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("统计modbus设备信息")
    @ApiOperation("统计modbus设别信息")
    @GetMapping(value = "getModbusStatistics")
    public ResponseEntity<Object> getModbusStatistics(){
        //获取在线设备数量
        Long online = dmModbusDeviceService.getModbusCount(1);
        // 获取离线设备数量
        Long offline = dmModbusDeviceService.getModbusCount(0);
        // 获取全部设备数量
        Long allCount = dmModbusDeviceService.getModbusCount(null);
        // 获取在线率
         BigDecimal onlineRate = (BigDecimal.valueOf(online).divide(BigDecimal.valueOf(allCount), 2, BigDecimal.ROUND_HALF_UP)).multiply(BigDecimal.valueOf(100));
        Map<String,Object> map = new HashMap<>();
        map.put("allCount", allCount);
        map.put("online",online);
        map.put("offline",offline);
        map.put("onlineRate",onlineRate.toString());
        return new ResponseEntity<>(map,HttpStatus.OK);
    }
}