package com.fs.store.controller;

import java.util.List;

import com.fs.common.utils.ServletUtils;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.store.domain.FsUserOnlineState;
import com.fs.store.service.IFsUserOnlineStateService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 用户上线情况Controller
 *
 * @author fs
 * @date 2025-06-30
 */
@RestController
@RequestMapping("/store/userOnlineState")
public class FsUserOnlineStateController extends BaseController
{
    @Autowired
    private IFsUserOnlineStateService fsUserOnlineStateService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询用户上线情况列表
     */
    @PreAuthorize("@ss.hasPermi('store:userOnlineState:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserOnlineState fsUserOnlineState)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 如果是管理员，可以查询当前公司所有数据
        if(loginUser.getUser().isAdmin()){
            fsUserOnlineState.setCompanyId( loginUser.getCompany().getCompanyId());
        } else{
            fsUserOnlineState.setCompanyUserId( loginUser.getUser().getUserId());
        }
        List<FsUserOnlineState> list = fsUserOnlineStateService.selectFsUserOnlineStateList(fsUserOnlineState);
        return getDataTable(list);
    }

    /**
     * 导出用户上线情况列表
     */
    @PreAuthorize("@ss.hasPermi('store:userOnlineState:export')")
    @Log(title = "用户上线情况", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserOnlineState fsUserOnlineState)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 如果是管理员，可以查询当前公司所有数据
        if(loginUser.getUser().isAdmin()){
            fsUserOnlineState.setCompanyId( loginUser.getCompany().getCompanyId());
        } else{
            fsUserOnlineState.setCompanyUserId( loginUser.getUser().getUserId());
        }
        List<FsUserOnlineState> list = fsUserOnlineStateService.selectFsUserOnlineStateList(fsUserOnlineState);
        ExcelUtil<FsUserOnlineState> util = new ExcelUtil<FsUserOnlineState>(FsUserOnlineState.class);
        return util.exportExcel(list, "未上线会员");
    }

    /**
     * 获取用户上线情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:userOnlineState:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(fsUserOnlineStateService.selectFsUserOnlineStateById(userId));
    }

    /**
     * 新增用户上线情况
     */
    @PreAuthorize("@ss.hasPermi('store:userOnlineState:add')")
    @Log(title = "用户上线情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserOnlineState fsUserOnlineState)
    {
        return toAjax(fsUserOnlineStateService.insertFsUserOnlineState(fsUserOnlineState));
    }

    /**
     * 修改用户上线情况
     */
    @PreAuthorize("@ss.hasPermi('store:userOnlineState:edit')")
    @Log(title = "用户上线情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserOnlineState fsUserOnlineState)
    {
        return toAjax(fsUserOnlineStateService.updateFsUserOnlineState(fsUserOnlineState));
    }

    /**
     * 删除用户上线情况
     */
    @PreAuthorize("@ss.hasPermi('store:userOnlineState:remove')")
    @Log(title = "用户上线情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(fsUserOnlineStateService.deleteFsUserOnlineStateByIds(userIds));
    }

}
