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.TenantCloudBill;
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.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 外部账单
 *
 * @author admin
 */
@Repository
public class TenantCloudBillRepository extends BasicGenericDao<TenantCloudBill, Long> {

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

    public List<TenantCloudBill> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.*,c.name vendor_name ,r.name region_name from tenant_cloud_bill a  "
                + " left join cloud_vendor c on a.vendor_id = c.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.list(TenantCloudBill.class, sql, paramMap);
    }

    public int count(List<Param> params) throws Exception {
        String sql = "select count(a.id) from tenant_cloud_bill 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 List<TenantCloudBill> list() throws Exception {
        String sql = "select * from tenant_cloud_bill";
        return this.list(TenantCloudBill.class, sql, new HashMap<>());
    }

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

    public List<TenantCloudBill> listByVendor(Long vendorId, String regionId) throws Exception {
        String sql = "select * from tenant_cloud_bill a where a.vendor_id = :vendorId  ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        if (!StringUtils.isEmpty(regionId)) {
            sql += " and a.region_id = :regionId ";
            params.put(Common.REGIONID, regionId);
        }
        return this.list(TenantCloudBill.class, sql, params);
    }

    public List<TenantCloudBill> listByMonth(Long vendorId, String month) throws Exception {
        String sql = "select * from tenant_cloud_bill a where a.vendor_id = :vendorId  ";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        if (!StringUtils.isEmpty(month)) {
            sql += " and a.month = :month ";
            params.put("month", month);
        }
        return this.list(TenantCloudBill.class, sql, params);
    }

    public List<TenantCloudBill> listByTenantId(Long tenantId) throws Exception {
        String sql = "select * from tenant_cloud_bill a where a.tenant_id = :tenantId";
        Map<String, Object> params = MapTools.simpleMap("tenantId", tenantId);
        return this.list(TenantCloudBill.class, sql, params);
    }

    public BigDecimal summary(Long tenantId) throws Exception {
        String sql = "select sum(a.tenant_money) summary from tenant_cloud_bill a where a.tenant_id = :tenantId and a.tenant_money is not null  ";
        Map<String, Object> params = MapTools.simpleMap("tenantId", tenantId);
        List<Map<String, Object>> list = this.list(sql, params);
        Map<String, Object> resultMap = list.get(0);
        Object result = resultMap.get("summary");
        if (null == result) {
            return BigDecimal.valueOf(0);
        }
        return (BigDecimal) result;
    }

    public BigDecimal summary(Long tenantId, Long userId) throws Exception {
        String sql = "select sum(a.tenant_money) summary from tenant_cloud_bill a where a.tenant_id = :tenantId and a.tenant_money is not null  ";
        Map<String, Object> params = MapTools.simpleMap("tenantId", tenantId);
        if (null != userId && 0L != userId) {
            sql += " and a.user_id = :userId";
            params.put("userId", userId);
        }
        List<Map<String, Object>> list = this.list(sql, params);
        Map<String, Object> resultMap = list.get(0);
        Object result = resultMap.get("summary");
        if (null == result) {
            return BigDecimal.valueOf(0);
        }
        return (BigDecimal) result;
    }

    public boolean deleteByVendor(Long vendorId, String regionId) throws Exception {
        String sql = "delete from tenant_cloud_bill where vendor_id = :vendorId  ";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        if (!StringUtils.isEmpty(regionId)) {
            sql += " and region_id = :regionId ";
            params.put(Common.REGIONID, regionId);
        }
        return this.execute(sql, params) > 0;
    }

    public boolean delete(Long id) throws Exception {
        String sql = "delete from tenant_cloud_bill where id = :id  ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.execute(sql, params) > 0;
    }

    public boolean removeByVid(Long vendorId) {
        String sql = "delete from tenant_cloud_bill where vendor_id = :vendorId  ";
        Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
        return this.execute(sql, params) > 0;
    }


    public List<TenantCloudBill> queryTypeList() throws Exception {
        String sql = "select  product_code, product_detail from tenant_cloud_bill where vendor_type = 'ALIYUN' group by product_code, product_detail ";
        return this.list(TenantCloudBill.class, sql);
    }

}
