package com.jiayue.ipfcst.console.controller;


import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.security.user.SysUser;
import com.jiayue.ipfcst.common.security.user.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息restful接口
 *
 * @author bizy
 * @version 3.0
 * @since 2020/6/28 10:12
 */
@RestController
@Slf4j
public class SysUserController {

    private final SysUserService sysUserService;

    @Autowired
    public SysUserController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

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

    @GetMapping(value = "sysUser/")
    public ResponseVO getAll() {

        try {
            List<SysUser> ps = this.sysUserService.getAll();

            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 {
            sysUserService.save(sysUser);
            return ResponseVO.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 保存用户信息异常");
            return ResponseVO.fail(" 保存用户信息异常");
        }

    }

    /**
     * 更新用户信息
     *
     * @param sysUser 参数
     * @return 执行结果
     */
    @PutMapping(value = "sysUser/")
    public ResponseVO update(@RequestBody SysUser sysUser) {
        try {
            sysUserService.save(sysUser);
            return ResponseVO.success("修改成功");
        } 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 {
            sysUserService.delete(ids);
            return ResponseVO.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 删除用户信息异常");
            return ResponseVO.fail("删除用户信息异常");
        }

    }

}
