package com.koobietech.eas.controller;

import com.github.pagehelper.PageHelper;
import com.koobietech.eas.common.result.JsonPageResult;
import com.koobietech.eas.common.result.JsonResult;
import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.dao.pojo.RolePojo;
import com.koobietech.eas.mbg.model.EasSysUserinfo;
import com.koobietech.eas.service.EasSysUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Tag(name = "用户信息模块")
@RequestMapping("/userinfo")
public class EasSysUserInfoController {

    @Resource
    EasSysUserInfoService easSysUserInfoService;

    @PostMapping("/queryUserByRoleId/{departmentId}")
    @Operation(summary = "查询用户信息根据角色ID", description = "查询用户信息根据角色ID")
    public JsonResult queryUserByRoleId(@PathVariable int departmentId) {
        List<RolePojo> ret = easSysUserInfoService.queryUserByRoleId(departmentId);
        return JsonResult.data(ret);
    }

    @PostMapping("/getAllUsers")
    @Operation(summary = "查询用户信息所有", description = "查询用户信息所有")
    public JsonResult getAllUser() {
        List<RolePojo> ret = easSysUserInfoService.getAllUsers();
        return JsonResult.data(ret);
    }

    @PostMapping("/add")
    @Operation(summary = "添加用户信息", description = "录入基本用户信息")
    public JsonResult add(@RequestBody EasSysUserinfo userinfo) {
        Boolean ret = easSysUserInfoService.add(userinfo);
        if (ret) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户信息", description = "删除用户信息表数据根据ID")
    public JsonResult delete(@RequestParam Long id) {
        Boolean ret = easSysUserInfoService.delete(id);
        if (ret) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息", description = "更新用户信息表信息")
    public JsonResult update(@RequestBody EasSysUserinfo userinfo) {
        Boolean ret = easSysUserInfoService.update(userinfo);
        if (ret) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @PostMapping("/query")
    @Operation(summary = "查询用户信息", description = "根据参数查询用户信息表信息")
    public JsonPageResult query(@RequestBody(required = false) EasSysUserinfo userinfo,
                                @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageDataResult ret = easSysUserInfoService.query(userinfo);
        return JsonPageResult.data(ret);
    }

    @GetMapping("/queryUserById/{uid}")
    @Operation(summary = "查询用户信息根据ID", description = "根据参数查询用户信息表信息")
    public JsonResult queryByUid(@PathVariable Long uid ) {
        EasSysUserinfo userinfo = easSysUserInfoService.queryByUid(uid);
        return JsonResult.data(userinfo);
    }

    @GetMapping("/queryUserByRealname/{realname}")
    @Operation(summary = "查询用户信息根据用户名", description = "根据参数查询用户信息表信息")
    public JsonResult queryByRealname(@PathVariable String realname ) {
        return JsonResult.data(easSysUserInfoService.queryByRealname(realname));
    }

    @GetMapping("/queryAll")
    @Operation(summary = "查询用户信息", description = "查询用户字典表所有信息")
    public JsonResult queryAll() {
        List<EasSysUserinfo> list = easSysUserInfoService.queryAll();
        return JsonResult.data(list);
    }
}
