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

import cn.hutool.core.date.DateUtil;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.enums.StatsTimeType;
import com.slodon.b2b2c.model.statistics.StoreDayModel;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.statistics.example.StoreDayExample;
import com.slodon.b2b2c.statistics.pojo.StoreDay;
import com.slodon.b2b2c.statistics.pojo.base.StoreBase;
import com.slodon.b2b2c.util.StatsModelUtil;
import com.slodon.b2b2c.vo.statistics.FlowChangeTrendVO;
import com.slodon.b2b2c.vo.statistics.SellerFlowOverviewVO;
import com.slodon.b2b2c.vo.statistics.SellerFlowReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
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 javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Api(tags = "seller-流量分析")
@RestController
@RequestMapping("v3/statistics/seller/flow/analysis")
public class SellerFlowAnalysisController extends BaseController {

    @Resource
    private StoreDayModel storeDayModel;
    @Resource
    private StatsModelUtil statsModelUtil;

    @ApiOperation("流量总览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "webSite", value = "站点")
    })
    @GetMapping("flowOverview")
    public JsonResult<SellerFlowOverviewVO> flowOverview(HttpServletRequest request, Date startTime, Date endTime,@RequestParam(name = "webSite", required = false,defaultValue = "1") String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        SellerFlowOverviewVO vo = new SellerFlowOverviewVO();
        List<? extends StoreBase> list = statsModelUtil.getStoreList(timeType, startTime, endTime, webSite,1, "flow", vendor.getStoreId(), null);
        if (!CollectionUtils.isEmpty(list)) {
            StoreBase storeBase = list.get(0);
            //上期数据
            StoreBase previousData = null;
            Date preStartTime;
            Date preEndTime;
            int differentDay = StatsTimeType.getDifferentDay(startTime, endTime);
            if (timeType == StatsTimeType.DAY && differentDay != 1) {
                preStartTime = StatsTimeType.getPreStartTime(startTime, differentDay);
                preEndTime = StatsTimeType.getPreEndTime(endTime, differentDay);
            } else {
                preStartTime = StatsTimeType.getStartTime(startTime, timeType);
                preEndTime = StatsTimeType.getEndTime(endTime, timeType);
            }
            if (preStartTime != null) {
                List<? extends StoreBase> previousList = statsModelUtil.getStoreList(timeType, preStartTime, preEndTime, webSite,1, "flow", vendor.getStoreId(), null);
                if (!CollectionUtils.isEmpty(previousList)) {
                    previousData = previousList.get(0);
                }
            }
            if (storeBase != null) {
                vo = new SellerFlowOverviewVO(storeBase, previousData);
            }
        }
        return SldResponse.success(vo);
    }

    @ApiOperation("近30天变化趋势")
    @GetMapping("changeTrend")
    @ApiImplicitParam(name = "webSite", value = "站点")
    public JsonResult<List<FlowChangeTrendVO>> changeTrend(HttpServletRequest request,@RequestParam(value = "webSite",required = false,defaultValue = "1") String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        //获取当前时间30天之前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -30);
        Date startTime = DateUtil.beginOfDay(calendar.getTime());
        StoreDayExample example = new StoreDayExample();
        example.setStatsTimeAfter(startTime);
        example.setStoreId(vendor.getStoreId());
        example.setWebSite(webSite);
        example.setOrderBy("stats_time ASC");
        List<StoreDay> list = storeDayModel.getStoreDayList(example, null);
        List<FlowChangeTrendVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(storeDay -> {
                vos.add(new FlowChangeTrendVO(storeDay));
            });
        }
        return SldResponse.success(vos);
    }

    @ApiOperation("流量报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "type", value = "排序类型；desc-降序;asc-升序;默认类型为降序"),
            @ApiImplicitParam(name = "sort", value = "排序:stats_time-时间;visitor_num-店铺访客数;view_num-店铺浏览量;" +
                    "per_view_num-人均浏览量;goods_visitor_num-商品访客数;goods_view_num-商品浏览量;默认按时间降序排序"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("flowReport")
    public JsonResult<PageVO<SellerFlowReportVO>> flowReport(HttpServletRequest request, Date startTime, Date endTime,
                                                             @RequestParam(value = "type", required = false, defaultValue = "desc") String type,
                                                             @RequestParam(value = "sort", required = false, defaultValue = "stats_time") String sort,
                                                             @RequestParam(value = "webSite",required = false,defaultValue = "1") String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        StoreDayExample example = new StoreDayExample();
        example.setStatsTimeAfter(startTime);
        example.setStatsTimeBefore(endTime);
        example.setStoreId(vendor.getStoreId());
        example.setWebSite(webSite);
        example.setOrderBy(sort + " " + type);
        List<StoreDay> list = storeDayModel.getStoreDayList(example, pager);
        List<SellerFlowReportVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(storeBase -> {
                vos.add(new SellerFlowReportVO(storeBase));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("导出报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "type", value = "排序类型；desc-降序;asc-升序;默认类型为降序"),
            @ApiImplicitParam(name = "sort", value = "排序:stats_time-时间;visitor_num-店铺访客数;view_num-店铺浏览量;" +
                    "per_view_num-人均浏览量;goods_visitor_num-商品访客数;goods_view_num-商品浏览量;默认按时间降序排序")
    })
    @GetMapping("export")
    public JsonResult export(HttpServletRequest request, HttpServletResponse response, Date startTime, Date endTime,
                             @RequestParam(value = "type", required = false, defaultValue = "desc") String type,
                             @RequestParam(value = "sort", required = false, defaultValue = "stats_time") String sort,
                             @RequestParam(value = "webSite",required = false,defaultValue = "1") String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        StoreDayExample example = new StoreDayExample();
        example.setStoreId(vendor.getStoreId());
        example.setStatsTimeAfter(startTime);
        example.setStatsTimeBefore(endTime);
        example.setWebSite(webSite);
        example.setOrderBy(sort + " " + type);
        storeDayModel.flowReportExport(request, response, example);
        return null;
    }

}
