package com.ruoyi.controller;

import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.frequency.customer.mapper.CustomerMapper;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.RyController;
import com.ruoyi.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *在线
 */
@CrossOrigin
@RestController
@RequestMapping("/api/online")
@Slf4j
@Api(tags = "在线")
public class ApiOnlineController extends RyController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private BusinessUtil businessUtil;

    @Autowired
    private CustomerMapper customerMapper;


    @PostMapping("/online")
    @ApiOperation(value = "定时调取统计在线时间", notes = "定时调取统计在线时间")
    public ResponseData onlineTimeStatistics() {

        UserVo userVo = getUserVo();

        if (userVo.getUserType() == -1 || userVo.getUserType().equals(UserEnums.sale.getCode())){
            return ResponseData.success();
        }

        //累计时长
        String total_times_map_key = "total_times_" + userVo.getUserType();

        //上次调用此接口的时间
        String last_time_map_key = "last_time_" + userVo.getUserType();

        //首先从redis里查
        Long totalTime = 0L;
        if (redisTemplate.opsForHash().hasKey(total_times_map_key,userVo.getUserId().toString())) {
            totalTime = Long.valueOf(redisTemplate.opsForHash().get(total_times_map_key, userVo.getUserId().toString()).toString());
        } else {
            //从数据库查询的累计时长
            totalTime =  Long.valueOf(businessUtil.getUserFieldValue(userVo,"onlineDuration").toString()) * 60 * 1000;
        }

        Integer onlineStatus = 1;

        //查询上次调用此接口的时间
        Long lastTimes = 0L;
        if (redisTemplate.opsForHash().hasKey(last_time_map_key,userVo.getUserId().toString())) {
            lastTimes = Long.valueOf(redisTemplate.opsForHash().get(last_time_map_key, userVo.getUserId().toString()).toString());

            log.info("用户" + userVo.getUserId() + "上次调取时长:" + (System.currentTimeMillis() - lastTimes)/60/1000 + "分钟");

            //判断当前时间和上次时间是否超过5.5分钟(考虑到网络延时和程序卡顿,多半分钟)
            if (System.currentTimeMillis() - lastTimes < (6* 60 * 1000)) {
                totalTime = totalTime + (System.currentTimeMillis() - lastTimes);
            }else {
                onlineStatus = 2;
            }

        }

        //上次调取时间
        redisTemplate.opsForHash().put(last_time_map_key, userVo.getUserId().toString(),System.currentTimeMillis());

        //累计时长
        redisTemplate.opsForHash().put(total_times_map_key, userVo.getUserId().toString(),totalTime);

        //修改数据库在线时长
        businessUtil.updateOnlineDuration(userVo,totalTime,onlineStatus);

        log.info("用户" + userVo.getUserId() + "累计在线时长:" + totalTime/60/1000 + "分钟");

        customerMapper.insertOnlineLog(userVo);

        return ResponseData.success();
    }

}
