/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.xrtygis.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.xrtygis.entity.TfCameraType;
import com.tofly.xrtygis.entity.vo.TfCameraTypeVo;
import com.tofly.xrtygis.mapper.TfCameraTypeMapper;
import com.tofly.xrtygis.service.TfCameraTypeService;
import org.springframework.stereotype.Service;

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

/**
 * 摄像头所属类型
 *
 * @author admin
 * @date Thu Jun 01 00:00:00 CST 2023
 */
@Service("tfCameraTypeService")
public class TfCameraTypeServiceImpl extends ServiceImpl<TfCameraTypeMapper, TfCameraType> implements TfCameraTypeService {

    @Override
    public List<TfCameraTypeVo> getTfCameraTypeList() {
       List<TfCameraType> codeList = this.list(Wrappers.<TfCameraType>lambdaQuery()
                .orderByAsc(TfCameraType::getShowNum));

        List<TfCameraTypeVo> customCodes = JSONObject.parseArray(JSON.toJSONString(codeList),TfCameraTypeVo.class);

        List<TfCameraTypeVo> customCodes1 = customCodes.stream().filter(m -> m.getParentId()==null)
                .map((m) -> {

                            m.setChildren(getChildrens(m, customCodes));
                            return m;
                        }
                ).collect(Collectors.toList());
        return customCodes1;


    }

    private List<TfCameraTypeVo> getChildrens(TfCameraTypeVo parent, List<TfCameraTypeVo> menuDatas) {
        List<TfCameraTypeVo> childList = menuDatas.stream().filter(m -> {
            return Objects.equals(m.getParentId(), parent.getId());
        }).map((m) -> {

            m.setChildren(getChildrens(m, menuDatas));
            return m;
        }).collect(Collectors.toList());
        return childList;
    }

}
