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

/**
 * 外部账单
 * 
 * @author admin
 *
 */
@Repository("cloudBillRepository")
public class CloudBillRepository extends BasicGenericDao<CloudBill, Long> {

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

	public List<CloudBill> 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 cloud_bill a  left join cloud_vendor c on a.vendor_id = c.id "
				+ " left join region r on a.vendor_id = r.vendor_id and a.region_id = r.region_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(CloudBill.class, sql, paramMap);
	}

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

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

	public List<CloudBill> listByVendor(Long vendorId, String regionId) throws Exception {
		String sql = "select * from 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(CloudBill.class, sql, params);
	}

	public boolean deleteByVendor(Long vendorId, String regionId) throws Exception {
		String sql = "delete from 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 cloud_bill where id = :id  ";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		return this.execute(sql, params) > 0;
	}

	public List<CloudBill> listTypes(Long vendorId) throws Exception {
		String sql = "select  product_code, product_detail , product_name from cloud_bill where vendor_id =  " + vendorId + " GROUP BY product_code ,product_detail , product_name ";
		return this.list(CloudBill.class, sql);
	}

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

	public List<CloudBill> listByMonth(Long vendorId, String month) throws Exception {
		String sql = "select * from 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(CloudBill.class, sql, params);
	}

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