package com.palmnest.service.estateserve.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.palmnest.application.Constants;
import com.palmnest.application.bean.Page;
import com.palmnest.application.bean.Pagenation;
import com.palmnest.application.dto.output.WxPublicnoDto;
import com.palmnest.dao.estateserve.PublicNoDao;
import com.palmnest.dao.property.PropertyDao;
import com.palmnest.model.estateserve.WxPublicno;
import com.palmnest.model.estateserve.WxPublicnoOrgRelation;
import com.palmnest.service.estateserve.PublicNoManager;
import com.palmnest.service.impl.GenericManagerImpl;


@Component("publicNoManager")
public class PublicNoManagerImpl extends GenericManagerImpl<Object, Integer> implements PublicNoManager {

	private PublicNoDao publicNoDao;

	@Autowired
	public void setPublicNoDao(PublicNoDao publicNoDao) {
		this.publicNoDao = publicNoDao;
		this.dao = publicNoDao;
	}

	@Autowired
	private PropertyDao propertyDao;


	@Override
	public Pagenation<WxPublicno> queryPublicNo(int pageNo,
												List<Criterion> filterList) {
		Map<String, String> aliasMap = Collections.<String, String>emptyMap();
		List<Order> orderList = new ArrayList<Order>();
		orderList.add(Order.desc("id"));
		return publicNoDao.queryPage(pageNo, Constants.LIMIT_PAGESIZE, aliasMap, filterList, orderList, WxPublicno.class);
	}

	@Override
	public Page queryPublicNoPage(int pageNo, String whereSQL) {
		return publicNoDao.queryPublicNoPage(pageNo, whereSQL);
	}

	@Override
	public WxPublicno getWxPublicnoById(int id) {
		return publicNoDao.getWxPublicnoById(id);
	}

	@Override
	public List<WxPublicno> getAllWxPublicno() {
		return publicNoDao.getAllWxPublicno();
	}

	@Override
	public WxPublicno getByDevelopId(String developId) {
		return publicNoDao.getByDevelopId(developId);
	}

	@Override
	public List<WxPublicnoOrgRelation> getWxPublicnoOrgRelations(int publicnoId) {
		return publicNoDao.getWxPublicnoOrgRelations(publicnoId);
	}

	@Override
	public List<Map<String, Object>> getWxPubOrgRelations(int publicnoId) {
		return publicNoDao.getWxPubOrgRelations(publicnoId);
	}

	@Override
	public String saveOrUpdatePublicno(WxPublicno publicno, String[] organizationIds) {
		String retCode = "0";
		if (publicno.getId() != null) {
			retCode = "2";
			publicNoDao.updateObj(publicno);
			publicNoDao.deleteWxPublicnoOrgRelation(publicno.getId());
		} else {
			publicNoDao.saveObj(publicno);
			retCode = "1";
		}
		if (organizationIds != null && organizationIds.length > 0) {
			for (String organizationId : organizationIds) {
				WxPublicnoOrgRelation relation = new WxPublicnoOrgRelation();
				relation.setPublicnoId(publicno.getId());
				relation.setOrganizationId(Integer.parseInt(organizationId));
				publicNoDao.saveObj(relation);
			}
		}
		return retCode;
	}

	@Override
	public List<Map<String, Object>> getAllWxPublicno(String organizationIdsStr) {
		return publicNoDao.getAllWxPublicno(organizationIdsStr);
	}

	@Override
	public List<Map<String, Object>> getWxPublicnoOrgRelations(String developId) {
		return publicNoDao.getWxPublicnoOrgRelations(developId);
	}

	@Override
	public Map<String, List<Map<String, Object>>> getWxPublicnoOrgRelationsGroupByCity(
			List<Map<String, Object>> relationOrgs) {
		String organizationIdsStr = "";
		for (Map<String, Object> relation : relationOrgs) {
			organizationIdsStr += relation.get("id") + ",";
		}
		organizationIdsStr = organizationIdsStr.substring(0, organizationIdsStr.length() - 1);

		List<Map<String, Object>> organizations = propertyDao.getOrganizationList(organizationIdsStr);
		Map<String, List<Map<String, Object>>> resultMap = new HashMap<String, List<Map<String, Object>>>();
		for (Map<String, Object> map : organizations) {
			String cityName = (String) map.get("cityName");
			if (resultMap.get(cityName) == null) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				list.add(map);
				resultMap.put(cityName, list);
			} else {
				resultMap.get(cityName).add(map);
			}
		}
		return resultMap;
	}

	@Override
	public WxPublicno getByApp(String appId, String appSecret) {
		return publicNoDao.getByApp(appId, appSecret);
	}

	@Override
	public List<WxPublicno> getAllWxPublicnoWithOuterUrl() {
		return publicNoDao.getAllWxPublicnoWithOuterUrl();
	}

	@Override
	public WxPublicno getByOrgClientCode(String clientCode) {
		return publicNoDao.getByOrgClientCode(clientCode);
	}

	@Override
	public WxPublicnoDto convertToDto(WxPublicno wxPublicno) {
		return new WxPublicnoDto(wxPublicno.getId(), wxPublicno.getName(), wxPublicno.getAppId(), wxPublicno.getAppSecret(),
				wxPublicno.getWxApiKey(), wxPublicno.getMchId(), wxPublicno.getGetAccessTokenUrl(),
				wxPublicno.getSslcertPath(), wxPublicno.getSslcertPassword());
	}
}
