package cn.iocoder.yudao.module.pms.service.yfclass;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotProductClassifyListReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yfclass.vo.IotYfCasCaderVo;
import cn.iocoder.yudao.module.pms.controller.admin.yfclass.vo.IotYfClassifyPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yfclass.vo.IotYfClassifySaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yfclass.IotYfClassifyDO;
import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.yfclass.IotYfClassifyMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 油服设备分类 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotYfClassifyServiceImpl implements IotYfClassifyService {

    @Resource
    private IotYfClassifyMapper iotYfClassifyMapper;
    @Resource
    private IotDeviceMapper iotDeviceMapper;

    @Override
    public List<IotYfClassifyDO> getYfClassifyList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return iotYfClassifyMapper.selectBatchIds(ids);
    }

    @Override
    public List<IotYfCasCaderVo> getCasCaderList() {
        List<IotYfClassifyDO> nodes = iotYfClassifyMapper.selectList();
        // 创建节点ID到CascaderNode的映射，方便查找
        Map<Long, IotYfCasCaderVo> nodeMap = new HashMap<>();

        // 先创建所有节点
        for (IotYfClassifyDO node : nodes) {
            IotYfCasCaderVo cascaderNode = new IotYfCasCaderVo(node.getCode(), node.getName());
            nodeMap.put(node.getId(), cascaderNode);
        }

        // 构建树形结构
        List<IotYfCasCaderVo> result = new ArrayList<>();
        for (IotYfClassifyDO node : nodes) {
            IotYfCasCaderVo currentNode = nodeMap.get(node.getId());
            Long parentId = node.getParentId();

            if (parentId == null || parentId == 0) {
                // 没有父节点，是根节点
                result.add(currentNode);
            } else {
                // 有父节点，添加到父节点的children中
                IotYfCasCaderVo parentNode = nodeMap.get(parentId);
                if (parentNode != null) {
                    parentNode.addChild(currentNode);
                } else {
                    // 如果父节点不存在，作为根节点处理
                    result.add(currentNode);
                }
            }
        }
        return result;
    }

    @Override
    public void initClassify() {
        List<IotYfClassifyDO> list = iotYfClassifyMapper.selectList();

        List<IotYfClassifyDO> collect = list.stream().filter(e -> e.getParentId() == 0).collect(Collectors.toList());
        collect.stream().forEach(e -> {
            List<IotYfClassifyDO> iotYfClassifyDOS = iotYfClassifyMapper.selectList("parent_id", e.getId());
            iotYfClassifyDOS.forEach(f ->{
                f.setCompany(e.getCode());
                iotYfClassifyMapper.updateById(f);
                List<IotYfClassifyDO> iotYfClassifyDOS1 = iotYfClassifyMapper.selectList("parent_id", f.getId());
                iotYfClassifyDOS1.forEach(g -> {
                    g.setCompany(e.getCode());
                    iotYfClassifyMapper.updateById(g);
                });
            });
        });
    }

    @Override
    public List<IotYfClassifyDO> getYfClassifyList(IotProductClassifyListReqVO reqVO) {
        List<IotYfClassifyDO> list = iotYfClassifyMapper.selectList(reqVO);
        list.sort(Comparator.comparing(IotYfClassifyDO::getSort));
        return list;
    }

    @Override
    public Long createIotYfClassify(IotYfClassifySaveReqVO createReqVO) {
        // 插入
        IotYfClassifyDO iotYfClassify = BeanUtils.toBean(createReqVO, IotYfClassifyDO.class);
        iotYfClassifyMapper.insert(iotYfClassify);
        // 返回
        return iotYfClassify.getId();
    }

    @Override
    public void updateIotYfClassify(IotYfClassifySaveReqVO updateReqVO) {
        // 校验存在
        validateIotYfClassifyExists(updateReqVO.getId());
        // 更新
        IotYfClassifyDO updateObj = BeanUtils.toBean(updateReqVO, IotYfClassifyDO.class);
        iotYfClassifyMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotYfClassify(Long id) {
        // 校验存在
        validateIotYfClassifyExists(id);
        // 删除
        iotYfClassifyMapper.deleteById(id);
    }

    private void validateIotYfClassifyExists(Long id) {
        if (iotYfClassifyMapper.selectById(id) == null) {
            throw exception(new ErrorCode(1000, "编码分类不存在"));
        }
    }

    @Override
    public IotYfClassifyDO getIotYfClassify(Long id) {
        return iotYfClassifyMapper.selectById(id);
    }

    @Override
    public PageResult<IotYfClassifyDO> getIotYfClassifyPage(IotYfClassifyPageReqVO pageReqVO) {
        return iotYfClassifyMapper.selectPage(pageReqVO);
    }

}
