package com.gyee.wisdom.alarm.sharding.controller;
import com.gyee.wisdom.alarm.sharding.config.AjaxResult;
import com.gyee.wisdom.alarm.sharding.entity.StopTypeCount;
import com.gyee.wisdom.alarm.sharding.service.ShutdownEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/shutdown")
@CrossOrigin
public class ShutdownEventController {


    @Autowired
    private ShutdownEventService shutdownEventService;



    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping(value = "/list")
    public AjaxResult  getRecentFault(
            @RequestParam(value = "stId", required = true) String stId,
            @RequestParam(value = "startDate", required = true) String startDate,
            @RequestParam(value = "endDate", required = true) String endDate

    )throws Exception {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startdate = sdf.parse(startDate);
            Date edtend = sdf.parse(endDate);
            return AjaxResult.successData(shutdownEventService.queryShutdownEventList(stId,startdate,edtend));
        } catch (Exception e) {
            return AjaxResult.error();
        }


    }


    /**
     * 停机事件全场统计次数和时间
     * @param stId 场站id（例：MHS_FDC）
     * @return
     */
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping(value = "/count/alltotal")
    public AjaxResult  getRecentFaultCount(@RequestParam(value = "stId", required = false) String stId ){
        try {
            if(null ==stId || "0".equals(stId.trim())){
                stId = "";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date currentDate = sdf.parse( sdf.format(new Date()));
            Map<String, StopTypeCount> map = shutdownEventService.queryShutdownEventCurrentDate(stId,currentDate);
            return  AjaxResult.successData(map);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }


}
