package com.xzzn.web.controller.ems;

import com.xzzn.common.core.controller.BaseController;
import com.xzzn.common.core.domain.AjaxResult;
import com.xzzn.common.core.page.TableDataInfo;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.EmsAlarmRecords;
import com.xzzn.ems.domain.vo.AlarmRecordListRequestVo;
import com.xzzn.ems.domain.vo.AlarmRecordListResponseVo;
import com.xzzn.ems.service.IEmsAlarmRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单站监控-故障告警
 * 
 * @author xzzn
 */
@RestController
@RequestMapping("/ems/siteAlarm")
public class EmsAlarmRecordsController extends BaseController
{

    @Autowired
    private IEmsAlarmRecordsService iEmsAlarmRecordsService;

    /**
     * 获取告警详情列表
     */
    @GetMapping("/getAlarmDetailList")
    public TableDataInfo getAlarmDetailList(AlarmRecordListRequestVo requestVo)
    {
        startPage();
        List<AlarmRecordListResponseVo> list = iEmsAlarmRecordsService.getAlarmRecordDetailList(requestVo);
        return getDataTable(list);
    }

    /**
     * 生成工单
     */
    @PostMapping("/createTicketNo")
    public AjaxResult createTicketNo(@RequestBody EmsAlarmRecords emsAlarmRecords)
    {
        Long id = emsAlarmRecords.getId();
        if (id == null) {
            return error("告警id不能为空");
        }
        String ticketNo= iEmsAlarmRecordsService.createTicketNo(id,getUserId());
        if (StringUtils.isNotEmpty(ticketNo) && ticketNo.contains("T")) {
            return  AjaxResult.success("操作成功", ticketNo);
        } else {
            return error(ticketNo);
        }
    }

    /**
     * 关闭告警
     */
    @PostMapping("/closeAlarm")
    public AjaxResult closeAlarm(@RequestBody EmsAlarmRecords emsAlarmRecords)
    {
        Long id = emsAlarmRecords.getId();
        if (id == null) {
            return error("告警id不能为空");
        }
        String result = iEmsAlarmRecordsService.closeAlarm(id, getUserId());
        if ("success".equals(result) || "告警已关闭".equals(result)) {
            return AjaxResult.success("操作成功");
        }
        return error(result);
    }

}
