package org.dromara.payment.coverpage.service.impl;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.enums.BusRole;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.bill.domain.bo.MerBillBo;
import org.dromara.payment.coverpage.domain.CoverPageBo;
import org.dromara.payment.coverpage.domain.CoverPageInfo;
import org.dromara.payment.coverpage.domain.MerBillInfo;
import org.dromara.payment.coverpage.mapper.CoverPageMapper;
import org.dromara.payment.coverpage.service.ICoverPageService;
import org.dromara.payment.merchant.mapper.BusMerchantAccountMapper;
import org.dromara.payment.task.domain.bo.MerTaskBo;
import org.dromara.payment.util.DateTimeUtil;
import org.dromara.payment.util.MoneyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * @Title CoverPageServiceImpl
 * @Description
 * @Author wuchuang
 * @Date 2024/5/30 13:56
 */
@RequiredArgsConstructor
@Service
public class CoverPageServiceImpl implements ICoverPageService {


    @Resource
    private final CoverPageMapper coverPageMapper;

    @Override
    public CoverPageInfo getCoverInfo(CoverPageBo bo) {

        CoverPageInfo info = new CoverPageInfo();
        //根据角色不同 查询不同得数据
        Integer busRole = LoginHelper.getBusRole();
        if(busRole == BusRole.MERCHANT.getId() ){
           info = coverPageMapper.selectMerCountInfo(LoginHelper.getBusId(), bo.getSydId() );
        }else if(busRole == BusRole.OPERATOR.getId() ){
            info = coverPageMapper.selectOpCountInfo(LoginHelper.getBusId());

            bo.setOpId(LoginHelper.getBusId());
            //查询30条收入数据
            List<MerBillInfo> dayList = coverPageMapper.select30DaysById(bo);
            //企业交易量汇总统计
            dealLineData(dayList,info);
            List<MerBillInfo> list = coverPageMapper.selectMerTransactionListById(bo);
            info.setMerBillList(list);

        }else if(busRole == BusRole.AGENT.getId() ){
            info = coverPageMapper.selectChannelCountInfo(LoginHelper.getBusId(),LoginHelper.getNo());
            //企业交易量汇总统计
            bo.setChannelId(LoginHelper.getBusId());
            bo.setChannelNo(LoginHelper.getNo());

            //查询30条收入数据
            List<MerBillInfo> dayList = coverPageMapper.select30DaysById(bo);
            //企业交易量汇总统计
            dealLineData(dayList,info);


            List<MerBillInfo> list = coverPageMapper.selectMerTransactionListById(bo);
            info.setMerBillList(list);
        }else if(busRole == BusRole.SERVICE.getId() ){
            info = coverPageMapper.selectSydCountInfo(LoginHelper.getBusId());


        }else if(busRole == BusRole.PLATFORM.getId() ){
            //查询所有数据
            info = coverPageMapper.selectAllCountInfo();
            //查询30条收入数据
            List<MerBillInfo> dayList = coverPageMapper.select30DaysById(bo);
            //企业交易量汇总统计
            dealLineData(dayList,info);
            List<MerBillInfo> list = coverPageMapper.selectMerTransactionListById(bo);
            info.setMerBillList(list);
        }

        return info;
    }

    private void dealLineData(List<MerBillInfo> dayList, CoverPageInfo info) {
        List<String> days =
            DateTimeUtil.getDateList(DateTimeUtil.getPreviousMonthSameTime(DateTimeUtil.formatNow("yyyy-MM-dd HH:mm:ss")),
                DateTimeUtil.formatNow("yyyy-MM-dd HH:mm:ss"),3,DateTimeUtil.YYYY_MM_DD);
        info.setDays(days.toArray(new String[days.size()]));

        Map<String, List<MerBillInfo>> grop
            = dayList.stream().filter(iem -> isNotBlank(iem.getPayTime()) )
            .collect(Collectors.groupingBy(MerBillInfo::getPayTime));

        List<BigDecimal> costList = new ArrayList<>();
        for(String day : days){
            if(null == grop.get(day)){
                costList.add(new BigDecimal("0.00"));
            }else{
                costList.add(MoneyUtils.getMoneyWan(grop.get(day).get(0).getAmountEnt()));
            }
        }

        info.setCosts(costList.toArray(new BigDecimal[costList.size()]));

    }

    /**
     * 判断角色查询条件 拒绝全部数据输出
     */
    private void valQueryContion(MerBillBo bo) {
        Integer busRole = LoginHelper.getBusRole();
        if(busRole == BusRole.MERCHANT.getId() ){
            bo.setMerId(LoginHelper.getBusId());
            if(null == bo.getSydId() ){
                throw new IllegalArgumentException("请选择服务商");
            }
        }else if(busRole == BusRole.OPERATOR.getId() ){
            bo.setOpId(LoginHelper.getBusId());
        }else if(busRole == BusRole.AGENT.getId() ){
            bo.setChannelId(LoginHelper.getBusId());
        }else if(busRole == BusRole.SERVICE.getId() ){
            bo.setSydId(LoginHelper.getBusId());
        }else if(busRole == BusRole.PLATFORM.getId() ){
            //查询所有数据
        }
    }
}
