/*
 * 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.common.core.util.StringUtil;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.pojo.CustomCode;
import com.tofly.xrtygis.entity.TfCameraManage;
import com.tofly.xrtygis.entity.TfCameraType;
import com.tofly.xrtygis.entity.vo.TfCameraTypeVo;
import com.tofly.xrtygis.service.TfCameraManageService;
import com.tofly.xrtygis.service.TfCameraTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.xrtygis.mapper.TfCameraTypeMapper;

import java.util.ArrayList;
import java.util.Arrays;
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 {

    @Autowired
    TfCameraManageService tfCameraManageService;

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

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

        List<TfCameraTypeVo> customCodes1;
        if(StringUtil.isNotEmpty(id)){
            customCodes1 = customCodes.stream().filter(m -> m.getId().equals(id))
                    .map((m) -> {

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

        }else{
            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;
    }

    @Override
    public boolean deleteByIds(String ids) {
        List<String> list = Arrays.asList(ids.split(","));

        Integer  count_check =0;
        count_check=tfCameraManageService.count(Wrappers.<TfCameraManage>lambdaQuery()
                .in(TfCameraManage::getTypeId,list));
        if (count_check > 0) {
            throw new RuntimeException("摄像头管理里面有对应的类别数据，请先修改对应的类别，再执行删除操作");
        }
        count_check=this.count(Wrappers.<TfCameraType>lambdaQuery()
        .isNull(TfCameraType::getParentId)
         .in(TfCameraType::getParentId,list));
        if (count_check > 0) {
            throw new RuntimeException("选择的数据存在子类别，请先删除子类别，再删除该类别");
        }
        return this.removeByIds(list);
    }
}
