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

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.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.f5.BiPool;
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;

@Component("f5BiPoolRepository")
public class F5BiPoolRepository extends BasicGenericDao<BiPool, Long> {

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

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from bi_pool 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 List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.monitor from bi_pool a where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<BiPool> pools = this.list(BiPool.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (BiPool pool : pools) {
            beans.add(new SimpleBean(pool.getId(), pool.getName()));
        }
        return beans;
    }

    public List<BiPool> list(Integer page, Integer rows, List<Param> params, Map<String, String> sorter)
            throws Exception {
        String sql = "select a.*,bp.name as partition_name, ifnull(c.member_count ,0)AS member_count from bi_pool a left join bi_partition bp on a.partition_id = bp.id LEFT JOIN ( SELECT b.pool_id, COUNT(*) AS member_count FROM bi_pool_member b WHERE b.is_deleted = 0 GROUP BY b.pool_id ) c ON a.id = c.pool_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(BiPool.class, sql, paramMap);
    }

    public BiPool query(Long id) throws Exception {
        String sql = "select a.* ,bp.`name` AS partition_name from bi_pool a LEFT JOIN bi_partition bp on a.partition_id = bp.id where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> paramMap = MapTools.simpleMap("id", id);
        List<BiPool> list = this.list(BiPool.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public Boolean remove(Long id, Long userId) throws Exception {
        String sql = "update bi_pool 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("gmtModify", new Date());
        params.put("menderId", userId);
        return this.execute(sql, params) > 0;
    }

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

    public BiPool queryByName(String name, Long vendorId, Long partitionId) throws Exception {
        String sql = "select a.* from bi_pool a where a.is_deleted = 0 and a.name = :name and a.vendor_id = :vendorId and partition_id = :partitionId ";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.NAME, name);
        paramMap.put("vendorId", vendorId);
        paramMap.put("partitionId", partitionId);
        List<BiPool> list = this.list(BiPool.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public List<BiPool> list(Long vendorId) throws Exception {
        String sql = "select a.* from bi_pool a where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> paramMap = MapTools.simpleMap("vendorId", vendorId);
        return this.list(BiPool.class, sql, paramMap);
    }

    public List<BiPool> queryLikeMonitorId(Long monitorId, Long vendorId) throws Exception {
        String sql = "select a.* from bi_pool a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.monitor like :monitorId ";
        Map<String, Object> paramMap = MapTools.simpleMap("vendorId", vendorId);
        paramMap.put("monitorId", "%" + monitorId + "%");
        return this.list(BiPool.class, sql, paramMap);
    }

    public List<BiPool> queryByParentId(Long monitorId, Long vendorId) throws Exception {
        String sql = "select a.* from bi_pool a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.parent_monitor_id = :monitorId ";
        Map<String, Object> paramMap = MapTools.simpleMap("vendorId", vendorId);
        paramMap.put("monitorId", monitorId);
        return this.list(BiPool.class, sql, paramMap);
    }

    public boolean existName(String name, Long vendorId, Long partitionId) throws Exception {
        String sql = "select a.* from bi_rule a where a.is_deleted = 0 and binary a.name = :name and a.vendor_id = :vendorId and partition_id = :partitionId";
        Map<String, Object> paramMap = MapTools.simpleMap("name", name);
        paramMap.put("vendorId", vendorId);
        paramMap.put("partitionId", partitionId);
        List<BiPool> biPools = this.list(BiPool.class, sql, paramMap);
        if (biPools.isEmpty()) {
            return false;
        }
        return true;
    }

}
