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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialgroup.vo.IotMaterialGroupListReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialgroup.vo.IotMaterialGroupPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialgroup.vo.IotMaterialGroupSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterialgroup.IotMaterialGroupDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotmaterialgroup.IotMaterialGroupMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_MATERIAL_GROUP_NOT_EXISTS;


/**
 * PMS 功能优化 物料组 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotMaterialGroupServiceImpl implements IotMaterialGroupService {

    @Resource
    private IotMaterialGroupMapper iotMaterialGroupMapper;

    @Override
    public Long createIotMaterialGroup(IotMaterialGroupSaveReqVO createReqVO) {
        // 插入
        IotMaterialGroupDO iotMaterialGroup = BeanUtils.toBean(createReqVO, IotMaterialGroupDO.class);
        iotMaterialGroupMapper.insert(iotMaterialGroup);
        // 返回
        return iotMaterialGroup.getId();
    }

    @Override
    public void updateIotMaterialGroup(IotMaterialGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMaterialGroupExists(updateReqVO.getId());
        // 更新
        IotMaterialGroupDO updateObj = BeanUtils.toBean(updateReqVO, IotMaterialGroupDO.class);
        iotMaterialGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMaterialGroup(Long id) {
        // 校验存在
        validateIotMaterialGroupExists(id);
        // 删除
        iotMaterialGroupMapper.deleteById(id);
    }

    private void validateIotMaterialGroupExists(Long id) {
        if (iotMaterialGroupMapper.selectById(id) == null) {
            throw exception(IOT_MATERIAL_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public IotMaterialGroupDO getIotMaterialGroup(Long id) {
        return iotMaterialGroupMapper.selectById(id);
    }

    @Override
    public PageResult<IotMaterialGroupDO> getIotMaterialGroupPage(IotMaterialGroupPageReqVO pageReqVO) {
        return iotMaterialGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IotMaterialGroupDO> getMaterialGroupList(IotMaterialGroupListReqVO reqVO) {
        List<IotMaterialGroupDO> list = iotMaterialGroupMapper.selectList(reqVO);
        list.sort(Comparator.comparing(IotMaterialGroupDO::getSort));
        return list;
    }

    @Override
    public List<IotMaterialGroupDO> getMaterialGroupList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return iotMaterialGroupMapper.selectBatchIds(ids);
    }

    @Override
    public List<IotMaterialGroupDO> getChildMaterialGroups(Collection<Long> ids) {
        List<IotMaterialGroupDO> children = new LinkedList<>();
        // 遍历每一层
        Collection<Long> parentIds = ids;
        for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下，存在死循环
            // 查询当前层，所有的子物料组分类
            List<IotMaterialGroupDO> groups = iotMaterialGroupMapper.selectListByParentId(parentIds);
            // 1. 如果没有子物料组分类，则结束遍历
            if (CollUtil.isEmpty(groups)) {
                break;
            }
            // 2. 如果有子物料组分类，继续遍历
            children.addAll(groups);
            parentIds = convertSet(groups, IotMaterialGroupDO::getId);
        }
        return children;
    }

}