package com.ytpm.question.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ytpm.app.enums.LoginType;
import com.ytpm.app.model.YtDyzUser;
import com.ytpm.app.param.VisitorLoginParam;
import com.ytpm.general.Result;
import com.ytpm.handle.LoginServiceFactory;
import com.ytpm.question.dao.AppUserMapper;
import com.ytpm.question.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * 游客登陆
 *
 * @author Marx
 * @date 2025/9/2 15:02
 */
@RefreshScope
@RestController
@Api(tags = "游客管理")
@RequestMapping("/visitor")
public class VisitorController {

    @Resource
    private AppUserMapper appUserMapper;

    @Resource
    private LoginServiceFactory loginServiceFactory;

    @Resource
    private RedisService redisService;

    @ApiOperation("获取游客登陆幂等Token")
    @GetMapping("/get/requestId")
    public Result<String> getRequestId(@RequestParam("ditchId") String ditchId, @RequestParam("appType") String appType,
                                       @RequestParam("deviceId") String deviceId){
        String redisKey = StrUtil.format("visitor:requestId:{}:{}:{}", ditchId, appType, deviceId);
        String requestId = "";
        if (redisService.hasKey(redisKey)) {
            requestId = redisService.getStr(redisKey);
        }
        if (StrUtil.isEmpty(requestId)) {
            requestId = IdUtil.fastSimpleUUID();
            boolean setSuccess = redisService.setIfAbsent(redisKey, requestId, 1, TimeUnit.MINUTES);
            if (!setSuccess) {
                requestId = redisService.getStr(redisKey);
                if (StrUtil.isEmpty(requestId)) {
                    requestId = IdUtil.fastSimpleUUID();
                    redisService.setTimeOutMinutesStr(redisKey, requestId, 1);
                } else {
                    redisService.expire(redisKey, 1, TimeUnit.MINUTES);
                }
            }
        }
        return Result.resultObjOk(requestId);
    }

    @PostMapping("/login")
    @ApiOperation("游客登录")
    @Transactional(rollbackFor = Exception.class)
    public Result<YtDyzUser> visitorLogin(@RequestBody VisitorLoginParam param, HttpServletRequest request) {
        return loginServiceFactory.login(LoginType.VISITOR, param, request);
    }

    @ApiOperation("设备在指定时间内注册的渠道")
    @GetMapping("/getDitchCount")
    public int getDitchCount(@RequestParam("deviceId") String deviceId, @RequestParam("hours") Integer hours) {
        return appUserMapper.countDitch(deviceId, hours);
    }

    @ApiOperation("设备在指定时间内注册的渠道")
    @GetMapping("/getLoginDitchCount")
    public int getLoginDitchCount(@RequestParam("deviceId") String deviceId, @RequestParam("hours") Integer hours) {
        return appUserMapper.countLoginDitch(deviceId, hours);
    }


}
