package com.zcxk.charge.web.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.charge.core.dao.DeviceTypeMapper;
import com.zcxk.charge.api.dto.TreeDataDto;
import com.zcxk.charge.core.entity.DeviceTypeEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.google.common.collect.Lists.newArrayList;

/**
 * 设备型号Service接口
 *
 * @author: WYY
 * @date 2020-11-18 08:21
 */
@Service
public class DeviceTypeService extends ServiceImpl<DeviceTypeMapper, DeviceTypeEntity> {

    @Resource
    private DeviceTypeMapper deviceTypeMapper;


    public List<DeviceTypeEntity> findListByName(DeviceTypeEntity deviceTypeEntity){
        return deviceTypeMapper.findListByName(deviceTypeEntity);
    }

    public List<DeviceTypeEntity> findList(DeviceTypeEntity deviceTypeEntity) {
        return deviceTypeMapper.findList(deviceTypeEntity);
    }

    public DeviceTypeEntity findDeviceTypeById(Long id) {
        return deviceTypeMapper.findDeviceTypeById(id);
    }

    public List<TreeDataDto> getTreeData(String  name) {
        DeviceTypeEntity deviceTypeEntity = new DeviceTypeEntity();
        deviceTypeEntity.setManufacturerName(name);
        List<DeviceTypeEntity> list = deviceTypeMapper.findList(deviceTypeEntity);

        List<TreeDataDto> datalist = new ArrayList<>();

        Map<String, List<DeviceTypeEntity>> manufacturerNameGroupBy = list.stream().collect(Collectors.groupingBy(DeviceTypeEntity::getManufacturerName));
        for (String manufacturerNameKey:manufacturerNameGroupBy.keySet()){
            List<DeviceTypeEntity> manufacturerNameList = manufacturerNameGroupBy.get(manufacturerNameKey);
            TreeDataDto manufacturerNameTreeDataDto = new TreeDataDto();
            manufacturerNameTreeDataDto.setLabel(manufacturerNameKey);
            manufacturerNameTreeDataDto.setValue(manufacturerNameKey);
            datalist.add(manufacturerNameTreeDataDto);

            Map<String, List<DeviceTypeEntity>> equipmentTypeGroupBy = manufacturerNameList.stream().collect(Collectors.groupingBy(DeviceTypeEntity::getDeviceType));
            for (String equipmentTypeKey:equipmentTypeGroupBy.keySet()){
                TreeDataDto equipmentTypeTreeDataDto = new TreeDataDto();
                equipmentTypeTreeDataDto.setLabel(equipmentTypeKey);
                equipmentTypeTreeDataDto.setValue(equipmentTypeKey);
                if (manufacturerNameTreeDataDto.getChildren() != null) {
                    manufacturerNameTreeDataDto.getChildren().add(equipmentTypeTreeDataDto);
                }else {
                    manufacturerNameTreeDataDto.setChildren(newArrayList(equipmentTypeTreeDataDto));
                }

                List<DeviceTypeEntity> equipmentTypeKeyList = equipmentTypeGroupBy.get(equipmentTypeKey);
                for (DeviceTypeEntity deviceTypeDto : equipmentTypeKeyList) {
                    TreeDataDto modelTreeDataDto = new TreeDataDto();
                    modelTreeDataDto.setId(deviceTypeDto.getId());
                    modelTreeDataDto.setLabel(deviceTypeDto.getDeviceMode());
                    modelTreeDataDto.setValue(deviceTypeDto.getDeviceMode());

                    if (equipmentTypeTreeDataDto.getChildren() != null) {
                        equipmentTypeTreeDataDto.getChildren().add(modelTreeDataDto);
                    }else {
                        equipmentTypeTreeDataDto.setChildren(newArrayList(modelTreeDataDto));
                    }
                }
            }
        }
        return datalist;
    }
}
