package org.cs.web.me;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.cs.constants.Constants;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.order.dao.IMgmOrderCommissionDao;
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.JSONObject;


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

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IMgmUserService mgmUserService;
   
	@Autowired
	private IMgmOrderCommissionDao mgmOrderCommissionDao;
 
	@Autowired
	public IMgmMessageService mgmMessageService;
	/**
	 * 
	 * <p>我的页面</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("me")
	public String me(HttpServletRequest request,ModelMap mm){
		Map<String, String> user = (Map<String, String>)  request.getSession().getAttribute(Constants.SESSION_WEB_USER);
	    BigDecimal sum = this.mgmOrderCommissionDao.getMgmOrderCommissionNum(Integer.parseInt(user.get("id").toString()));
	    try {
	    	int count=mgmMessageService.countMgmMessage(user.get("id"));
			mm.put("MESSAGE_COUNT", count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    mm.put("USER", user);
		mm.put("SUM", sum);
		return "app/me/me";	
	}
	/**
	 * 
	 * <p>个人信息</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("mepeo_info")
	public String mepeo_info(HttpServletRequest request,@RequestParam("userId") String userId,ModelMap mm){
		Map<String, String> map=new HashMap<String, String>();
		map.put("userId", userId);
		JSONObject json=mgmUserService.getUserInfo(map);
		Map<String, String> user = getMapFromJson(json.getJSONObject("data").toString());

		mm.put("USER", user);
		return "app/me/mepeo_info";	
	}
	
	/**
	 * 
	 * <p>认证信息</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("mepeo_info2")
	public String mepeo_info2(HttpServletRequest request,@RequestParam("userId") String userId,ModelMap mm){
		Map<String, String> map=new HashMap<String, String>();
		map.put("userId", userId);
		JSONObject json=mgmUserService.getUserInfo(map);
		Map<String, String> user = getMapFromJson(json.getJSONObject("data").toString());

		mm.put("USER", user);
		return "app/me/mepeo_info2";	
	}
	
	@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) {
			
			json = getJson(0, "","修改用户信息失败");
		}
		return json;
	}
	
	
}

