package com.fzm.mall.server.front.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.goods.model.Category;
import com.fzm.mall.server.front.goods.mapper.CategoryMapper;
import com.fzm.mall.server.front.goods.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * <p>
 * 商品类别 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-03-02
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> getList() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("level", 1);
        return list(queryWrapper);
    }

    @Override
    public Category getByName(String searchKey) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", searchKey);
        queryWrapper.last("limit 1");
        queryWrapper.eq("status", 1);
        return getOne(queryWrapper);
    }

    @Override
    public List<Category> getList(String categoryId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("clazz_id", categoryId);
        return list(queryWrapper);
    }

    /**
     * 递归查询所有子目录
     *
     * @param
     * @return
     */
    @Override
    public List<String> getCateGoryIdsList(List<String> categoryIds, List<String> newCategoryIds) {
        //type为2代表没有子集
        for (String categoryId : categoryIds) {
            QueryWrapper<Category> q = new QueryWrapper<>();
            q.eq("status", 1);
            q.eq("clazz_id", categoryId);
            List<Category> list = list(q);
            if (CollectionUtils.isEmpty(list)) {
                continue;
            } else {
                List<String> categoryNodes = list.stream().distinct().map(Category::getCategoryId).collect(Collectors.toList());
                newCategoryIds.addAll(categoryNodes);
                getCateGoryIdsList(categoryNodes, newCategoryIds);
                Iterator<String> iterator = categoryIds.iterator();
                while (iterator.hasNext()){
                    String assistEcord = iterator.next();
                    if (categoryNodes.contains(assistEcord)) {
                        iterator.remove();
                    }
                }
            }
        }
        return newCategoryIds;
    }

    @Override
    public Category getByCategoryId(String categoryId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("category_id", categoryId);
        return getOne(queryWrapper);
    }

    @Override
    public List<Category> getCategorys(List<String> categorys) {
        List<Category> list = new ArrayList<>();
        for (String categoryId : categorys) {
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 1);
            queryWrapper.eq("category_id", categoryId);
            Category category = getOne(queryWrapper);
            if (category != null) {
                list.add(category);
            }
        }
        return list;
    }

}
