package com.bocloud.ims.repository.resource.hmc;

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.JdbcGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.hmc.HmcServer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * HMC服务器数据访问对象实现
 *
 * @author sxp
 * @since May 22, 2018
 */
@Deprecated
@Repository("hmcServerRepository")
public class HmcServerRepository extends JdbcGenericDao<HmcServer, Long> {

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

    public List<HmcServer> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        StringBuilder sb = new StringBuilder();
        sb.append("select a.*, b.current_available_system_processor_units processor_units,  ");
        sb.append("       c.current_available_system_memory system_memory  ");
        sb.append("  from hmc_server a ");
        sb.append("  left join hmc_system_processor_config b on b.server_id = a.id  ");
        sb.append("  left join hmc_system_memory_config c on c.server_id = a.id  ");
        sb.append(" where a.is_deleted = 0 ");
        String sql = sb.toString();

        sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<HmcServer> servers = this.list(HmcServer.class, sql, paramMap);

        Long memory;
        double memoryGB;
        for (HmcServer server : servers) {
            memory = server.getSystemMemory();
            if (memory == null) {
                server.setSystemMemoryGB(0D);
            } else {
                memoryGB = Math.round((double) memory / 1024 * 100) / 100D;
                server.setSystemMemoryGB(memoryGB);
            }
        }

        return servers;
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
        StringBuilder sb = new StringBuilder();
        sb.append("select a.id, a.name  ");
        sb.append("  from hmc_server a  ");
        sb.append("  left join hmc_system_processor_config b on b.server_id = a.id  ");
        sb.append("  left join hmc_system_memory_config c on c.server_id = a.id  ");
        sb.append(" where a.is_deleted = 0 ");
        String sql = sb.toString();

        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<HmcServer> entities = this.list(HmcServer.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();

        for (HmcServer entity : entities) {
            beans.add(new SimpleBean(entity.getId(), entity.getName()));
        }
        return beans;
    }

    public int count(List<Param> params) {
        String sql = "select count('x') from hmc_server a where a.is_deleted = 0 ";
        sql = QueryBuilder.build(sql, params, null, "a");

        Map<String, Object> paramMap = QueryBuilder.getParam(params);

        return this.countQuery(sql, paramMap).intValue();
    }

    public List<HmcServer> listByVendorId(Long vendorId) {
        Objects.requireNonNull(vendorId, "vendorId");

        String sql = "select a.* from hmc_server a where a.is_deleted = 0 and a.vendor_id = :vendorId ";

        Map<String, Object> params = new HashMap<>();
        params.put("vendorId", vendorId);

        return this.list(HmcServer.class, sql, params);
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update hmc_server 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 Boolean serverHostIsExisted(String hostIp, Long vendorId) {
        String sql = "select a.* from hmc_server a where a.vendor_id = :vendorId and a.primary_ip_address = :hostIp and a.is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("hostIp", hostIp);
        List<HmcServer> servers = this.list(HmcServer.class, sql, params);
        if (servers.isEmpty()) {
            return false;
        }
        return true;
    }

    public HmcServer getServerById(Long id) {
        String sql = "select a.* from hmc_server a where a.id = :id and a.is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<HmcServer> servers = this.list(HmcServer.class, sql, params);
        if (servers.isEmpty()) {
            return null;
        }
        return servers.get(0);
    }

    public HmcServer getServerByUuid(String uuid) {
        String sql = "select a.* from hmc_server a where a.uuid = :uuid and a.is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
        List<HmcServer> servers = this.list(HmcServer.class, sql, params);
        if (servers.isEmpty()) {
            return null;
        }
        return servers.get(0);
    }

    public List<HmcServer> getServersById(List<Long> ids) {
        Objects.requireNonNull(ids);

        String sql = "select a.* from hmc_server a where a.is_deleted = 0 and a.id in (:ids)";
        Map<String, Object> params = MapTools.simpleMap("ids", ids);
        List<HmcServer> list = this.list(HmcServer.class, sql, params);

        return list;
    }
}
