package com.ruoyi.dataanalysis.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.dataanalysis.mapper.DataAnalysisMapper;
import com.ruoyi.dataanalysis.model.param.CustomerChartsParam;
import com.ruoyi.dataanalysis.model.param.SaleChartsParam;
import com.ruoyi.dataanalysis.model.param.StoreChartsParam;
import com.ruoyi.dataanalysis.model.result.*;
import com.ruoyi.dataanalysis.service.DataAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页数据统计
 *
 * @author liwenlong
 * @date 2023-02-13
 */
@Service
public class DataAnalysisServiceImpl implements DataAnalysisService {

    @Autowired
    private DataAnalysisMapper dataAnalysisMapper;


    @Override
    public List<CustomerChartsResult> customerCharts(CustomerChartsParam customerChartsParam) {

        List<CustomerChartsResult> customerChartsResultList = customerChartsResultList(customerChartsParam);
        return customerChartsResultList;
    }

    private List<CustomerChartsResult> customerChartsResultList(CustomerChartsParam customerChartsParam) {
        List<CustomerChartsResult> customerChartsResultList = dataAnalysisMapper.customerCharts(customerChartsParam);
        return customerChartsResultList;
    }

    @Override
    public CustomerChartsStatisticsResult customerChartsStatistics(CustomerChartsParam customerChartsParam) {
        List<CustomerChartsResult> customerChartsResultList = customerChartsResultList(customerChartsParam);

        BigDecimal orderAmount = BigDecimal.ZERO;
        BigDecimal payAmount = BigDecimal.ZERO;
        Integer orderNum = 0;
        CustomerChartsStatisticsResult customerChartsStatisticsResult = new CustomerChartsStatisticsResult();
        for (CustomerChartsResult result : customerChartsResultList) {
            orderAmount = orderAmount.add(result.getOrderAmount());
            payAmount = payAmount.add(result.getPayAmount());
            orderNum = orderNum + result.getOrderNum();
        }

        customerChartsStatisticsResult.setOrderAmount(orderAmount);
        customerChartsStatisticsResult.setPayAmount(payAmount);
        customerChartsStatisticsResult.setOrderNum(orderNum);

        return customerChartsStatisticsResult;
    }

    @Override
    public List<StoreChartsResult> storeCharts(StoreChartsParam storeChartsParam) {
        List<StoreChartsResult> storeChartsResultList = storeChartsResultList(storeChartsParam);
        return storeChartsResultList;
    }

    private List<StoreChartsResult> storeChartsResultList(StoreChartsParam storeChartsParam) {
        if (storeChartsParam != null && StrUtil.isNotBlank(storeChartsParam.getAuthType())) {
            storeChartsParam.setAuthTypeList(new ArrayList<>());
            String[] split = storeChartsParam.getAuthType().split(",");
            for (String cur : split) {
                storeChartsParam.getAuthTypeList().add(cur);
            }
        }
        List<StoreChartsResult> storeChartsResultList = dataAnalysisMapper.storeCharts(storeChartsParam);
        return storeChartsResultList;
    }


    @Override
    public Object storeChartsStatistics(StoreChartsParam storeChartsParam) {
        List<StoreChartsResult> storeChartsResultList = storeChartsResultList(storeChartsParam);

        BigDecimal orderAmount = BigDecimal.ZERO;
        BigDecimal payAmount = BigDecimal.ZERO;
        Integer orderNum = 0;
        StoreChartsStatisticsResult storeChartsStatisticsResult = new StoreChartsStatisticsResult();
        for (StoreChartsResult result : storeChartsResultList) {
            orderAmount = orderAmount.add(result.getOrderAmount());
            payAmount = payAmount.add(result.getHasReceivedAmount());
            orderNum = orderNum + result.getOrderNum();
        }

        storeChartsStatisticsResult.setOrderAmount(orderAmount);
        storeChartsStatisticsResult.setPayAmount(payAmount);
        storeChartsStatisticsResult.setOrderNum(orderNum);

        return storeChartsStatisticsResult;
    }

    @Override
    public List<SaleChartsResult> saleCharts(SaleChartsParam saleChartsParam) {
        List<SaleChartsResult> saleChartsResultList = saleChartsResultList(saleChartsParam);
        return saleChartsResultList;
    }


    @Override
    public Object saleChartsStatistics(SaleChartsParam saleChartsParam) {
        List<SaleChartsResult> saleChartsResultList = saleChartsResultList(saleChartsParam);

        BigDecimal commission = BigDecimal.ZERO;
        Integer customerNum = 0;
        SaleChartsStatisticsResult saleChartsStatisticsResult = new SaleChartsStatisticsResult();
        for (SaleChartsResult result : saleChartsResultList) {
            commission = commission.add(result.getCommission());
            customerNum = customerNum + result.getCustomerNum();
        }

        saleChartsStatisticsResult.setCustomerNum(customerNum);
        saleChartsStatisticsResult.setCommission(commission);

        return saleChartsStatisticsResult;
    }

    private List<SaleChartsResult> saleChartsResultList(SaleChartsParam saleChartsParam) {
        List<SaleChartsResult> saleChartsResultList = dataAnalysisMapper.saleCharts(saleChartsParam);
        return saleChartsResultList;
    }
}
