package com.qmth.jkserver.controller.system;

import com.qmth.jkserver.core.annotation.LoginValidate;
import com.qmth.jkserver.enums.LoginStatus;
import com.qmth.jkserver.model.User;
import com.qmth.jkserver.service.UserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户管理
 *
 * @author tianjun
 */
@Api(tags = "用户管理controller")
@RestController
@RequestMapping("/system/user")
@Validated
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object addUser(User user) throws Exception {
        return userService.saveService(user);
    }

    @RequestMapping("/deleteById")
    public Object deleteUser(String id) {
        userService.deleteById(Long.parseLong(id));
        return "success";
    }

    @RequestMapping("/findById")
    public Object findUserById(String id) {
        return userService.findById(Long.parseLong(id));
    }

    @RequestMapping("/update")
    public Object updateUser(User user) throws Exception {
        return userService.saveService(user);
    }

    @RequestMapping("/pageQuery")
    public Object findAll(
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) int pageNo,
            @RequestParam(required = false) int pageSize) {
        User searchCondition = new User();
        if (StringUtils.isNotEmpty(userName)) {
            searchCondition.setUserName(userName);
        }
        return userService.findAll(searchCondition, pageNo, pageSize);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @LoginValidate(status = LoginStatus.IGNORE)
    public Object login(String userName, String password) throws Exception {
        return userService.login(userName, password);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @LoginValidate(status = LoginStatus.IGNORE)
    public Object logout(HttpServletRequest request) {
        userService.logout(request);
        return "success";
    }

    @RequestMapping(value = "/superResetPwd", method = RequestMethod.POST)
    public Object superResetPwd(HttpServletRequest request, String userName, String password, Long schoolId) throws Exception {
        userService.superResetPwd(request, userName, password, schoolId);
        return true;
    }

    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public Object resetPwd(HttpServletRequest request, String oldPwd, String newPwd) throws Exception {
        return userService.resetPwd(request, oldPwd, newPwd);
    }

    @RequestMapping(value = "/userLog", method = RequestMethod.POST)
    public Object lastTimeUserLog(HttpServletRequest request) throws Exception {
        return userService.queryLastTimeLoginUser(request);
    }
}
