package com.goafanti.user.controller;

import java.math.BigDecimal;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.enums.IdentityAuditStatus;
import com.goafanti.common.enums.IdentityProcess;
import com.goafanti.common.enums.OrgProFields;
import com.goafanti.common.enums.OrganizationTechFields;
import com.goafanti.common.enums.UserAbilityFields;
import com.goafanti.common.enums.UserAuthentication;
import com.goafanti.common.enums.UserCareerFields;
import com.goafanti.common.enums.UserEduFields;
import com.goafanti.common.enums.UserIdentityFields;
import com.goafanti.common.enums.UserType;
import com.goafanti.common.model.OrganizationIdentity;
import com.goafanti.common.model.OrganizationTech;
import com.goafanti.common.model.User;
import com.goafanti.common.model.UserAbility;
import com.goafanti.common.model.UserCareer;
import com.goafanti.common.model.UserEdu;
import com.goafanti.common.model.UserIdentity;
import com.goafanti.common.utils.PasswordUtil;
import com.goafanti.common.utils.TimeUtils;
import com.goafanti.common.utils.VerifyCodeUtils;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.easemob.EasemobUtils;
import com.goafanti.user.bo.InputOrgPro;
import com.goafanti.user.bo.InputOrganizationIdentity;
import com.goafanti.user.bo.InputOrganizationTech;
import com.goafanti.user.bo.InputUserAbility;
import com.goafanti.user.bo.InputUserCareer;
import com.goafanti.user.bo.InputUserEdu;
import com.goafanti.user.bo.InputUserIdentity;
import com.goafanti.user.bo.OrgIdentityBo;
import com.goafanti.user.bo.UserContactBo;
import com.goafanti.user.service.OrganizationIdentityService;
import com.goafanti.user.service.OrganizationTechService;
import com.goafanti.user.service.UserAbilityService;
import com.goafanti.user.service.UserCareerService;
import com.goafanti.user.service.UserContactService;
import com.goafanti.user.service.UserEduService;
import com.goafanti.user.service.UserIdentityService;
import com.goafanti.user.service.UserService;

@RestController
@RequestMapping(value = "/api/user")
public class UserApiController extends BaseApiController {
	@Resource
	private UserService						userService;
	@Resource(name = "passwordUtil")
	private PasswordUtil					passwordUtil;
	@Resource
	private UserIdentityService				userIdentityService;
	@Resource
	private UserEduService					userEduService;
	@Resource
	private UserCareerService				userCareerService;
	@Resource
	private UserAbilityService				userAbilityService;
	@Resource
	private OrganizationIdentityService		organizationIdentityService;
	@Resource
	private OrganizationTechService			organizationTechService;
	@Resource
	private UserContactService 				userContactService;
	@Autowired
	private EasemobUtils					easemobUtils;

	private static final Integer			STEP_ONE			= 1;

	// private static final Integer STEP_TWO = 2;

	private static final Integer			STEP_THREE			= 3;

	// private static final Integer STEP_FOUR = 4;

	private static final Integer			STEP_FIVE			= 5;

	private static final Integer			MAX_WRONG_COUNT		= 3;

	private static final Integer			INIT_WRONG_COUNT	= 0;

	/**
	 * 修改密码
	 * 
	 * @param password
	 * @param newPassword
	 * @return
	 */
	@RequestMapping(value = "/pwd", method = RequestMethod.POST)
	public Result updatePwd(String password, String newPassword) {
		Result res = new Result();
		if (StringUtils.isBlank(password) || StringUtils.isBlank(newPassword)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "必须指定", "密码"));
		}
		if (password.equals(newPassword)) {
			res.getError().add(buildError(ErrorConstants.PWD_SAME_ERROR, "新密码必须与原密码不同!"));
		}

		if (res.getError().isEmpty() && TokenManager.isLogin()) {
			User u = new User();
			User user = userService.selectByPrimaryKey(TokenManager.getUserId());
			u.setId(user.getId());
			u.setCreateTime(user.getCreateTime());
			u.setPassword(newPassword);
			u.setPassword(passwordUtil.getEncryptPwd(u));
			if (!user.getPassword().equals(passwordUtil.getEncryptPwd(password, user.getCreateTime()))) {
				res.getError().add(buildError(ErrorConstants.PWD_NOT_MATCH_ERROR));
			} else {
				res.setData(userService.updateByPrimaryKeySelective(u));
			}
		}
		return res;
	}

	/**
	 * 更换手机
	 * 
	 * @param mobile
	 * @param mobileCode
	 * @return
	 */
	@RequestMapping(value = "/mobile", method = RequestMethod.POST)
	public Result updateMobile(String mobile, String mobileCode) {
		Result res = new Result();
		if (StringUtils.isBlank(mobile)) {
			res.getError().add(buildError(ErrorConstants.MOBILE_EMPTY_ERROR, "手机号不能为空!"));
			return res;
		}

		if (StringUtils.isBlank(mobileCode)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "手机验证码"));
			return res;
		}
		// 验证码15分钟有效
		if (TimeUtils.checkOverTime("register")) {
			res.getError().add(buildError(ErrorConstants.MCODE_OVERTIME_ERROR, "手机验证码超时失效"));
			return res;
		}
		if (!TokenManager.getSession().getAttribute(VerifyCodeUtils.M_CODE).equals(mobileCode)) {
			res.getError().add(buildError(ErrorConstants.MCODE_ERROR, "手机验证码错误"));
			return res;
		}
		User user = userService.selectByPrimaryKey(TokenManager.getUserId());
		if (mobile.equals(user.getMobile())) {
			res.getError().add(buildError(ErrorConstants.MOBILE_SAME_ERROR));
			return res;
		}
		User u = userService.selectByMobieAndType(mobile, user.getType());
		if (null != u) {
			res.getError().add(buildError(ErrorConstants.DUNPLICATE_KAY_ERROR, "", mobile));
			return res;
		}
		if (res.getError().isEmpty()) {
			user.setMobile(mobile);
			res.setData(userService.updateByPrimaryKeySelective(user));
		}
		return res;
	}

	

	/**
	 * 公共
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/avatar/upload", method = RequestMethod.POST)
	public Result avatar(HttpServletRequest req) {
		Result res = new Result();
		res.setData(handleFile(res, "/avatar/", false, req, ""));
		return res;
	}

	/**
	 * 公共可替换
	 */
	@RequestMapping(value = "/avatar/uploadReplace", method = RequestMethod.POST)
	public Result avatarReplace(HttpServletRequest req, String sign) {
		Result res = new Result();
		res.setData(handleFile(res, "/avatar/", false, req, sign));
		return res;
	}

	/**
	 * 认证
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/identity/upload", method = RequestMethod.POST)
	public Result identityFile(HttpServletRequest req, String sign) {
		Result res = new Result();
		res.setData(handleFile(res, "/identity/", true, req, sign));
		return res;
	}

	/**
	 * 个人资料
	 * 
	 * @param userInfoBo
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "/userInfo", method = RequestMethod.POST)
	public Result personalData(BindingResult bindingResult) {
		Result res = new Result();
		
		return res;
	}

	/**
	 * 个人会员教育信息
	 * 
	 * @param userEdu
	 * @return
	 */
	@RequestMapping(value = "/userEdu", method = RequestMethod.POST)
	public Result educationInfo(@Valid InputUserEdu userEdu, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					UserEduFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		UserEdu ue = new UserEdu();
		BeanUtils.copyProperties(userEdu, ue);
		UserEdu u = userEduService.selectUserEduByUserId(TokenManager.getUserId());
		if (null == u && StringUtils.isBlank(ue.getId())) {
			ue.setId(UUID.randomUUID().toString());
			ue.setUid(TokenManager.getUserId());
			userEduService.insert(ue);
		} else {
			if (StringUtils.isBlank(ue.getId())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "教育信息ID"));
				return res;
			}
			userEduService.updateByPrimaryKeySelective(ue);
		}
		res.setData(userEdu);
		return res;
	}

	/**
	 * 个人会员职业信息
	 * 
	 * @param userCareer
	 * @return
	 */

	@RequestMapping(value = "/userCareer", method = RequestMethod.POST)
	public Result careerInfo(@Valid InputUserCareer userCareer, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					UserCareerFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}
		UserCareer uc = new UserCareer();
		BeanUtils.copyProperties(userCareer, uc);
		UserCareer u = userCareerService.selectUserCareerByUserId(TokenManager.getUserId());
		if (null == u && StringUtils.isBlank(uc.getId())) {
			uc.setId(UUID.randomUUID().toString());
			uc.setUid(TokenManager.getUserId());
			userCareerService.insert(uc);
		} else {
			if (StringUtils.isBlank(uc.getId())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "职业信息ID"));
				return res;
			}
			userCareerService.updateByPrimaryKeySelective(uc);
		}
		res.setData(userCareer);
		return res;
	}

	/**
	 * 会员能力标签
	 * 
	 * @param userAbility
	 * @return
	 */

	@RequestMapping(value = "/userAbility", method = RequestMethod.POST)
	public Result abilityLabel(@Valid InputUserAbility userAbility, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					UserAbilityFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}
		UserAbility ua = new UserAbility();
		BeanUtils.copyProperties(userAbility, ua);
		UserAbility u = userAbilityService.selectUserAbilityByUserId(TokenManager.getUserId());
		if (null == u && StringUtils.isBlank(ua.getId())) {
			ua.setId(UUID.randomUUID().toString());
			ua.setUid(TokenManager.getUserId());
			userAbilityService.insert(ua);
		} else {
			if (StringUtils.isBlank(ua.getId())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "能力标签ID"));
				return res;
			}
			userAbilityService.updateByPrimaryKeySelective(ua);
		}
		res.setData(userAbility);
		return res;
	}

	/**
	 * 团体会员单位资料
	 * 
	 * @param orgInfo
	 * @return
	 */
	@RequestMapping(value = "/orgPro", method = RequestMethod.POST)
	public Result groupProInfo(@Valid InputOrgPro pro, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					OrgProFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		if (StringUtils.isBlank(pro.getCompanyName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到单位名称", "单位名称"));
			return res;
		}
		return res;
	}

	/**
	 * 团体会员技术能力
	 * 
	 * @param orgTech
	 * @return
	 */
	@RequestMapping(value = "/orgTech", method = RequestMethod.POST)
	public Result tech(@Valid InputOrganizationTech orgTech, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					OrganizationTechFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		OrganizationTech ot = new OrganizationTech();
		BeanUtils.copyProperties(orgTech, ot);
		OrganizationTech o = organizationTechService.selectOrgTechByUserId(TokenManager.getUserId());
		if (null == o && StringUtils.isBlank(ot.getId())) {
			ot.setId(UUID.randomUUID().toString());
			ot.setUid(TokenManager.getUserId());
			organizationTechService.insert(ot);

		} else {
			organizationTechService.updateByPrimaryKeySelective(ot);
		}
		res.setData(orgTech);
		return res;
	}

	/**
	 * 个人会员基本信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getUserDetail", method = RequestMethod.GET)
	public Result getUserDetail() {
		Result res = new Result();
		res.setData(userIdentityService.selectUserIdentityBoByUserId(TokenManager.getUserId()));
		return res;
	}
	
	/**
	 * 个人会员基本信息
	 * 
	 * @param userIdentity
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "/updateUserDetail", method = RequestMethod.POST)
	public Result updateUserDetail(@Valid InputUserIdentity userIdentity, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					UserIdentityFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}
		if(StringUtils.isBlank(userIdentity.getUid())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "用户ID"));
			return res;
		}
		if(userIdentity.getAuthentication().intValue() == UserAuthentication.AUTHENTICATION.getCode().intValue()
				&& userIdentity.getType().intValue() == UserType.ORGANIZATION.getCode().intValue()){
			res.getError().add(buildError(null, "", "用户已经企业认证"));
			return res;
		}else if(userIdentity.getAuthentication().intValue() == UserAuthentication.UN_AUTHENTICATION.getCode().intValue()){
			userIdentity.setType(UserType.PERSONAL.getCode());
			userIdentity.setAuthentication(UserAuthentication.AUTHENTICATION.getCode());
		}
		if (res.getError().isEmpty()) {
			UserIdentity ui = new UserIdentity();
			User user = new User();
			BeanUtils.copyProperties(userIdentity, ui);
			BeanUtils.copyProperties(userIdentity, user);
			UserIdentity u = userIdentityService.selectUserIdentityByUserId(TokenManager.getUserId());
			if (null == u) {
				ui.setId(UUID.randomUUID().toString());
				ui.setUid(TokenManager.getUserId());
				userIdentityService.insert(ui);
			} else {
				ui.setId(u.getId());
				userIdentityService.updateByPrimaryKeySelective(ui);
			}
			user.setId(TokenManager.getUserId());
			userService.updateByPrimaryKeySelective(user);
		}
		res.setData(userIdentity);
		return res;
	}
	
	/**
	 * 企业信息详情
	 * @return
	 */
	@RequestMapping(value = "/getOrganizationDetail", method = RequestMethod.GET)
	public Result getOrganizationDetail(){
		Result res = new Result();
		OrgIdentityBo o = organizationIdentityService.selectOrgIdentityBoByUserId(TokenManager.getUserId());
		res.setData(o);
		return res;
	}
	
	/**
	 * 修改企业信息
	 * 
	 * @param userIdentity
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "/updateOrganizationDetail", method = RequestMethod.POST)
	public Result updateOrganizationDetail(@Valid InputOrganizationIdentity organizationIdentity, BindingResult bindingResult) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					UserIdentityFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}
		if(StringUtils.isBlank(organizationIdentity.getUid())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "用户ID"));
			return res;
		}
		if(organizationIdentity.getAuthentication() == UserAuthentication.AUTHENTICATION.getCode()
				&& organizationIdentity.getType() == UserType.PERSONAL.getCode()){
			res.getError().add(buildError(null, "", "用户已经个人认证"));
			return res;
		}else if(organizationIdentity.getAuthentication() == UserAuthentication.UN_AUTHENTICATION.getCode()){
			organizationIdentity.setType(UserType.ORGANIZATION.getCode());
			organizationIdentity.setAuthentication(UserAuthentication.AUTHENTICATION.getCode());
		}
		if (res.getError().isEmpty()) {
			OrganizationIdentity oi = new OrganizationIdentity();
			User user = new User();
			BeanUtils.copyProperties(organizationIdentity, oi);
			BeanUtils.copyProperties(organizationIdentity, user);
			OrganizationIdentity u = organizationIdentityService.selectOrgIdentityByUserId(TokenManager.getUserId());
			if (null == u) {
				oi.setId(UUID.randomUUID().toString());
				oi.setUid(TokenManager.getUserId());
				organizationIdentityService.insert(oi);
			} else {
				oi.setId(u.getId());
				organizationIdentityService.updateByPrimaryKeySelective(oi);
			}
			user.setId(TokenManager.getUserId());
			userService.updateByPrimaryKeySelective(user);
		}
		res.setData(organizationIdentity);
		return res;
	}
	
	/**
	 * 个人资料/团体会员资料
	 */
	@RequestMapping(value = "/member", method = RequestMethod.GET)
	public Result member() {
		Result res = new Result();
		
		return res;
	}

	/**
	 * 个人会员教育信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/educate", method = RequestMethod.GET)
	public Result educate() {
		Result res = new Result();
		UserEdu u = userEduService.selectUserEduByUserId(TokenManager.getUserId());
		res.setData(u);
		return res;
	}

	/**
	 * 个人会员职业信息/团体会员技术能力
	 * 
	 * @return
	 */
	@RequestMapping(value = "/job", method = RequestMethod.GET)
	public Result job() {
		Result res = new Result();
		if (UserType.PERSONAL.getCode().equals(TokenManager.getUserType())) {
			UserCareer u = userCareerService.selectUserCareerByUserId(TokenManager.getUserId());
			res.setData(u);
		} else {
			OrganizationTech o = organizationTechService.selectOrgTechByUserId(TokenManager.getUserId());
			res.setData(o);
		}
		return res;
	}

	/**
	 * 个人/团体能力标签
	 * 
	 * @return
	 */
	@RequestMapping(value = "/ability", method = RequestMethod.GET)
	public Result ability() {
		Result res = new Result();
		UserAbility u = userAbilityService.selectUserAbilityByUserId(TokenManager.getUserId());
		res.setData(u);
		return res;
	}

	/**
	 * 获取昵称
	 * 
	 * @return
	 */
	@RequestMapping(value = "/nickname", method = RequestMethod.GET)
	public Result nickname() {
		User u = userService.selectByPrimaryKey(TokenManager.getUserId());
		Assert.notNull(u, "用户不存在");
		return res().data(u.getNickname());
	}

	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/homePage", method = RequestMethod.GET)
	public Result homePage() {
		Result res = new Result();
		res.setData(userService.selectUserPageHomeBoByUserId(TokenManager.getUserId()));
		return res;

	}
	
	/**
	 * 用户基本信息
	 * @return
	 */
	@RequestMapping(value = "/userBase", method = RequestMethod.GET)
	public Result userBase(){
		Result res = new Result();
		res.setData(userService.selectUserBase(TokenManager.getUserId()));
		return res;
	}

	/**
	 * 修改用户基本信息
	 * @return
	 */
	@RequestMapping(value = "/updateUserBase", method = RequestMethod.POST)
	public Result updateUserBase(String identifyName,String mobile,String email,String nickname){
		Result res = new Result();
		if(StringUtils.isBlank(identifyName) || StringUtils.isBlank(mobile)
				||StringUtils.isBlank(nickname)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"用户信息不全"));
		}
		User user = new User();
		user.setId(TokenManager.getUserId());
		user.setIdentifyName(identifyName);
		user.setMobile(mobile);
		if(StringUtils.isNotBlank(email))user.setEmail(email);
		user.setNickname(nickname);
		userService.updateByPrimaryKeySelective(user);
		return res;
	}
	
	/**
	 * 登陆后返回用户基本信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/afterSignIn", method = RequestMethod.GET)
	public Result afterSignIn() {
		Result res = new Result();
		if (TokenManager.isLogin()) {
			User u = userService.selectByPrimaryKey(TokenManager.getUserId());
			res.setData(u);
		}
		return res;
	}

	/**
	 * 个人实名认证流程入口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/userPro", method = RequestMethod.GET)
	public Result userProcess() {
		Result res = new Result();
		if (TokenManager.isLogin()) {
			res.setData(userIdentityService.selectUserIdentityByUserId(TokenManager.getUserId()));
		}
		return res;
	}

	/**
	 * 个人实名认证流程下一步
	 */
	@RequestMapping(value = "/userNextPro", method = RequestMethod.POST)
	public Result userNextProcess(UserIdentity userIdentity, String verificationCode) {
		Result res = new Result();
		if (null == userIdentity.getProcess()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "process"));
			return res;
		}

		if (STEP_ONE.equals(userIdentity.getProcess())) {
			if (StringUtils.isBlank(verificationCode)) {
				res.getError().add(buildError(ErrorConstants.VCODE_EMPTY_ERROR));
				return res;
			}
			if (!VerifyCodeUtils.verifyCode(verificationCode)) {
				res.getError().add(buildError(ErrorConstants.VCODE_ERROR));
				return res;
			}
		} else if (STEP_THREE.equals(userIdentity.getProcess())) {
			UserIdentity u = userIdentityService.selectUserIdentityByUserId(TokenManager.getUserId());
			u.setProcess(IdentityProcess.COMMITTED.getCode());
			u.setAuditStatus(IdentityAuditStatus.COMMITTED.getCode());
			userIdentityService.updateByPrimaryKeySelective(u);
			return res;
		} else if (STEP_FIVE.equals(userIdentity.getProcess())) {
			UserIdentity u = userIdentityService.selectUserIdentityByUserId(TokenManager.getUserId());
			if ((System.currentTimeMillis() - u.getPaymentDate().getTime()) > 432000000) {
				u.setAuditStatus(IdentityAuditStatus.NOTPASSED.getCode());// 4
				userIdentityService.updateByPrimaryKeySelective(u);
				res.getError().add(buildError(ErrorConstants.IDENTITY_PAY_OVER_TIME));// 超过打款日期5日，无法提交确认
				return res;
			}
			if (MAX_WRONG_COUNT.equals(u.getWrongCount())) {
				u.setAuditStatus(IdentityAuditStatus.NOTPASSED.getCode());// 4
				u.setProcess(IdentityProcess.SUCCESS.getCode());// 5
				userIdentityService.updateByPrimaryKeySelective(u);
				res.getError().add(buildError(ErrorConstants.OVER_MAX_WRONG_COUNT));// 输入错误金额次数过多
				return res;
			}
			if (0 != (u.getAmountMoney().compareTo(userIdentity.getAmountMoney()))) {
				int t = 3 - u.getWrongCount() - 1;
				u.setWrongCount(u.getWrongCount() + 1);

				if (0 == t) {
					u.setAuditStatus(IdentityAuditStatus.NOTPASSED.getCode());// 4
					u.setProcess(IdentityProcess.SUCCESS.getCode());// 5
					res.getError().add(buildError(ErrorConstants.OVER_MAX_WRONG_COUNT));// 输入错误金额次数过多
				} else {
					res.getError().add(buildError(ErrorConstants.WRONG_MONEY, "", t));
				}
				userIdentityService.updateByPrimaryKeySelective(u);
				return res;
			}
			userIdentity.setAuditStatus(IdentityAuditStatus.PASSED.getCode());
			userIdentity.setProcess(IdentityProcess.SUCCESS.getCode());
		}
		return dealUserProcess(res, userIdentity, TokenManager.getUserId());
	}


	// 认证失败清除相关认证信息
	private Result dealFaild(Result res) {
		if (UserType.PERSONAL.getCode().equals(userService.selectByPrimaryKey(TokenManager.getUserId()).getType())) {
			UserIdentity u = userIdentityService.selectUserIdentityByUserId(TokenManager.getUserId());
			UserIdentity user = new UserIdentity();
			user.setId(u.getId());
			user.setUid(u.getUid());
			user.setProcess(IdentityProcess.UNCOMMITTED.getCode());
			user.setWrongCount(INIT_WRONG_COUNT);
			user.setAmountMoney(new BigDecimal(0));
			user.setAuditStatus(IdentityAuditStatus.UNCOMMITTED.getCode());
			res.setData(userIdentityService.updateByPrimaryKey(user));
		} else {
			OrganizationIdentity o = organizationIdentityService.selectOrgIdentityByUserId(TokenManager.getUserId());
			OrganizationIdentity org = new OrganizationIdentity();
			org.setId(o.getId());
			org.setUid(o.getUid());
			org.setProcess(IdentityProcess.UNCOMMITTED.getCode());
			org.setWrongCount(INIT_WRONG_COUNT);
			org.setAuditStatus(IdentityAuditStatus.UNCOMMITTED.getCode());
			org.setValidationAmount(new BigDecimal(0));
			res.setData(organizationIdentityService.updateByPrimaryKey(org));
		}
		return res;
	}

	// orgProcess
	private Result dealOrgProcess(Result res, OrganizationIdentity o, String uid) {
		//res.setData(organizationIdentityService.saveOrgIndentityProcess(res, o, uid));
		return res;
	}

	// userProcess
	private Result dealUserProcess(Result res, UserIdentity u, String uid) {
		res.setData(userIdentityService.saveUserIdentityProcess(res, u, uid));
		return res;
	}

	// 判断用户是否通过认证
	private Result checkCertify(Result res) {
		OrganizationIdentity o = organizationIdentityService.selectOrgIdentityByUserId(TokenManager.getUserId());
		if (null == o || !IdentityAuditStatus.PASSED.getCode().equals(o.getAuditStatus())) {
			res.getError().add(buildError(ErrorConstants.NON_CERTIFIED, "未通过实名认证，无法操作！"));
		}
		return res;
	}
	
	/**
	 * 新增客户联系人
	 * @param bo
	 * @return
	 */
	@RequestMapping(value = "/addUserContcat", method = RequestMethod.POST)
	public Result addUserContcat(UserContactBo bo){
		Result res = new Result();
		if(StringUtils.isBlank(bo.getMobile())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "手机号码不能为空"));
			return res;
		}
		if(userContactService.checkUserContact(TokenManager.getUserId(), bo.getMobile())){
			res.getError().add(buildError(ErrorConstants.USER_CONTACT_ALREADY_EXIST,"联系人已存在"));
		}
		bo.setUid(TokenManager.getUserId());
		userContactService.insertSelective(bo);
		return res;
	}
	
	/**
	 * 修改客户联系人
	 * @param bo
	 * @return
	 */
	@RequestMapping(value = "/udpateUserContact", method = RequestMethod.POST)
	public Result udpateUserContact(UserContactBo bo){
		Result res = new Result();
		if(StringUtils.isBlank(bo.getMobile())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "手机号码不能为空"));
			return res;
		}
		if(StringUtils.isBlank(bo.getId())){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "联系人ID"));
			return res;
		}
		userContactService.updateByPrimaryKeySelective(bo);
		return res;
	}
	
	/**
	 * 删除客户联系人
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteUserContact", method = RequestMethod.GET)
	public Result deleteUserContact(String id){
		Result res = new Result();
		if(StringUtils.isBlank(id)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "联系人ID"));
			return res;
		}
		userContactService.deleteUserContact(id);
		return res;
	}
	
	/**
	 * 获取用户联系人
	 * @return
	 */
	@RequestMapping(value = "/getUserContacts", method = RequestMethod.GET)
	public Result getUserContacts(){
		Result res = new Result();
		res.setData(userContactService.selectContactByUid(TokenManager.getUserId()));
		return res;
	}
}
