package org.fouram.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.fouram.constants.WebConstants;
import org.fouram.core.base.model.PageData;
import org.fouram.core.base.service.BaseService;
import org.fouram.core.base.service.RequestService;
import org.fouram.core.plugin.weixin.cp.util.WXCpUserUtil;
import org.fouram.core.util.Base64Util;
import org.fouram.core.util.JSONUtil;
import org.fouram.core.util.JsonNodeUtil;
import org.fouram.core.util.Tools;
import org.fouram.entity.SapOrg;
import org.fouram.entity.SapUser;
import org.fouram.enums.DelFlagEnum;
import org.fouram.enums.SapUserResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;

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

@Service
public class SapUserService extends BaseService {

	@Autowired
	private SapOrgService sapOrgService;
	@Autowired
	private RequestService requestService;
	@Autowired
	private WxDepartService wxDepartService;
	@Autowired
	private SapEmpJobService sapEmpJobService;
	@Autowired
	private SapOprLogService sapOprLogService;
	@Autowired
	private SapPersonalService sapPersonalService;

	private static boolean isTestWXSync = true;
	private static boolean isTestMobile = false;

	public void saveOrUpdateSapUsers() throws Exception {
		HttpHeaders headers = getHttpHeaders();
		String userUrl = "https://api15.sapsf.cn/odata/v2/User?$filter=status in 'active'&$format=json&$expand=personKeyNav";
		// get
		JsonNode node = requestService.getForObject(userUrl, headers);
		List<SapUser> sapUsers = Lists.newArrayList();
		if (node != null) {
			Iterator<JsonNode> iterator = node.get("d").get("results").elements();
			while (iterator.hasNext()) {
				JsonNode userNode = iterator.next();
				String userId = JsonNodeUtil.getValue(userNode, "userId");
				// 去除非员工账号和测试数据
				if (!userId.startsWith("Test")) {
					String username = JsonNodeUtil.getValue(userNode, "username");
					String name = JsonNodeUtil.getValue(userNode, "lastName")
							+ JsonNodeUtil.getValue(userNode, "firstName");
					String mobile = JsonNodeUtil.getValue(userNode, "cellPhone");
					String businessPhone = JsonNodeUtil.getValue(userNode, "businessPhone");
					String email = JsonNodeUtil.getValue(userNode, "email");
					String personId = JsonNodeUtil.getValue(userNode, "personKeyNav", "personIdExternal");
					// 手机号码脱敏
					if (isTestMobile) {
						if (StringUtils.isNotBlank(mobile) && mobile.length() == 11) {
							mobile = mobile.substring(0, 8) + "999";
						} else {
							mobile = "";
						}
					}
					SapUser sapUser = SapUser.builder().userId(userId).personId(personId).username(username).name(name)
							.mobile(mobile).formatMobile(mobile).businessPhone(businessPhone)
							.formatBusinessPhone(formatBusinessPhone(businessPhone)).email(email).formatEmail(email)
							.isPrimary(userId.equals(personId)).build();
					sapUsers.add(sapUser);
				}
			}
		}
		// 获取用户对应机构
		sapOrgService.setOrg(sapUsers);
		// 获取用户对应性别
		sapPersonalService.setGender(sapUsers);
		// 获取用户对应职务
		sapEmpJobService.setPosition(sapUsers);
		// 更新用户
		SapUser sapUserDb = null;
		SapUser sapUser = null;
		for (int i = 0; i < sapUsers.size(); i++) {
			sapUser = sapUsers.get(i);
			sapUser.setSortNumber(i + 1);
			sapUserDb = this.selectByUserId(sapUser.getUserId());
			if (sapUserDb == null) {
				this.saveSapUser(sapUser);
			} else {
				if (sapUserDb.toString().equals(sapUser.toString())) {
					// 用户数据没变，result保留SUCCESS，不用重复同步
					this.updateDelFlag(sapUser.getUserId(), DelFlagEnum.ENABLE);
				} else {
					this.updateSapUser(sapUser);
				}
			}
		}
	}

	public void updateResultByCondition() throws Exception {
		List<SapUser> normalList = selectListByResult(SapUserResultEnum.NORMAL.getCode());
		for (SapUser sapUser : normalList) {
			// ENTRY("ENTRY", "入职")
			if (sapUser.getFirstOrgWxDepartId() == null) {
				sapUser.setResult(SapUserResultEnum.ENTRY.getCode());
				this.updateResult(sapUser);
				// SENSITIVE("SENSITIVE", "敏感数据")
			} else if (checkSensitiveDataIsChange(sapUser)) {
				sapUser.setResult(SapUserResultEnum.SENSITIVE.getCode());
				this.updateResult(sapUser);
			}
		}
	}

	
	private boolean checkSensitiveDataIsChange(SapUser sapUser) {
		return false;
	}

	public void saveSapUser(SapUser entity) throws Exception {
		entity.setCreateDate(new Date());
		entity.setResult(SapUserResultEnum.NORMAL.getCode());
		entity.setDelFlag(DelFlagEnum.ENABLE.getCode());
		save("SapUserMapper.saveOne", entity);
	}

	public void updateSapUser(SapUser entity) throws Exception {
		entity.setResult(SapUserResultEnum.NORMAL.getCode());
		entity.setDelFlag(DelFlagEnum.ENABLE.getCode());
		update("SapUserMapper.updateOne", entity);
	}

	public void updateSyncWxResult(String personId) throws Exception {
		String result = createOrUpdateWXUser(personId);
		SapUser entity = SapUser.builder().personId(personId).result(result).build();
		update("SapUserMapper.updateResult", entity);
	}

	private String createOrUpdateWXUser(String personId) throws Exception {
		// 测试
		if (isTestWXSync) {
			return "TEST-" + SapUserResultEnum.SUCCESS.getCode();
		}
		List<SapUser> sapUsers = selectListByPersonId(personId);
		SapUser wxSapUser = sapUsers.get(0);
		Long[] firstOrgWxDepartIdList = new Long[sapUsers.size()];
		Long[] secondOrgWxDepartIdList = new Long[sapUsers.size()];
		Long[] thirdOrgWxDepartIdList = new Long[sapUsers.size()];
		Long[] fourthOrgWxDepartIdList = new Long[sapUsers.size()];
		for (int i = 0; i < sapUsers.size(); i++) {
			SapUser sapUser = sapUsers.get(i);
			String checkResult = checkSapUser(sapUser);
			if (StringUtils.isNotBlank(checkResult)) {
				return checkResult;
			}
			firstOrgWxDepartIdList[i] = sapUser.getFirstOrgWxDepartId();
			secondOrgWxDepartIdList[i] = sapUser.getSecondOrgWxDepartId();
			thirdOrgWxDepartIdList[i] = sapUser.getThirdOrgWxDepartId();
			fourthOrgWxDepartIdList[i] = sapUser.getFourthOrgWxDepartId();
		}
		return WXCpUserUtil.createOrUpdate(personId, wxSapUser.getName(), firstOrgWxDepartIdList,
				secondOrgWxDepartIdList, thirdOrgWxDepartIdList, fourthOrgWxDepartIdList, wxSapUser.getFormatPosition(),
				wxSapUser.getFormatMobile(), wxSapUser.getGender(), wxSapUser.getUsername(),
				wxSapUser.getFormatBusinessPhone(), wxSapUser.getFirstOrgName());
	}

	/**
	 * 判断是不是垃圾数据
	 * 
	 * @param sapUser
	 * @return
	 */
	public String checkSapUser(SapUser sapUser) {
		if (Tools.isEmpty(sapUser.getPersonId()) || Tools.isEmpty(sapUser.getFirstOrgName())
				|| WebConstants.INGORE_PERSONIDS.contains("," + sapUser.getPersonId() + ",")) {
			return SapUserResultEnum.GARBAGE.getCode();
		}
		return null;
	}

	public SapUser selectByUserId(String userId) throws Exception {
		return (SapUser) findObject("SapUserMapper.selectByUserId", userId);
	}

	@SuppressWarnings("unchecked")
	public List<SapUser> selectListByPersonId(String personId) throws Exception {
		return (List<SapUser>) findList("SapUserMapper.selectListByPersonId", personId);
	}

	@SuppressWarnings("unchecked")
	public List<SapUser> selectUnSyncList() throws Exception {
		return (List<SapUser>) findList("SapUserMapper.selectUnSyncList", null);
	}

	public void updateAllDelFlag(Integer delFlag) throws Exception {
		update("SapUserMapper.updateAllDelFlag", delFlag);
	}

	public void updateDelFlag(String userId, DelFlagEnum delFlagEnum) throws Exception {
		SapUser entity = SapUser.builder().userId(userId).delFlag(delFlagEnum.getCode()).build();
		update("SapUserMapper.updateDelFlag", entity);
	}

	public static void main(String[] args) {
		StringBuilder stringBuilder = new StringBuilder();
		String sapApiUser = "API01@cmccoltd:cmccoltd123";
		String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
		System.out.println(authorizationStr);
	}

	@SuppressWarnings("unchecked")
	public List<SapUser> selectListByName(String name) throws Exception {
		return (List<SapUser>) findList("SapUserMapper.selectListByName", name);
	}

	@SuppressWarnings("unchecked")
	public List<SapUser> selectListByResult(String result) throws Exception {
		return (List<SapUser>) findList("SapUserMapper.selectListByResult", result);
	}

	@SuppressWarnings("unchecked")
	public List<SapUser> selectListByDelFlag(Integer delFlag) throws Exception {
		return (List<SapUser>) findList("SapUserMapper.selectListByDelFlag", delFlag);
	}

	@SuppressWarnings("unchecked")
	public List<SapUser> selectListByDepartIdAndLevel(String departId) throws Exception {
		SapOrg sapOrg = sapOrgService.selectById(departId);
		if (StringUtils.isBlank(departId) || WebConstants.TOP_DEPART.toString().equals(departId)) {
			departId = WebConstants.TOP_DEPART.toString();
			sapOrg = SapOrg.builder().id(WebConstants.TOP_DEPART.toString()).sapName(WebConstants.TOP_DEPART_NAME)
					.level("1").build();
		}
		PageData pd = new PageData();
		pd.put("departId", departId);
		pd.put("level", sapOrg.getLevel());
		return (List<SapUser>) findList("SapUserMapper.selectListByDepartIdAndLevel", pd);
	}

	public SapUser toInfoDetail(SapUser sapUser) throws Exception {
		if (sapUser != null) {
			List<SapUser> users = selectListByPersonId(sapUser.getPersonId());
			List<List<SapOrg>> departList = Lists.newArrayList();
			for (int i = 0; i < users.size(); i++) {
				SapUser user = users.get(i);
				List<SapOrg> orgs = Lists.newArrayList();
				if (Tools.notEmpty(user.getFirstOrgId())) {
					orgs.add(sapOrgService.selectById(user.getFirstOrgId()));
				}
				if (Tools.notEmpty(user.getSecondOrgId())) {
					orgs.add(sapOrgService.selectById(user.getSecondOrgId()));
				}
				if (Tools.notEmpty(user.getThirdOrgId())) {
					orgs.add(sapOrgService.selectById(user.getThirdOrgId()));
				}
				if (Tools.notEmpty(user.getFourthOrgId())) {
					orgs.add(sapOrgService.selectById(user.getFourthOrgId()));
				}
				departList.add(orgs);
			}
			sapUser.setDepartList(departList);
		}
		return sapUser;
	}

	public List<SapUser> toInfoDetails(List<SapUser> sapUsers) throws Exception {
		for (SapUser sapUser : sapUsers) {
			toInfoDetail(sapUser);
		}
		return sapUsers;
	}

	public SapUser getDetailById(String userId) throws Exception {
		SapUser sapUser = selectByUserId(userId);
		return toInfoDetail(sapUser);
	}

	public void updateAvatar(SapUser sapUser) throws Exception {
		update("SapUserMapper.updateAvatar", sapUser);
	}

	public void updateResult(SapUser sapUser) throws Exception {
		update("SapUserMapper.updateResult", sapUser);
	}

	public void deleteQuitUserByUserId(String userId, String oprUserId) throws Exception {
		SapUser sapUser = selectByUserId(userId);
		String personId = sapUser.getPersonId();
		List<SapUser> sapUsers = selectListByPersonId(personId);
		for (SapUser user : sapUsers) {
			updateDelFlag(user.getUserId(), DelFlagEnum.DELETED);
		}
		WXCpUserUtil.delete(personId);
		sapOprLogService.saveOne(JSONUtil.toString(sapUsers), WebConstants.OPRTYPE_DELETE, oprUserId);
	}
}
