package org.cs.mgr.user.ctl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.service.IMgmUserService;
import org.cs.mgr.user.service.ISmsService;
import org.cs.util.AES;
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.ResponseBody;

import com.alibaba.fastjson.JSONObject;


@Controller
@RequestMapping("/rest/mgmUser")
public class MgmUserAPI extends BaseCtl{

	private Logger log = Logger.getLogger(MgmUserAPI.class);
	
	@Autowired
	public ISmsService smsService;
	
	@Autowired
	public IMgmUserService mgmUserService;
	
	@ResponseBody
	@RequestMapping("/getCode")
	public JSONObject getCode(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		
		try {
			json = smsService.getCode(map);
		} catch (Exception e) {
			log.error("获取验证码失败："+e.getMessage());
			json = getJson(0, "","获取验证码失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/checkCode")
	public JSONObject checkCode(String data){
		Map<String, String> map = getMapFromJson(data);
		
		JSONObject json = new JSONObject();
		
		try {
			json = smsService.checkCode(map.get("phone"), Integer.valueOf(map.get("type")), map.get("code"));
		} catch (Exception e) {
			log.error("获取验证码失败："+e.getMessage());
			json = getJson(0, "","获取验证码失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/loginApp")
	public JSONObject login(String data){
		MgmUser user = getBeanFromJson(data, MgmUser.class);
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = getMapFromJson(data);
			map.put("pwd", AES.Decrypt2(map.get("pwd"), AES.sKey));
			json=mgmUserService.login(map);
		} catch (Exception e) {
			log.error("登录失败:"+e.getMessage());
			json = getJson(0, "","登录失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/register")
	public JSONObject register(String data){
		MgmUser user = getBeanFromJson(data, MgmUser.class);
		
		JSONObject json = new JSONObject();
		try {
			user.setPwd(AES.Decrypt2(user.getPwd(), AES.sKey));
			json = mgmUserService.register(user);
			if(json.getIntValue("ret")==1) 
				json.getJSONObject("data").put("messages", 1);
		} catch (Exception e) {
			log.error("注册失败:"+e.getMessage());
			json = getJson(0, "","注册失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/forgetPWD")
	public JSONObject forgetPWD(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		try {
			map.put("type", "2");
			map.put("pwd", AES.Decrypt2(map.get("pwd").toString(), AES.sKey));
			map.put("oldPwd",  AES.Decrypt2(map.get("oldPwd").toString(), AES.sKey));
			json = mgmUserService.forgetPwd(map);
		} catch (Exception e) {
			log.error("忘记密码修改失败："+e.getMessage());
			json = getJson(0, "","密码修改失败");
		}
		return json;
	}
	
	
	@ResponseBody
	@RequestMapping("/idnumberAuthentication")
	public JSONObject idnumberAuthentication(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		try {
			json = mgmUserService.register(map);
		} catch (Exception e) {
			log.error("身份认证失败："+e.getMessage());
			json = getJson(0, "","身份认证失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/phoneBinding")
	public JSONObject phoneBinding(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		try {
			map.put("pwd",  AES.Decrypt2(map.get("pwd"), AES.sKey));
			json = mgmUserService.phoneBinding(map);
		} catch (Exception e) {
			log.error("绑定失败："+e.getMessage());
			json = getJson(0, "","绑定失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/updateUserinfo")
	public JSONObject updateUserinfo(String data){
		MgmUser user = getBeanFromJson(data, MgmUser.class);
		JSONObject json = new JSONObject();
		try {
			json=mgmUserService.updateUserInfo(user);
		} catch (Exception e) {
			log.error("修改用户信息失败："+e.getMessage());
			json = getJson(0, "","修改用户信息失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/getUserInfo")
	public JSONObject getUserinfo(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		try {
			json=mgmUserService.getUserInfo(map);
		} catch (Exception e) {
			log.error("获取用户信息失败："+e.getMessage());
			json = getJson(0, "", "获取用户信息失败");
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/getUserQRCode")
	public JSONObject getUserQRCode(HttpServletRequest request,String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject(); 
		try {
			json=mgmUserService.getUserQRCode(request, map); 
		} catch (Exception e) {
			log.error("获取二维码失败："+e.getMessage());
			json = getJson(0, "", "获取二维码失败");
		}
		return json;
	}
}
