package org.jeecg.modules.stm.metric.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.stm.metric.entity.MetricMonitorSet;
import org.jeecg.modules.stm.metric.mapper.MetricMonitorSetMapper;
import org.jeecg.modules.stm.metric.pojo.QueryMetricMonitorSetVo;
import org.jeecg.modules.stm.metric.service.IMetricMonitorSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Description: 监测方案
 * @Author: jeecg-boot
 * @Date:   2023-07-15
 * @Version: V1.0
 */
@Service
public class MetricMonitorSetServiceImpl extends ServiceImpl<MetricMonitorSetMapper, MetricMonitorSet> implements IMetricMonitorSetService {

    @Autowired
    private IStDomainService iStDomainService;

    private static final String METRIC_BASE_PRE = "ZB";  // 基本指标
    private static final String METRIC_COMPUTER_PRE = "MS"; //计算指标
    private static final  int NO_SIZE = 5;

    @Override
    public IPage<QueryMetricMonitorSetVo> queryMonitorSet(IPage<QueryMetricMonitorSetVo> page, QueryMetricMonitorSetVo queryVo) {
        IPage<QueryMetricMonitorSetVo> data = this.baseMapper.queryMonitorSet(page,queryVo);
        return data;
    }
    @Override
    public String metricNo(int ctp){
        QueryWrapper<MetricMonitorSet> wapper = new QueryWrapper();
        wapper.eq("mtrc_ctp",ctp);
        wapper.like("mtrc_no",ctp==1?METRIC_BASE_PRE:METRIC_COMPUTER_PRE);
        wapper.orderByDesc("mtrc_no");
        wapper.last("limit 1");
        MetricMonitorSet noUpper = super.baseMapper.selectOne(wapper);
        StringBuffer sb = new StringBuffer();
        if(noUpper == null) {
            if(ctp==1) sb.append(METRIC_BASE_PRE);
            if(ctp==2) sb.append(METRIC_COMPUTER_PRE);
            sb.append(StringUtils.leftPad("1",NO_SIZE,"0"));
            return sb.toString();
        }else{
            if(ctp==1) {
                Integer no  = Integer.valueOf(noUpper.getMtrcNo().replace(METRIC_BASE_PRE,""))+1;
                sb.append(METRIC_BASE_PRE).append(StringUtils.leftPad(no.toString(),NO_SIZE,"0"));
            } else {
                Integer no  = Integer.valueOf(noUpper.getMtrcNo().replace(METRIC_COMPUTER_PRE,""))+1;
                sb.append(METRIC_COMPUTER_PRE).append(StringUtils.leftPad(no.toString(),NO_SIZE,"0"));
            }
            return sb.toString();
        }
    }

    public List<String> getNextMetricNoByCurNo(int ctp, Integer count) {
        QueryWrapper<MetricMonitorSet> wrapper = new QueryWrapper<>();
        wrapper.eq("mtrc_ctp", ctp)
                .likeRight("mtrc_no", ctp == 1 ? METRIC_BASE_PRE : METRIC_COMPUTER_PRE)
                .orderByDesc("mtrc_no")
                .last("LIMIT 1");
        MetricMonitorSet metricMonitorSet = super.baseMapper.selectOne(wrapper);
        String curNo = metricMonitorSet != null ? metricMonitorSet.getMtrcNo() : null;
        String prefix = (ctp == 1) ? METRIC_BASE_PRE : METRIC_COMPUTER_PRE;
        int currentNumber = Integer.parseInt(curNo.replace(prefix, ""));

        return IntStream.range(1, count + 1)
                .mapToObj(i -> {
                    String paddedNumber = StringUtils.leftPad(
                            String.valueOf(currentNumber + i), NO_SIZE, "0");
                    return prefix + paddedNumber;
                })
                .collect(Collectors.toList());
    }


    @Override
    public IPage<MetricMonitorSet> queryWithPage(IPage<MetricMonitorSet> page, QueryWrapper<MetricMonitorSet> queryWrapper) {
        IPage<MetricMonitorSet> data = this.page(page,queryWrapper);
        List<MetricMonitorSet> list =  data.getRecords();
        List<String> listCode = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            listCode.add(list.get(i).getDomainNo());
        }
        Map<String,String> codeNameMap = iStDomainService.getAllParentCode(listCode);
        if(codeNameMap!=null&&codeNameMap.size()>0) {
            for(int i=0;i<list.size();i++) {
                String strDomainCode = list.get(i).getDomainNo();
                if (strDomainCode != null) {
                    String strDomain = codeNameMap.get(strDomainCode);
                    if (strDomain != null) {
                        list.get(i).setDomainName(strDomain);
                    }
                }
            }
        }
        return data;
    }

}
