package com.bocloud.ims.service.resource.sangfor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.Commt;
import com.bocloud.ims.entity.resource.SangforCommunication;
import com.bocloud.ims.entity.resource.Server;
import com.bocloud.ims.interfaces.resource.sangfor.SangforCommtService;
import com.bocloud.ims.repository.resource.SangforCommunicationRepository;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("sangforCommtService")
public class SangforCommtServiceImpl implements SangforCommtService {
    @Autowired
    private SangforCommunicationRepository cntRepository;

    @Override
    public BocloudResult list(Pager pager) {
        List<SangforCommunication> list = Lists.newArrayList();
        int total = 0;
        try {
            List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
            Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
            Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);
            sorter.put("gmtCreate", Common.ONE);
            Long vendorId = null;
            for (Param param: params) {
                Map<String, Object> paramMap = param.getParam();
                if (paramMap.containsKey("vendorId")){
                    vendorId = Long.valueOf(String.valueOf(paramMap.get("vendorId")));
                    paramMap.remove("vendorId");
                }
            }
            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            sorter.put("gmtCreate", Common.ONE);
            total = cntRepository.cntCount(params,vendorId);
            list = cntRepository.cntList(page, rows, params, sorter,vendorId);
            List<SangforCommunication> list1 = Lists.newArrayList();
            for (SangforCommunication communication : list){
                if (communication.getIfaces().equalsIgnoreCase("business")){
                    SangforCommunication communication1 = new SangforCommunication();
                    BeanUtils.copyProperties(communication, communication1);
                    communication1.setBdrSwitch(communication.getBdr_switch());
                    String subdatas = communication.getSubdatas();
                    List<SangforCommunication> sangforCommunications = JSON.parseArray(subdatas, SangforCommunication.class);
                    List<SangforCommunication> newCommtList = new ArrayList<>();
                    for (SangforCommunication cms :sangforCommunications){
                        SangforCommunication cm = new SangforCommunication();
                        BeanUtils.copyProperties(cms,cm);
                        cm.setMembers(JSON.parseArray(cms.getNodeMembers()));
                        List<Commt> commts = JSON.parseArray(cms.getPrimarys(), Commt.class);
                        cm.setPrimary(commts);
                        newCommtList.add(cm);
                    }
                    communication1.setSubdata(newCommtList);
                    list1.add(communication1);
                    System.out.println(communication1);
                }else {
                    SangforCommunication communication1 = new SangforCommunication();
                    BeanUtils.copyProperties(communication, communication1);
                    List<Commt> commons = JSON.parseArray(communication.getPrimarys(), Commt.class);
                    communication1.setPrimary(commons);
                    communication1.setMembers(JSON.parseArray(communication.getNodeMembers()));
                    list1.add(communication1);
                }
            }
//            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult(true, list1, "查询成功");
        }catch (Exception e){
            return new BocloudResult<>(false, "查询成功");
        }
    }
}
