
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.QosPolicy;
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;

/**
 * qos policy
 *
 * @author ymh
 * @时间 2019年11月13日
 */
@Repository("qosPolicyRepository")
public class QosPolicyRepository extends BasicGenericDao<QosPolicy, Long> {

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

    public List<QosPolicy> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = " select a.*, r.max_kbps,r.id rule_id, r.max_burst_kbps , r.direction, v.`name` vendor_name,v.type vendor_type from qos_policy a left join qos_policy_rules r on a.id = r.policy_id and a.vendor_id = r.vendor_id and r.is_deleted = 0 left join cloud_vendor v on a.vendor_id = v.id  where a.is_deleted = 0  ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(QosPolicy.class, sql);
    }

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from qos_policy v where v.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, params, null, "v");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

    public int count(List<Param> params, String tenantUuid) {
        String sql = "select count(1) from qos_policy a where a.is_deleted = 0";
        if (null != tenantUuid && !"".equals(tenantUuid)) {
            sql += " and ( a.tenant_uuid = '" + tenantUuid + "' or a.shared = 1 ) ";
        }
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

    public List<QosPolicy> list(int page, int rows, List<Param> params, Map<String, String> sorter, String tenantUuid) {
        String sql = " select a.*, r.max_kbps,r.id rule_id, r.max_burst_kbps , r.direction, v.`name` vendor_name,v.type vendor_type , o.name os_tenant_name from qos_policy a left join qos_policy_rules r on a.id = r.policy_id and a.vendor_id = r.vendor_id and r.is_deleted = 0 left join cloud_vendor v on a.vendor_id = v.id left join os_tenant o on a.vendor_id = o.vendor_id and a.tenant_uuid = o.tenant_uuid  where a.is_deleted = 0  ";
        if (null != tenantUuid && !"".equals(tenantUuid)) {
            sql += " and ( a.tenant_uuid = '" + tenantUuid + "' or a.shared = 1 ) ";
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(QosPolicy.class, sql);
    }

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

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

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.name,a.status,a.policy_uuid from qos_policy 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<QosPolicy> volumes = this.list(QosPolicy.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<>();
        for (QosPolicy qosPolicy : volumes) {
            SimpleBean simpleBean = new SimpleBean(qosPolicy.getId(), qosPolicy.getName(), qosPolicy.getStatus());
            simpleBean.setUuid(qosPolicy.getPolicyUuid());
            beans.add(simpleBean);
        }
        return beans;
    }

    public QosPolicy query(Long id) throws Exception {
        String sql = " select a.*, r.max_kbps,r.id rule_id, r.max_burst_kbps , r.direction, v.`name` vendor_name,v.type vendor_type ,  o.name os_tenant_name from qos_policy a left join qos_policy_rules r on a.id = r.policy_id and a.vendor_id = r.vendor_id and r.is_deleted = 0 left join cloud_vendor v on a.vendor_id = v.id left join os_tenant o on a.vendor_id = o.vendor_id and a.tenant_uuid = o.tenant_uuid  where a.is_deleted = 0 and a.id = :id ";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<QosPolicy> list = this.list(QosPolicy.class, sql, params);
        return list.isEmpty() ? null : list.get(0);
    }

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

    public List<QosPolicy> listPolicysByRule(Long vendorId) throws Exception {
        String sql = " select a.* from qos_policy a where a.is_deleted = 0 and a.vendor_id = :vendorId and  not EXISTS ( select r.policy_id  from qos_policy_rules r where a.id = r.policy_id and r.vendor_id = a.vendor_id and r.is_deleted = 0) ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(QosPolicy.class, sql, params);
    }


    public List<QosPolicy> listPolicysByVm(Long vendorId, String tenantUuid) throws Exception {
        String sql = "  select a.*,q.max_kbps from qos_policy a  left join qos_policy_rules q on a.policy_uuid = q.policy_uuid and a.vendor_id = q.vendor_id and q.is_deleted = 0  where a.is_deleted = 0 and a.vendor_id = :vendorId and EXISTS ( select r.policy_id  from qos_policy_rules r where a.id = r.policy_id and r.vendor_id = a.vendor_id and r.is_deleted = 0 ) ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        if (null != tenantUuid) {
            sql += "  and (a.tenant_uuid = :tenantUuid or shared = 1)  ";
            params.put("tenantUuid", tenantUuid);
        }
        return this.list(QosPolicy.class, sql, params);
    }

    public QosPolicy queryByUuid(String policyUuid, Long vendorId) throws Exception {
        String sql = "select a.* , r.max_kbps from qos_policy a  left join qos_policy_rules r on a.policy_uuid = r.policy_uuid and a.vendor_id = r.vendor_id and r.is_deleted = 0 where a.is_deleted = 0 and a.policy_uuid = :policyUuid and a.vendor_id = :vendorId ";
        Map<String, Object> param = MapTools.simpleMap("policyUuid", policyUuid);
        param.put(Common.VENDORID, vendorId);
        List<QosPolicy> list = this.list(QosPolicy.class, sql, param);
        return list.isEmpty() ? null : list.get(0);
    }

}
