package com.agri.system.controller;

import java.time.LocalDateTime;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.agri.common.annotation.Log;
import com.agri.common.core.controller.BaseController;
import com.agri.common.core.domain.AjaxResult;
import com.agri.common.enums.BusinessType;
import com.agri.system.domain.SysDtuData;
import com.agri.system.service.ISysDtuDataService;
import com.agri.common.utils.poi.ExcelUtil;
import com.agri.common.core.page.TableDataInfo;

/**
 * DTU温湿度上报数据Controller
 * 
 * @author agri
 * @date 2025-12-23
 */
@RestController
@RequestMapping("/system/data")
public class SysDtuDataController extends BaseController
{
    @Autowired
    private ISysDtuDataService sysDtuDataService;

    /**
     * 查询DTU温湿度上报数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDtuData sysDtuData)
    {
        startPage();
        List<SysDtuData> list = sysDtuDataService.selectSysDtuDataList(sysDtuData);
        return getDataTable(list);
    }

    /**
     * 查询DTU温湿度上报数据列表
     */
    @GetMapping("/findDtuDataByInfo")
    public AjaxResult findDtuDataByInfo(SysDtuData sysDtuData)
    {
        SysDtuData dtuData = sysDtuDataService.findDtuDataByInfo(sysDtuData);
        return AjaxResult.success(dtuData);
    }


    /**
     * 导出DTU温湿度上报数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:export')")
    @Log(title = "DTU温湿度上报数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDtuData sysDtuData)
    {
        List<SysDtuData> list = sysDtuDataService.selectSysDtuDataList(sysDtuData);
        ExcelUtil<SysDtuData> util = new ExcelUtil<SysDtuData>(SysDtuData.class);
        util.exportExcel(response, list, "DTU温湿度上报数据数据");
    }

    /**
     * 获取DTU温湿度上报数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysDtuDataService.selectSysDtuDataById(id));
    }

    /**
     * 新增DTU温湿度上报数据
     */
    @PreAuthorize("@ss.hasPermi('system:data:add')")
    @Log(title = "DTU温湿度上报数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDtuData sysDtuData)
    {
        return toAjax(sysDtuDataService.insertSysDtuData(sysDtuData));
    }

    /**
     * 修改DTU温湿度上报数据
     */
    @PreAuthorize("@ss.hasPermi('system:data:edit')")
    @Log(title = "DTU温湿度上报数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDtuData sysDtuData)
    {
        return toAjax(sysDtuDataService.updateSysDtuData(sysDtuData));
    }

    /**
     * 删除DTU温湿度上报数据
     */
    @PreAuthorize("@ss.hasPermi('system:data:remove')")
    @Log(title = "DTU温湿度上报数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDtuDataService.deleteSysDtuDataByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/getHistoryData")
    public AjaxResult getHistoryData(@RequestParam("imei") String imei,
                                     @RequestParam(value = "startTime",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                     @RequestParam(value = "endTime",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime)
    {
        return success(sysDtuDataService.getHistoryData(imei,startTime,endTime));
    }
}
