package org.fouram.service;

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

import org.apache.commons.lang3.StringUtils;
import org.fouram.core.base.service.BaseService;
import org.fouram.core.plugin.weixin.cp.util.WXCpMessageUtil;
import org.fouram.core.plugin.weixin.cp.util.WXCpUserUtil;
import org.fouram.core.util.DateUtil;
import org.fouram.core.util.LoggerUtil;
import org.fouram.entity.SapUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import me.chanjar.weixin.cp.bean.WxCpUser;

@Service
public class TaskService extends BaseService {

	@Autowired
	private SapOrgService sapOrgService;
	@Autowired
	private SapUserService sapUserService;

	@Transactional
	public void syncWeixinUser() throws Exception {
		LoggerUtil.info("同步开始->" + DateUtil.getTime());
		// 更新所有数据删除
		sapUserService.updateAllDelete();

		// 获取SAP所有用户数据
		List<SapUser> getSapUsers = sapUserService.getSapUsers();

		// 根据机构获取对应企业微信id，同时更新企业微信对应部门名称
		Map<String, Long> orgWxDepartMap = sapOrgService.updateAllWxDepartId();

		LoggerUtil.info("接口获取所有有效数据->" + getSapUsers.size());

		// 遍历用户，企业微信用户没有，新增用户并且同步企业微信；数据已有情况，数据未发生变化的，更新为未删除；数据发生变化的，同步微信用户，更新为未删除
		for (int i = 0; i < getSapUsers.size(); i++) {
			SapUser sapUser = getSapUsers.get(i);
			sapUser.setFirstOrgWxDepartId(orgWxDepartMap.get(sapUser.getFirstOrgId()));
			sapUser.setSecondOrgWxDepartId(orgWxDepartMap.get(sapUser.getSecondOrgId()));
			sapUser.setThirdOrgWxDepartId(orgWxDepartMap.get(sapUser.getThirdOrgId()));
			sapUser.setFourthOrgWxDepartId(orgWxDepartMap.get(sapUser.getFourthOrgId()));
			sapUser.setSortNumber(i + 1);
			SapUser sapUserDb = sapUserService.selectByUserId(sapUser.getUserId());
			if (sapUserDb == null) {
				sapUserService.saveOneAndSyncWX(sapUser);
			} else {
				if (!isEquals(sapUserDb, sapUser)) {
					sapUserService.updateOneAndSyncWX(sapUser);
				} else {
					sapUserService.updateEnable(sapUser.getUserId());
				}
			}
		}

		// 更新用户头像
		List<SapUser> successUsers = sapUserService.selectSuccessList();
		for (SapUser sapUser : successUsers) {
			WxCpUser user = WXCpUserUtil.getByIdOrEmail(sapUser.getPersonId(), sapUser.getEmail());
			if (user != null && StringUtils.isNotBlank(user.getAvatar())) {
				sapUser.setAvatar(user.getAvatar());
				sapUserService.updateAvatar(sapUser);
			}
		}

		// 查询禁用企业微信账号
		List<SapUser> unableSapUsers = sapUserService.selectDeleteList();
		LoggerUtil.info("SAP禁用数据->" + unableSapUsers.size());
		if (unableSapUsers.size() > 0) {
			WXCpMessageUtil.sendQuitUserMsg("离职用户"+unableSapUsers.size()+"个，请确认");
		}

		// 部分数据插入微信未成功，处理数据后，重新插入
		List<SapUser> unSyncSapUsers = sapUserService.selectUnSyncList();
		LoggerUtil.info("部分未成功插入数据->" + unSyncSapUsers.size());
		for (SapUser sapUser : unSyncSapUsers) {
			sapUserService.updateOneAndSyncWX(sapUser);
		}
		LoggerUtil.info("同步结束->" + DateUtil.getTime());
	}

	private boolean isEquals(SapUser sapUserDb, SapUser sapUser) {
		if (sapUserDb.toCompareString().equals(sapUser.toCompareString())) {
			return true;
		}
		return false;
	}
}
