package com.njcn.device.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.enums.DeviceResponseEnum;
import com.njcn.device.enums.LineBaseEnum;
import com.njcn.device.mapper.TerminalMaintainMapper;
import com.njcn.device.pojo.param.TerminalMainQueryParam;
import com.njcn.device.pojo.param.TerminalParam;
import com.njcn.device.pojo.po.DevMeal;
import com.njcn.device.pojo.po.DevStrategy;
import com.njcn.device.pojo.po.Device;
import com.njcn.device.pojo.vo.TerminalMaintainVO;
import com.njcn.device.pojo.vo.TerminalVersionVO;
import com.njcn.device.service.IDevMealService;
import com.njcn.device.service.IDevStrategyService;
import com.njcn.device.service.IDeviceService;
import com.njcn.device.service.TerminalMaintainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * pqs
 * 终端运维
 * @author cdf
 * @date 2022/5/11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TerminalMaintainServiceImpl implements TerminalMaintainService {

    private final TerminalMaintainMapper terminalMaintainMapper;

    private final IDeviceService iDeviceService;

    private final IDevMealService iDevMealService;

    private final IDevStrategyService iDevStrategyService;




    @Override
    public List<TerminalMaintainVO> getTerminalMainList(TerminalMainQueryParam terminalMainQueryParam) {
        List<TerminalMaintainVO> devList = terminalMaintainMapper.getTerminalDevInfo();
        if(CollectionUtil.isEmpty(devList)){
            throw new BusinessException(DeviceResponseEnum.DEVICE_EMPTY);
        }
        List<String> subIndexes = devList.stream().map(TerminalMaintainVO::getPid).collect(Collectors.toList());
        List<TerminalMaintainVO> subList = terminalMaintainMapper.getPqLineGdAndSubList(subIndexes);
        if(CollectionUtil.isEmpty(subList)){
            throw new BusinessException(DeviceResponseEnum.SUB_NO);
        }
        List<String> gdIds = subList.stream().map(TerminalMaintainVO::getPid).collect(Collectors.toList());
        List<TerminalMaintainVO> gdList = terminalMaintainMapper.getPqLineGdAndSubList(gdIds);
        if(CollectionUtil.isEmpty(gdList)){
            throw new BusinessException(DeviceResponseEnum.GD_NO);
        }
        List<String> proIds = gdList.stream().map(TerminalMaintainVO::getPid).collect(Collectors.toList());
        List<TerminalMaintainVO> proList = terminalMaintainMapper.getPqLineProvList(proIds);
        if(CollectionUtil.isEmpty(proList)){
            throw new BusinessException(DeviceResponseEnum.PROVINCE_EMPTY);
        }

        List<TerminalMaintainVO> subListTem = subList.stream().peek(item->item.setChildren(getChildren(item.getId(),devList))).collect(Collectors.toList());
        List<TerminalMaintainVO>  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<TerminalMaintainVO> getManageList(List<String> ids, Integer type) {
        if(type == 0){
            //终端状态管理
            List<TerminalMaintainVO> devList = terminalMaintainMapper.getRunFlagList(ids);
            return assemble(devList);
        }else if(type == 1){
            //流量套餐
            List<TerminalMaintainVO> devList = terminalMaintainMapper.getFlowMealList(ids);
            return assemble(devList);
        }else if(type == 2){
            //流量策略
            List<TerminalMaintainVO> devList = terminalMaintainMapper.getFlowStrategyList(ids);
            return assemble(devList);
        }
        throw new BusinessException(CommonResponseEnum.FAIL);
    }

    @Override
    public boolean updateRunFlagManage(TerminalParam terminalParam){
            //终端状态管理
            List<Device> tem = new ArrayList<>();
        terminalParam.getIds().forEach(item-> {
                Device device = new Device();
                device.setId(item);
                device.setRunFlag(terminalParam.getRunFlag());
                tem.add(device);
            });
        return iDeviceService.updateBatchById(tem);
    }

    @Override
    public boolean updateFlowMeal(TerminalParam terminalParam) {
        List<DevMeal> tem = new ArrayList<>();
        iDevMealService.removeByIds(terminalParam.getIds());
        terminalParam.getIds().forEach(item-> {
            DevMeal devMeal = new DevMeal();
            devMeal.setLineId(item);
            devMeal.setBaseMealId(terminalParam.getBaseMealId());
            devMeal.setReamMealId(terminalParam.getExtendMealId());
            tem.add(devMeal);
        });
       return iDevMealService.saveBatch(tem);
    }

    @Override
    public boolean updateFlowStrategy(TerminalParam terminalParam) {
        List<DevStrategy> tem = new ArrayList<>();
        iDevStrategyService.removeByIds(terminalParam.getIds());
        terminalParam.getIds().forEach(item-> {
            DevStrategy devStrategy = new DevStrategy();
            devStrategy.setLineId(item);
            devStrategy.setStrategyId(terminalParam.getFlowStrategyId());
            tem.add(devStrategy);
        });
        return iDevStrategyService.saveBatch(tem);
    }

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

    private List<TerminalMaintainVO> assemble(List<TerminalMaintainVO> devList){
        List<String> subIds =  devList.stream().map(TerminalMaintainVO::getPid).collect(Collectors.toList());
        List<TerminalMaintainVO> subList = terminalMaintainMapper.getPqLineGdAndSubList(subIds);
        List<String> gdIds =  subList.stream().map(TerminalMaintainVO::getPid).collect(Collectors.toList());
        List<TerminalMaintainVO> gdList = terminalMaintainMapper.getPqLineGdAndSubList(gdIds);
        List<String> proIds =  gdList.stream().map(TerminalMaintainVO::getPid).collect(Collectors.toList());
        List<TerminalMaintainVO> provList = terminalMaintainMapper.getPqLineProvList(proIds);

        List<TerminalMaintainVO> subTem = subList.stream().peek(item->item.setChildren(getChildren(item.getId(),devList))).collect(Collectors.toList());
        List<TerminalMaintainVO> gdTem = gdList.stream().peek(item->item.setChildren(getChildren(item.getId(),subTem))).collect(Collectors.toList());
        return provList.stream().peek(item->item.setChildren(getChildren(item.getId(),gdTem))).collect(Collectors.toList());
    }





}
