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.BiVirtualServer;
import com.google.common.collect.Maps;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

@Repository("f5BiVirtualServerRepository")
public class F5BiVirtualServerRepository extends BasicGenericDao<BiVirtualServer, Long> {

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

    public List<BiVirtualServer> list(int page, int rows, List<Param> params, Map<String, String> sorter)
            throws Exception {
        String sql = "select a.*,bp.name as partition_name from bi_virtual_server a left join bi_partition bp on a.partition_id = bp.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(BiVirtualServer.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select b.id,b.name from bi_virtual_server b where b.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "b");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<BiVirtualServer> listBiVirServer = this.list(BiVirtualServer.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (BiVirtualServer biVirtualServer : listBiVirServer) {
            beans.add(new SimpleBean(biVirtualServer.getId(), biVirtualServer.getName()));
        }
        return beans;
    }

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from bi_virtual_server 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 boolean remove(Long id, Long userId) throws Exception {
        String sql = "update bi_virtual_server set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where 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 BiVirtualServer query(Long id) throws Exception {
        String sql = "select a.*, b. NAME AS pool_name,c. NAME AS http_profile_name,d. NAME AS snat_pool_name,e. NAME AS protocol_profile_client_name,e. NAME AS protocol_profile_client_name,f.name AS protocol_profile_server_name,h.name AS default_persistence_profile_name,i.name AS fallback_persistence_profile_name, bp.name AS partition_name from bi_virtual_server a LEFT JOIN (SELECT * FROM bi_pool WHERE is_deleted = 0 ) b ON a.pool_id = b.id LEFT JOIN (SELECT * FROM bi_profile WHERE is_deleted = 0 ) c ON a.http_profile_id = c.id LEFT JOIN ( SELECT * FROM bi_snat_pool WHERE is_deleted = 0 ) d ON a.snat_pool_id = d.id LEFT JOIN (SELECT * FROM bi_profile WHERE is_deleted = 0 ) e ON a.protocol_profile_client_id = e.id LEFT JOIN (SELECT * FROM bi_profile WHERE is_deleted = 0 )f ON a.protocol_profile_server_id = f.id LEFT JOIN (SELECT * FROM bi_profile WHERE is_deleted = 0 )h ON a.default_persistence_profile_id = h.id LEFT JOIN (SELECT * FROM bi_profile WHERE is_deleted = 0 )i ON a.fallback_persistence_profile_id = i.id LEFT JOIN bi_partition bp ON a.partition_id=bp.id where a.id =:id and a.is_deleted = 0 ";
        Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
        List<BiVirtualServer> listBiVirServer = this.list(BiVirtualServer.class, sql, param);
        if (listBiVirServer.isEmpty()) {
            return null;
        }
        return listBiVirServer.get(0);
    }

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

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

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

    public int applyListCount(List<Long> projectIds, List<Param> paramList) throws Exception {
        String sql = "SELECT count(1) FROM bi_virtual_server a LEFT JOIN user b ON a.owner_id = b.id LEFT JOIN project c ON a.project_id = c.id WHERE project_id in (:projectIds) and a.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, paramList, null, "a");
        Map<String, Object> paramMap = MapTools.simpleMap("projectIds", projectIds);
        return this.countQuery(sql, paramMap).intValue();
    }

    public List<BiVirtualServer> queryByProjectId(int page, int rows, Map<String, String> sorter, List<Long> projectIds,
                                                  List<Param> paramList) throws Exception {
        String sql = "SELECT a.*,b.name AS user_name, c.name AS project_name FROM bi_virtual_server a LEFT JOIN user b ON a.owner_id = b.id LEFT JOIN project c ON a.project_id = c.id WHERE project_id in (:projectIds) and a.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, paramList, sorter), "a");
        Map<String, Object> paramMap = MapTools.simpleMap("projectIds", projectIds);
        return this.list(BiVirtualServer.class, sql, paramMap);
    }

    public boolean tenantCheck(Long projectId) throws Exception {
        String sql = "select count(1) from bi_virtual_server a where a.is_deleted = 0 and a.project_id = :projectId ";
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("projectId", projectId);
        return this.countQuery(sql, paramMap).intValue() > 0;
    }

    public int existsProject(Long vendorId) throws Exception {
        String sql = "SELECT COUNT(a.project_id)as projectCount FROM bi_virtual_server a WHERE a.is_deleted = 0 AND a.vendor_id = :vendorId AND a.project_id IS NOT NULL AND a.project_id != 0 ";
        sql = QueryBuilder.buildRaw(sql, null, null, "a");
        Map<String, Object> paramMap = MapTools.simpleMap("vendorId", vendorId);
        return this.countQuery(sql, paramMap).intValue();
    }
}
