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

import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.Network;
import com.bocloud.ims.entity.resource.Port;
import com.bocloud.ims.entity.resource.PortGroup;
import com.bocloud.ims.entity.resource.Server;
import com.bocloud.ims.interfaces.resource.sangfor.SangforNetWorkService;
import com.bocloud.ims.repository.resource.NetworkRepository;
import com.bocloud.ims.repository.resource.PortGroupRepository;
import com.bocloud.ims.repository.resource.PortRepository;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service("sangforNetWorkService")
public class SangforNetWorkServiceImpl implements SangforNetWorkService {
    @Autowired
    private NetworkRepository networkRepository;
    @Autowired
    private PortGroupRepository portGroupRepository;
    @Autowired
    private PortRepository portRepository;

    @Override
    public BocloudResult<GridBean<Network>> list(Pager pager, RequestContext context) {
        List<Network> list = Lists.newArrayList();
        int total = 0;
        GridBean gridBean = null;
        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;
            String netWorkType= 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");
                }
                if (paramMap.containsKey("netWorkType")){
                    netWorkType = String.valueOf(paramMap.get("netWorkType"));
                    paramMap.remove("netWorkType");
                }
            }
            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            sorter.put("gmtCreate", Common.ONE);
            total = networkRepository.ntCount(params,vendorId,netWorkType);
            list = networkRepository.ntList(page, rows, params, sorter,vendorId,netWorkType);
            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        }catch (Exception e){
            return new BocloudResult<>(false, "查询成功");
        }
    }

    @Override
    public BocloudResult<GridBean<PortGroup>> dvVlanGroupList(Pager pager, RequestContext context) {
        List<PortGroup> list = Lists.newArrayList();
        int total = 0;
        GridBean gridBean = null;
        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 = portGroupRepository.portCount(params,vendorId);
            list = portGroupRepository.portList(page, rows, params, sorter,vendorId);
            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        }catch (Exception e){
            return new BocloudResult<>(false, "查询成功");
        }
    }

    @Override
    public BocloudResult<GridBean<Port>> dvPhyIfList(Pager pager, RequestContext context) {
        List<Port> list = Lists.newArrayList();
        int total = 0;
        GridBean gridBean = null;
        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 = portRepository.portCount(params,vendorId);
            list = portRepository.portList(page, rows, params, sorter,vendorId);
            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        }catch (Exception e){
            return new BocloudResult<>(false, "查询成功");
        }
    }
}
