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.LbListenerRule;
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 majiali
 * @version 1.0
 * @since 2021年1月19日
 */
@Repository("lbListenerRuleRepository")
public class LbListenerRuleRepository extends BasicGenericDao<LbListenerRule, Long> {

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

    public List<LbListenerRule> list(int page, int rows, List<Param> params, Map<String, String> sorter)
            throws Exception {
        String sql = "select a.*,p.name pool_name from lb_listener_rule a left join lb_pool p on a.pool_uuid = p.pool_uuid and a.vendor_id = p.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(LbListenerRule.class, sql, paramMap);
    }

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


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

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from lb_listener_rule 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_rule 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_rule 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 LbListenerRule query(Long id) throws Exception {
        String sql = "select a.*,p.name pool_name from lb_listener_rule a left join lb_pool p on a.pool_uuid = p.pool_uuid where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<LbListenerRule> list = this.list(LbListenerRule.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public LbListenerRule queryByUuid(String ruleUuid) throws Exception {
        String sql = "select a.* from lb_listener_rule a  where a.is_deleted = 0 and a.rule_uuid = :ruleUuid";
        Map<String, Object> params = MapTools.simpleMap("ruleUuid", ruleUuid);
        List<LbListenerRule> list = this.list(LbListenerRule.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_rule 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("vendorId", vendorId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        params.put("regionId", regionId);
        return this.execute(sql, params) > 0;
    }

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

    public List<LbListenerRule> listByVid(Long vendorId, String regionId, String listenerUuid) throws Exception {
        String sql = "select a.* from lb_listener_rule a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId " +
                " and listener_uuid = :listenerUuid";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("regionId", regionId);
        params.put("listenerUuid", listenerUuid);
        return this.list(LbListenerRule.class, sql, params);
    }

    public List<LbListenerRule> listByVid(Long vendorId, String regionId) throws Exception {
        String sql = "select a.* from lb_listener_rule a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId ";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        params.put("regionId", regionId);
        return this.list(LbListenerRule.class, sql, params);
    }

    public List<LbListenerRule> listByPolicyId(Long policyId) throws Exception {
        String sql = "select a.* from lb_listener_rule a  where a.is_deleted = 0 and a.policy_id = :policyId order by a.gmt_create desc";
        Map<String, Object> params = MapTools.simpleMap("policyId", policyId);
        return this.list(LbListenerRule.class, sql, params);
    }

    public List<LbListenerRule> listByListenerId(Long listenerId) {
        String sql = "select a.* from lb_listener_rule a  where a.is_deleted = 0 and a.listener_id = :listenerId order by a.gmt_create desc";
        Map<String, Object> params = MapTools.simpleMap("listenerId", listenerId);
        return this.list(LbListenerRule.class, sql, params);
    }
}
