package com.ruoyi.zongzhi.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zongzhi.domain.TcTown;
import com.ruoyi.zongzhi.mapper.TcTownMapper;
import com.ruoyi.zongzhi.service.ITcTownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.ruoyi.zongzhi.domain.vo.TreeNode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 区域Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-10
 */
@Service
public class TcTownServiceImpl implements ITcTownService {
    @Autowired
    private TcTownMapper tcTownMapper;

    /**
     * 查询区域
     *
     * @param id 区域主键
     * @return 区域
     */
    @Override
    public TcTown selectTcTownById(Long id) {
        return tcTownMapper.selectTcTownById(id);
    }

    /**
     * 查询区域列表
     *
     * @param tcTown 区域
     * @return 区域
     */
    @Override
    public List<TcTown> selectTcTownList(TcTown tcTown) {
        return tcTownMapper.selectTcTownList(tcTown);
    }

    /**
     * 新增区域
     *
     * @param tcTown 区域
     * @return 结果
     */
    @Override
    public int insertTcTown(TcTown tcTown) {
        tcTown.setCreateTime(DateUtils.getNowDate());
        return tcTownMapper.insertTcTown(tcTown);
    }

    /**
     * 修改区域
     *
     * @param tcTown 区域
     * @return 结果
     */
    @Override
    public int updateTcTown(TcTown tcTown) {
        return tcTownMapper.updateTcTown(tcTown);
    }

    /**
     * 批量删除区域
     *
     * @param ids 需要删除的区域主键
     * @return 结果
     */
    @Override
    public int deleteTcTownByIds(Long[] ids) {
        return tcTownMapper.deleteTcTownByIds(ids);
    }

    /**
     * 删除区域信息
     *
     * @param id 区域主键
     * @return 结果
     */
    @Override
    public int deleteTcTownById(Long id) {
        return tcTownMapper.deleteTcTownById(id);
    }


    @Override
    public String importUser(List<TcTown> tcTownList) {
        StringBuilder successMsg = new StringBuilder();
        if (!tcTownList.isEmpty()) {
            for (TcTown tcTown : tcTownList) {
                tcTownMapper.insertTcTown(tcTown);
            }
            successMsg.append("导入成功");
        } else {
            successMsg.append("导入失败，文件为空");
        }

        return successMsg.toString();
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param tcTowns 区域列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeNode> buildDeptTreeSelect(List<TcTown> tcTowns) {
        List<TcTown> deptTrees = buildDeptTree(tcTowns);
        return deptTrees.stream().map(TreeNode::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<TcTown> buildDeptTree(List<TcTown> depts) {
        List<TcTown> returnList = new ArrayList<TcTown>();
        List<Long> tempList = new ArrayList<Long>();
        for (TcTown dept : depts) {
            tempList.add(dept.getId());
        }
        for (Iterator<TcTown> iterator = depts.iterator(); iterator.hasNext(); ) {
            TcTown dept = (TcTown) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId())) {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<TcTown> list, TcTown t) {
        // 得到子节点列表
        List<TcTown> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TcTown tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }


    /**
     * 得到子节点列表
     */
    private List<TcTown> getChildList(List<TcTown> list, TcTown t) {
        List<TcTown> tlist = new ArrayList<TcTown>();
        Iterator<TcTown> it = list.iterator();
        while (it.hasNext()) {
            TcTown n = (TcTown) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TcTown> list, TcTown t) {
        return getChildList(list, t).size() > 0;
    }
}
