package com.slodon.b2b2c.controller.statistics.seller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.enums.StatsConstant;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.util.SellerPresentDataUtil;
import com.slodon.b2b2c.vo.statistics.SellerPresentDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
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.Date;

@Api(tags = "seller-实时分析")
@RestController
@RequestMapping("v3/statistics/seller/present/analysis")
public class SellerPresentAnalysisController {

    @Resource
    private RedisTemplate<String, Object> objectRedisTemplate;
    @Resource
    private SellerPresentDataUtil sellerPresentDataUtil;

    @ApiOperation("获取实时统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "refresh", value = "true-刷新,默认false", defaultValue = "false"),
            @ApiImplicitParam(name = "webSite", value = "站点来源 1:海外站 2:国内站")
    })
    @GetMapping("presentData")
    public JsonResult<SellerPresentDataVO> getSellerPresentData(HttpServletRequest request,
                                                                @RequestParam(value = "refresh", required = false, defaultValue = "false") Boolean refresh,
                                                                @RequestParam(value = "webSite", required = false,defaultValue = "1") String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        if (refresh) {
            //刷新请求，重新计算实时统计，并缓存
            Date endTime = new Date();//截止当前时间
            DateTime startTime = DateUtil.beginOfDay(endTime);//今日0时开始

            //计算实时统计数据
            SellerPresentDataVO vo = sellerPresentDataUtil.calculatePresentData(startTime, endTime,webSite, vendor.getStoreId());

            //缓存数据
            objectRedisTemplate.opsForHash().put(StatsConstant.SELLER_PRESENT_REDIS_KEY + "_" + webSite, vendor.getStoreId() + "", vo);

            return SldResponse.success(vo);
        }

        //查询缓存数据
        Object presentData = objectRedisTemplate.opsForHash().get(StatsConstant.SELLER_PRESENT_REDIS_KEY+"_"+webSite, vendor.getStoreId() + "");
        if (presentData == null) {
            //无缓存数据，重新计算实时统计，并缓存
            Date endTime = new Date();//截止当前时间
            DateTime startTime = DateUtil.beginOfDay(endTime);//今日0时开始

            //计算实时统计数据
            SellerPresentDataVO vo = sellerPresentDataUtil.calculatePresentData(startTime, endTime, webSite,vendor.getStoreId());

            //缓存数据
            objectRedisTemplate.opsForHash().put(StatsConstant.SELLER_PRESENT_REDIS_KEY + "_" + webSite, vendor.getStoreId() + "", vo);

            return SldResponse.success(vo);
        }
        //直接返回缓存
        return SldResponse.success(JSON.parseObject(JSON.toJSONString(presentData), SellerPresentDataVO.class));
    }
}
