package com.community.huiju.controller;

import com.alibaba.fastjson.JSONObject;
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.ToUser;
import com.community.huiju.service.IToUserService;
import com.community.huiju.vo.ToUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
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;

/**
 * 用户 控制器
 * @author weiximei
 */
@RestController
@RequestMapping("/")
@RefreshScope
@Api(value = "运营web用户API",description = "运营web用户API")
public class UserController extends BaseController {

    @Autowired
    private IToUserService iToUserService;

    @ApiOperation(value = "用户登录",nickname = "根据手机号, 验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "loginName登陆名, code 验证码")
    })
    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public ResponseBean login(@RequestBody String parameter, HttpSession session, HttpServletRequest request){

        ResponseBean response = new ResponseBean();

        JSONObject jsonObject = JSONObject.parseObject(parameter);
        response = iToUserService.login(jsonObject.getString("loginName"),jsonObject.getString("code"));
        ToUserVO userVO = (ToUserVO) response.getData();

        if (null != userVO) {
            setUserElement(session, userVO);
            userVO.setToken(session.getId());
        }

        return response;
    }


    @ApiOperation(value = "获取用户信息",nickname = "获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
    })
    @RequestMapping(value = "/user/info",method = RequestMethod.GET)
    public ResponseBean getInfo(HttpSession session) {
        ResponseBean response = new ResponseBean();

        UserElement userElement = getUserElement(session);
        if (null == userElement) {
            response.addError("请登录!");
            return response;
        }

        response = iToUserService.getInfo(userElement.getId());

        return response;
    }

    @ApiOperation(value = "注销登录", notes = "注销登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
    })
    @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){
        ResponseBean  responseBean = iToUserService.userList(parameter);
        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 = iToUserService.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 = iToUserService.upDate(parameter,userElement.getId());
        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 = iToUserService.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 = iToUserService.upDateStatus(parameter,userElement.getId());
        return responseBean;
    }

}
