package com.ruoyi.sim.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sim.domain.vo.FaultNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sim.mapper.FaultMapper;
import com.ruoyi.sim.domain.Fault;
import com.ruoyi.sim.service.IFaultService;

/**
 * 故障Service业务层处理
 *
 * @author tom
 * @date 2024-12-13
 */
@Service
public class FaultServiceImpl implements IFaultService {
    @Autowired
    private FaultMapper faultMapper;

    /**
     * 查询故障
     *
     * @param faultId 故障主键
     * @return 故障
     */
    @Override
    public Fault selectFaultByFaultId(String faultId) {
        return faultMapper.selectFaultByFaultId(faultId);
    }

    /**
     * 查询故障列表
     *
     * @param fault 故障
     * @return 故障
     */
    @Override
    public List<Fault> selectFaultList(Fault fault) {
        return faultMapper.selectFaultList(fault);
    }

    /**
     * 新增故障
     *
     * @param fault 故障
     * @return 结果
     */
    @Override
    public int insertFault(Fault fault) {
        fault.setCreateTime(DateUtils.getNowDate());
        return faultMapper.insertFault(fault);
    }

    /**
     * 修改故障
     *
     * @param fault 故障
     * @return 结果
     */
    @Override
    public int updateFault(Fault fault) {
        fault.setUpdateTime(DateUtils.getNowDate());
        return faultMapper.updateFault(fault);
    }

    /**
     * 批量删除故障
     *
     * @param faultIds 需要删除的故障主键
     * @return 结果
     */
    @Override
    public int deleteFaultByFaultIds(String[] faultIds) {
        return faultMapper.deleteFaultByFaultIds(faultIds);
    }

    /**
     * 删除故障信息
     *
     * @param faultId 故障主键
     * @return 结果
     */
    @Override
    public int deleteFaultByFaultId(String faultId) {
        return faultMapper.deleteFaultByFaultId(faultId);
    }

    // -------------------------------- tom add  --------------------------------

    private static final Logger log = LoggerFactory.getLogger(FaultServiceImpl.class);

    /**
     * 查询故障列表tree
     *
     * @param fault 故障
     * @return 故障
     */
    @Override
    public List<FaultNode> selectFaultListAllTree(Fault fault) {
        //
        // fault.getParams().put(TableSupport.PAGE_NUM, 1);
        // fault.getParams().put(TableSupport.PAGE_SIZE, 100);
        //
        List<Fault> list = faultMapper.selectFaultList(fault);
        log.info("AAAAAAAAAAAAAAAAAAAAAAAAAAAA000000000000");
        log.info(Objects.requireNonNull(list).toString());
        log.info(Objects.requireNonNull(fault.getParams()).toString());
        log.info(Objects.requireNonNull(list.size()).toString());

        List<FaultNode> tempListNode = new ArrayList<>();
        for (Fault source : list) {
            FaultNode target = new FaultNode();
            BeanUtils.copyProperties(source, target);
            tempListNode.add(target);
        }
        log.info(Objects.requireNonNull(tempListNode).toString());
        List<FaultNode> tree = toTree(tempListNode, ROOT_FAULT_ID);
        log.info(Objects.requireNonNull(tree).toString());
        return tree;
    }

    private static String ROOT_FAULT_ID = "000000000000";

//    private static List<FaultNode> toTree2(List<FaultNode> list) {
//        List<FaultNode> tree = new ArrayList<>();
//        for (FaultNode fn1 : list) {
//            if (fn1.getParentFaultId().equals(ROOT_FAULT_ID)) {
//                tree.add(fn1);
//            }
//            for (FaultNode fn2 : list) {
//                if (fn2.getParentFaultId().equals(fn1.getParentFaultId())) {
//                    if (fn1.getChildren() == null) {
//                        fn1.setChildren(new ArrayList<>());
//                    }
//                    fn1.getChildren().add(fn2);
//                }
//            }
//        }
//        return tree;
//    }

    private static List<FaultNode> toTree(List<FaultNode> list, String parentFaultId) {
        List<FaultNode> tree = list
                .stream()
                .filter(
                        parent ->
                                parent.getParentFaultId().equals(parentFaultId))
                .map(child -> {
                    child.setChildren(toTree(list, child.getFaultId()));
                    return child;
                })
                .collect(Collectors.toList());
        return tree;
    }
}
