package com.yunzhupaas.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.Page;
import com.yunzhupaas.base.Pagination;
import com.yunzhupaas.base.model.online.BatchOnlineModel;
import com.yunzhupaas.base.service.UserOnlineService;
import com.yunzhupaas.base.vo.ListVO;
import com.yunzhupaas.base.vo.PageListVO;
import com.yunzhupaas.base.vo.PaginationVO;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.message.model.UserOnlineModel;
import com.yunzhupaas.message.model.UserOnlineVO;
import com.yunzhupaas.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 在线用户
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2024-09-26 上午9:18
 */
@Tag(name = "在线用户", description = "Online")
@RestController
@RequestMapping("/api/system/OnlineUser")
public class UserOnlineController {

    @Autowired
    private UserOnlineService userOnlineService;

    /**
     * 列表
     *
     * @param page 分页参数
     * @return ignore
     */
    @Operation(summary = "获取在线用户列表")
    @SaCheckPermission("permission.userOnline")
    @GetMapping
    public ActionResult<PageListVO<UserOnlineVO>> list(Pagination page) {
        List<UserOnlineModel> data = userOnlineService.getList(page);
        List<UserOnlineVO> voList= data.stream().map(online->{
            UserOnlineVO vo = JsonUtil.getJsonToBean(online, UserOnlineVO.class);
            vo.setUserId(online.getToken());
            //vo.setUserName(vo.getUserName() + "/" + online.getDevice());
            return vo;
        }).collect(Collectors.toList());
        PaginationVO paginationVO = JsonUtil.getJsonToBean(page, PaginationVO.class);
        return ActionResult.page(voList, paginationVO);
    }

    /**
     * 注销
     *
     * @param token 主键值
     * @return
     */
    @Operation(summary = "强制下线")
    @Parameter(name = "token", description = "token", required = true)
    @SaCheckPermission("permission.userOnline")
    @DeleteMapping("/{token}")
    public ActionResult delete(@PathVariable("token") String token) {
        userOnlineService.delete(token);
        return ActionResult.success(MsgCode.SU005.get());
    }

    /**
     * 批量下线用户
     *
     * @param model 主键值
     * @return ignore
     */
    @Operation(summary = "批量下线用户")
    @Parameter(name = "model", description = "在线用户id集合", required = true)
    @SaCheckPermission("permission.userOnline")
    @DeleteMapping
    public ActionResult clear(@RequestBody BatchOnlineModel model) {
        userOnlineService.delete(model.getIds());
        return ActionResult.success(MsgCode.SU005.get());
    }
}

