package com.njcn.device.device.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.enums.DeviceResponseEnum;
import com.njcn.device.device.mapper.DevVersionMapper;
import com.njcn.device.device.mapper.TerminalVersionMapper;
import com.njcn.device.device.service.TerminalVersionService;
import com.njcn.device.pq.pojo.param.TerminalMainQueryParam;
import com.njcn.device.pq.pojo.po.DevVersion;
import com.njcn.device.pq.pojo.vo.TerminalVersionVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * pqs
 *
 * @author cdf
 * @date 2022/5/12
 */
@Slf4j
@Service
@AllArgsConstructor
public class TerminalVersionServiceImpl implements TerminalVersionService {

    private final TerminalVersionMapper terminalVersionMapper;

    private final DevVersionMapper devVersionMapper;



    @Override
    public List<TerminalVersionVO> getTerminalVersionList(TerminalMainQueryParam terminalMainQueryParam) {

        List<TerminalVersionVO> devList = terminalVersionMapper.getTerminalVersionInfo(terminalMainQueryParam);
        if(CollectionUtil.isEmpty(devList)){
            return devList;
        }
        List<String> subIndexes = devList.stream().map(TerminalVersionVO::getPid).collect(Collectors.toList());
        List<TerminalVersionVO> subList =terminalVersionMapper.getPqLineGdAndSubList(subIndexes);
        if(CollectionUtil.isEmpty(subList)){
            throw new BusinessException(DeviceResponseEnum.SUB_NO);
        }
        List<String> gdIndexes = subList.stream().map(TerminalVersionVO::getPid).collect(Collectors.toList());
        List<TerminalVersionVO> gdList  =terminalVersionMapper.getPqLineGdAndSubList(gdIndexes);
        if(CollectionUtil.isEmpty(gdList)){
            throw new BusinessException(DeviceResponseEnum.GD_NO);
        }
        List<String> provinceIndexes = gdList.stream().map(TerminalVersionVO::getPid).collect(Collectors.toList());
        List<TerminalVersionVO> proList =terminalVersionMapper.getPqLineProvList(provinceIndexes);
        if(CollectionUtil.isEmpty(proList)){
            throw new BusinessException(DeviceResponseEnum.PROVINCE_EMPTY);
        }

        List<TerminalVersionVO> subListTem = subList.stream().peek(item->item.setChildren(getChildren(item.getId(),devList))).collect(Collectors.toList());
        List<TerminalVersionVO>  gdListTem = gdList.stream().peek(item->item.setChildren(getChildren(item.getId(),subListTem))).collect(Collectors.toList());
        return proList.stream().peek(item->item.setChildren(getChildren(item.getId(),gdListTem))).collect(Collectors.toList());
    }

    @Override
    public List<DevVersion> getTerminalUpLog(String id) {
        return terminalVersionMapper.selectListById(id);
    }

    @Override
    public List<TerminalVersionVO.Version> getVersionList(List<String> ids) {
        return terminalVersionMapper.selectDevVersion(ids);
    }


    private List<TerminalVersionVO> getChildren(String id,List<TerminalVersionVO> all){
        return all.stream().filter(item->item.getPid().equals(id)).collect(Collectors.toList());
    }


}
