package com.palmnest.service.estateserve.airconditioner.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.application.core.util.HttpKit;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.dao.estateserve.NeigouOrderDao;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.OrganizationManager;
import com.palmnest.service.UserManager;
import com.palmnest.service.estateserve.*;
import com.palmnest.service.estateserve.airconditioner.SuperAppService;
import com.palmnest.service.redis.BaseRedisService;
import com.palmnest.service.virtProject.UserAccountService;
import com.palmnest.service.virtProject.manager.VirtualAccountManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service("superAppService")
public class SuperAppServiceImpl implements SuperAppService {


	public static String meeting_url = PropertiesKit.readPropertiesValue("meeting_url", "wechat.properties");

	@Autowired
	private AppUserManager         appUserManager;
	@Autowired
	private ScWxuserManager        scWxuserManager;
	@Autowired
	private NeigouOrderDao         neigouOrderDao;
	@Autowired
	private BaseRedisService       baseRedisService;
	@Autowired
	private UserManager            userManager;
	@Autowired
	private VirtualArchManager     virtualArchManager;
	@Autowired
	private UserAccountService     userAccountService;
	@Autowired
	private VirtualAccountManager  virtualAccountManager;
	@Autowired
	private OrganizationManager    organizationManager;
	@Autowired
	private PublicNoManager        publicNoManager;
	@Autowired
	private ServiceTypeInfoManager serviceTypeInfoManager;

	@Override
	public List getMeetingRanking(String orgId) {

		String              retMsg             = "";
		Map<String, String> oauthLoginResponse = null;
		Map                 depCountMap        = new HashMap<String, Integer>();

		String monthStr   = "";
		List   returnList = new ArrayList();

		Date currentDate = new Date();
		monthStr = DateUtils.getPrevMonth(DateUtils.getDate(currentDate, "yyyy-MM-dd"), "yyyy-MM");

		if (StringUtil.isNotEmpty(orgId)) {
			TbOrganization org           = organizationManager.getOrganizationById(Integer.parseInt(orgId));
			String         mettingClient = org.getMettingClient();
			if (StringUtil.isNotEmpty(mettingClient)) {


				JSONObject json = new JSONObject();
				json.put("client", mettingClient);

				monthStr = DateUtils.getPrevMonth(DateUtils.getDate(currentDate, "yyyy-MM-dd"), "yyyy-MM");
				//json.put("month", "2019-01");
				json.put("month", monthStr);
				String jsonMapStr = JSONObject.toJSONString(json);
				System.out.println("getMeetingRanking jsonMapStr:" + jsonMapStr);
				//同步企业账号
				retMsg = HttpKit.HttpClientPost(jsonMapStr, meeting_url + "/api/BookOperateWXAPI/GetBookReportByMonth");

				oauthLoginResponse = JSONObject.parseObject(retMsg, new TypeReference<Map<String, String>>() {
				});
				String dateStr = oauthLoginResponse.get("data");
				List<Map<String, String>> dataOj = JSONObject.parseObject(dateStr, new TypeReference<List<Map<String, String>>>() {
				});

				log.info("getMeetingRanking retMsg:" + retMsg);
				log.info("getMeetingRanking dataOj:" + dataOj);
				Map mobileMap = new HashMap<String, Map>();
				System.out.println("getMeetingRanking retMsg:" + retMsg);

				//手机号\部门信息
				Map                       MOBILEDepMap = new HashMap<String, String>();
				List<Map<String, Object>> ListUser     = appUserManager.getUserMobileAndDepName(Integer.parseInt(orgId));
				for (Map<String, Object> appUserMap : ListUser) {
					MOBILEDepMap.put(appUserMap.get("userMobile"), appUserMap.get("depName"));
				}

				//手机号\预定数量
				for (Map<String, String> dataList : dataOj) {
					String MOBILE = dataList.get("MOBILE");
					if (mobileMap.containsKey(MOBILE)) {
						Map depMap = (Map) mobileMap.get(MOBILE);
						mobileMap.put(MOBILE, (Integer) depMap.get("count") + 1);

						if (depMap.get("depName") != null) {
							if (depCountMap.containsKey(MOBILEDepMap.get(MOBILE))) {
								int depCount = (Integer) depCountMap.get(depMap.get("depName"));
								depCountMap.put(depMap.get("depName"), depCount + 1);
							} else {
								depCountMap.put(MOBILEDepMap.get(MOBILE), 1);
							}
						}
					} else {
						Map depMap = new HashMap();
						depMap.put("depName", MOBILEDepMap.get(MOBILE));//手机获得部门信息
						depMap.put("count", 1);
						mobileMap.put("depMap", depMap);

						if (depMap.get("depName") != null) {
							if (depCountMap.containsKey(MOBILEDepMap.get(MOBILE))) {
								int depCount = (Integer) depCountMap.get(depMap.get("depName"));
								depCountMap.put(depMap.get("depName"), depCount + 1);
							} else {
								depCountMap.put(MOBILEDepMap.get(MOBILE), 1);
							}
						}
					}
				}
			}
		}

		Iterator itDep = depCountMap.keySet().iterator();
		while (itDep.hasNext()) {
			String depKey    = (String) itDep.next();
			Map    returnMap = new HashMap();
			returnMap.put("depName", depKey);
			returnMap.put("sumCount", depCountMap.get(depKey));
			returnList.add(returnMap);
		}

		Collections.sort(returnList, new Comparator() {

			boolean ASC_order = false;

			@Override
			public int compare(Object o1, Object o2) {
				Map<String, Object> map1    = (Map<String, Object>) o1;
				Map<String, Object> map2    = (Map<String, Object>) o2;
				Integer             intMap1 = (Integer) map1.get("sumCount");
				Integer             intMap2 = (Integer) map2.get("sumCount");
				return ASC_order ? intMap1.compareTo(intMap2) : intMap2.compareTo(intMap1);
			}
		});

		return returnList;
	}

}
