package com.njcn.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.system.pojo.param.DictTreeParam;
import com.njcn.system.pojo.vo.DictTreeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.system.mapper.SysDicTreePOMapper;
import com.njcn.system.pojo.po.SysDicTreePO;
import com.njcn.system.service.SysDicTreePOService;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * Description:
 * Date: 2023/6/9  14:00【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class SysDicTreePOServiceImpl extends ServiceImpl<SysDicTreePOMapper, SysDicTreePO> implements SysDicTreePOService{

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addDictTree(DictTreeParam dictTreeParam) {
        SysDicTreePO sysDicTreePO = new SysDicTreePO();
        BeanUtils.copyProperties(dictTreeParam, sysDicTreePO);
        if(!Objects.equals(sysDicTreePO.getPid(), "0")){
            QueryWrapper<SysDicTreePO> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("id", sysDicTreePO.getPid());
            SysDicTreePO instance = this.baseMapper.selectOne(queryWrapper);


                sysDicTreePO.setPids(instance.getPids()+","+instance.getId());

        }else{
            sysDicTreePO.setPids("0");
        }
        sysDicTreePO.setStatus(0);
        boolean save = this.save(sysDicTreePO);

        return save;
    }

    @Override
    public List<DictTreeVO> queryByPid(String pid) {
        List<DictTreeVO> collect = new ArrayList<>();
        LambdaQueryWrapper<SysDicTreePO> query = new LambdaQueryWrapper<>();
        query.eq(SysDicTreePO::getPid, pid).eq(SysDicTreePO::getStatus,"0");
        List<SysDicTreePO> resultList = this.list(query);

        SysDicTreePO byId = this.getById(pid);

        if (CollUtil.isNotEmpty(resultList)) {
            collect = resultList.stream().map(temp -> {
                DictTreeVO resultVO = new DictTreeVO();
                BeanUtils.copyProperties(temp, resultVO);
                resultVO.setPname(byId.getName());
                return resultVO;
            }).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public DictTreeVO queryByCode(String code) {
        LambdaQueryWrapper<SysDicTreePO> query = new LambdaQueryWrapper<>();
        query.clear();
        query.eq(SysDicTreePO::getCode, code).eq(SysDicTreePO::getStatus,"0");
        SysDicTreePO result = this.getOne(query);

        if (result != null) {
            DictTreeVO resultVO = new DictTreeVO();
            BeanUtils.copyProperties(result, resultVO);
            return resultVO;
        }

        return null;
    }

    @Override
    public List<DictTreeVO> queryByid(String id) {
        List<DictTreeVO> dictTreeVOS=this.baseMapper.queryByid(id);
        return dictTreeVOS;
    }

    @Override
    public SysDicTreePO queryById(String id) {
        return this.lambdaQuery().eq(SysDicTreePO::getId,id).eq(SysDicTreePO::getStatus,0).one();
    }

    @Override
    public List<SysDicTreePO> queryAll() {
        LambdaQueryWrapper<SysDicTreePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDicTreePO::getStatus, 0);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public List<SysDicTreePO> queryTree() {
        LambdaQueryWrapper<SysDicTreePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDicTreePO::getStatus, 0);
       List<SysDicTreePO> sysDicTreePOList = this.list(lambdaQueryWrapper);
       List<SysDicTreePO> parent = sysDicTreePOList.stream().filter(item->item.getPid().equals("0") && !item.getCode().equals("Device_Unit")).peek(item->{item.setLevel(0);item.setChildren(getChildren(item,sysDicTreePOList));}).collect(Collectors.toList());
       return parent;
    }


    private List<SysDicTreePO> getChildren(SysDicTreePO sysDicTreePO,List<SysDicTreePO> all){
       return all.stream().filter(item->item.getPid().equals(sysDicTreePO.getId())).peek(item->{item.setLevel(sysDicTreePO.getLevel()+1);item.setChildren(getChildren(item,all));}).collect(Collectors.toList());
    }

}
