package org.cs.mgr.admin.ctl;

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

import org.apache.log4j.Logger;
import org.cs.constants.Constants;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.admin.model.Admin;
import org.cs.mgr.admin.service.IAdminService;
import org.cs.mgr.admin.service.IMenuService;
import org.cs.mgr.admin.service.IRoleService;
import org.cs.mgr.product.model.MgmProductCategory;
import org.cs.mgr.product.service.IMgmProductCategoryService;
import org.cs.util.Md5;
import org.cs.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/mgr")
public class LoginCtl extends BaseCtl{

	private  Logger log = Logger.getLogger(LoginCtl.class);
	
	private static String LOGIN = "redirect:/login.jsp";
	private static String LOGIN_SUCCESS = "redirect:/mgr/main.cs";
	
	@Autowired 
	private IMenuService menuService;
	
	@Autowired 
	private IAdminService adminService;
	
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private IMgmProductCategoryService mgmProductCategoryService;
	
	@RequestMapping("index")
	public String index(){
		return "index";
	}

	@RequestMapping("loginin")
	public String loginin(){
		return "login";
	}

	@RequestMapping("login")
	public String login(String account, String pwd, String code,  ModelMap mm){
		
		
		if(StringUtil.isBlank(account, pwd)){
			log.error("登录信息错误");
			return  LOGIN;
		}
		
		Admin user = this.adminService.getByAccount(account);
		
		if(user == null){
			log.error("账户信息不存在");
			return  LOGIN;
		}
		
		if(user.getPwd().equals(Md5.getMd5(pwd.trim() + account.trim()))){
			this.set(Constants.SESSION_USER, user);   // 用户信息
			//用户角色
			Map<String, String> map = new HashMap<>();
			map.put("id", user.getCode());
			Map<String, Object> role = this.roleService.findRoleByUserId(map);
			this.set(Constants.USER_ROLE, role);//FOUR_CATEGORY_ID
			if(role.get("ONE_CATEGORY_ID") != null && Integer.valueOf(role.get("ONE_CATEGORY_ID").toString()).intValue()!=0){
				map.put("id", role.get("ONE_CATEGORY_ID").toString());
				this.set(Constants.TWOCATEGORY, this.mgmProductCategoryService.queryShowCategory(map));
			}else{
				this.set(Constants.TWOCATEGORY, new ArrayList<MgmProductCategory>());
			}
			if(role.get("TWO_CATEGORY_ID") != null && Integer.valueOf(role.get("TWO_CATEGORY_ID").toString()).intValue()!=0){
				map = new HashMap<>();
				map.put("id", role.get("TWO_CATEGORY_ID").toString());
				this.set(Constants.THREECATEGORY, this.mgmProductCategoryService.queryShowCategory(map));
			}else{
				this.set(Constants.THREECATEGORY, new ArrayList<MgmProductCategory>());
			}
			if(role.get("THREE_CATEGORY_ID") != null && Integer.valueOf(role.get("THREE_CATEGORY_ID").toString()).intValue()!=0){
				map = new HashMap<>();
				map.put("id", role.get("THREE_CATEGORY_ID").toString());
				this.set(Constants.FOURCATEGORY, this.mgmProductCategoryService.queryShowCategory(map));
			}else{
				this.set(Constants.FOURCATEGORY, new ArrayList<MgmProductCategory>());
			}

			return LOGIN_SUCCESS;
		}
		else {
			log.error("密码错误");
			return  LOGIN;
		}
		
	}
	
	
	@RequestMapping("main")
	public String main(ModelMap mm){
		
		// 初始化左侧菜单及权限信息
		Admin user = (Admin) this.get(Constants.SESSION_USER);
		
		if(user == null) {
			return "redirect:http://yx.dong-xu.com/login.jsp";
		}
		
		List<Map<String,Object>> permissions =  this.adminService.getPermissions(user.getCode());
		
		this.set(Constants.SESSION_PERMISSIONS, permissions);  //权限信息
		
		mm.put("user", user);
		mm.put("roleName", permissions.get(0).get("role_name"));
		mm.put(Constants.SESSION_PERMISSIONS, permissions);
		 return "main";
	}
	
	
	@RequestMapping("logout")
	public String login(ModelMap mm){
		
		 return "redirect:http://yx.dong-xu.com/login.jsp";
	}
	

	
	
}
