package com.palmnest.webapp.controller.wechat;

import com.palmnest.application.Constants;
import com.palmnest.application.bean.ReturnObject;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.service.estateserve.AppUserManager;
import com.palmnest.service.property.PropertyManager;
import com.palmnest.service.wechat.WeChatApiManager;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 商城获取用户信息控制类
 *
 * @version 1.0
 * @authr xiemin
 * @created 2017-2-10 上午9:58:20
 */
@Slf4j
@Controller
@RequestMapping("/api/user/*")
public class ObtainController extends BaseFormController {

	@Autowired
	private AppUserManager   appUserManager;
	@Autowired
	private WeChatApiManager weChatApiManager;
	@Autowired
	private PropertyManager  propertyManager;

	/**
	 * 获取用户信息接口
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getUserInfo", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	ReturnObject getUserInfo(HttpServletRequest request, HttpServletResponse response) {

		ReturnObject rt         = new ReturnObject();
		String       userMobile = request.getParameter("userMobile");
		try {
			log.info("getUserInfo userMobile:" + userMobile);
			if (StringUtils.isNotBlank(userMobile)) {
				AppUser userInfo = appUserManager.getAppUsersByPhone(userMobile);
				if (userInfo != null) {
					VirtualArchitecture virtualArchitecture = propertyManager.getVirtualArchitectureById(userInfo.getVirtualArchId());
					Map<String, String> mapUserInfo         = new HashMap<String, String>();
					mapUserInfo.put("mPollCode", userInfo.getId() + "");
					mapUserInfo.put("mMobile", userInfo.getUserMobile());
					mapUserInfo.put("mAccount", userInfo.getUserMobile());
					if (StringUtils.isEmpty(userInfo.getUserCardNo())) {
						mapUserInfo.put("mIdCard", "");
					} else {
						mapUserInfo.put("mIdCard", userInfo.getUserCardNo());
					}
					mapUserInfo.put("mName", userInfo.getUserName());
					mapUserInfo.put("mCompany", userInfo.getOrganizationId() + "");
					if (null == userInfo.getVirtualArchId() || userInfo.getVirtualArchId().equals("")) {
						mapUserInfo.put("mBranchCompany", "");
					} else {
						mapUserInfo.put("mBranchCompany", userInfo.getVirtualArchId() + "");
					}
					if (StringUtils.isEmpty(virtualArchitecture.getCodeNumber())) {
						mapUserInfo.put("mECode", "");
					} else {
						mapUserInfo.put("mECode", virtualArchitecture.getCodeNumber());
					}
					if (virtualArchitecture.getId() == null || virtualArchitecture.getId().equals("")) {
						mapUserInfo.put("mEID", "");
					} else {
						mapUserInfo.put("mEID", virtualArchitecture.getId() + "");
					}
					if (StringUtils.isEmpty(userInfo.getUserCardNo())) {
						mapUserInfo.put("mIdCard", "");
					} else {
						mapUserInfo.put("mIdCard", userInfo.getUserCardNo());
					}
					mapUserInfo.put("mDepartment", "");
					mapUserInfo.put("mTeam", "");
					mapUserInfo.put("mPosition", "");
					mapUserInfo.put("mWelfareLevel", "");
					mapUserInfo.put("mCardNum", "");
					mapUserInfo.put("mJobNum", "");

					rt.setResult(Constants.RESULT_SUCC);
					rt.setResultCode(Constants.RESULTCODE_SUCC);
					rt.setResultDesc("success");
					rt.setReturnDTO(mapUserInfo);
				} else {
					rt.setResult(Constants.RESULT_FIAL);
					rt.setResultCode(Constants.RESULTCODE_NODATA);
					rt.setResultDesc("user not exist.");
					rt.setReturnDTO("");
				}
			} else {
				rt.setResult(Constants.RESULT_FIAL);
				rt.setResultCode(Constants.RESULTCODE_PARAMINVALID);
				rt.setResultDesc("please check params.");
				rt.setReturnDTO("");
			}
		} catch (Exception e) {
			e.printStackTrace();
			LogExceptionStackUtil.LogExceptionStack(e);
			rt.setResult(Constants.RESULT_FIAL);
			rt.setResultCode(Constants.RESULTCODE_EXCEPTION);
			rt.setResultDesc("server occur error.");
			rt.setReturnDTO("");
		}
		return rt;
	}

	public static void main(String args[]) throws ParseException {
		String fromIMEI      = "0000";
		long   fromTimeStamp = DateUtils.getTimeCur(DateUtils.YYYY_MM_DD_MM_HH_SS, new Date());
		String appkey        = PropertiesKit.readPropertiesValue("ClientAppKey", "app.properties");
		String syssgin       = StringUtil.getSgin(fromIMEI, appkey, fromTimeStamp + "");
		System.out.println(syssgin);
	}

}
