package jtznyj.engineer.controller;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import jtznyj.common.api.ICommonApi;
import jtznyj.common.api.vo.AFocusWarn;
import jtznyj.engineer.service.IAFoucusWarnSectionService;
import jtznyj.engineer.service.IWellDataDiagnosticService;
import jtznyj.engineer.service.dataDiagnosticService.data.MonitorAlarmRanges;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.sign.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/engineer/warnResult")
@Slf4j
public class WarnResultController {
    @Autowired
    IAFoucusWarnSectionService foucusWarnSectionService;
    @Autowired
    IWellDataDiagnosticService wellDataDiagnosticService;
    @Autowired
    ICommonApi iCommonApi;

    /**
     * 从库中读取预警数据
     *
     * @param jh
     * @param time
     * @return
     */
    @RequestMapping(value = "/getGcyjNewList", method = RequestMethod.GET)
    public Result<List<MonitorAlarmRanges>> getGcyjNewList(String jh, String time) {
        Result<List<MonitorAlarmRanges>> result = new Result<>();
        if (StringUtils.isEmpty(jh)) {
            return result.error500("井号不能为空");
        }
        if (StringUtils.isEmpty(time)) {
            return result.error500("时间不能为空");
        }

        List<MonitorAlarmRanges> resList = foucusWarnSectionService.getGcyjList(jh, time);

        result.setSuccess(true);
        result.setResult(resList);
        return result;
    }   /**
     * 从库中读取预警数据
     *
     * @param jh
     * @param time
     * @return
     */
    @RequestMapping(value = "/getGcyjNewList_new", method = RequestMethod.GET)
    public Result<List<MonitorAlarmRanges>> getGcyjNewList_new(String jh, String startTime, String endTime) {
        Result<List<MonitorAlarmRanges>> result = new Result<>();
        if (StringUtils.isEmpty(jh)) {
            return result.error500("井号不能为空");
        }
        if (StringUtils.isEmpty(startTime)) {
            return result.error500("时间不能为空");
        }

        List<MonitorAlarmRanges> resList = foucusWarnSectionService.getGcyjList(jh, startTime,endTime);

        result.setSuccess(true);
        result.setResult(resList);
        return result;
    }

    /**
     * 重新计算
     */
    @RequestMapping(value = "/getGcyjResetList", method = RequestMethod.GET)
    public Result<List<MonitorAlarmRanges>> getGcyjResetList(String jh, String startTime, HttpServletRequest request) {
        Result<List<MonitorAlarmRanges>> result = new Result<>();
        if (StringUtils.isEmpty(jh)) {
            return result.error500("井号不能为空");
        }
        if (StringUtils.isEmpty(startTime)) {
            return result.error500("时间不能为空");
        }

        List<MonitorAlarmRanges> resList = foucusWarnSectionService.getGcyjResetList(jh, startTime, 1440);

        result.setSuccess(true);
        result.setResult(resList);
        return result;
    }

    /**
     * 重新填充定时任务 某一井号，某一天的数据，手动调用，可删除
     *
     * @param jh
     * @param startTime
     * @return
     */
    @RequestMapping(value = "/refillAFocusWarnWellDay", method = RequestMethod.GET)
    public Result<String> fixGcyjEmptyError(String jh, String startTime) throws ParseException {
        log.info(String.format("补充开始， jh:%s, time: %s", jh, startTime));
        Result<String> result = new Result<>();
        if (StringUtils.isEmpty(jh)) {
            return result.error500("井号不能为空");
        }
        if (StringUtils.isEmpty(startTime)) {
            return result.error500("时间不能为空");
        }
        Date startDate = org.jeecg.common.util.DateUtils.parseDate(startTime, "yyyy-MM-dd");

        for (int i = 1; i <= 15; i++) {
            Map<String, MonitorAlarmRanges> monitorAlarmRangeMap = wellDataDiagnosticService.test(jh, DateUtils.addDays(startDate, i), 60 * 24, true);
            foucusWarnSectionService.savaWarnMessage(jh, monitorAlarmRangeMap);

        }
        result.setSuccess(true);
        result.setResult("finish");
        return result;
    }

    /**
     * 重新填充定时任务某一天的数据，手动调用，可删除
     *
     * @param startTime
     * @return
     */
    @RequestMapping(value = "/refillAFocusWarnDay", method = RequestMethod.GET)
    public Result<String> fixGcyjEmptyError(String startTime) throws ParseException {
        log.info(String.format("补充开始， time: %s", startTime));
        Result<String> result = new Result<>();
        if (StringUtils.isEmpty(startTime)) {
            return result.error500("时间不能为空");
        }
        List<Map<String, Object>> jhList = iCommonApi.queryAllAJh();
        Date begin = org.jeecg.common.util.DateUtils.str2Date(startTime, org.jeecg.common.util.DateUtils.datetimeFormat.get());
        Integer index = 0;
        if (jhList.size() > 0) {
            for (Map<String, Object> itemMap : jhList) {
                index++;
                String ltd = itemMap.get("LTD").toString();
                String jh = itemMap.get("JH").toString();
                if (ltd.equals("1")) {
                    try {
                        List<Map<String, Object>> date = iCommonApi.queryDepthWithLastTime(jh);
                        if (date.size() > 0) {
                            List<Map<String, Object>> warnHisLast = iCommonApi.queryWellAfocusWarnLast(jh);
                            long minute = 0;
                            if (warnHisLast.size() > 0) {
                                String warnTime = warnHisLast.get(0).get("WARN_TIME").toString().substring(0, 19);
                                Date warnTimeValue = DateUtil.parse(warnTime);
                                Date now = new Date();
                                minute = DateUtil.between(warnTimeValue, now, DateUnit.MINUTE);
                            }

                            if (minute >= 1) {
                                log.info(String.format("%s 计算 %s %s ", begin, jh, index + "/" + jhList.size()));
                                Map<String, MonitorAlarmRanges> monitorAlarmRangeMap = wellDataDiagnosticService
                                        .test(jh, DateUtils.addMinutes(begin, 24 * 60), 24 * 60, true);
                                foucusWarnSectionService.savaWarnMessage(jh, monitorAlarmRangeMap);
                            }
                        }
                    } catch (Exception e) {
                        log.warn(e.getMessage());
                    }
                }
            }
        }

        result.setSuccess(true);
        result.setResult("finish");
        return result;
    }
}
