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

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.BasicDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.azure.db.PerformanceTier;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: PerformanceTierRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2020/12/22 14:56
 */
@Repository("performanceTierRepository")
public class PerformanceTierRepository extends BasicDao {


    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select * from performance_tier where 1=1";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<PerformanceTier> list = this.queryForList(sql, paramMap, PerformanceTier.class).stream().map(o -> (PerformanceTier) o).collect(Collectors.toList());
        List<SimpleBean> beans = Lists.newArrayList();
        for (PerformanceTier entity : list) {
            beans.add(new SimpleBean(entity.getId(), entity.getUuid()));
        }
        return beans;
    }

    public List<PerformanceTier> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "SELECT a.* ,v.name vendor_name , r.name region_name "
                + " FROM performance_tier a "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id "
                + " WHERE 1=1 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.queryForList(sql, paramMap, PerformanceTier.class).stream().map(o -> (PerformanceTier) o).collect(Collectors.toList());
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "SELECT COUNT(1) "
                + " FROM performance_tier a "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id "
                + " WHERE 1=1 ";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public PerformanceTier query(Long id) throws BoCloudDBException {
        String sql = "SELECT a.* ,v.name vendor_name , r.name region_name "
                + " FROM performance_tier a "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id "
                + " WHERE a.id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<PerformanceTier> list = this.queryForList(sql, params, PerformanceTier.class).stream().map(o -> (PerformanceTier) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public PerformanceTier save(PerformanceTier ip) throws BoCloudDBException {
        return (PerformanceTier) this.saveEntity(ip);
    }

    public Boolean update(PerformanceTier ip) throws BoCloudDBException {
        return this.updateEntity(ip);
    }

    public Boolean delete(Long id) throws BoCloudDBException {
        String sql = "delete from performance_tier where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }

    public List<PerformanceTier> listByVid(Long vendorId, String regionId) throws BoCloudDBException {
        String sql = "select * from performance_tier where vendor_id = :vendorId and region_id = :regionId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put(Common.REGIONID, regionId);
        return this.queryForList(sql, params, PerformanceTier.class).stream().map(o -> (PerformanceTier) o).collect(Collectors.toList());
    }

    public Boolean removeByVid(Long vendorId, String regionId) throws BoCloudDBException {
        String sql = "delete from performance_tier where vendor_id = :vendorId and region_id = :regionId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        params.put(Common.REGIONID, regionId);
        return this.update(sql, params) > 0;
    }

}