package com.ruoyi.tuya.service.impl;

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

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tuya.domain.TreeSelectS;
import com.ruoyi.tuya.domain.TuyaArticleInfo;
import com.ruoyi.tuya.domain.TuyaSitesInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tuya.mapper.TuyaCategoryInfoMapper;
import com.ruoyi.tuya.domain.TuyaCategoryInfo;
import com.ruoyi.tuya.service.ITuyaCategoryInfoService;

/**
 * 栏目信息Service业务层处理
 *
 * @author zf
 * @date 2022-07-07
 */
@Service
public class TuyaCategoryInfoServiceImpl implements ITuyaCategoryInfoService
{
    @Autowired
    private TuyaCategoryInfoMapper tuyaCategoryInfoMapper;


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

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

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

    @Override
    public String checkCategoryNameUnique(TuyaCategoryInfo tuyaCategoryInfo) {
        Long categoryId = StringUtils.isNull(tuyaCategoryInfo.getCategoryId()) ? -1L : tuyaCategoryInfo.getCategoryId();
        TuyaCategoryInfo info = tuyaCategoryInfoMapper.checkCategoryNameUnique(tuyaCategoryInfo);
        if (StringUtils.isNotNull(info) && info.getCategoryId() != categoryId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 构建前端所需要树结构
     *
     * @param categorys 目录列表
     * @return 树结构列表
     */
    @Override
    public List<TuyaCategoryInfo> buildCategoryTree(List<TuyaCategoryInfo> categorys)
    {
        List<TuyaCategoryInfo> returnList = new ArrayList<TuyaCategoryInfo>();
        List<Long> tempList = new ArrayList<Long>();
        for (TuyaCategoryInfo category : categorys)
        {
            tempList.add(category.getCategoryId());
        }
        for (TuyaCategoryInfo category : categorys)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categorys, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categorys;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param categorys 栏目列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelectS> buildCategoryTreeSelect(List<TuyaCategoryInfo> categorys)
    {
        List<TuyaCategoryInfo> categoryTrees = buildCategoryTree(categorys);
        return categoryTrees.stream().map(TreeSelectS::new).collect(Collectors.toList());
    }

    /**
     * 查询栏目信息
     *
     * @param categoryId 栏目信息主键
     * @return 栏目信息
     */
    @Override
    public TuyaCategoryInfo selectTuyaCategoryInfoByCategoryId(Long categoryId)
    {
        return tuyaCategoryInfoMapper.selectTuyaCategoryInfoByCategoryId(categoryId);
    }

    /**
     * 查询栏目信息列表
     *
     * @param tuyaCategoryInfo 栏目信息
     * @return 栏目信息
     */
    @Override
    public List<TuyaCategoryInfo> selectTuyaCategoryInfoList(TuyaCategoryInfo tuyaCategoryInfo)
    {
        return tuyaCategoryInfoMapper.selectTuyaCategoryInfoList(tuyaCategoryInfo);
    }


    /**
     * 新增栏目信息
     *
     * @param tuyaCategoryInfo 栏目信息
     * @return 结果
     */
    @Override
    public int insertTuyaCategoryInfo(TuyaCategoryInfo tuyaCategoryInfo)
    {
        tuyaCategoryInfo.setCreateTime(DateUtils.getNowDate());
        return tuyaCategoryInfoMapper.insertTuyaCategoryInfo(tuyaCategoryInfo);
    }

    /**
     * 修改栏目信息
     *
     * @param tuyaCategoryInfo 栏目信息
     * @return 结果
     */
    @Override
    public int updateTuyaCategoryInfo(TuyaCategoryInfo tuyaCategoryInfo)
    {
        tuyaCategoryInfo.setUpdateTime(DateUtils.getNowDate());
        return tuyaCategoryInfoMapper.updateTuyaCategoryInfo(tuyaCategoryInfo);
    }

    /**
     * 批量删除栏目信息
     *
     * @param categoryIds 需要删除的栏目信息主键
     * @return 结果
     */
    @Override
    public int deleteTuyaCategoryInfoByCategoryIds(Long[] categoryIds)
    {
        return tuyaCategoryInfoMapper.deleteTuyaCategoryInfoByCategoryIds(categoryIds);
    }

    /**
     * 删除栏目信息信息
     *
     * @param categoryId 栏目信息主键
     * @return 结果
     */
    @Override
    public int deleteTuyaCategoryInfoByCategoryId(Long categoryId)
    {
        return tuyaCategoryInfoMapper.deleteTuyaCategoryInfoByCategoryId(categoryId);
    }

    /**
     * 是否存在子节点
     *
     * @param categoryId ID
     * @return 结果
     */
    @Override
    public boolean hasChildByCategoryId(Long categoryId)
    {
        int result = tuyaCategoryInfoMapper.hasChildByCategoryId(categoryId);
        return result > 0;
    }
}
