package com.palmnest.service.estateserve.impl;

import com.palmnest.application.core.util.Sort;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.output.enterprise.VirOutDTO;
import com.palmnest.dao.estateserve.NeigouDao;
import com.palmnest.model.InPuchaseUser;
import com.palmnest.model.estateserve.WxPublicno;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.service.estateserve.NeigouManager;
import com.palmnest.service.estateserve.PublicNoManager;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.impl.GenericManagerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author hxt
 * @Description TODO
 * @Date 2018/12/21 15:28
 **/
@Service(value = "neigouManager")
public class NeigouManagerImpl extends GenericManagerImpl<Object, Long> implements NeigouManager {

	private Logger logger = LoggerFactory.getLogger(NeigouManagerImpl.class);

	private NeigouDao neigouDao;

	@Autowired
	public void setNeigouDao(NeigouDao neigouDao) {
		this.neigouDao = neigouDao;
		dao = neigouDao;
	}

	@Autowired
	private ScWxuserManager scWxuserManager;
	@Autowired
	private PublicNoManager publicNoManager;

	@Override
	public InPuchaseUser getPuchaseUser(String openId, String developId, String mobile) {
		return neigouDao.getPuchaseUser(openId, developId, mobile);
	}

	@Override
	public List<Map<String, Object>> getVirListByUser(InPuchaseUser puchaseUser) {

		// 根据developId查询公众号信息
		WxPublicno publicno = publicNoManager.getByDevelopId(puchaseUser.getDevelopId());
		// 根据公众号id和用户手机号查询该用户在此公众号下的企业列表
		List<Map<String, Object>> virList = neigouDao.getVirListByUser(publicno.getId(), puchaseUser.getMobile());

		// 此处进行数据校验;如果查询到的列表为空(可能白名单被禁用了),则需要将该条登录信息删除
		if (virList == null || virList.size() < 1) {
			neigouDao.remove(puchaseUser);
			return null;
		}
		return virList;
	}

	@Override
	public void neigouLogin(String openId, String developId, String mobile) throws Exception {
		// 登录成功后更新该公众号下的登录信息(因为有可能存在不同的openId登录同一手机号的情况)
		InPuchaseUser user = neigouDao.getPuchaseUser(null, developId, mobile);
		if (user != null) {
			user.setOpenId(openId);
			neigouDao.updateObj(user);
		} else {
			user = new InPuchaseUser();
			user.setMobile(mobile);
			user.setDevelopId(developId);
			user.setOpenId(openId);
			user.setIsEnable(1);
			WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);
			user.setWxUserId(wxUser.getId());
			user.setCreateTime(new Date());
			neigouDao.save(user);
		}
	}

	@Override
	public void quitNeigouLogin(String openId) {
		neigouDao.quitNeigouLogin(openId);
	}

	@Override
	public void quitNeigouLogin(String openId, String mobile) {
		// 登录成功后 清除同一openid的所有记录;
		neigouDao.quitNeigouLogin(openId);
		// 清空同一手机号的所有记录;
		neigouDao.removeByMobile(mobile);
	}

	@Override
	public List<Map<String, Object>> getCompanyListByMobile(String userMobile, String virName) {

		// 企业列表
		List<Map<String, Object>> companyList = neigouDao.getCompanyListByMobile(userMobile, virName);
		if (Objects.isNull(companyList))
			return null;

		List<VirOutDTO> list = new ArrayList<VirOutDTO>();
		// 对象数据封装
		for (Map<String, Object> companyMap : companyList) {
			// 校验当前企业是否上架企业购,没上架,则跳过
			Boolean result = neigouDao.checkIsPutaway(Integer.parseInt(companyMap.get("orgId").toString()), "QIYENEIGOU");
			if (!result) {
				continue;
			}
			VirOutDTO dto = new VirOutDTO();
			dto.setOrgId(companyMap.get("orgId") == null ? null : Integer.parseInt(companyMap.get("orgId").toString()));
			dto.setOrgName(companyMap.get("orgName") == null ? null : companyMap.get("orgName").toString());
			dto.setVirId(companyMap.get("virId") == null ? null : Integer.parseInt(companyMap.get("virId").toString()));
			dto.setVirName(companyMap.get("virName") == null ? null : companyMap.get("virName").toString());
			dto.setUserMobile(companyMap.get("userMobile") == null ? null : companyMap.get("userMobile").toString());
			dto.setUserName(companyMap.get("userName") == null ? null : companyMap.get("userName").toString());
			dto.setUserId(companyMap.get("userId") == null ? null : companyMap.get("userId").toString());
			list.add(dto);
		}

		Sort sort = new Sort();
		Map<String, List<VirOutDTO>> listMap = sort.sortCompany(list);

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		if (!listMap.isEmpty()) {
			for (Map.Entry<String, List<VirOutDTO>> set : listMap.entrySet()) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("key", set.getKey());
				map.put("data", set.getValue());
				resultList.add(map);
			}
		}

		return resultList;
	}

	/**
	 * 商城那边调用的时候 同时 记录数据
	 *
	 * @param openId
	 * @param developId
	 * @param mobile
	 * @throws Exception
	 */
	@Override
	public void newNeigouLogin(String openId, String developId, String mobile) throws Exception {

		// 登录成功后 清除同一openid的所有记录;
		neigouDao.quitNeigouLogin(openId);

		// 清空同一手机号的所有记录;
		neigouDao.removeByMobile(mobile);

		InPuchaseUser user = new InPuchaseUser();

		user.setMobile(mobile);
		user.setDevelopId(developId);
		user.setOpenId(openId);
		user.setIsEnable(1);
		user.setCreateTime(new Date());

		neigouDao.save(user);
	}

	@Override
	public InPuchaseUser getPuchaseUser(String userMobile) {
		return neigouDao.getPuchaseUser(userMobile);
	}

}
