package org.jeecg.modules.yjComprehensiveLog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.yjComprehensiveLog.entity.YjComprehensiveRealLog;
import org.jeecg.modules.yjComprehensiveLog.mapper.YjComprehensiveRealLogMapper;
import org.jeecg.modules.yjComprehensiveLog.model.YjComprehensiveRealLogModel;
import org.jeecg.modules.yjComprehensiveLog.model.YjComprehensiveSumModel;
import org.jeecg.modules.yjComprehensiveLog.service.YjComprehensiveRealLogService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 综合异常日志信息;(yj_comprehensive_log)表服务实现类
 * @author : http://www.chiner.pro
 * @date : 2025-4-9
 */
@Service
public class YjComprehensiveRealLogServiceImpl extends ServiceImpl<YjComprehensiveRealLogMapper, YjComprehensiveRealLog> implements YjComprehensiveRealLogService {


    @Override
    public List<YjComprehensiveRealLog> getRealTimeLogByJh(String jh) {
        QueryWrapper<YjComprehensiveRealLog> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(YjComprehensiveRealLog::getJh,jh);
        List<YjComprehensiveRealLog> list = this.list(wrapper);
        return list;
    }

    /**
     * 分页查询
     * @param param
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @Override
    public IPage<YjComprehensiveRealLog> queryPage(YjComprehensiveRealLog param, Integer pageNo,
                                               Integer pageSize, HttpServletRequest req){
        QueryWrapper<YjComprehensiveRealLog> queryWrapper = QueryGenerator.initQueryWrapper(param, req.getParameterMap());
        Page<YjComprehensiveRealLog> page = new Page<YjComprehensiveRealLog>(pageNo, pageSize);
        return this.page(page, queryWrapper);
    }

    /**
     * 获取当前最新的风险数据
     * @return
     */
    @Override
    public List<YjComprehensiveRealLogModel> queryRealList() {
        List<YjComprehensiveRealLogModel> res=new ArrayList<>();
        List<YjComprehensiveRealLog> list = this.list();
        if(!list.isEmpty()){
            Map<String, List<YjComprehensiveRealLog>> groupedMap = list.stream()
                    .collect(Collectors.groupingBy(YjComprehensiveRealLog::getJh));
            groupedMap.forEach((jh, logList) -> {
                YjComprehensiveRealLogModel model = new YjComprehensiveRealLogModel();
                model.setJh(jh);
                model.setWarnTime(logList.get(0).getWarnTime());
                model.setWorkingCondition(logList.get(0).getWorkingCondition());
                model.setMotion(logList.get(0).getMotion());
                model.setDepth(logList.get(0).getDepth());
                model.setStartTime(logList.get(0).getStartTime());
                model.setEndTime(logList.get(0).getEndTime());
                logList.forEach(item->{
                    switch (item.getWarnType()){
                        case "井漏":
                            model.setJlfx(item.getWarnLevel());
                            break;
                        case "溢流":
                            model.setYlfx(item.getWarnLevel());
                            break;
                        case "卡钻":
                            model.setQzfx(item.getWarnLevel());
                            break;
                        default:
                            break;
                    }
                });
                res.add(model);
            });
        }

        return res.stream()
                .sorted((o1, o2) -> {
                    int p1 = getModelPriority(o1);
                    int p2 = getModelPriority(o2);
                    return Integer.compare(p2, p1); // 倒序排列
                })
                .collect(Collectors.toList());
    }

    private int getModelPriority(YjComprehensiveRealLogModel model) {
        if (model.getQzfx() != null) return 3; // 卡钻最高优先级
        if (model.getJlfx() != null) return 2;  // 井漏次之
        if (model.getYlfx() != null) return 1;  // 溢流最低
        return 0; // 无风险类型
    }
    /**
     * 获取各类风险统计数量
     * @return
     */
    @Override
    public List<YjComprehensiveSumModel> getRiskCount(){
        List<YjComprehensiveSumModel> res=new ArrayList<>();
        List<YjComprehensiveRealLog> list = this.list();
        if(list.isEmpty()){
            return res ;
        }
        Map<String, List<YjComprehensiveRealLog>> groupedMap = list.stream()
                .collect(Collectors.groupingBy(YjComprehensiveRealLog::getWarnType));
        groupedMap.forEach((key,value)->{
            YjComprehensiveSumModel resModel=new YjComprehensiveSumModel();
            String warnType=key;
            resModel.setWarnType(key);
            Integer dfxCount = resModel.getDfxCount();
            Integer zfxCount =resModel.getZfxCount();
            Integer gfxCount =resModel.getGfxCount();
            AtomicReference<Integer> dfxInt= new AtomicReference<>(0);
            AtomicReference<Integer> zfxInt= new AtomicReference<>(0);
            AtomicReference<Integer> gfxInt= new AtomicReference<>(0);
            value.forEach(item->{
                BigDecimal warnLevel = item.getWarnLevel();
                if(warnLevel.compareTo(BigDecimal.ZERO) >= 0 && warnLevel.compareTo(new BigDecimal(50)) < 0) {
                    dfxInt.getAndSet(dfxInt.get() + 1);
                } else if(warnLevel.compareTo(new BigDecimal(50)) >= 0 && warnLevel.compareTo(new BigDecimal(80)) < 0) {
                    zfxInt.getAndSet(zfxInt.get() + 1);
                } else if(warnLevel.compareTo(new BigDecimal(80)) >= 0 && warnLevel.compareTo(new BigDecimal(100)) <= 0) {
                    gfxInt.getAndSet(gfxInt.get() + 1);
                }
            });
            // 最后设置统计结果
            resModel.setDfxCount(dfxInt.get());
            resModel.setZfxCount(zfxInt.get());
            resModel.setGfxCount(gfxInt.get());
            res.add(resModel);
        });
        return res;
    }




}
