package cn.lihongjie.coal.user.controller;

import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.base.controller.BaseController;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.permission.dto.PermissionSimpleDto;
import cn.lihongjie.coal.resource.dto.ResourceDto;
import cn.lihongjie.coal.user.dto.ChangeUserPwdDto;
import cn.lihongjie.coal.user.dto.CreateUserDto;
import cn.lihongjie.coal.user.dto.UpdateUserDto;
import cn.lihongjie.coal.user.dto.UserDto;
import cn.lihongjie.coal.user.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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.RestController;

import java.util.List;

@RequestMapping("/user")
@RestController
@SysLog(module = "用户管理")
@OrgScope
public class UserController extends BaseController {

    @Autowired UserService service;

    @PostMapping("/create")
    @SysLog(action = "新增")
    public UserDto create(@RequestBody CreateUserDto dto) {
        return this.service.create(dto);
    }

    @PostMapping("/update")
    @SysLog(action = "编辑")
    public UserDto update(@RequestBody UpdateUserDto dto) {
        return this.service.update(dto);
    }

    @PostMapping("/delete")
    @SysLog(action = "删除")
    public Object delete(@RequestBody IdRequest dto) {
        this.service.delete(dto);
        return true;
    }

    @PostMapping("/changePwd")
    public UserDto create(@RequestBody ChangeUserPwdDto dto) {
        return this.service.changePwd(dto);
    }

    @PostMapping("/list")
    public Page<UserDto> list(@RequestBody CommonQuery dto) {
        return this.service.list(dto);
    }

    @PostMapping("/getById")
    public UserDto getById(@RequestBody IdRequest dto) {
        return this.service.getById(dto.getId());
    }

    @PostMapping("/resources")
    public List<ResourceDto> resources() {
        return this.service.resources(Ctx.currentUser().getId());
    }

    @PostMapping("/permissions")
    public List<PermissionSimpleDto> permissions() {
        return this.service.permissions(Ctx.currentUser().getId());
    }

    @PostMapping("/clearCache")
    public Boolean clearUserCache() {
        this.service.clearUserCache(Ctx.currentUser().getId());
        return true;
    }
}
