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.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.f5.BiPoolMember;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

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

@Component("f5BiPoolMumberRepository")
public class F5BiPoolMemberRepository extends BasicGenericDao<BiPoolMember, Long> {

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

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from bi_pool_member 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 List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.monitor from bi_pool_member a where a.is_deleted = 0";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<BiPoolMember> poolMembers = this.list(BiPoolMember.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (BiPoolMember poolMumber : poolMembers) {
            beans.add(new SimpleBean(poolMumber.getId(), poolMumber.getName()));
        }
        return beans;
    }

    public List<BiPoolMember> list(Integer page, Integer rows, List<Param> params, Map<String, String> sorter)
            throws Exception {
        String sql = "select a.*, bp.name as partition_name from bi_pool_member a left join bi_partition bp on a.partition_id = bp.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(BiPoolMember.class, sql, paramMap);
    }

    public BiPoolMember query(Long id) throws Exception {
        String sql = "select a.* from bi_pool_member a where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> paramMap = MapTools.simpleMap("id", id);
        List<BiPoolMember> list = this.list(BiPoolMember.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public Boolean remove(Long id, Long userId) throws Exception {
        String sql = "update bi_pool_member 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("gmtModify", new Date());
        params.put("menderId", userId);
        return this.execute(sql, params) > 0;
    }

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

    public Boolean removeByPoolId(Long poolId, Long userId) throws Exception {
        String sql = "update bi_pool_member set is_deleted = 1 , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and pool_id = :poolId";
        Map<String, Object> params = MapTools.simpleMap("userId", userId);
        params.put("gmtModify", new Date());
        params.put("menderId", userId);
        params.put("poolId", poolId);
        return this.execute(sql, params) > 0;
    }

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

    public BiPoolMember queryByAddress(String address, Long vendorId, Long partitionId) throws Exception {
        String sql = "select a.* from bi_pool_member a where a.is_deleted = 0 and a.pool_member_address = :address and a.vendor_id = :vendorId and partition_id = :partitionId ";
        Map<String, Object> paramMap = MapTools.simpleMap("address", address);
        paramMap.put("vendorId", vendorId);
        paramMap.put("partitionId", partitionId);
        List<BiPoolMember> list = this.list(BiPoolMember.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public Boolean removeByVIdAndPoolId(Long vendorId, Long userId, Long poolId) throws Exception {
        String sql = "update bi_pool_member set is_deleted = 1 , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and pool_id = :poolId and vendor_id = :vendorId ";
        Map<String, Object> params = MapTools.simpleMap("userId", userId);
        params.put("gmtModify", new Date());
        params.put("menderId", userId);
        params.put("poolId", poolId);
        params.put("vendorId", vendorId);
        return this.execute(sql, params) > 0;
    }

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

    public List<BiPoolMember> queryByPoolId(Long poolId) throws Exception {
        String sql = "select a.* from bi_pool_member a where a.is_deleted = 0 and pool_id = :poolId ";
        Map<String, Object> params = MapTools.simpleMap("poolId", poolId);
        return this.list(BiPoolMember.class, sql, params);
    }

    public Boolean removeByAddressAndPort(String address, String port, Long vendorId, Long userId, Long partitionId)
            throws Exception {
        String sql = "update bi_pool_member set is_deleted = 1 , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and pool_member_address = :address and pool_member_port = :port and vendor_id = :vendorId and partition_id = :partitionId ";
        Map<String, Object> params = MapTools.simpleMap("userId", userId);
        params.put("gmtModify", new Date());
        params.put("menderId", userId);
        params.put("address", address);
        params.put("port", port);
        params.put("vendorId", vendorId);
        params.put("partitionId", partitionId);
        return this.execute(sql, params) > 0;
    }

}
