package com.sunxung.factoring.web.sys;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.vo.SeachUser;
import com.sunxung.factoring.entity.sys.vo.SysGradationVo;
import com.sunxung.factoring.entity.weekWorkPlan.vo.SearchWeekWorkPlan;
import com.sunxung.factoring.service.sys.PermissionService;
import com.sunxung.factoring.service.sys.RoleService;
import com.sunxung.factoring.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @className:UserController
 * @description:用户信息controller
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年1月17日 下午5:42:20
 */
@Controller
public class UserController {

    private static String PREFIX = "sys/user/";
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 个人信息修改
     */
    @RequestMapping("/user/userInfoUpdate")
    public String userInfoForm(Model model) {
        User user = userService.getUserRoleRelCheck(ThreadLocalUtil.getUserId());
        model.addAttribute("user", user);
        return PREFIX + "userInfoUpdate";
    }

    /**
     * 个人信息修改
     */
    @RequestMapping(value = "/user/do_updateSimpleUser", method = RequestMethod.POST)
    @ResponseBody
    @OperationLog(operationModule = "用户信息",operationType = OperationTypeEnum.UPDATE,description = "个人信息修改")
    public Object do_userInfoUpdate(User user) {
        return userService.updateSimpleUserInfo(user);
    }

    /**
     * 修改密码
     */
    @RequestMapping("/user/updatePassword")
    public String currSetPassword(Model model) {
        model.addAttribute("userId", ThreadLocalUtil.getUserId());
        return PREFIX + "updatePassword";
    }

    @RequestMapping(value = "/user/do_updatePassword", method = RequestMethod.POST)
    @ResponseBody
    @OperationLog(operationModule = "用户信息",operationType = OperationTypeEnum.UPDATE,description = "修改密码")
    public Object do_updatePassword(Long id, String password, String oldPassword, String password1) {
        return userService.updatePassword(id, password, oldPassword, password1);
    }

    @RequestMapping("/user/isAgreement")
    @ResponseBody
    public boolean isAgreement(String oldPassword, String password) {
        if (oldPassword != null && !"".equals(oldPassword) && password != null && !"".equals(password)) {
            if (!oldPassword.equals(password)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 重置密码
     */
    @RequestMapping("/user/resetpassword")
    public String resetpasswords(Model model) {
        model.addAttribute("user", userService.get(ThreadLocalUtil.getUserId()));
        return PREFIX + "resetPassword";
    }

    /**
     * 用户管理页面
     */
    @RequestMapping("/user/list")
    public String list(Model model) {
        return PREFIX + "userList";
    }

    /**
     * 用户管理页面添加模态框
     */
    @RequestMapping("/user/add")
    public String add(User user, Model model) {
        model.addAttribute("user", user);
        model.addAttribute("roles", roleService.findAll());
        return PREFIX + "userAdd";
    }

    /**
     * 用户管理修改模态框
     */
    @RequestMapping("/user/update")
    public String update(Long id, Model model) {
        User user = userService.getUserRoleRelCheck(id);
        model.addAttribute("user", user);
        return PREFIX + "userUpdate";
    }

    @RequestMapping(value = "/user/do_list", method = RequestMethod.GET)
    @ResponseBody
    public GridPage<User> do_list(SeachUser search) {
        return userService.findBySearch(search);
    }

    @RequestMapping(value = "/user/do_listByRole", method = RequestMethod.GET)
    @ResponseBody
    public GridPage<User> do_listByRole(SeachUser search) {
        return userService.findByRoleId(search);
    }

    @RequestMapping(value = "/user/do_add", method = RequestMethod.POST)
    @ResponseBody
    @OperationLog(operationModule = "用户信息",operationType = OperationTypeEnum.INSERT,description = "添加用户")
    public Object do_add(User user, HttpServletRequest request) {
        return userService.addUser(user, request);
    }

    @RequestMapping(value = "/user/do_deletes", method = RequestMethod.POST)
    @ResponseBody
    @OperationLog(operationModule = "用户信息",operationType = OperationTypeEnum.DELETE,description = "删除用户")
    public Object do_delete(String[] ids, Model model) {
        return userService.deletes(ids);
    }

    @RequestMapping(value = "/user/do_get", method = RequestMethod.GET)
    @ResponseBody
    public Object do_get(Long id, Long orgId) {
        return userService.getDetail(id, orgId);
    }

    @RequestMapping(value = "/user/do_modifyEnabled", method = RequestMethod.POST)
    @ResponseBody
    @OperationLog(operationModule = "用户信息",operationType = OperationTypeEnum.UPDATE,description = "do_modifyEnabled")
    public Object do_modifylock(String[] ids, boolean enabled) {
        return userService.modifyEnabled(ids, enabled);
    }

    @RequestMapping(value = "/user/do_resetpassword", method = RequestMethod.POST)
    @ResponseBody
    @OperationLog(operationModule = "用户信息",operationType = OperationTypeEnum.UPDATE,description = "重置密码")
    public Object do_resetpassword(User user) {
        return userService.resetPassword(user);
    }

    @RequestMapping(value = "/user/do_update", method = RequestMethod.POST)
    @ResponseBody
    @OperationLog(operationModule = "用户信息",operationType = OperationTypeEnum.UPDATE,description = "修改用户")
    public Object do_update(User user, HttpServletRequest request) {
        return userService.update(user, request);
    }

    @RequestMapping(value = "/user/validatorLoginName", method = RequestMethod.GET)
    @ResponseBody
    public Object do_validatorLoginName(String loginName, Long userId) {
        User user = userService.validatorLoginName(loginName, userId);
        if (user != null) {
            return false;
        }
        return true;
    }

    /**
     * 展示系统用户列表
     */
    @RequestMapping("/user/showUserList")
    public String showUserList(Model model) {
        return PREFIX + "showUserList";
    }

    /**
     * 获取当前选择的部门下当前登录用户所能看到的用户（周计划查看，周例会）
     */
    @RequestMapping(value = "/user/do_getUserByOrg")
    @ResponseBody
    public Object do_getUserByOrg(SearchWeekWorkPlan search) {
        return userService.findByOrgcodeList(search);
    }

    @RequestMapping(value = "/user/do_adjustOrder", method = RequestMethod.POST)
    @ResponseBody
    public Object do_adjustOrder(SysGradationVo sysVo) {
        return userService.adjustOrder(sysVo);
    }

    @RequestMapping(value = "/user/do_adjustDepartment", method = RequestMethod.POST)
    @ResponseBody
    public Object do_adjustDepartment(User user) {
        return userService.adjustDepartment(user);
    }

    /**
     * 获取到当前登录用户的信息
     */
    @RequestMapping(value = "/user/do_getLongUser", method = RequestMethod.GET)
    @ResponseBody
    public Object do_getLongUser() {
        return ThreadLocalUtil.getUser();
    }

    @RequestMapping(value = "/user/do_permission", method = RequestMethod.GET)
    @ResponseBody
    public Object do_permission() {
        return permissionService.findAllByUserId(ThreadLocalUtil.getUserId());
    }

    /**
     * 获取用户下所有权限的ename
     *
     * @return
     */
    @RequestMapping(value = "/user/do_getAllPermissionEname", method = RequestMethod.GET)
    @ResponseBody
    public Object do_getAllPermissionEname() {
        return permissionService.getAllPermissionEname(ThreadLocalUtil.getUserId());
    }

    /**
     *
     * @param roleName
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/findUserByRoleName")
    public ResponseJson findUserByRoleName(@RequestParam String roleName){
        List<User> userByRoleName = userService.findUserByRoleName(roleName);
        return new ResponseJson(userByRoleName);
    }
}
