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

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

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: RouterTableRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/1/4 16:15
 */
@Component("routerTableRepository")
public class RouterTableRepository extends BasicGenericDao<RouterTable, Long> {

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

    public List<RouterTable> list(int page, int rows, List<Param> params, Map<String, String> sorter)
            throws Exception {
        String sql = "select a.*,b.name group_name,c.name region_name from router_table a left join resource_group b on a.resource_group_id = b.id"
                + " left join region c on a.region_id = c.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(RouterTable.class, sql, paramMap);
    }

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

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

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

    public RouterTable getByName(Long vendorId, Long resourceGroupId, String name) throws BoCloudDBException {
        String sql = "select a.* from router_table a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.name = :name and a.resource_group_id = :resourceGroupId ";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
        paramMap.put(Common.NAME, name);
        paramMap.put("resourceGroupId", resourceGroupId);
        List<RouterTable> list = this.list(RouterTable.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

    public List<RouterTable> listByVendorId(long vendorId, String regionId) throws BoCloudDBException {
        String sql = "select * from router_table where is_deleted = 0 and vendor_id = :vendorId and region_id = :regionId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put(Common.REGIONID, regionId);
        return this.list(RouterTable.class, sql, params);
    }

    public List<RouterTable> list(Long vendorId, String regionId) {
        String sql = "select * from router_table where vendor_id = :vendorId and is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        if (!StringUtils.isEmpty(regionId)) {
            sql += " and region_id = :regionId ";
            params.put(Common.REGIONID, regionId);
        }
        return this.list(RouterTable.class, sql, params);
    }

    public RouterTable getByUuid(Long vendorId, String uuid) throws BoCloudDBException {
        String sql = "select a.* from router_table 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<RouterTable> list = this.list(RouterTable.class, sql, paramMap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

}