package com.bocloud.ims.repository.resource.azure.monitor;

import com.bocloud.common.exception.BoCloudDBException;
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.ListTool;
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.azure.monitor.AzureAlarm;
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
public class AzureAlarmRepository extends BasicGenericDao<AzureAlarm, Long> {

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

    public int count(List<Param> params, Long tenantId, List<Long> projectIds, Long userId) {
        String sql = " select count(a.id) from azure_alarm a where a.is_deleted = 0 ";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (null != tenantId && 0L != tenantId) {
            sql += " and a.tenant_id = :tenantId";
            paramMap.put("tenantId", tenantId);
        }
        if (!ListTool.isEmpty(projectIds)) {
            sql += " and a.project_id in (:projectIds) ";
            paramMap.put("projectIds", projectIds);
        }
        if (null != userId && 0L != userId) {
            sql += " and a.user_id = :userId";
            paramMap.put("userId", userId);
        }
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        return this.countQuery(sql, paramMap).intValue();
    }

    public List<AzureAlarm> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long tenantId,
                                 List<Long> projectIds, Long userId) {
        String sql = "select a.*,v.name vendor_name,v.type vendor_type,t.name tenant_name, p.name project_name "
                + " from azure_alarm a "
                + " left join tenant t on a.tenant_id = t.id "
                + " left join project p on a.project_id = p.id "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " where a.is_deleted = 0 ";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (null != tenantId && 0L != tenantId) {
            sql += " and a.tenant_id = :tenantId";
            paramMap.put("tenantId", tenantId);
        }
        if (!ListTool.isEmpty(projectIds)) {
            sql += " and a.project_id in (:projectIds) ";
            paramMap.put("projectIds", projectIds);
        }
        if (null != userId && 0L != userId) {
            sql += " and a.user_id = :userId";
            paramMap.put("userId", userId);
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(AzureAlarm.class, sql, paramMap);
    }

    public int count(List<Param> params, Long tenantId, List<Long> projectIds) {
        String sql = " select count(a.id) from azure_alarm a where a.is_deleted = 0 ";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (null != tenantId && 0L != tenantId) {
            sql += " and a.tenant_id = :tenantId";
            paramMap.put("tenantId", tenantId);
        }
        if (!ListTool.isEmpty(projectIds)) {
            sql += " and a.project_id in (:projectIds) ";
            paramMap.put("projectIds", projectIds);
        }
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        return this.countQuery(sql, paramMap).intValue();
    }

    public List<AzureAlarm> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long tenantId,
                                 List<Long> projectIds) {
        String sql = "select a.*,v.name vendor_name,v.type vendor_type,t.name tenant_name, p.name project_name "
                + " from azure_alarm a "
                + " left join tenant t on a.tenant_id = t.id "
                + " left join project p on a.project_id = p.id "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " where a.is_deleted = 0 ";
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        if (null != tenantId && 0L != tenantId) {
            sql += " and a.tenant_id = :tenantId";
            paramMap.put("tenantId", tenantId);
        }
        if (!ListTool.isEmpty(projectIds)) {
            sql += " and a.project_id in (:projectIds) ";
            paramMap.put("projectIds", projectIds);
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(AzureAlarm.class, sql, paramMap);
    }


    public List<AzureAlarm> list(int page, int rows, List<Param> params, Map<String, String> sorter)
            throws BoCloudDBException {
        String sql = "select a.* ,v.name vendor_name,v.type vendor_type, t.name tenant_name, p.name project_name from azure_alarm a "
                + " left join tenant t on a.tenant_id = t.id "
                + " left join project p on a.project_id = p.id "
                + " 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");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(AzureAlarm.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select a.id,a.name from azure_alarm 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<AzureAlarm> objects = this.list(AzureAlarm.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (AzureAlarm object : objects) {
            beans.add(new SimpleBean(object.getId(), object.getName()));
        }
        return beans;
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "select count(1) from azure_alarm 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 BoCloudDBException {
        String sql = "update azure_alarm 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 AzureAlarm query(Long id) throws BoCloudDBException {
        String sql = "select a.*,v.name vendor_name,v.type vendor_type, t.name tenant_name, p.name project_name  from azure_alarm a "
                + " left join tenant t on a.tenant_id = t.id "
                + " left join project p on a.project_id = p.id "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
        List<AzureAlarm> list = this.list(AzureAlarm.class, sql, param);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public boolean removeByVid(Long vendorId) throws BoCloudDBException {
        String sql = "update azure_alarm set is_deleted = true where is_deleted = 0 and vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return this.execute(sql, params) > 0;
    }


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

    public AzureAlarm getByUuid(Long vendorId, String uuid) throws BoCloudDBException {
        String sql = "select a.* from azure_alarm a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.uuid = :uuid";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
        paramMap.put(Common.UUID, uuid);
        List<AzureAlarm> list = this.list(AzureAlarm.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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