package com.ruoyi.dataanalysis.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dataanalysis.model.param.CustomerChartsParam;
import com.ruoyi.dataanalysis.model.param.OrderSortParam;
import com.ruoyi.dataanalysis.model.param.SaleChartsParam;
import com.ruoyi.dataanalysis.model.param.StoreChartsParam;
import com.ruoyi.dataanalysis.service.DataAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 后台——数据分析
 *
 * @author: liwenlong
 * @date: 2023-02-21
 */
@RestController
@RequestMapping("/dataAnalysis")
public class DataAnalysisController extends BaseController {


    @Autowired
    private DataAnalysisService dataAnalysisService;

    private void customerFillParam(CustomerChartsParam customerChartsParam){
        //如果sort没有值，去除
        Iterator<OrderSortParam> iterator = customerChartsParam.getOrderSortParamList().iterator();
        while (iterator.hasNext()){
            OrderSortParam next = iterator.next();
            if(StringUtils.isBlank(next.getSort())){
                iterator.remove();
            }
        }
        if (customerChartsParam.getOrderSortParamList() == null || customerChartsParam.getOrderSortParamList().size() == 0) {
            customerChartsParam.setOrderSortParamList(new ArrayList<>());
            OrderSortParam orderSortParam = new OrderSortParam();
            orderSortParam.setColumn("payAmount");
            orderSortParam.setSort("desc");
            customerChartsParam.getOrderSortParamList().add(orderSortParam);
        }
    }

    /**
     * 设计师排行榜
     *
     * @return
     */
    @PostMapping("/customerCharts")
    public TableDataInfo customerCharts(@RequestBody CustomerChartsParam customerChartsParam) {
        startPage();
        customerFillParam(customerChartsParam);
        return getDataTable(dataAnalysisService.customerCharts(customerChartsParam));
    }


    /**
     * 设计师排行榜统计
     *
     * @return
     */
    @PostMapping("/customerChartsStatistics")
    public AjaxResult customerChartsStatistics(@RequestBody CustomerChartsParam customerChartsParam) {
        customerFillParam(customerChartsParam);
        return AjaxResult.success(dataAnalysisService.customerChartsStatistics(customerChartsParam));
    }

    private void storeFillParam(StoreChartsParam storeChartsParam){
        //如果sort没有值，去除
        Iterator<OrderSortParam> iterator = storeChartsParam.getOrderSortParamList().iterator();
        while (iterator.hasNext()){
            OrderSortParam next = iterator.next();
            if(StringUtils.isBlank(next.getSort())){
                iterator.remove();
            }
        }
        if (storeChartsParam.getOrderSortParamList() == null || storeChartsParam.getOrderSortParamList().size() == 0) {
            storeChartsParam.setOrderSortParamList(new ArrayList<>());
            OrderSortParam orderSortParam = new OrderSortParam();
            orderSortParam.setColumn("hasReceivedAmount");
            orderSortParam.setSort("desc");
            storeChartsParam.getOrderSortParamList().add(orderSortParam);
        }
    }

    /**
     * 表现师师排行榜
     *
     * @return
     */
    @PostMapping("/storeCharts")
    public TableDataInfo storeCharts(@RequestBody StoreChartsParam storeChartsParam) {
        startPage();
        storeFillParam(storeChartsParam);

        return getDataTable(dataAnalysisService.storeCharts(storeChartsParam));
    }



    /**
     * 表现师师排行榜统计
     *
     * @return
     */
    @PostMapping("/storeChartsStatistics")
    public AjaxResult storeChartsStatistics(@RequestBody StoreChartsParam storeChartsParam) {
        storeFillParam(storeChartsParam);
        return AjaxResult.success(dataAnalysisService.storeChartsStatistics(storeChartsParam));
    }

    private void saleFillParam(SaleChartsParam saleChartsParam){
        //如果sort没有值，去除
        Iterator<OrderSortParam> iterator = saleChartsParam.getOrderSortParamList().iterator();
        while (iterator.hasNext()){
            OrderSortParam next = iterator.next();
            if(StringUtils.isBlank(next.getSort())){
                iterator.remove();
            }
        }
        if (saleChartsParam.getOrderSortParamList() == null || saleChartsParam.getOrderSortParamList().size() == 0) {
            saleChartsParam.setOrderSortParamList(new ArrayList<>());
            OrderSortParam orderSortParam = new OrderSortParam();
            orderSortParam.setColumn("commission");
            orderSortParam.setSort("desc");
            saleChartsParam.getOrderSortParamList().add(orderSortParam);
        }
    }

    /**
     * 推广员排行榜
     *
     * @return
     */
    @PostMapping("/saleCharts")
    public TableDataInfo saleCharts(@RequestBody SaleChartsParam saleChartsParam) {
        startPage();
        saleFillParam(saleChartsParam);

        return getDataTable(dataAnalysisService.saleCharts(saleChartsParam));
    }

    /**
     * 推广员排行榜统计
     *
     * @return
     */
    @PostMapping("/saleChartsStatistics")
    public AjaxResult saleChartsStatistics(@RequestBody SaleChartsParam saleChartsParam) {
        saleFillParam(saleChartsParam);
        return AjaxResult.success(dataAnalysisService.saleChartsStatistics(saleChartsParam));
    }

}
