package org.cs.mgr.user.ctl;

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.user.dao.IMgmUserBankDao;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.model.MgmUserBank;
import org.cs.mgr.user.service.IMgmUserBankService;
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: MgmUser 
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:55
 */
@Controller
@RequestMapping("/mgmUserCtl")
public class MgmUserCtl extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IMgmUserService mgmUserService;
	
	@Autowired
	private IMgmUserBankService mgmUserBankService;
	
	@Autowired
	private IMgmUserBankDao mgmUserBankDao;
	
	@RequestMapping("list")
	public String list(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		this.set(MENU_CODE, menuCode);
		return "mgr/user/list";	
	}
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String list2(int page, int pageSize, String search){
		Map<String, String> map = getMapFromJson(search);
		Pager pager = this.mgmUserService.findMgmUser(new Pager(page, pageSize),map);
		JSONObject json = new JSONObject();
		json.put("rows", pager.getResults()); 
		json.put("total", pager.getTotal());
		return JSONObject.toJSONString(json, features);
	
	}
	
	@RequestMapping("transfer")
	public String transfer(Integer id, String action, ModelMap mm){
		MgmUser mgmUser = null;
		if(id != null){
			mgmUser = this.mgmUserService.findById(id);
			mm.put("mgmUser", mgmUser);
			
			MgmUserBank userBank=null;
			if(mgmUser!=null){
				userBank=this.mgmUserBankDao.getByPropertie(id.toString(), "userId");
				mm.put("mgmUserBank", userBank);
			}
		}
		return "mgr/user/" + action;
	}
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(MgmUser mgmUser){
		if(mgmUser != null ){
			this.mgmUserService.addMgmUser(mgmUser);
		}
		return AJAX_SUCCESS;
	}

    @RequestMapping(value="edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(MgmUser mgmUser){
        if(mgmUser != null ){
            this.mgmUserService.updateMgmUser(mgmUser);
        }
        return AJAX_SUCCESS;
    }
	
	@RequestMapping("del")
	@ResponseBody
	public String del(Integer id){
		if(id != null){
			this.mgmUserService.delMgmUser(id);
		}
		
		return AJAX_SUCCESS;
	}
 
	/**
	 * 
	 * <p>登录页面</p>
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "ajaxLogin", method = { RequestMethod.POST })
	public JSONObject login(HttpServletRequest request, 
			@RequestParam("username") String username, 
			@RequestParam("password") String password) throws Exception{ 
		try {
			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());
				//是否首次登陆
				if(user.get("FIRST_LOGIN").equals("0") && user.get("TYPE").toString().equals("0")){
					request.setAttribute("errorMsg", "首次登录成功,请绑定手机号并重置密码"); 
					return getJson(0, "app/login/phone_bang", "请输入用户名和密码！");
				}
				//登录成功。
				request.getSession().setAttribute(Constants.SESSION_WEB_USER, user);
				return getJson(0, "app/login/phone_bang", "请输入用户名和密码！");
			}else{
				request.setAttribute("errorMsg", json.getString("msg"));
				return getJson(0, "app/login/login", "请输入用户名和密码！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return getJson(0, "app/login/login", "请输入用户名和密码！");
	}
}

