package com.hwrj.cloud.admin.service.impl;

import com.hwrj.cloud.admin.dao.BigCompanyDao;
import com.hwrj.cloud.admin.dto.PmsProductCategoryDto;
import com.hwrj.cloud.admin.mapper.PmsProductCategoryMapper;
import com.hwrj.cloud.admin.model.PmsProductCategory;
import com.hwrj.cloud.admin.model.PmsProductCategoryExample;
import com.hwrj.cloud.admin.service.BigCompanyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class BigCompanyServiceImpl implements BigCompanyService {
    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    @Autowired
    private BigCompanyDao bigCompanyDao;
    @Override
    public List<Map<String, Object>> getCompanyList(Long typeId, String key) {
        List<Map<String, Object>> maps = bigCompanyDao.bigCompanyInfo(key, typeId);
        return maps;
    }

    @Override
    public List<PmsProductCategoryDto> getCategoryDto() {
        PmsProductCategoryExample example = new PmsProductCategoryExample();
        List<PmsProductCategory> categoryList = pmsProductCategoryMapper.selectByExample(example);
        List<PmsProductCategoryDto> result = categoryList.stream()
                .filter(item -> item.getParentId().equals(0L))
                .map(item -> covert(item, categoryList)).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<String> getCat() {
        List<String> cat = bigCompanyDao.getCat();
        return cat;
    }


    private PmsProductCategoryDto covert(PmsProductCategory item, List<PmsProductCategory> allList) {
        PmsProductCategoryDto node = new PmsProductCategoryDto();
        BeanUtils.copyProperties(item, node);
        List<PmsProductCategoryDto> children = allList.stream()
                .filter(subItem -> subItem.getParentId().equals(item.getId()))
                .map(subItem -> covert(subItem, allList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }
}
