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.Storage;
import com.bocloud.ims.interfaces.resource.sangfor.SangforStorageService;
import com.bocloud.ims.repository.resource.DataStoreRepository;
import com.bocloud.ims.repository.resource.StorageRepository;
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("sangforStorageService")
public class SangfotStorageServiceImpl implements SangforStorageService {
    @Autowired
    private StorageRepository storageRepository;
    @Autowired
    private DataStoreRepository dataStoreRepository;

    @Override
    public BocloudResult<GridBean<Storage>> list(Pager pager, RequestContext context) {
        List<Storage> 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 isSystemDisk = null;
            String type = null;
            String isVs = 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("isSystemDisk")) {
                    isSystemDisk = String.valueOf(paramMap.get("isSystemDisk"));
                    paramMap.remove("isSystemDisk");
                }
                if (paramMap.containsKey("type")) {
                    type = String.valueOf(paramMap.get("type"));
                    paramMap.remove("type");
                }
                if (paramMap.containsKey("isVs")) {
                    isVs = String.valueOf(paramMap.get("isVs"));
                    paramMap.remove("isVs");
                }
            }
            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            sorter.put("gmtCreate", Common.ONE);
            total = storageRepository.stCount(params, vendorId, isSystemDisk, type, isVs);
            list = storageRepository.stList(page, rows, params, sorter, vendorId, isSystemDisk, type, isVs);
            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            return new BocloudResult<>(false, "查询成功");
        }
    }

    /*@Override
    public BocloudResult<GridBean<DataStore>> listStorage(Pager pager, RequestContext context) {
        List<DataStore> list;
        int total;
        GridBean gridBean;

        try {
            List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
            Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
            sorter.put("id", Common.ZERO);

            Long vendorId = null;
            String type = 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("isSystemDisk")) {
                    //isSystemDisk = String.valueOf(paramMap.get("isSystemDisk"));
                    paramMap.remove("isSystemDisk");
                }
                if (paramMap.containsKey("type")) {
                    type = String.valueOf(paramMap.get("type"));
                    paramMap.remove("type");
                }
            }

            Integer page = pager.getPage();
            Integer rows = pager.getRows();
            total = dataStoreRepository.stCount(params, vendorId, type);
            list = dataStoreRepository.stList(page, rows, params, sorter, vendorId, type);

            Integer vmNum;
            for (DataStore dataStore : list) {
                vmNum = dataStore.getVmNum();
                if (vmNum == null) {
                    vmNum = dataStoreRepository.getVmNum(vendorId, dataStore.getId());
                }
                if (vmNum != null) {
                    dataStore.setVmNum(vmNum);
                }
            }

            gridBean = GridHelper.getBean(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            return new BocloudResult<>(false, "查询成功");
        }
    }*/
}
