package org.cs.web.user;

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

import javax.servlet.http.HttpServletRequest;

import org.cs.constants.Constants;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.product.service.IMgmProductService;
import org.cs.mgr.setting.service.ISysAdvService;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.service.IMgmMessageService;
import org.cs.mgr.user.service.IMgmUserService;
import org.cs.util.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/**
 * 
 * @ClassName: MgmUser 
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:55
 */
@Controller
@RequestMapping("web/mgmUserCtl")
public class MgmUserWebCtl extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IMgmUserService mgmUserService;
	@Autowired
	private IMgmProductService mgmProductService;
	@Autowired
	private ISysAdvService sysAdvService;
	@Autowired 
	public IMgmMessageService mgmMessageService;
	/**
	 * 
	 * <p>主页</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request,ModelMap mm){
		Map<String, String> user = null;
		try{
			user = (Map<String, String>)request.getSession().getAttribute(Constants.SESSION_WEB_USER);
		}catch (Exception e){
			e.printStackTrace();
		}
		int count=mgmMessageService.countMgmMessage(user.get("id").toString());
		mm.put("MESSAGE_COUNT", count);
		mm.put("USER", user);
		
		Map<String, String> map = new HashMap<>();
		
		map.put("userType",user.get("type"));		
		map.put("ismore", "2");
		JSONObject object = this.mgmProductService.categoryProduct(map);
		String ret = object.getString("ret");
		JSONObject data = new JSONObject();
		if("1".equals(ret)){
			data = object.getJSONObject("data");
			mm.put("category", data.get("category"));
			mm.put("pager", data.get("pager"));
		}else if("0".equals(ret)){
			data = object.getJSONObject("data");
			mm.put("category", new JSONArray());
			mm.put("pager", new JSONArray());
		}
		//获取轮播图
		Pager pageData = sysAdvService.getSysAdvs(new Pager(1));
		mm.put("imglist", pageData.getResults());
		return "app/index";	
	}
	
	
	/**
	 * 
	 * <p>身份证认证页面</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("info_personal")
	public String list( @RequestParam(required=false)String userId,@RequestParam(required=false)String phone,ModelMap mm){
		mm.put("USER_ID", userId);
		mm.put("PHONE", phone);
		return "app/login/info_personal";	
	}
	
	/**
	 * 
	 * <p>注册页面</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("register")
	public String register( @RequestParam(required=false)String code,ModelMap mm){
		mm.put("CODE", code);
		return "app/login/register";	
	}
	
	/**
	 * 
	 * <p>手机绑定页面</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("phone_bang")
	public String phone_bang( @RequestParam(required=false)String userId,@RequestParam(required=false)String phone,ModelMap mm){
		mm.put("USER_ID", userId);
		mm.put("PHONE", phone);
		return "app/login/phone_bang";	
	}
	
	 
	/**
	 * 
	 * <p>登录页面</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "ajaxLogin", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public JSONObject login(HttpServletRequest request,@RequestParam("username") String username, 
			@RequestParam("password") String password) throws Exception{ 
			if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
				request.setAttribute("errorMsg", "请输入用户名和密码！");
				return getJson(0, null, "请输入用户名和密码！");
			}
			 
			MgmUser appuser = new MgmUser();
			appuser.setPhone(username);
			appuser.setPwd(password);
			Map<String, String> map=new HashMap<String, String>();
			map.put("phone", username);
			map.put("pwd",password);
			JSONObject json = mgmUserService.login(map);			
			if (json.getIntValue("ret") == 1){
				Map<String, String> user = getMapFromJson(json.getJSONObject("data").toString());
				request.getSession().setAttribute(Constants.SESSION_WEB_USER, user);
				return getJson(1, user, "成功");
			}else{
				return getJson(0, null, json.getString("msg"));
			}
	}
	
	/**
	 * 
	 * <p>Discription:[退出登录]</p>
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/logout", method = { RequestMethod.GET, RequestMethod.POST })
	public String logout(HttpServletRequest request)
	{
		request.getSession().removeAttribute(Constants.SESSION_WEB_USER);
		return "app/login/login";
	}
	
	/**
	 * 
	 * <p>注册界面</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "register", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public JSONObject register(HttpServletRequest request,
			@RequestParam("data") String data ) throws Exception{ 
			MgmUser user = getBeanFromJson(data, MgmUser.class);
			JSONObject json=new JSONObject();
			try {
				json = mgmUserService.register(user);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
	}
	
	/**
	 * 
	 * <p>身份证认证页面</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "idnumberAuthentication", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public JSONObject r(HttpServletRequest request,
			@RequestParam("userId") String userId, 
			@RequestParam("name") String name,
			@RequestParam("idnumber") String idnumber) throws Exception{ 
			if ( StringUtils.isEmpty(name) || StringUtils.isEmpty(idnumber)){
				request.setAttribute("errorMsg", "请输入姓名身份证！");
				return getJson(0, null, "请输入姓名身份证！");
			}
			Map<String, String> map=new HashMap<String, String>();
			map.put("userId", userId);
			map.put("name", name);
			map.put("idnumber", idnumber);
			JSONObject json = mgmUserService.register(map);
			Map<String, String> user = getMapFromJson(json.getJSONObject("data").toString());
			request.getSession().setAttribute(Constants.SESSION_WEB_USER, user);
			return json;
	}
	
	/**
	 * 
	 * <p>手机绑定</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "phoneBinding", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public JSONObject phoneBinding(HttpServletRequest request,
			@RequestParam("data") String data ) throws Exception{ 
		    Map<String, String> map = getMapFromJson(data);
			JSONObject json = mgmUserService.phoneBinding(map);
			return json;
	}
	/**
	 * 
	 * @Title: forgetPWD   
	 * @Description: 忘记密码
	 * @param: @param data
	 * @param: @return      
	 * @return: JSONObject      
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("/forgetPWD")
	public JSONObject forgetPWD(String data){
		Map<String, String> map = getMapFromJson(data);
		JSONObject json = new JSONObject();
		
		try {
			map.put("type", "2");
			json = mgmUserService.forgetPwd(map);
		} catch (Exception e) {
			json = getJson(0, "","密码修改失败");
		}
		return json;
	}
	 
}

