package com.ruoyi.project.system.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TAlarmhistory;
import com.ruoyi.project.system.service.ITAlarmhistoryService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 报警记录管理Controller
 *
 * @author ruoyi
 * @date 2021-01-11
 */
@RestController
@RequestMapping("/system/alarmhistory")
public class TAlarmhistoryController extends BaseController
{
    @Autowired
    private ITAlarmhistoryService tAlarmhistoryService;

    /**
     * 查询报警记录管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:alarmhistory:list')")
    @GetMapping("/list")
    public TableDataInfo list(TAlarmhistory tAlarmhistory) {
        startPage();
        List<TAlarmhistory> list = tAlarmhistoryService.selectTAlarmhistoryList(tAlarmhistory);
        return getDataTable(list);
    }

    /**
     * 首页查询报警记录信息
     */
    @GetMapping("/homeList")
    public String[] homeList(TAlarmhistory tAlarmhistory) {
        startPage();
        tAlarmhistory.setIsHome("1");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        tAlarmhistory.setHomeTime(sdf.format(new Date()));
        String homeTimeNight = sdf.format(new Date()) + " 23:59:59";
        tAlarmhistory.setHomeTimeNight(homeTimeNight);
        List<TAlarmhistory> list = tAlarmhistoryService.selectHomeList(tAlarmhistory);
        String[] warnInformation = new String[list.size()];
        int i = 0;
        for (TAlarmhistory a : list) {
            String info = a.getTableName() + "（" + a.getFieldName() + "）：" + a.getAlarmNum() + "条，" + a.getAlarmLevel() + "级预警，即将到期！";
            warnInformation[i] = info;
            i++;
        }
        return warnInformation;
    }

    /**
     * 导出报警记录管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:alarmhistory:export')")
    @Log(title = "报警记录管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TAlarmhistory tAlarmhistory)
    {
        List<TAlarmhistory> list = tAlarmhistoryService.selectTAlarmhistoryList(tAlarmhistory);
        ExcelUtil<TAlarmhistory> util = new ExcelUtil<TAlarmhistory>(TAlarmhistory.class);
        return util.exportExcel(list, "alarmhistory");
    }

    /**
     * 获取报警记录管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:alarmhistory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tAlarmhistoryService.selectTAlarmhistoryById(id));
    }

    /**
     * 新增报警记录管理
     */
    @PreAuthorize("@ss.hasPermi('system:alarmhistory:add')")
    @Log(title = "报警记录管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TAlarmhistory tAlarmhistory)
    {
        return toAjax(tAlarmhistoryService.insertTAlarmhistory(tAlarmhistory));
    }

    /**
     * 修改报警记录管理
     */
    @PreAuthorize("@ss.hasPermi('system:alarmhistory:edit')")
    @Log(title = "报警记录管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TAlarmhistory tAlarmhistory)
    {
        return toAjax(tAlarmhistoryService.updateTAlarmhistory(tAlarmhistory));
    }

    /**
     * 删除报警记录管理
     */
    @PreAuthorize("@ss.hasPermi('system:alarmhistory:remove')")
    @Log(title = "报警记录管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tAlarmhistoryService.deleteTAlarmhistoryByIds(ids));
    }
}
