package com.bocloud.ims.repository.powervc;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.powervc.PowervcHmc;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Povervc中的hmc连接
 *
 * @author luyanchao
 * @since 2019/1/23 10:03
 */
@Repository
public class PowervcHmcRepository extends JdbcGenericDao<PowervcHmc, Long> {

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

    public List<PowervcHmc> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select a.*, ifnull(b.server_count, 0) as server_count from powervc_hmc a " +
                "left join (select hmc_id, count(id) server_count " +
                "           from powervc_hmc_server group by hmc_id) b on a.id = b.hmc_id " +
                "where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(PowervcHmc.class, sql, paramMap);
    }

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

    public PowervcHmc getByUuid(String uuid, long vendorId) {
        String sql = "select a.* from powervc_hmc a where a.is_deleted = 0 and a.uuid = :uuid and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
        params.put("vendorId", vendorId);
        List<PowervcHmc> hmcs = list(PowervcHmc.class, sql, params);
        return hmcs == null || hmcs.size() == 0 ? null : hmcs.get(0);
    }

    public void removeByVendor(long vendorId, long userId) {
        String sql = "update powervc_hmc set is_deleted = 1, gmt_modify = now(), mender_id = :userId where vendor_id = :vendorId ";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("userId", userId);
        execute(sql, params);
    }

    public void remove(long id, long userId) {
        String sql = "update powervc_hmc set is_deleted = 1, gmt_modify = now(), mender_id = :userId where id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("userId", userId);
        execute(sql, params);
    }

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

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