
package com.tmerclub.cloud.flow.controller.platform;

import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.api.product.dto.ProdEffectDTO;
import com.tmerclub.cloud.api.product.vo.ProdEffectRespVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.flow.service.ProductAnalyseService;
import com.tmerclub.cloud.flow.vo.FlowProdEffectDataExcelVO;
import com.tmerclub.cloud.flow.vo.ShopFlowInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;

/**
 * 流量分析—商品分析
 *
 * @author YXF
 * @date 2021-05-21 15:25:19
 */
@RestController("platformProductAnalyseController")
@RequestMapping("/p/productAnalyse")
@Tag(name = "platform-商品分析")
public class ProductAnalyseController {

    @Autowired
    private ProductAnalyseService productAnalyseService;

    @GetMapping("/getProductEffect")
    @Operation(summary = "获取平台端商品洞察数据", description = "获取商品洞察数据")
    public ServerResponseEntity<PageVO<ProdEffectRespVO>> getProductEffect(@Valid PageDTO pageDTO, ProdEffectDTO prodEffectDTO) {
        prodEffectDTO.setLang(I18nMessage.getLang());
        return ServerResponseEntity.success(productAnalyseService.getProductEffect(pageDTO,prodEffectDTO));
    }

    @GetMapping("/prodEffectExport")
    @Operation(summary = "导出excel", description = "导出商品洞察数据")
    public ServerResponseEntity<Void> prodEffectExport(HttpServletResponse response, ProdEffectDTO prodEffectDTO) {
        prodEffectDTO.setLang(I18nMessage.getLang());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNum(0);
        pageDTO.setPageSize(PageDTO.MAX_PAGE_SIZE);
        List<FlowProdEffectDataExcelVO> list = productAnalyseService.listProdEffectExcel(pageDTO, prodEffectDTO);
        ExcelUtil.soleExcel(response, list, FlowProdEffectDataExcelVO.EXCEL_NAME, FlowProdEffectDataExcelVO.MERGE_ROW_INDEX, FlowProdEffectDataExcelVO.MERGE_COLUMN_INDEX, FlowProdEffectDataExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/shopRankIngByFlow")
    @Operation(summary = "获取店铺流量排行榜", description = "获取店铺流量排行榜")
    @Parameters({
            @Parameter(name = "dayCount", description = "天数（几天内的排行榜）"),
            @Parameter(name = "limit", description = "排行榜条数")
    })
    public ServerResponseEntity<List<ShopFlowInfoVO>> shopRankIngByFlow(@RequestParam(value = "dayCount", defaultValue = "30") Integer dayCount,
                                                                            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        List<ShopFlowInfoVO> shopFlowInfoVOList = productAnalyseService.listShopRankIngByFlow(DateUtil.endOfDay(DateUtil.date()), dayCount, limit);
        return ServerResponseEntity.success(shopFlowInfoVOList);
    }
}
