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.BiSnatPool;
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("f5BiSnatPoolRepository")
public class F5BiSnatPoolRepository extends BasicGenericDao<BiSnatPool, Long> {

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

    public List<BiSnatPool> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select * from bi_snat_pool a 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(BiSnatPool.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.name from bi_snat_pool 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<BiSnatPool> listSnatPool = this.list(BiSnatPool.class, sql, paramMap);
        List<SimpleBean> listBean = Lists.newArrayList();
        for (BiSnatPool biSnatPool : listSnatPool) {
            listBean.add(new SimpleBean(biSnatPool.getId(), biSnatPool.getName()));
        }
        return listBean;
    }

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

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from bi_snat_pool 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 bi_snat_pool set is_deleted = 1,gmt_modify = :gmtModify,mender_id = :menderId where 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 BiSnatPool query(Long id) throws Exception {
        String sql = "select * from bi_snat_pool a where a.is_deleted = 0 and id = :id";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.ID, id);
        List<BiSnatPool> listSnatPool = this.list(BiSnatPool.class, sql, paramMap);
        if (listSnatPool.isEmpty()) {
            return null;
        }
        return listSnatPool.get(0);
    }

    public BiSnatPool queryByName(String name, Long vendorId, Long partitionId) throws Exception {
        String sql = "select * from bi_snat_pool 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(Common.NAME, name);
        paramMap.put(Common.VENDORID, vendorId);
        paramMap.put("partitionId", partitionId);
        List<BiSnatPool> listSnatPool = this.list(BiSnatPool.class, sql, paramMap);
        if (listSnatPool.isEmpty()) {
            return null;
        }
        return listSnatPool.get(0);
    }

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

    public boolean existName(String name, Long vendorId, Long partitionId) throws Exception {
        String sql = "select * from bi_snat_pool 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(Common.NAME, name);
        paramMap.put(Common.VENDORID, vendorId);
        paramMap.put("partitionId", "partitionId");
        List<BiSnatPool> listSnatPool = this.list(BiSnatPool.class, sql, paramMap);
        if (listSnatPool.isEmpty()) {
            return false;
        }
        return true;
    }

}
