package cn.pluss.platform.app.impl;

import cn.hutool.core.date.DateUtil;
import cn.jiguang.common.utils.StringUtils;
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.MerchantOrderService;
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.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("agentStaffMainPageService")
public class AgentStaffMainPageServiceImpl implements MainPageService {

    private final UserAppService userAppService;

    private final MerchantProfitService merchantProfitService;

    @Autowired @Lazy
    private MerchantOrderService moService;

    @Autowired @Lazy
    private UserAppService uaService;

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

        if (StringUtils.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 promoteConsumeFee;
        Integer totalOrderCount;

        //分润
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("userId", queryUserApp.getParentId());
        queryMap.put("agentStaffId", userId);
        queryMap.put("startTime", DateUtils.getBeginMonth(0));
        queryMap.put("endTime", DateUtils.getBeginMonth(1));
        queryMap.put("status", 1);
        // 本月交易额
        promoteConsumeFee = merchantProfitService.queryMerchantProfitSumConsumeFee(queryMap);
        if (promoteConsumeFee == null) {
            promoteConsumeFee = 0d;
        }

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

        //今日新增商户数
        QueryWrapper<UserApp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("agentStaffId", queryUserApp.getUserId());
        queryWrapper.eq("parentId", queryUserApp.getParentId());

        queryWrapper.in("merchantAuditStatus", 3, 4);
        Integer newMerchantCount = userAppService.getUserAppWithChannelStatus(queryWrapper);
        resultMap.put("promoteFee", promoteConsumeFee + "");
        resultMap.put("totalOrderCount", totalOrderCount + "");
        resultMap.put("newMerchantCount", newMerchantCount + "");

        return resultMap;
    }

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