package com.bocloud.ims.repository.resource;

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.LbTargetGroup;
import org.apache.commons.lang3.StringUtils;
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.HashMap;
import java.util.List;
import java.util.Map;

@Repository("lbTargetGroupRepository")
public class LbTargetGroupRepository extends BasicGenericDao<LbTargetGroup, Long> {

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

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

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from lb_target_group 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 lb_target_group 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 boolean removeByUuid(String uuid, Integer port, Long vendorId, String regionId, Long userId) throws Exception {
        String sql = "update lb_target_group set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and uuid = :uuid and port = :port";
        Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
        params.put("port", port);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public LbTargetGroup queryByUuid(String uuid) throws Exception {
        String sql = "select a.* from lb_target_group a where a.is_deleted = 0 and a.uuid = :uuid";
        Map<String, Object> params = MapTools.simpleMap(Common.UUID, uuid);
        List<LbTargetGroup> list = this.list(LbTargetGroup.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public LbTargetGroup query(Long id) throws Exception {
        String sql = "select a.*,r.name region_name from lb_target_group a"
                + " 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<LbTargetGroup> list = this.list(LbTargetGroup.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 lb_target_group 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 List<LbTargetGroup> listByVid(Long vendorId, String regionId) throws Exception {
        String sql = "select a.* from lb_target_group 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(LbTargetGroup.class, sql, params);
    }

    public List<LbTargetGroup> queryByLbUuid(String lbUuid) throws Exception {
        String sql = "select a.* from lb_target_group a  where a.is_deleted = 0  and a.load_balancer_uuids like :lbUuid ";
        Map<String, Object> params = MapTools.simpleMap("lbUuid", "%\"" + lbUuid + "\"%");
        return this.list(LbTargetGroup.class, sql, params);
    }

    public int count(Long userId, Long tenantId, List<Long> projectIds, List<Param> params, String vendorType) {
        String subquerySql = "select a.* from lb_target_group a left join cloud_vendor c on a.vendor_id = c.id where a.is_deleted = 0";
        if (!StringUtils.isEmpty(vendorType)) {
            subquerySql += " and c.type = '" + vendorType + "'  ";
        }
        Map<String, Object> param = QueryBuilder.getParam(params);
        subquerySql = QueryBuilder.buildRaw(subquerySql, params, new HashMap<>(), "a");
        String sql = "select count(1) from ( " + subquerySql + " ) d  where ( 1=-1 ";
        if (null != userId && 0L != userId) {
            sql += " or d.owner_id = :userId or d.mender_id = :userId ";
            param.put("userId", userId);
        }
        if (null != tenantId && 0L != tenantId) {
            sql += " or d.tenant_id = :tenantId";
            param.put("tenantId", tenantId);
        }
        if (null != projectIds && !projectIds.isEmpty()) {
            sql += " or d.project_id in (:projectIds) ";
            param.put("projectIds", projectIds);
        }
        sql += ")";
        return this.countQuery(sql, param).intValue();
    }

    public int count2(Long userId, Long tenantId, List<Long> projectIds, List<Param> params, String vendorType) {
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        String sql = "select count(1) from lb_target_group a left join cloud_vendor c on a.vendor_id = c.id ";
        if (!StringUtils.isEmpty(vendorType)) {
            sql += " and c.type = '" + vendorType + "'  ";
        }
        sql += " where a.is_deleted = 0 ";
        if (null != userId && 0L != userId) {
            sql += " and ( a.owner_id = :userId or a.mender_id = :userId )";
            paramMap.put("userId", userId);
        }
        if (null != tenantId && 0L != tenantId) {
            sql += " and a.tenant_id = :tenantId";
            paramMap.put("tenantId", tenantId);
        }
        if (null != projectIds && !projectIds.isEmpty()) {
            sql += " and a.project_id in (:projectIds) ";
            paramMap.put("projectIds", projectIds);
        }
        sql = QueryBuilder.buildRaw(sql, params, new HashMap<>(), "a");
        return this.countQuery(sql, paramMap).intValue();
    }


    public List<LbTargetGroup> list(int page, int rows, Long userId, Long tenantId, List<Long> projectIds,
                                    List<Param> params, Map<String, String> sorter, String vendorType) {
        String subquerySql = "select a.*,v.type vendor_type,r.name region_name from lb_target_group a "
                + " left join cloud_vendor v on a.vendor_id= v.id "
                + " left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_id"
                + " where a.is_deleted = 0";
        if (!StringUtils.isEmpty(vendorType)) {
            subquerySql += " and v.type = '" + vendorType + "'  ";
        }
        subquerySql = QueryBuilder.buildRaw(subquerySql, params, new HashMap<>(), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        String sql = "select  d.*  from (" + subquerySql + ") d  where ( 1=-1 ";
        if (null != userId && 0L != userId) {
            sql += " or d.owner_id = :userId or d.mender_id = :userId ";
            paramMap.put("userId", userId);
        }
        if (null != tenantId && 0L != tenantId) {
            sql += " or d.tenant_id = :tenantId";
            paramMap.put("tenantId", tenantId);
        }
        if (null != projectIds && !projectIds.isEmpty()) {
            sql += " or d.project_id in (:projectIds) ";
            paramMap.put("projectIds", projectIds);
        }
        sql += " )";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, null, sorter), "d");
        return this.list(LbTargetGroup.class, sql, paramMap);
    }


    public List<LbTargetGroup> list2(int page, int rows, Long userId, Long tenantId, List<Long> projectIds,
                                     List<Param> params, Map<String, String> sorter, String vendorType) {
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        String sql = "select a.*,v.type vendor_type,r.name region_name from lb_target_group a "
                + " left join cloud_vendor v on a.vendor_id= v.id "
                + " left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_id ";
        if (!StringUtils.isEmpty(vendorType)) {
            sql += " and v.type = '" + vendorType + "'  ";
        }
        sql += " where a.is_deleted = 0 ";
        if (null != userId && 0L != userId) {
            sql += " and ( a.owner_id = :userId or a.mender_id = :userId ) ";
            paramMap.put("userId", userId);
        }
        if (null != tenantId && 0L != tenantId) {
            sql += " and a.tenant_id = :tenantId";
            paramMap.put("tenantId", tenantId);
        }
        if (null != projectIds && !projectIds.isEmpty()) {
            sql += " and a.project_id in (:projectIds) ";
            paramMap.put("projectIds", projectIds);
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(LbTargetGroup.class, sql, paramMap);
    }
}
