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.CloudCost;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

@Repository("cloudCostRepository")
public class CloudCostRepository extends BasicGenericDao<CloudCost, Long> {

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

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from cloud_cost a where 1=1";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

    public CloudCost query(Long id) throws Exception {
        String sql = "select a.* from cloud_cost a where a.id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<CloudCost> list = this.list(CloudCost.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public List<CloudCost> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select * from cloud_cost a where 1=1";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(CloudCost.class, sql, paramMap);
    }

    public List<CloudCost> listByVid(Long vendorId) throws Exception {
        String sql = "select a.*,v.name vendor_name from cloud_cost a left join cloud_vendor v on v.id = a.vendor_id where a.vendor_id = :vendorId";
        Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
        List<CloudCost> list = this.list(CloudCost.class, sql, paramMap);
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        return list;
    }

    /**
     * 获取指定结束时间的aws成本信息
     *
     * @param end
     * @return
     * @throws Exception
     */
    public List<CloudCost> listByEnd(Date end) throws Exception {
        String sql = "select a.group_uuid,a.amount,a.unit,b.private_ips " +
                " from cloud_cost a left join cloud_server b on a.group_uuid = b.instance_id " +
                " where a.end = :end and a.group_uuid is not null and b.is_deleted = 0";
        Map<String, Object> paramMap = MapTools.simpleMap("end", end);
        List<CloudCost> list = this.list(CloudCost.class, sql, paramMap);
        return list;
    }


}
