package com.bocloud.ims.repository.resource;

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

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.RouteTable;

@Repository("routeTableRepository")
public class RouteTableRepository extends BasicGenericDao<RouteTable, Long> {

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

    public List<RouteTable> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.*,r.name region_name,v.name vpc_name,s.main from route_table a "
            + "left join route_table_association s on s.route_table_id = a.id and s.subnet_uuid is null and s.gateway_uuid is null and s.is_deleted = 0 "
            + "left join vpc v on v.id = a.vpc_id "
            + "left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_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(RouteTable.class, sql, paramMap);
    }

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from route_table 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 route_table 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 RouteTable query(Long id) throws Exception {
        String sql = "select a.*, s.main, r.name region_name from route_table a "
            + "left join route_table_association s on s.route_table_id = a.id and s.subnet_uuid is null and s.gateway_uuid is null and s.is_deleted = 0 "
            + "left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_id "
            + "where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<RouteTable> list = this.list(RouteTable.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public RouteTable query(String routeTableUuid) throws Exception {
        String sql = "select a.*, s.main, r.name region_name from route_table a "
            + "left join route_table_association s on s.route_table_id = a.id and s.subnet_uuid is null and s.gateway_uuid is null and s.is_deleted = 0 "
            + "left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_id "
            + "where a.is_deleted = 0 and a.uuid = :routeTableUuid";
        Map<String, Object> params = MapTools.simpleMap("routeTableUuid", routeTableUuid);
        List<RouteTable> list = this.list(RouteTable.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

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

    public boolean removeByVpcId(Long vpcId, Long userId) throws Exception {
        String sql =
            "update route_table set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vpc_id = :vpcId ";
        Map<String, Object> params = MapTools.simpleMap("vpcId", vpcId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

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

    public RouteTable query(Long vendorId, String regionId, String vpcUuid, String type) throws Exception {
        String sql =
            "select a.* from route_table a  where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId and vpc_uuid = :vpcUuid and type = :type ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("regionId", regionId);
        params.put("vpcUuid", vpcUuid);
        params.put("type", type);
        List<RouteTable> list = this.list(RouteTable.class, sql, params);
        return list.size() > 0 ? list.get(0) : null;
    }

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

    public List<RouteTable> listNotAttachBySubnet(Long subnetId, Long vpcId) throws Exception {
        String sql = "select * from route_table a where a.vpc_id = :vpcId and a.id not in "
            + "(select route_table_id from subnet where id = :subnetId) ";
        Map<String, Object> params = MapTools.simpleMap("vpcId", vpcId);
        params.put("subnetId", subnetId);
        return this.list(RouteTable.class, sql, params);
    }

    public boolean deleteByVpcUuid(Long vpcId, Long target) {

        String sql = "update route_table set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vpc_id = :vpcId ";
        Map<String, Object> params = MapTools.simpleMap("vpcId", vpcId);
        params.put("menderId", target);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;

    }

    public RouteTable queryByVpcId(Long vpcId, Long vendorId) {
        String sql =
                "select a.* from route_table a  where a.is_deleted = 0 and a.vendor_id = :vendorId and vpc_id = :vpcId ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put("vpcId", vpcId);
        List<RouteTable> list = this.list(RouteTable.class, sql, params);
        return list.size() > 0 ? list.get(0) : null;
    }
}
