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

import com.tmerclub.cloud.api.order.dto.CustomerRetainedDTO;
import com.tmerclub.cloud.api.order.vo.CustomerRetainVO;
import com.tmerclub.cloud.api.user.dto.MemberReqDTO;
import com.tmerclub.cloud.api.user.vo.MemberContributeRespVO;
import com.tmerclub.cloud.api.user.vo.MemberDealRespVO;
import com.tmerclub.cloud.api.user.vo.MemberTrendRespVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.flow.service.CustomerAnalysisService;
import com.tmerclub.cloud.flow.vo.MemberSurveyRespVO;
import com.tmerclub.cloud.flow.vo.MemberTrendExcelVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
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.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 顾客分析接口
 *
 * @author
 */
@Tag(name = "顾客分析接口")
@RestController("adminCustomerAnalysisController")
@RequestMapping("/mp/customerAnalysis")
public class CustomerAnalysisController {

    @Autowired
    private CustomerAnalysisService customerAnalysisService;

    /**
     * 会员分析，会员概况
     */
    @Operation(summary = "会员分析，会员概况", description = "会员分析，会员概况")
    @GetMapping("/getMemberSurvey")
    public ServerResponseEntity<MemberSurveyRespVO> getMemberSurvey(MemberReqDTO param) {
        MemberSurveyRespVO memberSurveyRespVO = customerAnalysisService.getMemberSurvey(param);
        return ServerResponseEntity.success(memberSurveyRespVO);
    }

    /**
     * 会员分析，会员人数趋势/ 会员占比趋势
     */
    @Operation(summary = "会员分析，会员人数趋势/ 会员占比趋势", description = "会员分析，会员人数趋势/ 会员占比趋势")
    @GetMapping("/getMemberTrend")
    public ServerResponseEntity<List<MemberTrendRespVO>> getMemberTrend(MemberReqDTO param) {
        List<MemberTrendRespVO> resList = customerAnalysisService.getMemberTrend(param);
        return ServerResponseEntity.success(resList);
    }

    /**
     * 会员分析，会员贡献价值分析
     */
    @Operation(summary = "会员分析，会员贡献价值分析", description = "会员分析，会员贡献价值分析")
    @GetMapping("/getMemberContributeValue")
    public ServerResponseEntity<MemberContributeRespVO> getMemberContributeValue(MemberReqDTO param) {
        MemberContributeRespVO contributeRespVO = customerAnalysisService.getMemberContributeValue(param);
        return ServerResponseEntity.success(contributeRespVO);
    }

    /**
     * 会员分析，新老会员成交分析
     */
    @GetMapping("/getMemberDeal")
    @Operation(summary = "会员分析，新老会员成交分析", description = "会员分析，新老会员成交分析")
    public ServerResponseEntity<MemberDealRespVO> getMemberDeal(MemberReqDTO param) {
        MemberDealRespVO respParam = customerAnalysisService.getMemberDeal(param);
        return ServerResponseEntity.success(respParam);
    }

    /**
     * 客户分析，客户留存分析
     */
    @Operation(summary = "客户分析-客户留存分析", description = "客户分析，客户留存分析,不做周留存数据")
    @GetMapping("/getCustomerRetained")
    public ServerResponseEntity<List<CustomerRetainVO>> getCustomerRetained(CustomerRetainedDTO customerRetainedDTO) {
        //
        Integer dateType = customerRetainedDTO.getDateType();
        Integer dateRetainType = customerRetainedDTO.getDateRetainType();
        List<CustomerRetainVO> respList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        if (Objects.equals(1, dateType) && !Objects.equals(1, dateRetainType)) {
            // 最近一月，月留存。此时不显示数据
            return ServerResponseEntity.success(respList);
        }
        respList = customerAnalysisService.getCustomerRetained(customerRetainedDTO);

        return ServerResponseEntity.success(respList);
    }

    @GetMapping("/memberTrendExport")
    @Operation(summary = "导出excel", description = "导出导出会员分析，会员人数趋势/ 会员占比趋势")
    public ServerResponseEntity<Void> memberTrendExport(HttpServletResponse response, MemberReqDTO param) {
        List<MemberTrendExcelVO> list = customerAnalysisService.listMemberTrend(param);
        ExcelUtil.soleExcel(response, list, MemberTrendExcelVO.EXCEL_NAME, MemberTrendExcelVO.MERGE_ROW_INDEX, MemberTrendExcelVO.MERGE_COLUMN_INDEX, MemberTrendExcelVO.class);
        return ServerResponseEntity.success();
    }
}
