package com.jkcredit.invoice.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.model.entity.user.User;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @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 Integer id) {
        return new RespR<>(userService.selectUserById(id));
    }

    /**
     * 删除用户信息
     *
     * @param id ID
     * @return R
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value="根据用户名id删除用户信息", notes="根据用户名id删除用户信息")
    @LoginRequired
    public RespR userDel(@PathVariable Integer id) {
        User sysUser = userService.getById(id);
        return new RespR<>(userService.deleteUserById(sysUser));
    }

    /**
     * 添加用户
     *
     * @param user 用户信息
     * @return success/false
     */
    @PostMapping
    @ApiOperation(value="新增用户详细信息", notes="新增用户详细信息")
    @LoginRequired
    public RespR user(@RequestBody User user) {
        user.setPassword(MD5Util.encrypt(user.getPassword()));
        return new RespR<>(userService.saveUser(user));
    }

    /**
     * 更新用户信息
     *
     * @param user 用户信息
     * @return R
     */
    @PostMapping("/updateUser")
    @ApiOperation(value="更新用户信息", notes="更新用户信息")
    @LoginRequired
    public RespR updateUser(@RequestBody User user) {
        if(user.getPassword()!=null){
            user.setPassword(MD5Util.encrypt(user.getPassword()));
        }
        return new RespR<>(userService.updateUserInfo(user));
    }
    /**
     * 分页查询用户
     *
     * @param page    参数集
     * @param user 查询参数列表
     * @return 用户集合
     */
    @PostMapping("/page")
    @ApiOperation(value="分页查询用户", notes="分页查询用户")
    @LoginRequired
    public RespR getUserPage(Page page, User user) {
        return new RespR<>(userService.getUserWithRolePage(page, user));
    }

    /**
     * 重置密码
     *
     * @param user user
     * @return success/false
     */
    @PutMapping("/restPassword")
    @ApiOperation(value="重置密码", notes="重置密码")
    @LoginRequired
    public RespR updateUserInfoPassWord(@RequestBody User user) {
        user.setPassword(MD5Util.encrypt(user.getPassword()));
        return userService.restPassword(user);
    }

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