package com.palmnest.service.notice.impl;

import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.WxPublicnoUserDto;
import com.palmnest.dao.notice.WxMassmessageSendDao;
import com.palmnest.model.notice.WxMassmessageSendModel;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.notice.WxMassmessageSendManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("wxMassmessageSendManager")
public class WxMassmessageSendManagerImpl extends GenericManagerImpl<WxMassmessageSendModel, Integer> implements WxMassmessageSendManager {

	private WxMassmessageSendDao wxMassmessageSendDao;

	@Autowired
	public void setWxMassmessageSendDao(WxMassmessageSendDao wxMassmessageSendDao) {
		this.wxMassmessageSendDao = wxMassmessageSendDao;
		this.dao = wxMassmessageSendDao;
	}


	@Override
	public List<WxPublicnoUserDto> getUserByOrgId(String orgIds, String nameStr) {
		List<WxPublicnoUserDto>   dtoList = new ArrayList<WxPublicnoUserDto>();
		List<Map<String, Object>> list    = wxMassmessageSendDao.getUserByOrgId(orgIds, nameStr);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				if (null == map) {
					continue;
				}
				Integer           userId  = Integer.parseInt(map.get("userId").toString());
				String            openId  = map.get("openId").toString();
				String            name    = map.get("name").toString();
				String            mobile  = map.get("mobile").toString();
				Integer           orgId   = Integer.parseInt(map.get("orgId").toString());
				String            orgName = map.get("orgName").toString();
				String            developId = map.get("developId").toString();
				WxPublicnoUserDto dto     = new WxPublicnoUserDto(userId, openId, name, mobile, orgId, orgName,developId);
				dtoList.add(dto);
			}
		}
		return dtoList;
	}


	/**
	 * 获取公众号下面所有的用户
	 */
	@Override
	public List<String> getUserByDevelopid(String developid) {
		List<String>              openList = new ArrayList<String>();
		List<Map<String, Object>> list     = wxMassmessageSendDao.getUserByDevelopid(developid);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				if (null == map) {
					continue;
				}
				String openId = map.get("openId").toString();
				if (StringUtil.isNotEmpty(openId)) {
					openList.add(openId);
				}
			}
		}
		return openList;
	}

	@Override
	public List<Map<String, Object>> getByUser(String  userId) {
		return wxMassmessageSendDao.getByUser(userId);
	}
}
