package com.fs.his.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.domain.FsUserOnlineState;
import com.fs.his.service.IFsUserOnlineStateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import static com.fs.his.utils.PhoneUtil.decryptPhoneMk;
import static com.fs.his.utils.PhoneUtil.encryptPhone;

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

    /**
     * 查询用户上线情况列表
     */
    @PreAuthorize("@ss.hasPermi('store:userOnlineState:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserOnlineState fsUserOnlineState)
    {
        startPage();
        if(StringUtils.isNotEmpty(fsUserOnlineState.getPhone())){
            fsUserOnlineState.setPhone(encryptPhone(fsUserOnlineState.getPhone()));
        }
        List<FsUserOnlineState> list = fsUserOnlineStateService.selectFsUserOnlineStateList(fsUserOnlineState);
        if(!list.isEmpty()){
            for (FsUserOnlineState userOnlineState : list) {
                if(userOnlineState.getPhone() != null && userOnlineState.getPhone() != ""){
                    if (userOnlineState.getPhone().length()>11){
                        userOnlineState.setPhone(decryptPhoneMk(userOnlineState.getPhone()));
                    }else {
                        userOnlineState.setPhone(userOnlineState.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                    }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户上线情况列表
     */
    @PreAuthorize("@ss.hasPermi('store:userOnlineState:export')")
    @Log(title = "用户上线情况", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserOnlineState fsUserOnlineState)
    {
        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));
    }

    @ApiOperation("新增/更新未上线人员-定时任务")
    @PostMapping("/testTask")
    public void test(){
        fsUserOnlineStateService.insertUserNotOnline();
    }
}
