package com.xydl.cac.controller;

import com.alibaba.excel.EasyExcel;
import com.xydl.cac.entity.Warning;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.ConditionModel;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.DataService;
import com.xydl.cac.service.ReportService;
import com.xydl.cac.service.WarningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
@Api(tags = {"告警信息相关接口"})
@RequestMapping("warning")
@Slf4j
public class WarningController extends BasicController {

    @Resource
    WarningService service;

    @Resource
    DataService dataService;

    @GetMapping("list")
    @ApiOperation("查询列表")
    public Response<Page<Warning>> list(@Validated ConditionModel condition) throws Exception {
        condition.setPageNum(this.initPageNum(condition.getPageNum()));
        condition.setPageSize(this.initPageSize(condition.getPageSize()));
        Page<Warning> result = service.list(condition);
        return Response.success(result);
    }

    @GetMapping("export")
    @ApiOperation("导出告警数据")
    public void export(@Validated ConditionModel condition, HttpServletResponse response) throws Exception {
        if (condition.getPageNum() != null || condition.getPageSize() != null) {
            condition.setPageNum(this.initPageNum(condition.getPageNum()));
            condition.setPageSize(this.initPageSize(condition.getPageSize()));
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename="
                + URLEncoder.encode("告警数据.xlsx", "UTF-8"));
        List<Warning> result = service.listAll(condition);
        EasyExcel.write(response.getOutputStream(), Warning.class)
                .sheet("告警数据")
                .doWrite(result);
    }

    @PostMapping("updateState")
    @ApiOperation("修改处理状态")
    public Response<String> updateState(@Validated @RequestBody Warning warning) throws Exception {
        if (warning.getId() == null) {
            throw new BusinessException("id不能为空!");
        }
        service.updateState(warning);
        return Response.success("OK");

    }

    Boolean isHandle = false;

    @PostMapping("oneKeyUpdateState")
    @ApiOperation("一键修改处理状态")
    public Response<String> oneKeyUpdateState() throws Exception {
        try {
            if (isHandle) {
                throw new BusinessException("相同请求正在处理中, 请勿重复处理");
            }
            isHandle = true;
            service.oneKeyUpdateState();
            return Response.success("OK");
        } finally {
            isHandle = false;
        }
    }
}
