package com.kcim.web;

import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.dto.UserDto;
import com.kcim.vo.UserVO;
import com.kcim.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 用户表
 *
 * @author KCYG
 * @date 2021-07-26 08:52:56
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关操作")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     *  分页查询用户的信息数据
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页查询用户的信息")
    public Result list(@RequestParam(defaultValue = "1", value = "current") Integer current,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "name",required = false) String name){
        PageUtils pageUtils = userService.queryList(current,pageSize, UserContext.getHospId(),name);
        return Result.ok(pageUtils);
    }

    /**
     * 根据姓名获取用户信息
     */
    @GetMapping("/getByName")
    @ApiOperation("根据用户名获取用户信息")
    public Result getByName(String name){
		List<UserVO> user = userService.getByUserName(name);
        return Result.ok(user);
    }
//    @GetMapping("/getById")
//    public Result getById(String id){
//        User user = userService.getById(id);
//        return Result.ok(user);
//    }

    /**
     * 保存用户信息
     */
    @PostMapping("/save")
    @ApiOperation("保存用户信息")
    public Result save(@RequestBody @Valid UserDto userDto){
		userService.saveUser(userDto);
        return Result.ok();
    }

    /**
     * 修改
     *
     * 修改的操作是作废新增
     */
    @PostMapping("/update")
    @ApiOperation("修改用户的信息")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result update(@RequestBody @Valid UserDto userDto){
        userService.updateByUser(userDto);
        return Result.ok();
    }

    /**
     * 批量删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除用户的信息")
    public Result delete(@RequestBody Integer[] ids){
        userService.deleteByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
