package lingtao.net.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.Msg;
import lingtao.net.bean.SysUser;
import lingtao.net.service.SysUserService;
import lingtao.net.util.PublicMethods;

/**
 * 用户
 *
 */
@Controller
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;

	// 用户跳转页面
	@RequestMapping("/user/index")
	public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.sendRedirect(request.getContextPath() + "/views/system/user/user.jsp");
	}

	/**
	 * 根据条件查询用户
	 */
	@RequestMapping("/getUserList")
	@ResponseBody
	private Msg userList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, SysUser user) {
		PageHelper.startPage(page, limit);
		List<SysUser> list = sysUserService.getUsers(user);
		PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(list);
		return Msg.success().add("userList", pageInfo);
	}

	/**
	 * 检查用户名是否存在
	 */
	@ResponseBody
	@RequestMapping("/checkUsername")
	public Msg checkUsername(@RequestParam(value = "username") String username) {
		return sysUserService.checkUsername(username);
	}

	/**
	 * 添加用户
	 */
	@ResponseBody
	@RequestMapping("/addUser")
	public Msg addUser(SysUser user) {
		sysUserService.addUser(user);
		if (StringUtils.isNotEmpty(user.getRole())) {
			addUserRoles(user);
		}
		return Msg.success();
	}

	/**
	 * 修改用户
	 */
	@ResponseBody
	@RequestMapping("/updateUser")
	public Msg updateUserById(SysUser user) {
		sysUserService.updateUserById(user);
		sysUserService.deleteUserRoles(user.getUserId());
		if (StringUtils.isNotEmpty(user.getRole())) {
			addUserRoles(user);
		}
		return Msg.success();
	}

	/**
	 * 删除用户
	 */
	@RequestMapping("/deleteUserById")
	@ResponseBody
	public Msg deleteUserById(@RequestParam("id") Integer userId) {
		return sysUserService.deleteUserById(userId);
	}

	/**
	 * 根据用户编号更改角色
	 * 
	 * @param user
	 */
	public void addUserRoles(SysUser user) {
		String[] array = user.getRole().split(",");
		Integer[] ids = new Integer[array.length];
		for (int i = 0; i < array.length; i++) {
			ids[i] = Integer.parseInt(array[i]);
		}
		Integer userId = user.getUserId();
		sysUserService.addUserRoles(userId, ids);
	}

	/**
	 * 改变用户状态
	 */
	@ResponseBody
	@RequestMapping("/changeUserStatus")
	public Msg changeUserStatus(@RequestParam(value = "userId") Integer userId) {
		return sysUserService.changeUserStatus(userId);
	}

	/**
	 * 改变用户系统状态
	 */
	@ResponseBody
	@RequestMapping("/changeSysStatus")
	public Msg changeSysStatus(@RequestParam(value = "userId") Integer userId) {
		return sysUserService.changeSysStatus(userId);
	}

	/**
	 * 改变所有用户的Ip状态
	 */
	@ResponseBody
	@RequestMapping("/changeNeedIp")
	public Msg changeNeedIp() {
		return sysUserService.changeNeedIp();
	}

	/**
	 * 改变用户阅读日志状态
	 */
	@ResponseBody
	@RequestMapping("/changeReadLogStatus")
	public Msg changeReadLogStatus(HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		sysUserService.changeReadLogStatus(user.getUserId());
		return Msg.success();
	}

	/**
	 * 获取用户信息
	 * 
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getUserInfo")
	public Msg getUserInfo(HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		SysUser userInfo = sysUserService.getUserInfo(user.getUserId());
		if (userInfo != null) {
			return Msg.success().add("userInfo", userInfo);
		}
		return Msg.fail();
	}

	/**
	 * 修改密码
	 * 
	 */
	@ResponseBody
	@RequestMapping("/updatePassword")
	public Msg updatePassword(SysUser user) {
		return sysUserService.updatePassword(user);
	}

	/**
	 * 补充生日
	 * 
	 */
	@ResponseBody
	@RequestMapping("/addBirthDay")
	public Msg addBirthDay(SysUser user) {
		return sysUserService.addBirthDay(user);
	}

	/**
	 * 改变用户生日状态
	 */
	@ResponseBody
	@RequestMapping("/changeIsBirthDay")
	public Msg changeIsBirthDay(HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		sysUserService.changeIsBirthDay(user.getUserId());
		return Msg.success();
	}

	/**
	 * 从session中取出登录用户到信息
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loginAttribute")
	public Msg loginAttribute(HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		if (user != null) {
			return Msg.success().add("user", user);
		}
		return Msg.fail();
	}

	/**
	 * 客服数据 -- 根据搜索的店铺获取人员
	 * 
	 * @return
	 */
	@RequestMapping("/getRealnamesByShopname")
	@ResponseBody
	public Map<String, String> getRealnamesByShopname(@RequestParam(value = "shopname") String shopname,
			HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		SysUser user = (SysUser) request.getSession().getAttribute("USER_SESSION");
		// 没有选择店铺 没有超管或者 没有店铺身份
		if (StringUtils.isEmpty(shopname) && (!new PublicMethods().isSuper() && !user.getRole().contains("777"))) {
			return null;
		}
		List<SysUser> realnameList = sysUserService.getRealnamesByShopname(shopname);

		for (SysUser sysUser : realnameList) {
			map.put(sysUser.getRealname(), sysUser.getRealname());
		}

		return map;
	}

}
