package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.TServiceManage;
import com.ruoyi.system.domain.dto.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TDeviceList;
import com.ruoyi.system.service.ITDeviceListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备列Controller
 * 
 * @author zhengjie
 * @date 2023-08-30
 */
@RestController
@RequestMapping("/system/device")
public class TDeviceListController extends BaseController
{
    @Autowired
    private ITDeviceListService tDeviceListService;

    /**
     * 查询设备列列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDeviceList tDeviceList)
    {
        startPage();
        List<TDeviceList> list = tDeviceListService.selectTDeviceListList(tDeviceList);
        return getDataTable(list);
    }

    /**
     * 导出设备列列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "设备列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDeviceList tDeviceList)
    {
        List<TDeviceList> list = tDeviceListService.selectTDeviceListList(tDeviceList);
        ExcelUtil<TDeviceList> util = new ExcelUtil<TDeviceList>(TDeviceList.class);
        util.exportExcel(response, list, "设备列数据");
    }

    /**
     * 获取设备列详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(tDeviceListService.selectTDeviceListById(id));
    }

    /**
     * 新增设备列
     */
    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDeviceList tDeviceList)
    {
        return toAjax(tDeviceListService.insertTDeviceList(tDeviceList));
    }

    /**
     * 修改设备列
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDeviceList tDeviceList)
    {
        return toAjax(tDeviceListService.updateTDeviceList(tDeviceList));
    }

    /**
     * 删除设备列
     */
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tDeviceListService.deleteTDeviceListByIds(ids));
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TDeviceList> util = new ExcelUtil<TDeviceList>(TDeviceList.class);
        util.importTemplateExcel(response, "设备列表模板");
    }

    /**
     * 导入
     */
    @Log(title = "设备列表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<TDeviceList> util = new ExcelUtil<TDeviceList>(TDeviceList.class);
        List<TDeviceList> tDeviceLists = util.importExcel(file.getInputStream());
        String message = tDeviceListService.importTDeviceList(tDeviceLists);
        return AjaxResult.success(message);
    }

    /**
     * 查询心率列表
     */
    @GetMapping("/heartRate")
    public TableDataInfo heartRate(String device_id_code)
    {
        startPage();
        List<RateDto> list = tDeviceListService.selectTDeviceByDeviceId(device_id_code);
        for(RateDto dto : list){
            HeartRateDto heartRateDto = JSONObject.parseObject(dto.getContent(), HeartRateDto.class);
            dto.setContent(heartRateDto.getHeart_rate());
        }
        return getDataTable(list);
    }

    /**
     * 查询血氧列表
     */
    @GetMapping("/oxygenRate")
    public TableDataInfo oxygenRate(String device_id_code)
    {
        startPage();
        List<RateDto> list = tDeviceListService.selectTDeviceByOxygen(device_id_code);
        for(RateDto dto : list){
            OxygenRateDto oxygenRateDto = JSONObject.parseObject(dto.getContent(), OxygenRateDto.class);
            dto.setContent(oxygenRateDto.getOxygen());
        }
        return getDataTable(list);
    }

    /**
     * 查询体温列表
     */
    @GetMapping("/tempRate")
    public TableDataInfo tempRate(String device_id_code)
    {
        startPage();
        List<RateDto> list = tDeviceListService.selectTDeviceByTemp(device_id_code);
        for(RateDto dto : list){
            TempRateDto tempRateDto = JSONObject.parseObject(dto.getContent(), TempRateDto.class);
            dto.setContent(tempRateDto.getTemp());
        }
        return getDataTable(list);
    }

    /**
     * 查询血压列表
     */
    @GetMapping("/bloodRate")
    public TableDataInfo bloodRate(String device_id_code)
    {
        startPage();
        List<RateDto> list = tDeviceListService.selectTDeviceByBlood(device_id_code);
        List<BloodRateDto> list1 = new ArrayList<>();
        for(RateDto dto : list){
            BloodRateDto bloodRateDto = JSONObject.parseObject(dto.getContent(), BloodRateDto.class);
            bloodRateDto.setCreatetime(dto.getCreatetime());
            list1.add(bloodRateDto);
        }
        return getDataTable(list1);
    }

    /**
     * 查询步数列表
     */
    @GetMapping("/stepRate")
    public TableDataInfo stepRate(String device_id_code)
    {
        startPage();
        List<RateDto> list = tDeviceListService.selectTDeviceByStep(device_id_code);
        List<StepRateDto> list1 = new ArrayList<>();
        for(RateDto dto : list){
            StepRateDto stepRateDto = JSONObject.parseObject(dto.getContent(), StepRateDto.class);
            stepRateDto.setCreatetime(dto.getCreatetime());
            list1.add(stepRateDto);
        }
        return getDataTable(list1);
    }

    /**
     * 查询翻滚列表
     */
    @GetMapping("/rollRate")
    public TableDataInfo rollRate(String device_id_code)
    {
        startPage();
        List<RateDto> list = tDeviceListService.selectTDeviceByStep(device_id_code);
        List<RollRateDto> list1 = new ArrayList<>();
        for(RateDto dto : list){
            RollRateDto rollRateDto = JSONObject.parseObject(dto.getContent(), RollRateDto.class);
            rollRateDto.setCreatetime(dto.getCreatetime());
            list1.add(rollRateDto);
        }
        return getDataTable(list1);
    }

    /**
     * 查询步数列表
     */
    @GetMapping("/messageRate")
    public TableDataInfo messageRate(String device_id_code)
    {
        startPage();
        List<MessageDto> list = tDeviceListService.selectTDeviceByMessage(device_id_code);
        List<RateDto> list1 = new ArrayList<>();
        for(MessageDto dto : list){
            RateDto rateDto = new RateDto();
            rateDto.setContent(tDeviceListService.checkMessage(dto.getAlarm_type()));
            rateDto.setCreatetime(dto.getCreatetime());
            list1.add(rateDto);
        }
        return getDataTable(list1);
    }

    /**
     * 查询设备消息日志
     */
    @GetMapping("/messageLogRate")
    public TableDataInfo messageLogRate(MessageLogDto messageLogDto)
    {
        startPage();
        List<MessageLogDto> list = tDeviceListService.selectTDeviceByMessageLog(messageLogDto);
        return getDataTable(list);
    }

    /**
     * 修改设备阈值
     */
    @Log(title = "设备阈值", businessType = BusinessType.UPDATE)
    @PostMapping("/deviceValue")
    public AjaxResult deviceValue(@RequestBody CallPoliceDto callPoliceDto)
    {
        return toAjax(tDeviceListService.deviceValue(callPoliceDto));
    }
}