package com.njcn.gather.script.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.gather.detection.pojo.enums.DetectionCodeEnum;
import com.njcn.gather.detection.pojo.enums.DetectionResponseEnum;
import com.njcn.gather.script.mapper.PqScriptCheckDataMapper;
import com.njcn.gather.script.pojo.param.PqScriptCheckDataParam;
import com.njcn.gather.script.pojo.po.PqScriptCheckData;
import com.njcn.gather.script.service.IPqScriptCheckDataService;
import com.njcn.gather.system.dictionary.mapper.DictTreeMapper;
import com.njcn.gather.system.dictionary.pojo.po.DictTree;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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


/**
 * @Description:
 * @Author: wr
 * @Date: 2024/12/27 9:29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PqScriptCheckDataServiceImpl extends ServiceImpl<PqScriptCheckDataMapper, PqScriptCheckData> implements IPqScriptCheckDataService {

    private final String SYMBOL = "$";

    private final DictTreeMapper dictTreeMapper;

    @Override
    public List<String> getValueType(PqScriptCheckDataParam param) {
        String dataType = param.getDataType();
        if (StrUtil.isNotBlank(dataType)) {
            String[] split = dataType.split(",");
            if (split.length > 1) {
                dataType = split[0];
            }
            MPJLambdaWrapper<PqScriptCheckData> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.selectAll(PqScriptCheckData.class)
                    .leftJoin(DictTree.class, DictTree::getId, PqScriptCheckData::getValueType)
                    .eq(PqScriptCheckData::getScriptId, param.getScriptId())
                    .eq(ObjectUtil.isNotNull(param.getIndex()), PqScriptCheckData::getScriptIndex, param.getIndex())
                    .eq(PqScriptCheckData::getEnable, DataStateEnum.ENABLE.getCode());
            if (param.getIsValueTypeName()) {
                queryWrapper.selectAs(DictTree::getCode, PqScriptCheckData::getValueType);
            }
            List<PqScriptCheckData> pqScriptCheckData = this.getBaseMapper().selectJoinList(PqScriptCheckData.class, queryWrapper);
            if (CollUtil.isNotEmpty(pqScriptCheckData)) {
                if (param.getIsValueTypeName()) {
                    final String finalDataType = dataType;
                    List<String> checkData = pqScriptCheckData.stream().map(x -> finalDataType + SYMBOL + x.getValueType()).distinct().collect(Collectors.toList());
                    String U = finalDataType + SYMBOL + DetectionCodeEnum.U1.getCode();
                    if (!checkData.contains(U)) {
                        if (checkData.contains(finalDataType + SYMBOL + DetectionCodeEnum.V2_50.getCode())) {
                            checkData.add(U);
                        }
                    }
                    String I = finalDataType + SYMBOL + DetectionCodeEnum.I1.getCode();
                    if (!checkData.contains(I)) {
                        if (checkData.contains(finalDataType + SYMBOL + DetectionCodeEnum.I2_50.getCode())) {
                            checkData.add(I);
                        }
                    }
                    return checkData.stream().distinct().collect(Collectors.toList());
                } else {
                    return pqScriptCheckData.stream().map(PqScriptCheckData::getValueType).distinct().collect(Collectors.toList());
                }
            }
        }
        return new ArrayList<>();
    }

    @Override
    public Map<String, String> getValueTypeMap(String scriptId) {
        LambdaQueryWrapper<PqScriptCheckData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(PqScriptCheckData::getValueType)
                .eq(PqScriptCheckData::getScriptId, scriptId)
                .eq(PqScriptCheckData::getEnable, DataStateEnum.ENABLE.getCode());
        List<PqScriptCheckData> pqScriptCheckData = this.baseMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(pqScriptCheckData)) {
            throw new BusinessException(DetectionResponseEnum.SCRIPT_CHECK_DATA_NOT_EXIST);
        }
        List<String> valueTypeList = pqScriptCheckData.stream().map(PqScriptCheckData::getValueType).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<DictTree> dictTreeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dictTreeLambdaQueryWrapper.in(DictTree::getId, valueTypeList).eq(DictTree::getState, DataStateEnum.DELETED.getCode());
        List<DictTree> dictTreeList = dictTreeMapper.selectList(dictTreeLambdaQueryWrapper);
        Map<String, String> map = dictTreeList.stream().collect(Collectors.toMap(DictTree::getCode, DictTree::getId));
        return map;
    }

    @Override
    public List<PqScriptCheckData> listCheckDataCode(PqScriptCheckDataParam param) {
        MPJLambdaWrapper<PqScriptCheckData> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(PqScriptCheckData.class)
                .leftJoin(DictTree.class, DictTree::getId, PqScriptCheckData::getValueType)
                .selectAs(DictTree::getCode, PqScriptCheckData::getValueTypeCode)
                .eq(PqScriptCheckData::getScriptId, param.getScriptId())
                .eq(PqScriptCheckData::getScriptIndex, param.getIndex())
                .eq(PqScriptCheckData::getEnable, DataStateEnum.ENABLE.getCode());
        return this.getBaseMapper().selectJoinList(PqScriptCheckData.class, queryWrapper);
    }

    @Override
    public Double getCheckDataValue(PqScriptCheckDataParam param) {
        LambdaQueryWrapper<PqScriptCheckData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(PqScriptCheckData::getScriptId, param.getScriptId())
                .eq(ObjectUtil.isNotNull(param.getIndex()), PqScriptCheckData::getScriptIndex, param.getIndex())
                .eq(PqScriptCheckData::getValueType, param.getValueType())
                .eq(PqScriptCheckData::getPhase, param.getPhase())
                .eq(PqScriptCheckData::getEnable, DataStateEnum.ENABLE.getCode());

        PqScriptCheckData data = this.getBaseMapper().selectOne(queryWrapper);
        if (ObjectUtil.isNotNull(data)) {
            return data.getValue();
        }
        return null;
    }

    @Override
    public List<PqScriptCheckData> listCheckData(String scriptId, List<Integer> indexList) {
        LambdaQueryWrapper<PqScriptCheckData> queryWrapper = new LambdaQueryWrapper<PqScriptCheckData>()
                .eq(PqScriptCheckData::getScriptId, scriptId)
                .eq(PqScriptCheckData::getEnable, DataStateEnum.ENABLE.getCode())
                .eq(PqScriptCheckData::getErrorFlag, 1)
                .in(PqScriptCheckData::getScriptIndex, indexList)
                .orderByAsc(PqScriptCheckData::getHarmNum);
        return this.list(queryWrapper);
    }

    @Override
    public boolean deleteByScriptIds(List<String> ids) {
        return this.remove(new LambdaQueryWrapper<PqScriptCheckData>().in(PqScriptCheckData::getScriptId, ids));
    }
}
