package com.jiayue.ssi.controller;

import com.jiayue.ssi.entity.SysUser;
import com.jiayue.ssi.service.SysUserService;
import com.jiayue.ssi.util.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息controller
 *
 * @author xsl
 * @version 3.0
 */
@RestController
@RequestMapping("/sysUserController" )
@Slf4j
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */

    @GetMapping(value = "/getAll")
    public ResponseVO getAll() {
        try {
            List<SysUser> ps = sysUserService.queryAllUsers();
            return ResponseVO.success(ps);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 获取所有用户异常");
            return ResponseVO.error(null);
        }
    }

    /**
     * 保存用户信息
     *
     * @param sysUser 参数
     * @return 执行结果
     */
    @PostMapping(value = "sysUser/")
    public ResponseVO save(@RequestBody SysUser sysUser) {
        try {
            boolean bo = sysUserService.saveUser(sysUser);
            if (bo){
                return ResponseVO.success("保存用户信息成功");
            }
            else{
                log.error("保存用户信息失败");
                return ResponseVO.fail();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存用户信息异常");
            return ResponseVO.fail("保存用户信息异常");
        }
    }

    /**
     * 更新用户信息
     *
     * @param sysUser 参数
     * @return 执行结果
     */
    @PutMapping(value = "sysUser/")
    public ResponseVO update(@RequestBody SysUser sysUser) {
        try {
            boolean bo = sysUserService.updateUser(sysUser);
            if (bo){
                return ResponseVO.success("修改用户信息成功");
            }
            else{
                log.error("更新用户信息失败");
                return ResponseVO.fail();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("更新用户信息异常");
            return ResponseVO.fail();
        }
    }

    /**
     * 删除用户信息
     *
     * @param ids 删除id
     * @return 执行结果
     */
    @DeleteMapping(value = "sysUser/{ids}")
    public ResponseVO delete(@PathVariable("ids") String ids) {
        try {
            boolean bo = sysUserService.deleteUser(ids);
            if (bo){
                return ResponseVO.success("删除用户信息成功");
            }
            else{
                log.error("删除用户信息失败");
                return ResponseVO.fail();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除用户信息异常");
            return ResponseVO.fail("删除用户信息异常");
        }
    }
}
