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

/**
 * @author kyw
 */
@Repository("f5BiMonitorRepository")
public class F5BiMonitorRepository extends BasicGenericDao<BiMonitor, Long> {

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

    public List<BiMonitor> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId,
                                List<String> typeList) throws Exception {
        String sql = "select a.*,d.name as partition_name, IFNULL(c. NAME, '(none)') AS parent_monitor_name from bi_monitor a LEFT JOIN bi_partition d ON a.partition_id = d.id LEFT JOIN (SELECT * FROM bi_monitor b WHERE is_deleted = 0 AND b.vendor_id = :vendorId GROUP BY b.id ) c ON a.parent_monitor_id = c.id where a.is_deleted = 0 AND a.vendor_id = :vendorId ";
        if (!typeList.isEmpty()) {
            sql += " and a.type in(:typeList) ";
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        paramMap.put("vendorId", vendorId);
        paramMap.put("typeList", typeList);
        return this.list(BiMonitor.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter, Long vendorId) throws Exception {
        String sql = "select a.id,a.name from bi_monitor a where a.is_deleted = 0 AND a.vendor_id = :vendorId ";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        paramMap.put("vendorId", vendorId);
        List<BiMonitor> listBiMonitor = this.list(BiMonitor.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (BiMonitor biMonitor : listBiMonitor) {
            beans.add(new SimpleBean(biMonitor.getId(), biMonitor.getName()));
        }
        return beans;
    }

    public int count(List<Param> params, Long vendorId, List<String> typeList) throws Exception {
        String sql = "select count(1) from bi_monitor a where a.is_deleted = 0 AND a.vendor_id = :vendorId ";
        if (!typeList.isEmpty()) {
            sql += " and a.type in(:typeList) ";
        }
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        paramMap.put("vendorId", vendorId);
        paramMap.put("typeList", typeList);
        return this.countQuery(sql, paramMap).intValue();
    }

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

    public BiMonitor query(Long id) throws Exception {
        String sql = "select a.*,bp.`name`AS partition_name from bi_monitor a LEFT JOIN bi_partition bp ON a.partition_id = bp.id where a.id = :id and a.is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<BiMonitor> listBiMonitor = this.list(BiMonitor.class, sql, params);
        if (listBiMonitor.isEmpty()) {
            return null;
        }
        return listBiMonitor.get(0);
    }

    public boolean removeByVendor(Long vendorId, Long userId) throws Exception {
        String sql = "update bi_monitor set is_deleted = 1, 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(Common.MENDER_ID, userId);
        params.put("gmtModify", new Date());
        params.put("vendorId", vendorId);
        return this.execute(sql, params) > 0;
    }

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

    public BiMonitor queryByName(String name, Long vendorId, Long partitionId) throws Exception {
        String sql = "select * from bi_monitor where name = :name and is_deleted = 0 and vendor_id = :vendorId and partition_id = :partitionId ";
        Map<String, Object> params = MapTools.simpleMap(Common.NAME, name);
        params.put("vendorId", vendorId);
        params.put("partitionId", partitionId);
        List<BiMonitor> listBiMonitor = this.list(BiMonitor.class, sql, params);
        if (listBiMonitor.isEmpty()) {
            return null;
        }
        return listBiMonitor.get(0);
    }

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

    public List<BiMonitor> queryByParentId(Long parentId, Long vendorId) throws Exception {
        String sql = "select * from bi_monitor where is_deleted = 0 and vendor_id = :vendorId and parent_monitor_id = :parentId ";
        Map<String, Object> param = MapTools.simpleMap("vendorId", vendorId);
        param.put("parentId", parentId);
        return this.list(BiMonitor.class, sql, param);
    }

}
