package com.palmnest.service.estateserve.impl;

import com.palmnest.dao.estateserve.CountServerDao;
import com.palmnest.service.estateserve.CountServerManager;
import com.palmnest.service.impl.GenericManagerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 统计服务业务层接口实现（所有统计业务的服务）
 *
 * @author yubing
 * @since 2017-08-10
 */
@Service("countServerManager")
public class CountServerManagerImpl extends GenericManagerImpl<Object, Integer>
		implements CountServerManager {

	private CountServerDao countServerDao;

	@Autowired
	public void setCountServerDao(CountServerDao countServerDao) {
		this.countServerDao = countServerDao;
		this.dao = countServerDao;
	}

	@Override
	public List<Map<String, Object>> querySumRechargeGain(
			String organizationId, String month, String keyWords) {
		List<Map<String, Object>> list     = countServerDao.querySumRechargeGain(organizationId, month, keyWords);//有项目ID的充值记录
		List<Map<String, Object>> codeList = countServerDao.queryCountOrganizationCodeChargeGain(organizationId, month, keyWords);//没项目ID有项目编码的充值记录
		List<Map<String, Object>> list2    = new ArrayList<Map<String, Object>>();
		if (codeList != null) {
			for (int i = 0; i < codeList.size(); i++) {
				Map<String, Object> map1 = codeList.get(i);
				boolean             flag = false;
				for (int j = 0; j < list.size(); j++) {
					Map<String, Object> map2 = list.get(j);
					if (map1.get("ID").toString().equals(map2.get("ID").toString())) {
						flag = true;
						BigDecimal bigDecimal1 = new BigDecimal(map1.get("totalAmonut").toString());
						BigDecimal bigDecimal2 = new BigDecimal(map2.get("totalAmonut").toString());
						map2.put("totalAmonut", bigDecimal1.add(bigDecimal2));
						list.set(j, map2);
					}
				}

				if (!flag) {
					list2.add(map1);
				}
			}
		}

		for (int i = 0; i < list2.size(); i++) {
			list.add(list2.get(i));
		}
		Collections.reverseOrder();
		Collections.sort(list, new Comparator() {

			@Override

			public int compare(Object o1, Object o2) {
				Map<String, Object> map  = (Map<String, Object>) o1;
				Map<String, Object> map1 = (Map<String, Object>) o2;
				return new Double(map1.get("totalAmonut").toString()).compareTo(new Double(map.get("totalAmonut").toString()));

			}

		});


		return list;
	}

	@Override
	public List<Map<String, Object>> queryCountOrganizationFans(
			String organizationId, String keyWords) {
		return countServerDao.queryCountOrganizationFans(organizationId, keyWords);
	}

}
