package org.dromara.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzj.soopin.content.mapper.VlogMapper;
import com.wzj.soopin.goods.mapper.ProductMapper;
import com.wzj.soopin.member.domain.po.Feedback;
import com.wzj.soopin.member.service.IFeedbackService;
import com.wzj.soopin.member.service.IMemberAccountService;
import com.wzj.soopin.order.mapper.OrderMapper;
import com.wzj.soopin.transaction.domain.po.Withdraw;
import com.wzj.soopin.transaction.service.IWithdrawService;
import lombok.RequiredArgsConstructor;
import org.dromara.system.mapper.SysIntegralHistoryMapper;
import org.dromara.system.mapper.SysTenantMapper;
import org.dromara.system.mapper.SystemStatisticsMapper;
import org.dromara.web.service.IndexService;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fxh
 */
@Service
@RequiredArgsConstructor
public class IndexServiceImpl implements IndexService {


    private final SysTenantMapper sysTenantMapper;
    private final VlogMapper vlogMapper;
    private final IMemberAccountService memberService;
    private final IFeedbackService feedbackService;
    private final IWithdrawService withdrawService;
    private final SystemStatisticsMapper systemStatisticsMapper;
    private final SysIntegralHistoryMapper sysIntegralHistoryMapper;
    private final ProductMapper productMapper;
    private final OrderMapper orderMapper;

    @Override
    public Map<String, Object> getCount() {
        Map<String, Object> result = new HashMap<>(16);

        //（用户、视频、举报、提现）
        addBasicStatistics(result);

        //（商品、店铺、订单）
        addCompositeStatistics(result);

        // Top20排行榜
        addTop20Statistics(result);

        // 月度趋势统计
        addMonthlyTrendStatistics(result);

        return result;
    }

    private void addBasicStatistics(Map<String, Object> result) {
        result.put("memberCount", memberService.getCount());

        result.put("vlogCount", vlogMapper.countVlog());

        // 待审核举报
        result.put("feedbackCount", feedbackService.count(
            new QueryWrapper<Feedback>().eq("status", 0)
        ));

        // 待审核提现
        result.put("WithdrawCount", withdrawService.count(
            new QueryWrapper<Withdraw>().eq("audit_status", 0)
        ));
    }

    private void addCompositeStatistics(Map<String, Object> result) {
        // 商品统计
        Map<String, Object> productCounts = productMapper.countProduct();
        if(productCounts!=null){
            result.put("productCount", productCounts.get("count2"));
            result.put("productAudit", productCounts.get("count1"));
        }


        // 店铺统计
        Map<String, Object> storeCounts = sysTenantMapper.countProduct();
        if(storeCounts!=null){
            result.put("storeCount", storeCounts.get("count1"));
            result.put("StoreAudit", storeCounts.get("count2"));
        }

        // 订单统计
        Map<String, Object> orderCounts = orderMapper.countOrder();
        if(orderCounts!=null){
            result.put("orderCount", orderCounts.get("count1"));
            result.put("orderAudit", orderCounts.get("count2"));
        }

    }

    /** Top20排行榜 */
    private void addTop20Statistics(Map<String, Object> result) {
        result.put("top20Stores", sysTenantMapper.getTop20Stores());
        result.put("top20Product", productMapper.getTop20Product());
    }

    /** 月度趋势统计 */
    private void addMonthlyTrendStatistics(Map<String, Object> result) {
        result.put("monthlyUserVolume", systemStatisticsMapper.getMonthlyUserVolume());
        result.put("monthlyVlog", vlogMapper.getMonthlyVlog());
        result.put("monthlyIncomeExpense", sysIntegralHistoryMapper.getIncomeExpense());
    }

}
