package cn.pluss.platform.app.impl;

import cn.hutool.core.date.DateUtil;
import cn.pluss.platform.app.MainPageService;
import cn.pluss.platform.entity.MerchantOrderStatistics;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchantOrder.MerchantOrderStatisticsService;
import cn.pluss.platform.merchantProfit.MerchantProfitService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.DateUtils;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.MerchantOrderStatisticsVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推广者的主页面信息查询实现类
 */
@RequiredArgsConstructor
@Service("promoterMainPageService")
public class PromoterMainPageServiceImpl implements MainPageService {

    private final UserAppService userAppService;

    private final MerchantProfitService merchantProfitService;

    @Autowired
    @Lazy
    private MerchantOrderStatisticsService mosService;

    @Override
    public Map<String, Object> getSpreadData(String userId) {
        Map<String, Object> resultMap = new HashMap<>();

        if (StringUtil.isEmpty(userId)) {
            throw new MsgException("无用户Id！");
        }

        // 获取当前的用户信息
        UserApp queryUserApp = new UserApp();
        queryUserApp.setUserId(Long.parseLong(userId));
        List<UserApp> queryUserAppList2 = userAppService.queryUserAppList(queryUserApp);
        if (queryUserAppList2 == null || queryUserAppList2.size() == 0) {
            throw new MsgException("无用户信息！");
        } else {
            queryUserApp = queryUserAppList2.get(0);
        }

        Double promoteFee;
        Double todayProfit;
        Integer totalOrderCount;

        //分润
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("userId", userId);
        queryMap.put("status", 1);
        // 累计总分润
        promoteFee = merchantProfitService.queryMerchantProfitSumPrice(queryMap);
        if (promoteFee == null) {
            promoteFee = 0d;
        }

        //今日收款笔数
        queryMap.put("recordDate", DateUtils.getDayBegin());
        queryMap.put("retype", "1");
        totalOrderCount = merchantProfitService.queryMerchantProfitPageCount(queryMap);
        todayProfit = merchantProfitService.queryMerchantProfitSumPrice(queryMap);

        //今日新增商户数
        QueryWrapper<UserApp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentId", queryUserApp.getUserId());
        queryWrapper.in("merchantAuditStatus", 3, 4);
        queryWrapper.between("createDt", DateUtils.getDayBegin(), DateUtils.getDayEnd());
        Integer newMerchantCount = userAppService.getUserAppWithChannelStatus(queryWrapper);
        resultMap.put("promoteFee", promoteFee + "");
        resultMap.put("totalOrderCount", totalOrderCount + "");
        resultMap.put("newMerchantCount", newMerchantCount + "");
        resultMap.put("todayProfit", todayProfit == null? "0.00": todayProfit + "");

        String currentMonth = DateUtil.format(new Date(), "yyyy-MM");

        MerchantOrderStatistics directTotalCurrentMonth = mosService.getDirectTotal(queryUserApp.getUserId() + "", currentMonth);
        MerchantOrderStatistics teamTotalCurrentMonth = mosService.getIndirectTotal(queryUserApp.getUserId() + "", currentMonth);

        MerchantOrderStatisticsVO mosVO = new MerchantOrderStatisticsVO();
        mosVO.setMonthCount((directTotalCurrentMonth.getCount() + teamTotalCurrentMonth.getCount()) + "");
        mosVO.setDirectMonthTotalFee(directTotalCurrentMonth.getTotalFee());
        mosVO.setTeamMonthTotalFee(teamTotalCurrentMonth.getTotalFee());

        MerchantOrderStatistics directTotal = mosService.getDirectTotal(queryUserApp.getUserId() + "", null);
        MerchantOrderStatistics teamTotal = mosService.getIndirectTotal(queryUserApp.getUserId() + "", null);
        mosVO.setCount(directTotal.getCount() + teamTotal.getCount());
        mosVO.setDirectTotalFee(directTotal.getTotalFee());
        mosVO.setTeamTotalFee(teamTotal.getTotalFee());

        resultMap.put("merchStatistics", mosVO);

        return resultMap;
    }

    @Override
    public Map<String, Object> getHomeData(String userId) {
        return null;
    }
}
