package com.pannk.mms.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pannk.mms.common.base.BaseController;
import com.pannk.mms.common.base.Result;
import com.pannk.mms.modules.sys.entity.SysUserEntity;
import com.pannk.mms.modules.sys.service.SysUserService;
import com.pannk.mms.modules.sys.vo.SysUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by wolf on 20-11-2.
 */
@RequestMapping("/sys/user")
@RestController
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;


    /**
     * 用户列表
     *
     * @param params 参数
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        return Result.success(sysUserService.queryPage(params));
    }


    /**
     * 当前登录用户
     *
     * @return
     */
    @GetMapping("/currentUser")
    public Result currentUser() {
        return Result.success(sysUserService.queryUserInfo(getUserId()));
    }

    /**
     * 用户信息
     *
     * @param id id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable Long id) {
        return Result.success(sysUserService.queryUserInfo(id));
    }

    /**
     * 新增用户
     *
     * @param sysUserInfo 用户
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody SysUserInfo sysUserInfo) {
        createData(sysUserInfo);
        sysUserService.saveUserInfo(sysUserInfo);
        return Result.success(sysUserInfo);
    }

    /**
     * 更新信息
     *
     * @param sysUserInfo 用户信息
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody SysUserInfo sysUserInfo) {
        updateDate(sysUserInfo);
        sysUserService.updateUserInfo(sysUserInfo);
        return Result.success(sysUserInfo);
    }

    /**
     * 删除用户
     *
     * @param id id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        sysUserService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids id数组
     * @return
     */
    @DeleteMapping("/batchDelete")
    public Result batchDelete(Long[] ids) {
        sysUserService.remove(new QueryWrapper<SysUserEntity>().in("id", ids));
        return Result.success();
    }
}
