package org.dromara.ems.record.controller;

import org.dromara.common.log.annotation.Log;
import org.dromara.ems.common.web.EmsBaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.log.enums.BusinessType;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ems.base.service.IEmsBaseEnergyTypeService;
import org.dromara.ems.base.service.IEmsBaseMonitorInfoService;
import org.dromara.ems.record.domain.EmsIOTData;
import org.dromara.ems.record.domain.TWTempertureData;
import org.dromara.ems.record.service.ITWTempertureDataService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 物联网实时数据Controller
 * 
 * @author Caoql
 * @date 2024-08-21
 */
@RestController
@RequiredArgsConstructor

@RequestMapping("/ems/record/recordIOTInstant")
public class TWTempertureDataController extends EmsBaseController
{

    private final ITWTempertureDataService tWTempertureDataService;

    private final IEmsBaseMonitorInfoService emsBaseMonitorInfoService;

    private final IEmsBaseEnergyTypeService emsBaseEnergyTypeService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 查询物联网实时数据列表
     */
    @SaCheckPermission("ems/record:recordIOTInstant:list")
    @GetMapping("/list")
    public TableDataInfo list(TWTempertureData tWTempertureData)
    {
        startPage();
        List<TWTempertureData> list = tWTempertureDataService.selectTWTempertureDataList(tWTempertureData);
        return getDataTable(list);
    }

    /**
     * 查询物联网设备实时数据
     * @param tWTempertureData
     * @return
     */
    @GetMapping("/iotInstantList")
    public R<?> iotInstantList(TWTempertureData tWTempertureData){
        EmsIOTData emsIOTData = new EmsIOTData();
        int energyTypeid = Integer.parseInt(emsBaseMonitorInfoService.selectEmsBaseMonitorTypeByMonitorCode(tWTempertureData.getMonitorId()));
        String energyName = emsBaseEnergyTypeService.queryEnergyNameByEnergyTypeId(energyTypeid);
        if("温湿度".equals(energyName)){
            emsIOTData.monitorTypeArr.add("温度");
            emsIOTData.monitorTypeArr.add("湿度");
        }
        else{
            emsIOTData.monitorTypeArr.add(energyName);
        }
        emsIOTData.setType("line");
        emsIOTData.setSmooth(true);
        emsIOTData.setShowAllSymbol(true);
        emsIOTData.setSymbol("circle");
        emsIOTData.setSymbolSize(2);
        List<TWTempertureData> list = tWTempertureDataService.selectIotInstantList(tWTempertureData);
        emsIOTData.setDataList(list);
        return success(emsIOTData);
    }

    /**
     * 查询物联网设备实时监控数据
     * @param monitorCodeList
     * @return
     */
    @GetMapping("/iotInstantMonitoring")
    public R<?> iotInstantMonitoring(String monitorCodeList){
        List<TWTempertureData> list = new ArrayList<TWTempertureData>();
        if(ObjectUtil.isNotEmpty(monitorCodeList)){
            String[] split = monitorCodeList.split(",");
            for (String monitorId : split) {

                TWTempertureData tWTempertureData = new TWTempertureData();
                tWTempertureData.setMonitorId(monitorId);
                Map<String,Object> params = new HashMap<>();

                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.MINUTE,-5);
                Calendar startTime = (Calendar) calendar.clone();
                params.put("beginRecordTime",sdf.format(startTime.getTime()));
                params.put("endRecordTime",sdf.format(new Date()));
                tWTempertureData.setParams(params);

                List<TWTempertureData> twTempertureData = tWTempertureDataService.selectIotInstantList(tWTempertureData);
                if(twTempertureData.size()>0){
                    Optional<TWTempertureData> first = twTempertureData.stream().sorted(Comparator.comparing(TWTempertureData::getRecodeTime)).findFirst();

                    list.add(first.get());
                }


            }
        }
        logger.debug("实时监控设备数量: {}", list.size());
        return success(list);
    }

    /**
     * 导出物联网实时数据列表
     */
    @SaCheckPermission("ems/record:recordIOTInstant:export")
    @Log(title = "物联网实时数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TWTempertureData tWTempertureData)
    {
        List<TWTempertureData> list = tWTempertureDataService.selectTWTempertureDataList(tWTempertureData);
        ExcelUtil.exportExcel(list, "物联网实时数据数据", TWTempertureData.class, response);
    }

    /**
     * 获取物联网实时数据详细信息
     */
    @SaCheckPermission("ems/record:recordIOTInstant:query")
    @GetMapping(value = "/{objid}")
    public R<?> getInfo(@PathVariable("objid") Long objid)
    {
        return success(tWTempertureDataService.selectTWTempertureDataByObjid(objid));
    }

    /**
     * 新增物联网实时数据
     */
    @SaCheckPermission("ems/record:recordIOTInstant:add")
    @Log(title = "物联网实时数据", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody TWTempertureData tWTempertureData)
    {
        return toAjax(tWTempertureDataService.insertTWTempertureData(tWTempertureData));
    }

    /**
     * 修改物联网实时数据
     */
    @SaCheckPermission("ems/record:recordIOTInstant:edit")
    @Log(title = "物联网实时数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody TWTempertureData tWTempertureData)
    {
        return toAjax(tWTempertureDataService.updateTWTempertureData(tWTempertureData));
    }

    /**
     * 删除物联网实时数据
     */
    @SaCheckPermission("ems/record:recordIOTInstant:remove")
    @Log(title = "物联网实时数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{objids}")
    public R<?> remove(@PathVariable Long[] objids)
    {
        return toAjax(tWTempertureDataService.deleteTWTempertureDataByObjids(objids));
    }

    /**
     * 查询物联网实时分组数据列表
     */
    @SaCheckPermission("ems/record:recordIOTInstant:list")
    @PostMapping("/selectLastTWTempertureDataList")
    public TableDataInfo selectLastTWTempertureDataList(TWTempertureData tWTempertureData)
    {
        List<TWTempertureData> list = tWTempertureDataService.selectLastTWTempertureDataList(tWTempertureData);
        return getDataTable(list);
    }

}
