package com.xmomen.module.account.web.controller;

import javax.validation.Valid;

import com.xmomen.module.account.model.CreateUser;
import com.xmomen.module.account.mapper.UserMapper;
import com.xmomen.module.account.model.User;
import com.xmomen.module.account.service.UserService;
import com.xmomen.module.account.web.controller.vo.CreateUserVo;
import com.xmomen.module.account.web.controller.vo.UpdateUserVo;
import com.xmomen.module.user.entity.SysUsers;
import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.framework.web.exceptions.ArgumentValidException;
import com.xmomen.module.logger.Log;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.xmomen.framework.mybatis.dao.MybatisDao;

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


@RestController
public class UserController {

    // 注入UserService
    @Autowired
    UserService userService;

    // 注入UserMapper
    @Autowired
    UserMapper userMapper;

    // 注入MybatisDao
    @Autowired
    MybatisDao mybatisDao;

    // 用户列表
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @Log(actionName = "查询用户列表")
    public Page<User> getUserList(@RequestParam(value = "limit") Integer limit,
                                  @RequestParam(value = "offset") Integer offset,
                                  @RequestParam(value = "id", required = false) Integer id,
                                  @RequestParam(value = "keyword", required = false) String keyword,
                                  @RequestParam(value = "organizationId",required = false) Integer organizationId){
        // 创建一个Map，用于存储查询条件
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("id", id);
        map.put("keyword", keyword);
        map.put("organizationId", organizationId);
        // 调用MybatisDao的selectPage方法，查询用户列表
        return (Page<User>) mybatisDao.selectPage(UserMapper.UserMapperNameSpace + "getUsers", map, limit, offset);
    }

    // 用户列表
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    @Log(actionName = "查询用户")
    public SysUsers getUserList(@PathVariable(value = "id") Integer id){
        // 调用MybatisDao的selectByPrimaryKey方法，查询用户
        return mybatisDao.selectByPrimaryKey(SysUsers.class, id);
    }

    // 新增用户
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @Log(actionName = "新增用户")
    public SysUsers createUser(@RequestBody @Valid CreateUserVo createUser, BindingResult bindingResult) throws ArgumentValidException {
        // 验证参数是否合法
        if(bindingResult != null && bindingResult.hasErrors()){
            throw new ArgumentValidException(bindingResult);
        }
        // 创建一个CreateUser对象，用于存储用户信息
        CreateUser user = new CreateUser();
        user.setAge(createUser.getAge());
        user.setOfficeTel(createUser.getOfficeTel());
        user.setPhoneNumber(createUser.getPhoneNumber());
        user.setQq(createUser.getQq());
        user.setRealname(createUser.getRealName());
        user.setSex(createUser.getSex());
        user.setUsername(createUser.getUsername());
        user.setPassword(createUser.getPassword());
        user.setEmail(createUser.getEmail());
        user.setLocked(createUser.getLocked() != null && createUser.getLocked() == true ? true : false);
        user.setOrganizationId(createUser.getOrganizationId());
        user.setUserGroupIds(createUser.getUserGroupIds());
        // 调用UserService的createUser方法，新增用户
        return userService.createUser(user);
    }

    // 更新用户
    @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
    @Log(actionName = "更新用户")
    public void updateUser(@PathVariable(value = "id") Integer id,
                           @RequestBody @Valid UpdateUserVo updateUserVo, BindingResult bindingResult) throws ArgumentValidException {
        // 验证参数是否合法
        if(bindingResult != null && bindingResult.hasErrors()){
            throw new ArgumentValidException(bindingResult);
        }
        // 调用UserService的updateUser方法，更新用户
        userService.updateUser(updateUserVo);
    }

    // 删除用户
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    @Log(actionName = "删除用户")
    public void deleteUser(@PathVariable(value = "id") Long id){
        // 调用MybatisDao的deleteByPrimaryKey方法，删除用户
        mybatisDao.deleteByPrimaryKey(SysUsers.class, id);
    }

    // 锁定用户
    @RequestMapping(value = "/user/{id}/locked", method = RequestMethod.PUT)
    @Log(actionName = "修改用户信息")
    public void lockedUser(@PathVariable(value = "id") Integer id,
                           @RequestParam(value = "locked") Boolean locked){
        // 创建一个SysUsers对象，用于存储用户信息
        SysUsers sysUsers = new SysUsers();
        sysUsers.setLocked(locked ? 1 : 0);
        sysUsers.setId(id);
        // 调用MybatisDao的update方法，更新用户信息
        mybatisDao.update(sysUsers);
    }
    
    // 重置密码
    @RequestMapping(value = "/user/{id}/resetPassword", method = RequestMethod.PUT)
    @Log(actionName = "重置密码")
    public void resetPassword(@PathVariable(value = "id") Integer id){
        // 调用UserService的changePassword方法，重置密码
        userService.changePassword(id, "123456");
    }

}
