package com.zd.laboratory.controller;

import com.zd.common.core.constant.CacheConstants;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.enums.HardwareTypeEnum;
import com.zd.common.core.enums.SenseType;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.valid.ValidGroup;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.redis.service.RedisService;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.laboratory.domain.LabSensor;
import com.zd.laboratory.domain.vo.LabSensorVO;
import com.zd.laboratory.service.ILabSensorService;
import com.zd.system.api.laboratory.domain.SensorFunctionStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 传感器Controller
 *
 * @author zd
 * @date 2021-08-30
 */
@RestController
@RequestMapping("/sensor")
public class LabSensorController extends BaseController {
    @Autowired
    private ILabSensorService labSensorService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询传感器列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_SENSOR + PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(LabSensor labSensor) {
        Long subjectId=null;
        Collection <String> sensorData = redisService.keys(CacheConstants.SENSOR_DATA_KEY_ISONLINE + "*");
        for (String s : sensorData) {
            String key=s.replace(CacheConstants.SENSOR_DATA_KEY_ISONLINE, "");
            String[] keys=key.split(",");
            if(null==subjectId){
                subjectId=Long.parseLong(keys[1]);
            }
        }
        List<LabSensorVO> list;
        if(subjectId!=null){
            int countNum = labSensorService.selectSensorCountBySubIds(subjectId);
            startPage();
            list = labSensorService.selectLabSensorList(labSensor,countNum,subjectId);
        }else{
            startPage();
            list = labSensorService.selectLabSensorList(labSensor);
        }
        return getDataTable(list);
    }

    /**
     * 查询传感器的下拉框接口
     */
    @PostMapping("/queryOption")
    public AjaxResult queryOption(@RequestBody LabSensor labSensor) {
        List<LabSensorVO> list = labSensorService.selectLabSensorList(labSensor);
        return AjaxResult.success(list);
    }

    /**
     * 导出传感器列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_SENSOR + PerFun.EXPORT)
    @Log(title = "传感器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabSensor labSensor) throws IOException {
        List<LabSensorVO> list = labSensorService.selectLabSensorList(labSensor);
        ExcelUtil<LabSensorVO> util = new ExcelUtil<>(LabSensorVO.class);
        util.exportExcel(response, list, "传感器数据");
    }

    /**
     * 获取传感器详细信息
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_SENSOR + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(labSensorService.selectLabSensorById(id));
    }

    /**
     * 新增传感器
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_SENSOR + PerFun.ADD)
    @Log(title = "传感器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Crud.Create.class) LabSensor labSensor) {
        return toAjax(labSensorService.insertLabSensor(labSensor));
    }

    /**
     * 修改传感器
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_SENSOR + PerFun.EDIT)
    @Log(title = "传感器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Crud.Update.class) LabSensor labSensor) {
        Integer routeNum =labSensor.getRouteNum()==null?-1:labSensor.getRouteNum();
        labSensor.setRouteNum(routeNum);
        return toAjax(labSensorService.updateLabSensor(labSensor));
    }

    /**
     * 删除传感器
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_SENSOR + PerFun.REMOVE)
    @Log(title = "传感器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(labSensorService.deleteLabSensorByIds(ids));
    }

    /**
     * 查询传感器功能列表
     */
    @PostMapping("/getFunList")
    public AjaxResult getFunList() {
        List<SensorFunctionStatus> funList = labSensorService.getFunList();
        return AjaxResult.success(funList);
    }


    /**
     * 查询传感器属性列表
     */
    @GetMapping("/getSensorList")
    public AjaxResult getSensorList() {
        SenseType[] sesorList = SenseType.values();
        return AjaxResult.success(sesorList);
    }

}
