package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.SangforCommunication;
import com.bocloud.ims.entity.resource.Storage;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

/**
 * 存储服务器
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("storageRepository")
public class StorageRepository extends BasicGenericDao<Storage, Long>  {

    public StorageRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    
    public List<Storage> list(int page, int rows, List<Param> params, Map<String, String> sorter)
            {
        String sql = "select a.* from storage a where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(Storage.class, sql, paramMap);
    }

    
    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
        String sql = "select a.id,a.name from storage a where a.is_deleted = 0";
        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<Storage> list = this.list(Storage.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (Storage storage : list) {
            beans.add(new SimpleBean(storage.getId(), storage.getName()));
        }
        return beans;
    }

    
    public int count(List<Param> params) {
        String sql = "select count(1) from storage a where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public int syCount(List<Param> params, Long vendorId,String isSystemDisk,String type,String hostGuid){
        String sql = "select count(1) from storage a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        if (!StringUtils.isEmpty(isSystemDisk)){
            sql += " and a.is_sangfor_system_disk = '"+isSystemDisk+"' ";
        }
        if (!StringUtils.isEmpty(hostGuid)){
            sql += " and a.host = '"+hostGuid+"' ";
        }
        if (!StringUtils.isEmpty(type)){
            if (type.equals("VS")) {
                sql += " and a.type = '" + type + "' ";
            }else {
                sql += " and a.type <> '" + type + "' ";
            }
        }
        sql = QueryBuilder.build(sql,params,null,"a");
        Map<String,Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql,param).intValue();
    }

    public List<Storage> syList(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId
            ,String isSystemDisk,String type,String hostGuid) throws BoCloudDBException {
        String sql = "select * from storage a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        if (!StringUtils.isEmpty(isSystemDisk)){
            sql += " and a.is_sangfor_system_disk = '"+isSystemDisk+"' ";
        }
        if (!StringUtils.isEmpty(hostGuid)){
            sql += " and a.host = '"+hostGuid+"' ";
        }
        if (!StringUtils.isEmpty(type)){
            if (type.equals("VS")) {
                sql += " and a.type = '" + type + "' ";
            }else {
                sql += " and a.type <> '" + type + "' ";
            }
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(Storage.class, sql);
    }


    public int stCount(List<Param> params, Long vendorId,String isSystemDisk,String type,String isVs){
        String sql = "select count(1) from storage a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        if (!StringUtils.isEmpty(isSystemDisk)){
            sql += " and a.is_sangfor_system_disk = '"+isSystemDisk+"' ";
        }
        if (isVs.equals("true")) {
            if (!StringUtils.isEmpty(type)) {
                sql += " and a.type = '" + type + "' ";
            }
        }else {
            if (!StringUtils.isEmpty(type)) {
                sql += " and a.type <> '" + type + "' ";
            }
        }
        sql = QueryBuilder.build(sql,params,null,"a");
        Map<String,Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql,param).intValue();
    }

    public List<Storage> stList(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId,String isSystemDisk,String type,String isVs) throws BoCloudDBException {
        String sql = "select * from storage a  where a.is_deleted=0";
        if (vendorId !=null){
            sql += " and a.vendor_id = '" + vendorId + "'  ";
        }
        if (!StringUtils.isEmpty(isSystemDisk)){
            sql += " and a.is_sangfor_system_disk = '"+isSystemDisk+"' ";
        }
        if (isVs.equals("true")) {
            if (!StringUtils.isEmpty(type)) {
                sql += " and a.type = '" + type + "' ";
            }
        }else {
            if (!StringUtils.isEmpty(type)) {
                sql += " and a.type <> '" + type + "' ";
            }
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(Storage.class, sql);
    }
    
    public Storage getByUuid(String uuid) {
        String sql = "select a.* from storage a where a.is_deleted = 0 and a.uuid = :uuid";
        Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
        List<Storage> storages = list(Storage.class, sql, params);
        return storages == null || storages.size() == 0 ? null : storages.get(0);
    }

    
    public void remove(long id) {
        String sql = "update storage a set is_deleted = 1 where a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        execute(sql, params);
    }

    
    public void removeByVendor(long vendorId) {
        String sql = "update storage a set is_deleted = 1 where a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        execute(sql, params);
    }

    
    public List<Storage> listByVendor(long vendorId) {
        String sql = "select a.* from storage a where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return list(Storage.class, sql, params);
    }

    public List<Storage> listByVendor(long vendorId,String isSystemDisk) {
        String sql = "select a.* from storage a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.is_sangfor_system_disk= :isSystemDisk";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("isSystemDisk",isSystemDisk);
        return list(Storage.class, sql, params);
    }
    public Boolean remove(Long id, Long userId) throws Exception {
        String sql = "update storage set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public Storage get(long id) {
        String sql = "select a.* from storage a where a.id = :id and a.is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<Storage> storages = list(Storage.class, sql, params);
        return storages == null || storages.size() == 0 ? null : storages.get(0);
    }

    
    public Storage getByHostName(long vendorId, String host) {
        String sql = "select a.* from storage a " +
                "where a.is_deleted = 0 and a.vendor_id = :vendorId and a.host = :host";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("host", host);
        List<Storage> storages = list(Storage.class, sql, params);
        return storages == null || storages.size() == 0 ? null : storages.get(0);
    }

    
    public Storage getByName(long vendorId, String name) {
        String sql = "select a.* from storage a " +
                "where a.is_deleted = 0 and a.vendor_id = :vendorId and a.name = :name";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("name", name);
        List<Storage> storages = list(Storage.class, sql, params);
        return storages == null || storages.size() == 0 ? null : storages.get(0);
    }

    
    public Storage getIp(Long vendorId, String ip) {
        String sql = "select a.* from storage a " +
                "where a.is_deleted = 0 and a.vendor_id = :vendorId and a.ip = :ip";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("ip", ip);
        List<Storage> storages = list(Storage.class, sql, params);
        return storages == null || storages.size() == 0 ? null : storages.get(0);
    }

    
    public List<Storage> listByCloudServer(long serverId) {
        String sql = "select * from storage where id in (" +
                "   select storage_id from volume where id in (" +
                "       select volume_id from server_volume where server_id = :serverId" +
                "   )" +
                ")";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        return list(Storage.class, sql, params);
    }
}
