package org.jeecg.modules.newWarning.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xkcoding.http.util.StringUtil;
import ognl.IntHashMap;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.newWarning.entity.*;
import org.jeecg.modules.newWarning.service.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 报警历史数据表 前端控制器
 * </p>
 *
 * @author zrj
 * @since 2022-12-09
 */
@RestController
@RequestMapping("/newWarn/warnHistory")
public class NewWarnHistoryController {
    @Resource
    private NewWarnHistoryService warnHistoryService;
    @Resource
    private HookLoadWarnLogService hookService;

    @Resource
    private TotalPoolVolumeWarnLogService poolService;
    @Resource
    private PumpingPressureWarnLogService pumpService;
    @Resource
    private TorqueWarnLogService torqueService;

    @GetMapping("/page")
    public Result<?> getHisPage(@RequestParam("jh") String jh, @RequestParam("warnTime") String warnTime,
                                @RequestParam("warnType") String warnType,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<NewWarnHistory> wrapper = new QueryWrapper<>();
        if (jh != null && !Objects.equals(jh, "")) {
            wrapper.eq("jh", jh);
        }
        if (warnType != null && !Objects.equals(warnType, "")) {
            wrapper.eq("warn_type", warnType);
        }
        if (warnTime != null && !Objects.equals(warnTime, "")) {
            wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 00:00:00");
            wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 23:59:59");
        }
        wrapper.orderByDesc("warn_time");
        Page<NewWarnHistory> page = new Page<NewWarnHistory>(pageNo, pageSize);
        Page<NewWarnHistory> pagelist = warnHistoryService.page(page, wrapper);
        return Result.OK(pagelist);
    }

    @GetMapping("/page_new")
    public Result<?> getHisPage_new(@RequestParam("jh") String jh,
                                    @RequestParam("warnTime") String warnTime,
                                    @RequestParam("warnType") String warnType,
                                    @RequestParam("starTime") String starTime,
                                    @RequestParam("endTime") String endTime,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<NewWarnHistory> wrapper = new QueryWrapper<>();
        if (jh != null && !Objects.equals(jh, "")) {
            wrapper.eq("jh", jh);
        }
        if (warnType != null && !Objects.equals(warnType, "")) {
            wrapper.eq("warn_type", warnType);
        }
        if (!StringUtil.isEmpty(starTime)) {
            wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
        }
        if (!StringUtil.isEmpty(endTime)) {
            wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
//            wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
//            wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        wrapper.orderByDesc("warn_time");
        wrapper.select("'[' || JH || ',' || WARN_TYPE || ']' || chr( 10 ) || '钻头位置:' || ROUND( warn_bitdepth, 2 ) || 'm' || chr( 10 ) || '异常原因:' || (\n" +
                        "\tCASE\n" +
                        "\t\t\t\n" +
                        "\t\t\tWHEN WARN_TYPE = '钩载变化异常' THEN\n" +
                        "\t\t\t( CASE warn_condition WHEN 'DrillDown' THEN '符合钩载变化异常预警条件' WHEN 'Trip' THEN '符合钩载变化异常预警条件' END ) \n" +
                        "\t\t\t\tWHEN WARN_TYPE = '扭矩变化异常' THEN\n" +
                        "\t\t\t\t'符合' || warn_des || '条件' \n" +
                        "\t\t\t\tWHEN WARN_TYPE = '泵压变化异常' THEN\n" +
                        "\t\t\t\t'符合泵压变化异常预警条件' \n" +
                        "\t\t\t\tWHEN WARN_TYPE = '总池体积变化异常' THEN\n" +
                        "\t\t\t\t'符合总池体积变化预警条件' \n" +
                        "\t\t\tEND \n" +
                        "\t\t\t\t) || '(' || (\n" +
                        "\t\t\tCASE\n" +
                        "\t\t\t\t\t\n" +
                        "\t\t\t\t\tWHEN WARN_TYPE = '钩载变化异常' THEN\n" +
                        "\t\t\t\t\t(\n" +
                        "\t\t\t\t\t\t'当前点钩载:' || ROUND( warn_woh, 2 ) || ' KN, 参考柱钩载:' || ROUND( normal_woh, 2 ) || ' KN, 钻杆重量:' || ROUND( column_weight, 2 ) || 'KN, 大于报警阈值' || warn_value || ' KN' \n" +
                        "\t\t\t\t\t) \n" +
                        "\t\t\t\t\tWHEN WARN_TYPE = '扭矩变化异常' THEN\n" +
                        "\t\t\t\t\t(\n" +
                        "\t\t\t\t\t\t'当前点扭矩:' || ROUND( warn_woh, 2 ) || ' KN, 基准波动幅度:' || ROUND( normal_woh, 2 ) || ' KN, 变化量平均值:' || ROUND( njbhl_per_torBig, 2 ) || ' KN, 大于波动报警阈值' || torgue_wave_threshold || '%' || ',或大于变化量报警阈值' || torgue_variation_threshold || '%' \n" +
                        "\t\t\t\t\t) \n" +
                        "\t\t\t\t\tWHEN WARN_TYPE = '泵压变化异常' THEN\n" +
                        "\t\t\t\t\t( '当前点立压:' || ROUND( warn_woh, 2 ) || ' Mpa, 上一点立压:' || ROUND( per_spp, 2 ) || ' MPa, 大于力压报警阈值:' || loop_drilling_spp || '%' ) \n" +
                        "\t\t\t\t\tWHEN WARN_TYPE = '总池体积变化异常' THEN\n" +
                        "\t\t\t\t\t( '当前点总池体积' || ROUND( warn_woh, 2 ) || ' m^³, 起点总池体积' || ROUND( pit_start, 2 ) || ' m^³, 起点时间:' || TO_CHAR( pit_start_time, 'yyyy-MM-dd HH24:mi:ss' ) ) || ', 计算差值:' || ROUND( pit_sub, 2 ) || ' m^³,大于报警阈值' || ROUND( pit_value, 2 ) || ' m^³' \n" +
                        "\t\t\t\tEND \n" +
                        "\t\t\t\t) || ')' YCMS",
                "ID", "JH", "warn_hkh", "warn_bitdepth", "warn_woh", "whole_condition",
                "warn_type", "warn_time", "warn_condition", "NORMAL_STARTIME normalStarttime",
                "NORMAL_ENDTIME", "normal_woh", "normal_num", "warn_threshold", "bitdepth",
                "column_weight", "warn_value", "hkh_rang", "loop_drilling_pump", "loop_drilling_spp",
                "per_spp", "torquepreviousjs", "torguetimes", "torgue_variation_threshold", "torgue_wave_threshold",
                "warn_des", "njbhl_per_time", "njbhl_per_torBig", "pit_start_time", "pit_start", "pit_sub",
                "pit_value");
        Page<NewWarnHistory> page = new Page<NewWarnHistory>(pageNo, pageSize);
        Page<NewWarnHistory> pagelist = warnHistoryService.page(page, wrapper);
        return Result.OK(pagelist);
    }

    @GetMapping("/pageCount")
    public Result<?> pageCount(@RequestParam("jh") String jh,
                               @RequestParam("starTime") String starTime,
                               @RequestParam("endTime") String endTime) {

        Map<String, Integer> map = new HashMap<>();
        QueryWrapper<NewWarnHistory> gzWrapper = new QueryWrapper<>();
        gzWrapper.eq("warn_type", "钩载变化异常");
        if (jh != null && !Objects.equals(jh, "")) {
            gzWrapper.eq("jh", jh);
        }
        if (!StringUtil.isEmpty(starTime)) {
            gzWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
        }
        if (!StringUtil.isEmpty(endTime)) {
            gzWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }


        QueryWrapper<NewWarnHistory> byWrapper = new QueryWrapper<>();
        byWrapper.eq("warn_type", "泵压变化异常");
        if (jh != null && !Objects.equals(jh, "")) {
            byWrapper.eq("jh", jh);
        }
        if (!StringUtil.isEmpty(starTime)) {
            byWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
        }
        if (!StringUtil.isEmpty(endTime)) {
            byWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }


        QueryWrapper<NewWarnHistory> njWrapper = new QueryWrapper<>();
        njWrapper.eq("warn_type", "扭矩变化异常");
        if (jh != null && !Objects.equals(jh, "")) {
            njWrapper.eq("jh", jh);
        }
        if (!StringUtil.isEmpty(starTime)) {
            njWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
        }
        if (!StringUtil.isEmpty(endTime)) {
            njWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }


        QueryWrapper<NewWarnHistory> tjWrapper = new QueryWrapper<>();
        tjWrapper.eq("warn_type", "总池体积变化异常");
        if (jh != null && !Objects.equals(jh, "")) {
            tjWrapper.eq("jh", jh);
        }
        if (!StringUtil.isEmpty(starTime)) {
            tjWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
        }
        if (!StringUtil.isEmpty(endTime)) {
            tjWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }


        map.put("gjCount", warnHistoryService.count(gzWrapper));
        map.put("byCount", warnHistoryService.count(byWrapper));
        map.put("njCount", warnHistoryService.count(njWrapper));
        map.put("tjCount", warnHistoryService.count(tjWrapper));

        return Result.OK(map);
    }

    @GetMapping("/getWarnHisCount")
    public Result<?> getWarnHisCount(@RequestParam("jh") String jh, @RequestParam("warnTime") String warnTime) {
        QueryWrapper<NewWarnHistory> wrapper = new QueryWrapper<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        wrapper.eq("jh", jh);
        wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 00:00:00");
        wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 23:59:59");
        int count = warnHistoryService.count(wrapper);
        JSONObject res = new JSONObject();
        res.put("count", count);
        if (count > 0) {
            QueryWrapper<NewWarnHistory> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("jh", jh);
            wrapper1.eq("warn_type", "钩载变化异常");
            wrapper1.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 00:00:00");
            wrapper1.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 23:59:59");
            wrapper1.orderByDesc("WARN_TIME");
            List<NewWarnHistory> gzlist = warnHistoryService.list(wrapper1);
            QueryWrapper<NewWarnHistory> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("jh", jh);
            wrapper2.eq("warn_type", "泵压变化异常");
            wrapper2.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 00:00:00");
            wrapper2.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 23:59:59");
            wrapper2.orderByDesc("WARN_TIME");
            List<NewWarnHistory> bylist = warnHistoryService.list(wrapper2);
            QueryWrapper<NewWarnHistory> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("jh", jh);
            wrapper3.eq("warn_type", "扭矩变化异常");
            wrapper3.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 00:00:00");
            wrapper3.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 23:59:59");
            wrapper3.orderByDesc("WARN_TIME");
            List<NewWarnHistory> njlist = warnHistoryService.list(wrapper3);
            QueryWrapper<NewWarnHistory> wrapper4 = new QueryWrapper<>();
            wrapper4.eq("jh", jh);
            wrapper4.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 00:00:00");
            wrapper4.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 23:59:59");
            wrapper4.orderByDesc("WARN_TIME");
            NewWarnHistory zxData = warnHistoryService.selectOneOrcale(wrapper4);
            QueryWrapper<NewWarnHistory> wrapper5 = new QueryWrapper<>();
            wrapper5.eq("jh", jh);
            wrapper5.eq("warn_type", "总池体积变化异常");
            wrapper5.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 00:00:00");
            wrapper5.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", warnTime + " 23:59:59");
            wrapper5.orderByDesc("WARN_TIME");
            List<NewWarnHistory> jlData = warnHistoryService.list(wrapper5);
            res.put("jlData", jlData);
            res.put("gzdata", gzlist);
            res.put("bydata", bylist);
            res.put("njdata", njlist);
            res.put("zxdata", zxData);
        }
        return Result.OK(res);
    }

    /*带有起始时间结束时间查询*/
    @GetMapping("/getWarnHisCount_new")
    public Result<?> getWarnHisCount_new(@RequestParam("jh") String jh, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) {
        QueryWrapper<NewWarnHistory> wrapper = new QueryWrapper<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        wrapper.eq("jh", jh);
        wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
        wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        int count = warnHistoryService.count(wrapper);
        JSONObject res = new JSONObject();
        res.put("count", count);
        if (count > 0) {
            QueryWrapper<NewWarnHistory> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("jh", jh);
            wrapper1.eq("warn_type", "钩载变化异常");
            wrapper1.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
            wrapper1.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
            wrapper1.orderByDesc("WARN_TIME");
            List<NewWarnHistory> gzlist = warnHistoryService.list(wrapper1);
            QueryWrapper<NewWarnHistory> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("jh", jh);
            wrapper2.eq("warn_type", "泵压变化异常");
            wrapper2.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
            wrapper2.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
            wrapper2.orderByDesc("WARN_TIME");
            List<NewWarnHistory> bylist = warnHistoryService.list(wrapper2);
            QueryWrapper<NewWarnHistory> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("jh", jh);
            wrapper3.eq("warn_type", "扭矩变化异常");
            wrapper3.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
            wrapper3.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
            wrapper3.orderByDesc("WARN_TIME");
            List<NewWarnHistory> njlist = warnHistoryService.list(wrapper3);
            QueryWrapper<NewWarnHistory> wrapper4 = new QueryWrapper<>();
            wrapper4.eq("jh", jh);
            wrapper4.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
            wrapper4.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
            wrapper4.orderByDesc("WARN_TIME");
            NewWarnHistory zxData = warnHistoryService.selectOneOrcale(wrapper4);
            QueryWrapper<NewWarnHistory> wrapper5 = new QueryWrapper<>();
            wrapper5.eq("jh", jh);
            wrapper5.eq("warn_type", "总池体积变化异常");
            wrapper5.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
            wrapper5.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
            wrapper5.orderByDesc("WARN_TIME");
            List<NewWarnHistory> jlData = warnHistoryService.list(wrapper5);
            res.put("jlData", jlData);
            res.put("gzdata", gzlist);
            res.put("bydata", bylist);
            res.put("njdata", njlist);
            res.put("zxdata", zxData);
        }

//        JSONObject res=new JSONObject();
//        QueryWrapper<HookLoadWarnLog> wrapper =new QueryWrapper<>();
//        wrapper.eq("jh",jh);
//        wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
//        wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
//        List<HookLoadWarnLog> gzlist = hookService.list(wrapper);
//        res.put("gzdata",gzlist);
//        res.put("count",gzlist.size());

        return Result.OK(res);
    }

    @GetMapping("/groupSelct")
    public Result<?> groupSelct(@RequestParam("jh") String jh,
                                @RequestParam("warnTime") String warnTime,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        String start = warnTime + " 00:00:00";
        String end = warnTime + " 23:59:59";
        QueryWrapper<NewWarnHistory> wrapper0 = new QueryWrapper<>();
        if (jh != null && !"".equals(jh)) {
            wrapper0.like("jh", jh);
        }
        wrapper0.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", start);
        wrapper0.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", end);
        Page<NewWarnHistory> page = new Page<NewWarnHistory>(pageNo, pageSize);
        Page<NewWarnHistory> newWarnHistoryPage = warnHistoryService.groupSelct(wrapper0, page);
        List<NewWarnHistory> records = newWarnHistoryPage.getRecords();
        for (NewWarnHistory model : records) {
            QueryWrapper<NewWarnHistory> wrapper = new QueryWrapper<>();
            wrapper.eq("jh", model.getJh());
            wrapper.eq("warn_type", "起下钻预警");
            wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", start);
            wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", end);
            wrapper.orderByDesc("WARN_TIME");
            List<NewWarnHistory> qxzList = warnHistoryService.list(wrapper);
            QueryWrapper<NewWarnHistory> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("jh", model.getJh());
            wrapper1.eq("warn_type", "泵压变化预警");
            wrapper1.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", start);
            wrapper1.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", end);
            wrapper.orderByDesc("WARN_TIME");
            List<NewWarnHistory> byList = warnHistoryService.list(wrapper1);
            List<String> tag = new ArrayList<>();
            if (qxzList.size() > 0) {
                tag.add("起下钻预警");
            }
            if (byList.size() > 0) {
                tag.add("泵压变化预警");
            }
            model.setTag(String.join(",", tag));
        }
        return Result.OK(newWarnHistoryPage);
    }

    @GetMapping("/groupData")
    public Result<?> groupData() {
        List<String> dateList = warnHistoryService.groupData();
        return Result.OK(dateList);
    }


    /**
     * 钩载预警
     *
     * @param jh
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getGzWarning")
    public Result<?> getGzWarning(
            @RequestParam("jh") String jh,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime
    ) {

        //钩载
        JSONObject res = new JSONObject();
        QueryWrapper<HookLoadWarnLog> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(HookLoadWarnLog::getJh, jh).and(
                wrapper1 -> wrapper1
                        .apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime)
                        .apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime).or()
                        .apply("MOBIL_DRILLING_TOOLS_START_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime)
                        .apply("MOBIL_DRILLING_TOOLS_START_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime)
        ).orderByDesc(HookLoadWarnLog::getWarnTime, HookLoadWarnLog::getMobilDrillingToolsStartTime);
        List<HookLoadWarnLog> gzlist = hookService.list(wrapper);

        //总池体积
        QueryWrapper<TotalPoolVolumeWarnLog> totalWrapper = new QueryWrapper<>();
        totalWrapper.eq("jh", jh);
        totalWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
        totalWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        totalWrapper.orderByDesc("WARN_TIME");
        List<TotalPoolVolumeWarnLog> jlData = poolService.list(totalWrapper);

        //泵压
        QueryWrapper<PumpingPressureWarnLog> pumpWrapper = new QueryWrapper<>();
        pumpWrapper.eq("jh", jh);
        pumpWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
        pumpWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        pumpWrapper.orderByDesc("WARN_TIME");
        List<PumpingPressureWarnLog> bylist = pumpService.list(pumpWrapper);

        //扭矩
        QueryWrapper<TorqueWarnLog> torqueWrapper = new QueryWrapper<>();
        torqueWrapper.eq("jh", jh);
        torqueWrapper.apply("WARN_START_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
        torqueWrapper.apply("WARN_START_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        torqueWrapper.orderByDesc("WARN_START_TIME");
        List<TorqueWarnLog> njList = torqueService.list(torqueWrapper);
        for (TorqueWarnLog log : njList) {
            log.setWarnTime();
        }

        res.put("jlData", jlData);
        res.put("gzdata", gzlist);
        res.put("bydata", bylist);
        res.put("njdata", njList);
        res.put("zxdata", hookService.getOneWarning(startTime, endTime, jh));
        res.put("count", gzlist.size() + jlData.size() + bylist.size() + njList.size());

        return Result.OK(res);
    }


    /**
     * 钩载预警
     *
     * @param jh
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getGzWarningPage")
    public Result<?> getGzWarningPage(
            @RequestParam("jh") String jh,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper<HookLoadWarnLog> wrapper = new QueryWrapper<>();
        wrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(startTime)) {
        wrapper.and(
                wrapper1 -> wrapper1
                        .apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime)
                        .apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime)
        ).or(
                wrapper1 -> wrapper1
                        .apply("MOBIL_DRILLING_TOOLS_START_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime)
                        .apply("MOBIL_DRILLING_TOOLS_START_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime)
        );
        }
        Page<HookLoadWarnLog> page = new Page<HookLoadWarnLog>(pageNo, pageSize);

        Page<HookLoadWarnLog> gzlist = hookService.page(page, wrapper);

        return Result.OK(gzlist);
    }

    /**
     * 总池体积预警
     *
     * @param jh
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getZcWarningPage")
    public Result<?> getZcWarningPage(
            @RequestParam("jh") String jh,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper<TotalPoolVolumeWarnLog> wrapper = new QueryWrapper<>();
        wrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(startTime)) {
            wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
            wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        Page<TotalPoolVolumeWarnLog> page = new Page<TotalPoolVolumeWarnLog>(pageNo, pageSize);

        Page<TotalPoolVolumeWarnLog> gzlist = poolService.page(page, wrapper);
        return Result.OK(gzlist);
    }

    /**
     * 泵压预警
     *
     * @param jh
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getByWarningPage")
    public Result<?> getByWarningPage(
            @RequestParam("jh") String jh,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper<PumpingPressureWarnLog> wrapper = new QueryWrapper<>();
        wrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(startTime)) {
            wrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
            wrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        Page<PumpingPressureWarnLog> page = new Page<PumpingPressureWarnLog>(pageNo, pageSize);

        Page<PumpingPressureWarnLog> gzlist = pumpService.page(page, wrapper);

        return Result.OK(gzlist);
    }

    /**
     * 扭矩预警
     *
     * @param jh
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getNjWarningPage")
    public Result<?> getNjWarningPage(
            @RequestParam("jh") String jh,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper<TorqueWarnLog> wrapper = new QueryWrapper<>();
        wrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(startTime)) {
            wrapper.apply("WARN_START_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", startTime);
            wrapper.apply("WARN_START_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        Page<TorqueWarnLog> page = new Page<TorqueWarnLog>(pageNo, pageSize);

        Page<TorqueWarnLog> gzlist = torqueService.page(page, wrapper);
        for (TorqueWarnLog log : gzlist.getRecords()) {
            log.setWarnTime();
        }
        return Result.OK(gzlist);
    }

    @GetMapping("/pageNewCount")
    public Result<?> pageNewCount(@RequestParam("jh") String jh,
                                  @RequestParam("starTime") String starTime,
                                  @RequestParam("endTime") String endTime) {

        Map<String, Integer> map = new HashMap<>();


        //钩载
        QueryWrapper<HookLoadWarnLog> gzWrapper = new QueryWrapper<>();
        gzWrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(starTime)) {
            gzWrapper.lambda().eq(HookLoadWarnLog::getJh, jh).and(
                    wrapper1 -> wrapper1
                            .apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime)
                            .apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime).or()
                            .apply("MOBIL_DRILLING_TOOLS_START_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime)
                            .apply("MOBIL_DRILLING_TOOLS_START_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime)
            ).orderByDesc(HookLoadWarnLog::getWarnTime, HookLoadWarnLog::getMobilDrillingToolsStartTime);
        } else {
            gzWrapper.lambda().orderByDesc(HookLoadWarnLog::getWarnTime, HookLoadWarnLog::getMobilDrillingToolsStartTime);
        }
        //总池体积
        QueryWrapper<TotalPoolVolumeWarnLog> totalWrapper = new QueryWrapper<>();
        totalWrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(starTime)) {
            totalWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
            totalWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        totalWrapper.lambda().orderByDesc(TotalPoolVolumeWarnLog::getWarnTime);
        //泵压
        QueryWrapper<PumpingPressureWarnLog> pumpWrapper = new QueryWrapper<>();
        pumpWrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(starTime)) {
            pumpWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
            pumpWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        pumpWrapper.lambda().orderByDesc(PumpingPressureWarnLog::getWarnTime);
        //扭矩
        QueryWrapper<TorqueWarnLog> torqueWrapper = new QueryWrapper<>();
        torqueWrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(starTime)) {
            torqueWrapper.apply("WARN_START_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
            torqueWrapper.apply("WARN_START_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        torqueWrapper.lambda().orderByDesc(TorqueWarnLog::getWarnStartTime);

        map.put("gjCount", hookService.count(gzWrapper));
        map.put("byCount", pumpService.count(pumpWrapper));
        map.put("njCount", torqueService.count(torqueWrapper));
        map.put("tjCount", poolService.count(totalWrapper));

        return Result.OK(map);
    }


    @GetMapping("/getWarnCount")
    public Result<?> getWarnCount(@RequestParam("jh") String jh,
                                  @RequestParam("starTime") String starTime,
                                  @RequestParam("endTime") String endTime) {
        Map<String, Integer> map = new HashMap<>();
        //钩载
        QueryWrapper<HookLoadWarnLog> gzWrapper = new QueryWrapper<>();
        gzWrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(starTime)) {
            gzWrapper.lambda().eq(HookLoadWarnLog::getJh, jh).and(
                    wrapper1 -> wrapper1
                            .apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime)
                            .apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime).or()
                            .apply("MOBIL_DRILLING_TOOLS_START_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime)
                            .apply("MOBIL_DRILLING_TOOLS_START_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime)
            ).orderByDesc(HookLoadWarnLog::getWarnTime, HookLoadWarnLog::getMobilDrillingToolsStartTime);
        } else {
            gzWrapper.lambda().orderByDesc(HookLoadWarnLog::getWarnTime, HookLoadWarnLog::getMobilDrillingToolsStartTime);
        }
        //总池体积
        QueryWrapper<TotalPoolVolumeWarnLog> totalWrapper = new QueryWrapper<>();
        totalWrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(starTime)) {
            totalWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
            totalWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        totalWrapper.lambda().orderByDesc(TotalPoolVolumeWarnLog::getWarnTime);
        //泵压
        QueryWrapper<PumpingPressureWarnLog> pumpWrapper = new QueryWrapper<>();
        pumpWrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(starTime)) {
            pumpWrapper.apply("WARN_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
            pumpWrapper.apply("WARN_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        pumpWrapper.lambda().orderByDesc(PumpingPressureWarnLog::getWarnTime);
        //扭矩
        QueryWrapper<TorqueWarnLog> torqueWrapper = new QueryWrapper<>();
        torqueWrapper.eq("jh", jh);
        if (StringUtil.isNotEmpty(starTime)) {
            torqueWrapper.apply("WARN_START_TIME >= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", starTime);
            torqueWrapper.apply("WARN_START_TIME <= TO_DATE({0},'yyyy-MM-dd HH24:mi:ss')", endTime);
        }
        torqueWrapper.lambda().orderByDesc(TorqueWarnLog::getWarnStartTime);
        Integer gjCount = hookService.count(gzWrapper);
        Integer byCount = pumpService.count(pumpWrapper);
        Integer njCount = torqueService.count(torqueWrapper);
        Integer tjCount = poolService.count(totalWrapper);
        map.put("warnCount", gjCount + byCount + njCount + tjCount);
        return Result.OK(map);
    }

}
