package org.dromara.erp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.erp.domain.ErpModel;
import org.dromara.erp.domain.query.ErpModelQuery;
import org.dromara.erp.domain.vo.ErpModelVo;
import org.dromara.erp.mapper.ErpModelMapper;
import org.dromara.erp.service.IErpModelService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * ERP 型材档案 Service 业务层处理
 *
 * @Author: Antigravity
 */
@RequiredArgsConstructor
@Service
public class ErpModelServiceImpl implements IErpModelService {

    private final ErpModelMapper baseMapper;

    @Override
    public List<ErpModelVo> queryList(ErpModelQuery query) {
        if (query != null && ObjectUtil.isNotEmpty(query.getParentRowId())) {
            LambdaQueryWrapper<ErpModel> lqw = buildQueryWrapper(query);
            return baseMapper.selectVoList(lqw);
        }
        LambdaQueryWrapper<ErpModel> lqw = buildFirstLevelWrapper(query);
        List<ErpModelVo> all = baseMapper.selectVoList(lqw);
        Set<String> rowIds = all.stream()
            .map(ErpModelVo::getRowId)
            .filter(ObjectUtil::isNotEmpty)
            .collect(Collectors.toSet());
        return all.stream()
            .filter(item -> {
                String pid = item.getParentRowId();
                return ObjectUtil.isEmpty(pid) || !rowIds.contains(pid);
            })
            .collect(Collectors.toList());
    }

    private LambdaQueryWrapper<ErpModel> buildFirstLevelWrapper(ErpModelQuery query) {
        LambdaQueryWrapper<ErpModel> lqw = Wrappers.lambdaQuery();
        if (query != null) {
            lqw.like(ObjectUtil.isNotEmpty(query.getNum()), ErpModel::getNum, query.getNum());
            lqw.like(ObjectUtil.isNotEmpty(query.getName()), ErpModel::getName, query.getName());
            lqw.eq(ObjectUtil.isNotEmpty(query.getModelSort()), ErpModel::getModelSort, query.getModelSort());
            lqw.eq(ObjectUtil.isNotEmpty(query.getModelKind()), ErpModel::getModelKind, query.getModelKind());
        }
        lqw.eq(ErpModel::getStopUse, 0);
        lqw.orderByAsc(ErpModel::getNum);
        return lqw;
    }

    private LambdaQueryWrapper<ErpModel> buildQueryWrapper(ErpModelQuery query) {
        LambdaQueryWrapper<ErpModel> lqw = Wrappers.lambdaQuery();
        lqw.like(ObjectUtil.isNotEmpty(query.getNum()), ErpModel::getNum, query.getNum());
        lqw.like(ObjectUtil.isNotEmpty(query.getName()), ErpModel::getName, query.getName());
        lqw.eq(ObjectUtil.isNotEmpty(query.getModelSort()), ErpModel::getModelSort, query.getModelSort());
        lqw.eq(ObjectUtil.isNotEmpty(query.getModelKind()), ErpModel::getModelKind, query.getModelKind());
        lqw.eq(ErpModel::getParentRowId, query.getParentRowId());
        lqw.eq(ErpModel::getStopUse, 0);
        lqw.orderByAsc(ErpModel::getNum);
        return lqw;
    }

}
