package com.jkcredit.invoice.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.annotation.annotationdes.AuthenticationInterceptor;
import com.jkcredit.invoice.model.entity.user.User;
import com.jkcredit.invoice.model.vo.UserVo;
import com.jkcredit.invoice.service.user.UserService;
import com.jkcredit.invoice.util.Md5Util;
import com.jkcredit.invoice.util.RespR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @description:
 * @author: sunzhaoning
 * @create: 2019-05-29 09:14
 * @version: V1.0
 **/
@Api(tags = "用户操作")
@RestController
@RequestMapping(value = {"/user"})
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * 通过ID查询用户信息
     *
     * @param id ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询用户信息", notes = "通过ID查询用户信息")
    @LoginRequired
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用id", required = true, dataType = "Integer")
    })
    public RespR user(@PathVariable @NotNull(message = "用户id不能为空")Integer id, UserVo userVo) {
        User user = userVo.getUserFromUserVo();
        if (!AuthenticationInterceptor.AUTH_ADMIN.equals(user.getRoleId()) && (user.getId().compareTo(id) != 0)) {
            return new RespR<>(false, "无权限");
        }
        return new RespR<>(userService.selectUserById(id));
    }

    /**
     * 删除用户信息
     *
     * @param id ID
     * @return R
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据用户名id删除用户信息", notes = "根据用户名id删除用户信息")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR userDel(@PathVariable @NotNull(message = "用户id不能为空") Integer id) {
        if(null == id){
            return new RespR<>(false,"根据用户名id删除用户信息失败，请检查参数是否维护准确");
        }
        User sysUser = userService.getById(id);
        return new RespR<>(userService.deleteUserById(sysUser));
    }

    /**
     * 添加用户
     *
     * @param userVo 用户信息
     * @return success/false
     */
    @PostMapping
    @ApiOperation(value = "新增用户详细信息", notes = "新增用户详细信息")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR user(@RequestBody @Validated UserVo userVo) {
        User user = userVo.getUserFromUserVo();
        user.setPassword(Md5Util.encrypt(user.getPassword()));
        User user1 = userService.selectUserByUserName(user.getUserName());
        if (null != user1) {
            return new RespR<>(Boolean.FALSE, "该用户已存在");
        }
        return new RespR<>(userService.saveUser(user));
    }

    /**
     * 更新用户信息
     *
     * @param userVo 用户信息
     * @return R
     */
    @PostMapping("/updateUser")
    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR updateUser(@RequestBody @Validated UserVo userVo) {
        User user = userVo.getUserFromUserVo();
        if (user.getPassword() != null) {
            user.setPassword(Md5Util.encrypt(user.getPassword()));
        }
        return new RespR<>(userService.updateUserInfo(user));
    }

    /**
     * 分页查询用户
     *
     * @param page 参数集
     * @param userVo 查询参数列表
     * @return 用户集合
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询用户", notes = "分页查询用户")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR getUserPage(Page page, UserVo userVo) {
        User user = userVo.getUserFromUserVo();
        return new RespR<>(userService.getUserWithRolePage(page, user));
    }

    /**
     * 重置密码
     *
     * @param userVo user
     * @return success/false
     */
    @PutMapping("/restPassword")
    @ApiOperation(value = "重置密码", notes = "重置密码")
    public RespR updateUserInfoPassWord(@RequestBody @Validated({Update.class}) UserVo userVo) {
        User user = userVo.getUserFromUserVo();
        user.setPassword(Md5Util.encrypt(user.getPassword()));
        return userService.restPassword(user);
    }

    /**
     * 修改锁定状态
     *
     * @param userVo user
     * @return success/false
     */
    @PutMapping("/lock")
    @ApiOperation(value = "修改锁定状态", notes = "修改锁定状态")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR updateLock(@RequestBody @Validated UserVo userVo) {
        User user = userVo.getUserFromUserVo();
        return userService.updateLock(user);
    }
}
