package com.hw.ems.record.controller;

import com.alibaba.fastjson.JSONArray;

import com.hw.common.core.domain.R;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.ems.record.domain.RecordAlarmData;
import com.hw.ems.record.service.IRecordAlarmDataService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

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

/**
 * 异常数据记录Controller
 *
 * @author YinQ
 * @date 2023-04-09
 */
@RestController
@RequestMapping("/record/alarmData")
public class RecordAlarmDataController extends BaseController {
    private String prefix = "record/alarmData";

    @Autowired
    private IRecordAlarmDataService recordAlarmDataService;

    @RequiresPermissions("record:alarmData:view")
    @GetMapping()
    public String alarmData() {
        return prefix + "/alarmData";
    }

    /**
     * 查询异常数据记录列表
     */
//    @RequiresPermissions("record:alarmData:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordAlarmData recordAlarmData) {
        startPage();
        List<RecordAlarmData> list = recordAlarmDataService.selectRecordAlarmDataList(recordAlarmData);
        return getDataTable(list);
    }

    /**
     * 异常数据记录
     *
     * @return
     */
    @GetMapping("/alarmRecord")
    public String alarmRecord() {
        return prefix + "/alarmRecord";
    }

    /**
     * 报警数量
     *
     * @return
     */
    @PostMapping("/alarmCount")
    @ResponseBody
    public int alarmCount() {
        RecordAlarmData recordAlarmData = new RecordAlarmData();
        recordAlarmData.setAlarmStatus(1L);
//        recordAlarmData.setNotifyUser(ShiroUtils.getSysUser().getLoginName());
        List<RecordAlarmData> list = recordAlarmDataService.selectRecordAlarmDataList(recordAlarmData);
        return list.size();
    }

    /**
     * 异常处理
     *
     * @param objIds
     * @return
     */
    @PostMapping("/editAlarmStatus")
    @ResponseBody
    public String editAlarmStatus(@RequestBody RecordAlarmData objIds) {
        AjaxResult info = new AjaxResult();
        for (Long id : objIds.getObjIds()) {
            RecordAlarmData recordAlarmData = new RecordAlarmData();
            recordAlarmData.setObjId(id);
            recordAlarmData.setAlarmStatus(0L);
            recordAlarmData.setOperationName(SecurityUtils.getUsername());
            recordAlarmData.setOperationTime(new Date());
            recordAlarmDataService.updateRecordAlarmData(recordAlarmData);
        }
        return JSONArray.toJSONString(info);
    }


    /**
     * 导出异常数据记录列表
     */
    @RequiresPermissions("record:alarmData:export")
    @Log(title = "异常数据记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response, RecordAlarmData recordAlarmData) {
        List<RecordAlarmData> list = recordAlarmDataService.selectRecordAlarmDataList(recordAlarmData);
        ExcelUtil<RecordAlarmData> util = new ExcelUtil<RecordAlarmData>(RecordAlarmData.class);
        util.exportExcel(response, list, "异常数据记录数据");
    }

    /**
     * 新增异常数据记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存异常数据记录
     */
    @RequiresPermissions("record:alarmData:add")
    @Log(title = "异常数据记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordAlarmData recordAlarmData) {
        return toAjax(recordAlarmDataService.insertRecordAlarmData(recordAlarmData));
    }

    /**
     * 修改异常数据记录
     */
    @RequiresPermissions("record:alarmData:edit")
    @GetMapping("/edit/{objId}")
    public String edit(@PathVariable("objId") Long objId, ModelMap mmap) {
        RecordAlarmData recordAlarmData = recordAlarmDataService.selectRecordAlarmDataByObjId(objId);
        mmap.put("recordAlarmData", recordAlarmData);
        return prefix + "/edit";
    }

    /**
     * 修改保存异常数据记录
     */
    @RequiresPermissions("record:alarmData:edit")
    @Log(title = "异常数据记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordAlarmData recordAlarmData) {
        return toAjax(recordAlarmDataService.updateRecordAlarmData(recordAlarmData));
    }

    /**
     * 删除异常数据记录
     */
    @RequiresPermissions("record:alarmData:remove")
    @Log(title = "异常数据记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {
        return toAjax(recordAlarmDataService.deleteRecordAlarmDataByObjIds(ids));
    }

    @PostMapping("/deviceOfflineTimingTask")
    public R<Boolean>  deviceOfflineTimingTask() {
        return recordAlarmDataService.deviceOfflineTimingTask();
    }

    @PostMapping("/thresholdTimingTask")
    public R<Boolean> thresholdTimingTask() {
        return recordAlarmDataService.thresholdTimingTask();
    }

    @PostMapping("/hourLineLossTimingTask")
    public R<Boolean> hourLineLossTimingTask() {
        return recordAlarmDataService.hourLineLossTimingTask();
    }

    @PostMapping("/dailyConsumptionTimingTask")
    public R<Boolean> dailyConsumptionTimingTask() {
        return recordAlarmDataService.dailyConsumptionTimingTask();
    }

}
