package com.ytpm.middle.controller;

import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.middle.param.MiddleUserListParam;
import com.ytpm.middle.param.MiddleUserParam;
import com.ytpm.middle.service.UserService;
import com.ytpm.middle.view.MiddleUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RequestMapping("/user")
@RestController
@Api(tags = "用户管理模块")
public class UserController {

    @Resource
    private UserService userService;


    @ApiOperation("查询系统用户列表")
    @PostMapping("/queryList")
    public ResultTable<MiddleUserInfo> queryList(@RequestBody MiddleUserListParam param) {
        return userService.queryList(param);
    }

    @ApiOperation("新增用户")
    @PostMapping("/addOne")
    public Result<String> addOne(@RequestBody MiddleUserParam param, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        param.setOperatorId(userInfo.getUserId());
        return userService.addOne(param);
    }

    @ApiOperation("修改用户")
    @PostMapping("/updateOne")
    public Result<String> updateOne(@RequestBody MiddleUserParam param, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        param.setOperatorId(userInfo.getUserId());
        return userService.updateOne(param,userInfo.getLoginName());
    }

    @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    @ApiOperation("删除用户")
    @GetMapping("/deleteOne")
    public Result<String> deleteOne(@RequestParam(name = "userId")String userId) {
        return userService.delOne(userId);
    }

}
