package com.community.huiju.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.TaUser;
import com.community.huiju.model.User;
import com.community.huiju.service.IUserService;
import com.community.huiju.vo.UserVO;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 * 物业web端用户表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2018-12-18
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "物业端用户API", description = "物业端用户API")
public class UserController extends BaseController {
	
	@Autowired
	private IUserService userService;
	
	@ApiOperation(value = "获取用户列表",nickname = "获取用户信息")
	@ApiImplicitParams({
			//@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
	})
	@RequestMapping(value = "/users",method = RequestMethod.GET)
	public ResponseBean getInfo(HttpSession session) {
		ResponseBean response = new ResponseBean();
		response.addSuccess(userService.list());
		Page a = new Page(1,10);
//		response.addSuccess(userService.page(a));
		return response;
	}

	@ApiOperation(value = "登陆", notes = "登陆")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "parameter", value = "loginName手机号;code验证码;communityId小区Id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
	})
	@RequestMapping(value = "/user/login", method = RequestMethod.POST)
	public ResponseBean login(@RequestBody String parameter, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		JSONObject jsonObject = JSONObject.parseObject(parameter);
		responseBean = userService.login(jsonObject.getString("loginName"),
				jsonObject.getInteger("communityId"),
				jsonObject.getString("code"));

		// 为 0 表示成功
		if ("0".equals(responseBean.getCode())) {
			UserVO userVO = (UserVO)responseBean.getData();
			setUserElement(session,userVO);
			userVO.setToken(session.getId());
		}

		return responseBean;
	}

	@ApiOperation(value = "获取用户信息", notes = "获取用户信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
		@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
	})
	@RequestMapping(value = "/user/info", method = RequestMethod.GET)
	public ResponseBean info(HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = userService.userInfo(userElement);
		UserVO userVO = (UserVO)responseBean.getData();
		userVO.setCommunityId(userElement.getCommunityId());
		return responseBean;
	}

	@ApiOperation(value = "注销登录", notes = "注销登录")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
	})
	@RequestMapping(value = "/user/logOut", method = RequestMethod.POST)
	public ResponseBean longOut(HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		cleanUserSession(session);
		responseBean.addSuccess("退出成功!");
		return responseBean;
	}
	@ApiOperation(value = "用户成员列表", notes = "用户成员列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "loginName：手机号, " +
					"userName：用户名,pageNum:第几页，pageSize：每页长度")
	})
	@RequestMapping(value = "/user/userList", method = RequestMethod.POST)
	public ResponseBean userList(@RequestBody String parameter,HttpSession session){
		UserElement userElement = getUserElement(session);
		ResponseBean  responseBean = userService.userList(parameter,userElement.getCommunityId());
		return responseBean;
	}

	@ApiOperation(value = "添加成员", notes = "用户成员列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "loginName：手机号,userName：姓名，menuArray[]：角色ID,remark:备注" )
	})
	@RequestMapping(value = "/user/addUser", method = RequestMethod.POST)
	public ResponseBean addUser(@RequestBody String parameter,HttpSession session){
		UserElement userElement = getUserElement(session);
		ResponseBean  responseBean = userService.addUser(parameter,userElement.getId(),userElement.getCommunityId());
		return responseBean;
	}

	@ApiOperation(value = "修改成员", notes = "修改成员")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "loginName：手机号,userName：姓名，menuArray[]：角色ID,remark:备注，id：修改id" )
	})
	@RequestMapping(value = "/user/upDate", method = RequestMethod.POST)
	public ResponseBean upDate(@RequestBody String parameter,HttpSession session){
		UserElement userElement = getUserElement(session);
		ResponseBean  responseBean = userService.upDate(parameter,userElement.getId(),userElement.getCommunityId());
		return responseBean;
	}

	@ApiOperation(value = "查看成员详情", notes = "查看成员详情")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value="id:当前id")
	})
	@RequestMapping(value = "/user/selectById", method = RequestMethod.POST)
	public ResponseBean selectById(@RequestBody String parameter,HttpSession session){
		UserElement userElement = getUserElement(session);
		ResponseBean  responseBean = userService.selectById(parameter,userElement.getId());
		return responseBean;
	}

	@ApiOperation(value = "修改当前状态", notes = "修改当前状态")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value=("status:传入当前状态(0:无效，1：有效),id:当前修改的id"))
	})
	@RequestMapping(value = "/user/upDateStatus", method = RequestMethod.POST)
	public ResponseBean upDateStatus(@RequestBody String parameter,HttpSession session){
		UserElement userElement = getUserElement(session);
		ResponseBean  responseBean = userService.upDateStatus(parameter,userElement.getId());
		return responseBean;
	}

	@ApiOperation(value = "查看所有角色", notes = "查看所有角色")
	@RequestMapping(value = "/user/userRole", method = RequestMethod.POST)
	public ResponseBean userRole(@RequestBody String parameter,HttpSession session){
		UserElement userElement = getUserElement(session);
		ResponseBean  responseBean = userService.allUserRole(userElement.getCommunityId());
		return responseBean;
	}

	@ApiOperation(value = "获取令牌", notes = "获取令牌")
	@ApiImplicitParams({
			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "appid", value = "提供的appid"),
			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "datetime", value = "时间戳"),
			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "encrypt", value = "encrypt加密方式(目前值为md5)"),
			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "communityId", value = "提供的小区编号"),
			@ApiImplicitParam(dataTypeClass = String.class, paramType = "query", name = "sign", value = "生成的签名"),
	})
	@RequestMapping(value = "/user/token", method = RequestMethod.GET)
	public ResponseBean daSuToken(@RequestParam("appid") String appid,
								  @RequestParam("datetime") String datetime,
								  @RequestParam("encrypt") String encrypt,
								  @RequestParam("communityId") String communityId,
								  @RequestParam("sign") String sign,
								  HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		responseBean = userService.daSuToken(appid, datetime, encrypt, communityId, sign);
		// 为 0 表示成功
		if ("0".equals(responseBean.getCode())) {
			UserVO userVO = (UserVO)responseBean.getData();
			setUserElement(session,userVO);
			userVO.setToken(session.getId());

			Map<String, Object> map = Maps.newHashMap();
			map.put("token", session.getId());
			responseBean.addSuccess(map);
		}
		return responseBean;
	}

}
