package com.njcn.supervision.service.databank.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.supervision.enums.SupervisionResponseEnum;
import com.njcn.supervision.mapper.databank.LibAlgorithmMapper;
import com.njcn.supervision.pojo.param.databank.LibAlgorithmParam;
import com.njcn.supervision.pojo.po.databank.LibAlgorithm;
import com.njcn.supervision.service.databank.LibAlgorithmService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 
 * Description:
 * Date: 2024/9/10  14:16【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class LibAlgorithmServiceImpl extends ServiceImpl<LibAlgorithmMapper, LibAlgorithm> implements LibAlgorithmService {



    @Override
    @Transactional(rollbackFor = Exception.class)
    public LibAlgorithm add(LibAlgorithmParam libAlgorithmParam) {

        checkName(libAlgorithmParam,false);
        LibAlgorithm libAlgorithm = new LibAlgorithm();
        BeanUtils.copyProperties(libAlgorithmParam, libAlgorithm);
        libAlgorithm.setStatus(1);
        if(StringUtils.isEmpty(libAlgorithmParam.getPid())){
            libAlgorithm.setPid("0");
            libAlgorithm.setPids("0");
        }else {
            LibAlgorithm fatherLibAlgorithm = this.lambdaQuery().eq(LibAlgorithm::getId, libAlgorithmParam.getPid()).one();
            libAlgorithm.setPids(fatherLibAlgorithm.getPids() + "," + libAlgorithm.getPid());
        }


        this.save(libAlgorithm);
        return libAlgorithm;

    }

    private void checkName(LibAlgorithmParam libAlgorithmParam, boolean isExcludeSelf) {

        String pid =StringUtils.isEmpty(libAlgorithmParam.getPid())?"0":libAlgorithmParam.getPid();

        LambdaQueryWrapper<LibAlgorithm> sgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sgUserLambdaQueryWrapper
                .eq(LibAlgorithm::getName, libAlgorithmParam.getName())
                .eq(LibAlgorithm::getPid,pid)
                .eq(LibAlgorithm::getStatus, DataStateEnum.ENABLE.getCode());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (libAlgorithmParam instanceof LibAlgorithmParam.LibAlgorithmParamUpdate) {
                sgUserLambdaQueryWrapper.ne(LibAlgorithm::getId, (( LibAlgorithmParam.LibAlgorithmParamUpdate) libAlgorithmParam).getId());
            }
        }
        int countByAccount = this.count(sgUserLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new BusinessException(SupervisionResponseEnum.NAME_EXISTS);
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateAlgorithmLibrary(LibAlgorithmParam.LibAlgorithmParamUpdate paramUpdate) {
        checkName(paramUpdate,true);
        LibAlgorithm libAlgorithm = new LibAlgorithm();
        BeanUtil.copyProperties(paramUpdate, libAlgorithm);
        this.updateById(libAlgorithm);
        return libAlgorithm.getId();
    }

    @Override
    public List<LibAlgorithm> queryAll() {
        List<LibAlgorithm> libAlgorithmList = this.lambdaQuery().eq(LibAlgorithm::getStatus,1).list();
        return libAlgorithmList.stream().filter(item -> BizParamConstant.PARENT_ID.equals(item.getPid())).peek(item -> {
            item.setLevel(1);
            item.setChildren(getChildren(item, libAlgorithmList));
        }).collect(Collectors.toList());
    }
    private List<LibAlgorithm> getChildren(LibAlgorithm libAlgorithm, List<LibAlgorithm> all) {
        return all.stream().filter(item -> item.getPid().equals(libAlgorithm.getId())).peek(item -> {
            item.setLevel(libAlgorithm.getLevel()+1);
            item.setChildren(getChildren(item, all));
        }).collect(Collectors.toList());
    }
}
