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.Common;
import com.bocloud.common.utils.IDFactory;
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.Cluster;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

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日
 */
@Component("clusterRepository")
public class ClusterRepository extends BasicGenericDao<Cluster, Long> {

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

    @Override
    public void save(Cluster cluster) throws BoCloudDBException {
        cluster.setGuid(IDFactory.instance().uuid());
        super.save(cluster);
    }

    /**
     * @since 2018-04-12 modify by sxp 增加云平台名称的查询
     */

    public List<Cluster> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select a.*,b.name vdc_name,c.name vendor_name , r.name region_name from cluster a "
                + " left join vdc b on a.vdc_id = b.id " + " left join cloud_vendor c on a.vendor_id = c.id "
                + " left join region r on a.region_id = r.id and a.vendor_id = r.vendor_id   "
                + " 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(Cluster.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
        String sql = "select a.id,a.name from cluster 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<Cluster> clusters = this.list(Cluster.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (Cluster cluster : clusters) {
            beans.add(new SimpleBean(cluster.getId(), cluster.getName()));
        }
        return beans;
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update cluster 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 int count(List<Param> params) {
        String sql = "select count(1) from cluster 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();
    }

    /**
     * @since 2018-04-12 modify by sxp 增加云平台名称的查询
     */

    public Cluster query(Long id) {
        String sql = "select a.*,b.name vdc_name,c.name vendor_name , r.name region_name " + " from cluster a "
                + " left join vdc b on a.vdc_id = b.id " + " left join cloud_vendor c on a.vendor_id = c.id "
                + " left join region r on a.region_id = r.id and a.vendor_id = r.vendor_id   "
                + " where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
        List<Cluster> list = this.list(Cluster.class, sql, param);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public boolean removeByVendor(Long vendorId, Long userId) {
        String sql = "update cluster set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public List<Cluster> listByVdc(Long vdcId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vdc_id = :vdcId";
        Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
        return this.list(Cluster.class, sql, params);
    }

    public List<Cluster> listByVdc(Long vdcId, String category) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vdc_id = :vdcId and a.category = :category";
        Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
        params.put("category", category);
        return this.list(Cluster.class, sql, params);
    }

    public List<Cluster> listByVendor(Long vendorId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(Cluster.class, sql, params);
    }

    public boolean removeByVdc(Long vdcId, Long userId) {
        String sql = "update cluster set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and vdc_id = :vdcId";
        Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public Cluster queryByClusterVal(String clusterVal, Long vdcId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.cluster_val = :clusterVal and a.vdc_id = :vdcId";
        Map<String, Object> param = MapTools.simpleMap("clusterVal", clusterVal);
        param.put("vdcId", vdcId);
        List<Cluster> list = this.list(Cluster.class, sql, param);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public boolean exists(String name, Long vdcId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vdc_id = :vdcId and a.name = :name";
        Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
        params.put("name", name);
        List<Cluster> list = this.list(Cluster.class, sql, params);
        if (list.isEmpty()) {
            return true;
        }
        return false;
    }

    public List<Cluster> listByType(String type) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 ";
        Map<String, Object> paramMap = Maps.newHashMap();
        if (!StringUtils.isEmpty(type)) {
            sql += " and a.type = :type";
            paramMap.put("type", type);
        }
        return this.list(Cluster.class, sql, paramMap);
    }

    public boolean isExistInVendor(String name, Long vendorId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.name = :name";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("name", name);
        List<Cluster> list = this.list(Cluster.class, sql, params);
        return !list.isEmpty();
    }

    public Cluster getByName(String name, Long vdcId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vdc_id = :vdcId and a.name = :name";
        Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
        params.put("name", name);
        List<Cluster> list = this.list(Cluster.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public Cluster getByVal(String val, Long vdcId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vdc_id = :vdcId and a.cluster_val = :val";
        Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
        params.put("val", val);
        List<Cluster> list = this.list(Cluster.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 重置集群
     *
     * @param policyId
     * @return
     * @throws Exception
     */

    public boolean reset(Long policyId, Long userId) {
        String sql = "update cluster set policy_id = null , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and policy_id = :policyId";
        Map<String, Object> params = MapTools.simpleMap("policyId", policyId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    /**
     * 根据策略查询集群列表
     *
     * @param policyId
     * @return
     * @throws Exception
     */

    public List<Cluster> listByPolicyId(Long policyId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.policy_id = :policyId";
        Map<String, Object> params = MapTools.simpleMap("policyId", policyId);
        return this.list(Cluster.class, sql, params);
    }

    public Cluster queryByUuid(String uuid, Long vendorId) {
        String sql = "select a.* from cluster 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(Common.VENDORID, vendorId);
        List<Cluster> clusters = this.list(Cluster.class, sql, params);
        return clusters == null || clusters.size() == 0 ? null : clusters.get(0);
    }

    public Cluster queryByName(String name, Long vendorId) {
        String sql = "select a.* from cluster a where a.is_deleted = 0 and a.name = :name and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("name", name);
        params.put(Common.VENDORID, vendorId);
        List<Cluster> clusters = this.list(Cluster.class, sql, params);
        return clusters == null || clusters.size() == 0 ? null : clusters.get(0);
    }

    public boolean deleteByVendor(Long vendorId) {
        String sql = "DELETE FROM cluster where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.execute(sql, params) > 0;
    }

}
