UserController.java 3.73 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import vion.Global;
import vion.dto.DingDTO;
import vion.dto.UserDTO;
import vion.model.RUserRole;
import vion.model.Role;
import vion.model.TaskTemp;
import vion.model.User;
import vion.service.IRUserRoleService;
import vion.service.IRoleService;
import vion.service.ITaskTempService;
import vion.service.IUserService;
import vion.third.DingMod;
import vion.third.WechatMod;
import vion.vo.RoleVO;
import vion.vo.UserVO;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping(Global.BASE_URL)
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final IUserService userService;
    private final ITaskTempService taskTempService;
    private final IRUserRoleService userRoleService;
    private final IRoleService roleService;
    private final Converter converter;
    private final DingMod dingMod;
    private final WechatMod wechatMod;

    @GetMapping("/users")
    @SaCheckPermission(value = "user:list", orRole="admin")
    public Page<User> getUserList(UserDTO dto) {
        return userService.lambdaQuery(converter.convert(dto, new User()))
                .orderByDesc(User::getModifyTime)
                .page(Page.of(dto.getPageNum(), dto.getPageSize()));
    }

    @GetMapping("/user")
    @SaCheckPermission(value = "user:query", orRole="admin")
    public User getUserByID(@RequestParam(name = "id") Integer id) {
        return userService.getById(id);
    }

    @PostMapping("/user")
    @SaCheckPermission(value = "user:edit", orRole="admin")
    public String update(@RequestBody List<User> data) {
        return Opt.ofEmptyAble(data).map(userService::updateBatchById).orElse(false) ? "更新成功" : "更新失败";
    }
    @GetMapping("/user/{token}")
    @SaCheckPermission(value = "user:token:query", orRole = "admin")
    public UserVO getUserByToken(@PathVariable String token) {
        return Global.USERNAME_MAP.get(token);
    }

    @GetMapping("/orgTree")
    @SaCheckPermission(value = "user:orgTree", orRole="admin")
    public List<Tree<String>> getOrgTree(String deptId) {
        return userService.getOrgTree(deptId);
    }

    @GetMapping("/user/role/{id}")
    @SaCheckPermission(value = "user:role", orRole="admin")
    // todo 权限未加库
    public List<RoleVO> listRoleById(@PathVariable Long id) {
        List<RUserRole> userRoleList = userRoleService.lambdaQuery().eq(RUserRole::getUserId, id).list();
        List<Long> roleIdList = userRoleList.stream().map(RUserRole::getRoleId).collect(Collectors.toList());
        List<Role> roleList = roleService.lambdaQuery().in(Role::getId, roleIdList).list();
        return converter.convert(roleList, RoleVO.class);
    }

    @GetMapping("/ding/callback/{target}")
    public Object dingCallback(@PathVariable String target, DingDTO dto, HttpServletResponse res) {
        return dingMod.dingCallback(target, dto, res);
    }

    @GetMapping("/wechat/callback")
    public Object wechatCallback(String code, Long taskTempId) {
        Object obj = wechatMod.wechatCallback(code);
        if (obj instanceof String) {
            return taskTempService.lambdaUpdate().set(TaskTemp::getOpenid, obj).eq(TaskTemp::getId, taskTempId).update() ? new ModelAndView("weChatNeedAttention") : new ModelAndView("weChatError");
        } else {
            return obj;
        }
    }

}