package com.bocloud.ims.repository.resource;

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

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

/**
 * 负载均衡监听器Repository接口实现
 *
 * @author weiwei
 */
@Repository("lbListenerRepository")
public class LbListenerRepository extends BasicGenericDao<LbListener, Long> {

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

    public List<LbListener> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.*,t.name tenant_name ,r.name region_name from lb_listener a left join tenant t on a.tenant_id = t.id left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_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(LbListener.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.name,a.listener_uuid from lb_listener 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<LbListener> listeners = this.list(LbListener.class, sql, paramMap);
        List<SimpleBean> beans = Lists.newArrayList();
        for (LbListener listener : listeners) {
            beans.add(new SimpleBean(listener.getId(), listener.getName(), listener.getListenerUuid()));
        }
        return beans;
    }

    public List<LbListener> listByVid(Long vendorId) throws Exception {
        String sql = "select a.*,t.name tenant_name from lb_listener a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return this.list(LbListener.class, sql, params);
    }

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from lb_listener 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 lb_listener 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 removeByVid(Long vendorId, Long userId) throws Exception {
        String sql = "update lb_listener 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("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public LbListener query(Long id) throws Exception {
        String sql = "select a.*,t.name tenant_name from lb_listener a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<LbListener> list = this.list(LbListener.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

    public List<LbListener> listByLbId(Long lbId) throws Exception {
        String sql = "select a.*,t.name tenant_name from lb_listener a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.lb_id = :lbId";
        Map<String, Object> params = MapTools.simpleMap("lbId", lbId);
        return this.list(LbListener.class, sql, params);
    }

    public boolean tenantCheck(Long tenantId) throws Exception {
        String sql = "select count(1) from lb_listener a where a.is_deleted = 0 and a.tenant_id = :tenantId";
        Map<String, Object> paramMap = MapTools.simpleMap("tenantId", tenantId);
        return this.countQuery(sql, paramMap).intValue() > 0;
    }

    public boolean portExist(Integer port, String protocol, Long lbId) throws Exception {
        String sql = "select count(1) from lb_listener a where a.is_deleted = 0 and a.lb_id = :lbId and a.port = :port and a.protocol = :protocol";
        Map<String, Object> paramMap = MapTools.simpleMap("port", port);
        paramMap.put("protocol", protocol);
        paramMap.put("lbId", lbId);
        return this.countQuery(sql, paramMap).intValue() > 0;
    }

    public LbListener queryByUuid(String listenerUuid, Long vendorId) {
        String sql = " select a.* from lb_listener a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.listener_uuid = :listenerUuid ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("listenerUuid", listenerUuid);
        List<LbListener> list = this.list(LbListener.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

    public List<LbListener> listByVid(Long vendorId, String regionId) throws Exception {
        String sql = "select a.*,v.name vendor_name from lb_listener a left join cloud_vendor v on v.id = a.vendor_id where a.is_deleted = 0 and a.region_id = :regionId and a.vendor_id = :vendorId";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
        paramMap.put("regionId", regionId);
        return this.list(LbListener.class, sql, paramMap);
    }

    public List<LbListener> listByPoolUuid(Long vendorId, String regionId, String poolUuid) throws Exception {
        String sql = "select a.*,v.name vendor_name from lb_listener a left join cloud_vendor v on v.id = a.vendor_id where a.is_deleted = 0 and a.region_id = :regionId and a.vendor_id = :vendorId and a.defaultpool_uuid = :poolUuid ";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
        paramMap.put("regionId", regionId);
        paramMap.put("poolUuid", poolUuid);
        return this.list(LbListener.class, sql, paramMap);
    }

    public boolean portExist(Integer port, Long lbId) throws Exception {
        String sql = "select count(1) from lb_listener a where a.is_deleted = 0 and a.lb_id = :lbId and a.port = :port ";
        Map<String, Object> paramMap = MapTools.simpleMap("port", port);
        paramMap.put("lbId", lbId);
        return this.countQuery(sql, paramMap).intValue() > 0;
    }

    public boolean portAndProtocolExist(Integer port, String protocol, Long lbId) {
        String sql = "select count(1) from lb_listener a where a.is_deleted = 0 and a.lb_id = :lbId and a.port = :port and a.protocol = :protocol";
        Map<String, Object> paramMap = MapTools.simpleMap("port", port);
        paramMap.put("lbId", lbId);
        paramMap.put("protocol", protocol);
        return this.countQuery(sql, paramMap).intValue() > 0;
    }
}
